Gestão de LVM sobre UBUNTU

De Wiki do Bernardino
Revisão em 15h49min de 14 de junho de 2016 por JPB (Discussão | contribs) (Criação do Volume Group)
Ir para: navegação, pesquisa

A gestão de discos através da LVM (Logical Volume Manager) introduz uma serie de capacidades novas na gestão de partições de sistemas em discos, nomeadamente:

  • redimensionamento da capacidade das partições
  • capacidade de realização de snapshots
  • backups às partições com os sistemas a funcionar

Arquitetura

Lvm.Architecture.png


Instalação de LVM

Criação da Particão LVM num disco existente

# fdisk /dev/sdb

Exemplo:

# fdisk /dev/sdb

The number of cylinders for this disk is set to 10443.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK) 

Command (m for help): <-- m
Command action
  a   toggle a bootable flag
  b   edit bsd disklabel
  c   toggle the dos compatibility flag
  d   delete a partition
  l   list known partition types
  m   print this menu
  n   add a new partition
  o   create a new empty DOS partition table
  p   print the partition table
  q   quit without saving changes
  s   create a new empty Sun disklabel
  t   change a partition's system id
  u   change display/entry units
  v   verify the partition table
  w   write table to disk and exit
  x   extra functionality (experts only)

Command (m for help): <-- n
Command action
   e   extended
   p   primary partition (1-4)
<-- p
Partition number (1-4): <-- 1
First cylinder (1-10443, default 1): <-- <ENTER>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10443, default 10443): <-- +25000M

Command (m for help): <-- t
Selected partition 1
Hex code (type L to list codes): <-- L

 0  Empty           1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot
 1  FAT12           24  NEC DOS         81  Minix / old Lin bf  Solaris
 2  XENIX root      39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 5  Extended        41  PPC PReP Boot   85  Linux extended  c7  Syrinx
 6  FAT16           42  SFS             86  NTFS volume set da  Non-FS data
 7  HPFS/NTFS       4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 8  AIX             4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
 9  AIX bootable    4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt
 a  OS/2 Boot Manag 50  OnTrack DM      93  Amoeba          e1  DOS access
 b  W95 FAT32       51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
 c  W95 FAT32 (LBA) 52  CP/M            9f  BSD/OS          e4  SpeedStor
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  EFI GPT
10  OPUS            55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       a9  NetBSD          f4  SpeedStor
16  Hidden FAT16    63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fd  Linux raid auto
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fe  LANstep
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid ff  BBT
1c  Hidden W95 FAT3 75  PC/IX
Hex code (type L to list codes): <-- 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): <-- w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Criação do Volume Fisico

Preparação da partição ou partições para LVM

pvcreate /dev/sdb1 
  Physical volume "/dev/sdb1" successfully created


Visualização do resultado

pvdisplay 
   
 "/dev/sdb1" is a new physical volume of "300.00 GiB"
 --- NEW Physical volume ---
 PV Name               /dev/sdb1
 VG Name               
 PV Size               300.00 GiB
 Allocatable           NO
 PE Size               0   
 Total PE              0
 Free PE               0
 Allocated PE          0
 PV UUID               pj6qq8-dowE-ghG7-lVNz-ccQl-pgEg-7tNHWH


Criação do Volume Group

Para criar-mos o volume group temos

# vgcreate mariadb1 /dev/sdb1 

Podemos juntar mais que um volume físico disponível.

Visualização do resultado

# vgdisplay

 --- Volume group ---
 VG Name               mariadb1
 System ID             
 Format                lvm2
 Metadata Areas        1
 Metadata Sequence No  1
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                0
 Open LV               0
 Max PV                0
 Cur PV                1
 Act PV                1
 VG Size               300.00 GiB
 PE Size               4.00 MiB
 Total PE              76799
 Alloc PE / Size       0 / 0   
 Free  PE / Size       76799 / 300.00 GiB
 VG UUID               11olq3-QEY9-yNEj-uoKN-lGdT-bTwI-xiOQTC

Outro comando para identificação ddos Volumes LVM

# vgscan 
 Reading all physical volumes.  This may take a while...
 Found volume group "mariadb1" using metadata type lvm2


Criação de Volumes Lógicos

Para criarmos finalmente os volumes que vamos usar para os filesystem, por exemplo para conter uma base de dados, vamos alocar 200GB

# lvcreate --name database1 --size 200G mariadb1
  Logical volume "database1" created.

Remoção do snapshot, consolidando dados ou não

Consolidando alterações

Quando se remove o snapshot todas as alterações entretanto realizadas sobre no sistema serão consolidadas na partição original. Isto é não se perde nenhuma alteração enquanto o sistema funcionou com o snapshot ativo.

