Tuesday, April 7, 2009

Cheet Sheet on lvm

Before adding some new space to a volume group, first check out if we already have some space left: command vgdisplay will do the job:
testos:/root # vgdisplay
--- Volume group ---
VG Name lvm_data
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 10
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 4
Act PV 4
VG Size 312,53 GB
PE Size 4,00 MB
Total PE 80008
Alloc PE / Size 69632 / 272,00 GB
Free PE / Size 10376 / 40,53 GB
VG UUID auAv2K-gvrp-tB2v-6ncv-ij6w-oMjd-tFGH3U



Also, if we want to know which physical partitions we've associated to volume groups so far, pvdisplay is the command:
testos:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda8
VG Name lvm_data
PV Size 21,82 GB / not usable 0
Allocatable yes (but full)
PE Size (KByte) 4096
Total PE 5586
Free PE 0
Allocated PE 5586
PV UUID uPWS3f-NqcU-JZnm-iH6Y-skTC-95Qv-2W7EQB

--- Physical volume ---
PV Name /dev/sdb6
VG Name lvm_data
PV Size 191,89 GB / not usable 0
Allocatable yes (but full)
PE Size (KByte) 4096
Total PE 49124
Free PE 0
Allocated PE 49124
PV UUID d987rD-cSes-0erP-k6wg-gS9D-8bIC-hzJGl0

--- Physical volume ---
PV Name /dev/sda1
VG Name lvm_data
PV Size 18,54 GB / not usable 0
Allocatable yes
PE Size (KByte) 4096
Total PE 4745
Free PE 63
Allocated PE 4682
PV UUID M2Cz9P-eunF-1Rgv-A00j-sNtg-On8c-y3RqdQ

--- Physical volume ---
PV Name /dev/sda6
VG Name lvm_data
PV Size 80,29 GB / not usable 0
Allocatable yes
PE Size (KByte) 4096
Total PE 20553
Free PE 10313
Allocated PE 10240
PV UUID 6n3h9p-z0N0-xyb1-C8mF-f0Xn-XlEr-bfhYtA

Steps to increase the size of a logical volume:
1. I create a new partition (or reuse an old one), for example /dev/sda6 and mark it as lvm (label 8e, with fdisk)
2. Before adding it to an existing volume group, 'format' it with lvm stuff:
pvcreate /dev/sda6
3. Add the partition to an existing volume group (VG): it must obviously be the VG where the logical volume (LV) that we want to expand resides, and we can check this with lvdisplay. This is done with the command:
vgextend , in my case:
vgextend lvm_data /dev/sdb6 (where lvm_data is the volume group name, not the logical volume name: don't be confused, I did't choose the name properly...)
4. Now that we have some spare space left in our VG, expand the logical volume to the required size:
lvextend -L20G /dev/lvm_data/data
(-L20G indicates that the total new size shold be 20G; we can put -L+20G and that would increase the existing physical LV size in 20G; /dev/lvm_data/data is the path to the LV)
5. And finally, we still have to increase the file system size. The safe way always includes a previous backup, but after that:
a) unmount the file system
b) run a fsch (resizing the file system will ask for it anyway):
e2fsck -f /dev/lvm_data/data
c) resize the file system with e2resize command (this is only for extX file systems):
resize2fs /dev/lvm_data/data 20G
d) mount the file system again and check with df -h that it has the new size