Para isso usamos o comando:

# lvremove group/snap-name

Voltar à imagem do sistema anterior ao snapshot

Para voltar com sistema ao estado anterior ao snapshot, deitando fora todas as alterações entretanto efectuadas neste durante o seu funcionamento sobre o snapshot, temos de parar o sistema e de seguida efectuar:

# lvconvert --merge group/snap-name

Listagem de informação LVM

apresentação da informação sobre os volumes LVM, utilização, snapshots

root@test3:~# lvs
  LV             VG          Attr      LSize   Pool Origin                   Data%  Move Log Copy%  Convert
  test3-data     opennebula0 -wi-ao---   1.00t                                                             
  isos           test3-vg    -wi-ao--- 100.00g                                                             
  root           test3-vg    -wi-ao---  57.14g                                                             
  swap_1         test3-vg    -wi-ao---  35.99g                                                             
  Windows10      vmmachines1 swi-aos--  48.83g      [Windows10_vorigin]       89.45                        
  manage         vmmachines1 -wi-ao---  80.00g                                                             
  ubu12openwrt   vmmachines1 -wi-a----   8.00g                                                             
  ubu14consola   vmmachines1 swi-a-s--   8.00g      [ubu14consola_vorigin]    86.30                        
  windows10-data vmmachines1 swi-aos--   8.00g      [windows10-data_vorigin]  26.89                        
  zimbra         vmmachines1 owi-aos--  19.53g                                                             
  zimbra-snap1   vmmachines1 swi-a-s--  10.00g      zimbra                    33.32                        
  zimbra.opt     vmmachines1 -wi-ao---  48.83g


Backup de um volume usando snapshots

Criacção do snapshot

Vamos criar um snapshot de 500MB, ou seja o sistema continuará a funcionar sobre estes 500MB libertando a partição de base para backup. Temos de nos assegurar que o Volume Group. onde a partição está inserira dispõe de espaço livre suficiente.

# lvcreate -L592M -s -n dbbackup /dev/ops/databases 
lvcreate -- WARNING: the snapshot must be disabled if it gets full
lvcreate -- INFO: using default snapshot chunk size of 64 KB for "/dev/ops/dbbackup"
lvcreate -- doing automatic backup of "ops"
lvcreate -- logical volume "/dev/ops/dbbackup" successfully created


ATENÇÃO se o snapshot encher é desativado automáticamente.

Montagem do partição

Agora podemos montar a partição a que fizemos o snapshot:

# mkdir /mnt/ops/dbbackup
# mount /dev/ops/dbbackup /mnt/ops/dbbackup
mount: block device /dev/ops/dbbackup is write-protected, mounting read-only
       

Realização do Backup

# tar -cf /dev/rmt0 /mnt/ops/dbbackup
tar: Removing leading `/' from member names
         

Remoção do snapshot

# umount /mnt/ops/dbbackup
# lvremove /dev/ops/dbbackup 
lvremove -- do you really want to remove "/dev/ops/dbbackup"? [y/n]: y
lvremove -- doing automatic backup of volume group "ops"
             lvremove -- logical volume "/dev/ops/dbbackup" successfully removed

Sumula de operações disponíveis

Directory and Files
Directories and Files ## Directories

/etc/lvm                    - default lvm directory location
/etc/lvm/backup         - where the automatic backups go
/etc/lvm/cache          - persistent filter cache
/etc/lvm/archive        - where automatic archives go after a volume group change
/var/lock/lvm             - lock files to prevent metadata corruption

  1. Files

/etc/lvm/lvm.conf       - main lvm configuration file
$HOME/.lvm               - lvm history

Tools
diagnostic lvmdump

lvmdump -d <dir>
dmsetup [info|ls|status]

Note: by default the lvmdump command creates a tar ball

Physical Volumes
display

pvdisplay -v

pvs -v
pvs -a
pvs --segments (see the disk segments used)

pvs attributes are:
1. (a)llocatable

2. e(x)ported

scanning pvscan -v

Note: scans for disks for non-LVM and LVM disks

adding pvcreate /dev/sdb1

    1. Create physical volume with specific UUID, used to recover volume groups (see miscellaneous section)


pvcreate --uuid <UUID> /dev/sdb1

Common Attributes that you may want to use:

-M2 create a LVM2 physical volume

removing pvremove /dev/sdb1
checking pvck -v /dev/sdb1

Note: check the consistency of the LVM metadata

change physical attributes

## do not allow allocation of extents on this drive, however the partition must be in a vg otherwise you get an error
pvchange -x n /dev/sdb1

Common Attributes that you may want to use:

--addtag add a tag

-x allowed to allocate extents
-u change the uuid

moving pvmove -v /dev/sdb2 /dev/sdb3

Note: moves any used extents from this volume to another volume, in readiness to remove that volume. However you cannot use this on mirrored volumes, you must convert back to non-mirror using "lvconvert -m 0"

Volume Groups
display vgdisplay -v

vgs -v
vgs -a -o +devices

vgs flags:

  1. PV - number of physical devices
  2. LV - number of configured volumes        

    vgs

attributes are:
1. permissions (r)|(w)
2. resi(z)eable
3. e(x)ported
4. (p)artial
5. allocation policy - (c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited
6. (c)luster

scanning vgscan -v
creating

vgcreate VolData00 /dev/sdb1 /dev/sdb2 /dev/sdb3

vgcreate VolData00 /dev/sdb[123]

    1. Use 32MB extent size

vgcreate VolData00 -s 32 /dev/sdb1

Common Attributes that you may want to use:

-l  maximum logical volumes
-p maximum physical volumes
-s physical extent size (default is 4MB)

-A autobackup

extending vgextend VolData00 /dev/sdb3
reducing vgreduce VolData00 /dev/sdb3

vgreduce --removemissing --force VolData00

removing vgremove VolData00

Common Attributes that you may want to use:

-f force the removal of any logical volumes

checking vgck VolData00

Note: check the consistency of the LVM metadata

change volume attributes vgchange -a n VolData00

Common Attributes that you may want to use:

-a control availability of volumes within the group
-l  maximum logical volumes
-p maximum physical volumes
-s physical extent size (default is 4MB)
-x resizable yes or no (see VG status in vxdisplay)

renaming vgrename VolData00 Data_Vol_01

note: the volume group must not have any active logical volumes

converting metadata type vgconvert -M2 VolData00

Note: vgconvert allows you to convert from one type of metadata format to another for example from LVM1 to LVM2, LVM2 offers bigger capacity, clustering and mirroring

merging # the old volumes group will be merged into the new volume group

vgmerge New_Vol_Group Old_Vol_Group

Note: you must unmount any fielsystems and deactivate the vg that is being merged "vgchange -a n <vg>", then you can activiate it again afterwards "vgchange -a y <vg>", then perform a vgscan, dont forget to backup the configuration

spliting vgsplit Old_Vol_Group New_Vol_Group [physical volumes] [-n logical volume name]
importing vgimport VolData00

Common Attributes that you may want to use:

-a import all exported volume groups

exporting ## to see if a volume has already been export use "vgs" and look at the third attribute should be a x

vgexport VolData00

Common Attributes that you may want to use:

-a export all inactive volume groups

backing up

## Backup to default location (/etc/lvm/backup)

vgcfgbackup VolData00

  1. Backup to specific location
vgcfgbackup -f /var/backup/VolData00_bkup VolData00

# Backup to specific location all volume groups (notice the %s)

vgcfgbackup -f /var/backup/vg_backups_%s

Note: the backup is written in plain text and are by default located in /etc/lvm/backup

restoring vgcfgrestore -f /var/backup/VolData00_bkup VolData00

Common Attributes that you may want to use:

-l list backups of file
-f backup file
-M metadataype 1 or 2

cloning vgimportclone /dev/sdb1

Note: used to import and rename duplicated volume group

special files vgmknodes VolData00

Note: recreates volume group directory and logical volume special files in /dev

Logical Volumes
display

lvdisplay -v

lvdisplay --maps      display mirror volumes

lvs -v
lvs -a -o +devices

    1. lvs commands for mirror volumes


lvs -a -o +devices

lvs -a -o +seg_pe_ranges --segments

## Stripe size

lvs -v --segments

lvs -a -o +stripes,stripesize

## use complex command

lvs -a -o +devices,stripes,stripesize,seg_pe_ranges --segments

lvs attributes are:
1. volume type: (m)irrored, (M)irrored without initail sync, (o)rigin, (p)vmove, (s)napshot, invalid (S)napshot, (v)irtual, mirror (i)mage
                      mirror (I)mage out-of-sync, under (c)onversion
2. permissions: (w)rite, (r)ead-only
3. allocation policy - (c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited
4. fixed (m)inor
5. state: (a)ctive, (s)uspended, (I)nvalid snapshot, invalid (S)uspended snapshot, mapped (d)evice present with-out tables,
             mapped device present with (i)nactive table

6. device (o)pen (mounted in other words)

scanning lvscan -v

lvmdiskscan

creating

## plain old volume
lvcreate -L 10M VolData00

## plain old volume but use extents, use 10 4MB extents (if extent size is 4MB)

lvcreate -l 10 VolData00

    1. plain old volume but with a specific name web01

lvcreate -L 10M -n web01 VolData00

    1. plain old volume but on a specific disk

lvcreate -L 10M VolData00 /dev/sdb1

    1. a striped volume

called lvol1 (note the captial i for the stripe size), can use -l (extents) instead of -L

lvcreate -i 3 -L 24M -n lvol1 vg01

## Mirrored volume

lvcreate -L 10M -m1 -n data01 vg01

    1. Mirrored volume without a mirror log file

lvcreate -L 10M -m1 --mirrorlog core -n data01 vg01

Common Attributes that you may want to use:

-L size of the volume [kKmMgGtT]
-l number of extents
-C contiguous [y|n]
-i stripes
-I stripe size
-m mirrors
--mirrorlog

-n volume name

extending

lvextend -L 20M /dev/VolData00/vol01

Common Attributes that you may want to use:

-L size of the volume [kKmMgGtT]

-l number of extents
-C contiguous [y|n]
-i stripes
-I stripe size

Note: you can extend a ext2/ext3 filesystem using the "resize2fs" or "fsadm" command

fsadm resize /dev/VolData01/data01
resize2fs -p /dev/mapper/VolData01-data01 [size]

The -p option displays bars of progress while extendingthe filesystem

reducing/resizing

lvreduce -L 5M /dev/VolData00/vol01

lvresize -L 5M /dev/VolData00/vol01

Note: rounding will occur when extending and reducing volumes to the next extent (4MB by default), you can use resize2fs or fsadm to shrink the filesystem

fsadm resize /dev/VolData01/data01 [size]
resize2fs -p /dev/mapper/VolData01-data01 [size]

removing lvremove /dev/VolData00/vol01
adding a mirror to a non-mirrored volume

lvconvert -m1 --mirrorlog core /dev/VolData00/vol01 /dev/sdb2

Note: you can also use the above command to remove a unwanted log

removing a mirror from a mirrored volume

lvconvert -m0 /dev/VolData00/vol01 /dev/sdb2

Note: the disk in the command is the one you want to remove

Mirror a volume that has stripes lvconvert --stripes 3 -m1 --mirrorlog core /dev/VolData00/data01 /dev/sdd1 /dev/sde1 /devsdf1
change volume attributes

lvchange -a n /dev/VolData00/vol01

Common Attributes that you may want to use:

-a availability
-C contiguous [y|n]

renaming lvrename /dev/VolData00/vol_old /dev/VolData00/vol_new
snapshotting lvcreate --size 100M --snapshot -name snap /dev/vg01/data01
Miscellaneous
Simulating a disk failure dd if=/dev/zero of=/dev/sdb2 count=10
reparing a failed mirror no LVM corruption ## check volume, persume /dev/sdb2 has failed

lvs -a -o +devices

  1. remove the failed disk from the volume (if not already done so) , this will convert volume into a non-mirrored volume

vgreduce --removemissing --force VolData00

    1. replace the disk physically, remember to partion it with type 8e

fdisk /dev/sdb
........

    1. add new disk to LVM

pvcreate /dev/sdb2

    1. add the disk back into volume group

vgextend VolData00 /dev/sdb2

    1. mirror up the volume

lvconvert -m1 --mirrorlog core /dev/VolData00/vol02 /dev/sdb2

corrupt LVM metadata without replacing drive # attempt to bring the volume group online

vgchange -a y VolData00

  1. Restore the LVM configation

vgcfgrestore VolData00

  1. attempt to bring the volume grou online

vgchange -a y VolData00

  1. file system check

e2fsck /dev/VolData00/data01

corrupt LVM metadata but replacing the faulty disk

# attempt to bring the volume group online but you get UUID conflict errors make note of the UUID number

vgchange -a y VolData00
vgchange -a n VolData00

    1. sometimes it my only be a logical volume problem

lvchange -a y /dev/VolData00/web02
lvchange -a n /dev/Voldata00/web02

    1. replace the disk physically, remember to partion it with type 8e

fdisk /dev/sdb
........

  1. after replacing the faulty drive the disk must have the previuos UUID number or you can get it from /etc/lvm directory

pvcreate --uuid <previous UUID number taken from above command> /dev/sdb2

  1. Restore the LVM configation

vgcfgrestore VolData00

  1. attempt to bring the volume group online or logical volume

vgchange -a y VolData00
lvchange -a y /dev/VolData00/web02

  1. file system check
e2fsck /dev/VolData00/data01

Note: if you have backed the volume group configuration you can obtain the UUID number in the backup file by default located in /etc/lvm/backup or running "pvs -v"