LVM Reference
There are already some LVM tutorials out there and I don’t want to write another one. Most of the time you just need a reference for the most common commands.
create
Physical Volume (PV)
pvcreate /dev/sdb
Volume Group (LV)
vgcreate vg_data /dev/sdb
Logical Volume (LV)
lvcreate –L 10G –n lv_data01 vg_data mkfs.ext3 /dev/vg_data/lv_data01 mount /dev/vg_data/lv_data01 /data01
Info
pvdisplay pvs vgdisplay vg_data vgs lvdisplay lv_data lvs
Extend LV
Add a disk and use it
vgcreate vg_data /dev/sdc vgextend vg_data /dev/sdc lvextend –L+2G /dev/vg_data/lv_data01 resize2fs –p /dev/vg_data/lv_data01
Extent to maximal size:
lvextend -l+100%FREE /dev/vg_data/lv_data01
Shrink LV
umount /data01 e2fsck –f /dev/vg_data/lv_data01 resize2fs –p /dev/vg_data/lv_data01 19G lvreduce –L20G /dev/vg_data/lv_data01 mount /dev/vg_data/lv_data01 /data01 resize2fs –p /dev/vg_data/lv_data01
Rename
VG
vgrename vg_data vg_newname
LV
lvrename /dev/vg_data/lv_data01 /dev/vg_data/lv_new_name01 or lvrename vg_data lv_data01 lv_new_name01
Remove a disk
pvmove –v /dev/sdc vgreduce vg_data /dev/sdc pvremove /dev/sdc
Delete
LV
umount /dev/vg_data/lv_data01 lvremove /dev/vg_data/lv_data01
VG
vgremove vg_data
PV
pvremove /dev/sdc1
LVM Advanced
Striped LV
lvcreate –L 10G –i2 –I64 –n lv_data02 vg_data -L 10G = LV size -i2 = use two disks -I64 = chunk size 64kb
A striped LV is organizing the data into chunk and uses more disks to increase performances. It is similar to Raid0
VG must have PVs on different hardware. It makes no sense to put sda1 and sda2 into a VG and stripe the LV.
example:
# pvcreate /dev/sdb # pvcreate /dev/sdc # vgcreate vg_data /dev/sdb /dev/sdc # lvcreate -L 1G -n lv_data_default vg_data # pvs PV VG Fmt Attr PSize PFree /dev/sdb vg_data lvm2 a- 2.00g 1020.00m /dev/sdc vg_data lvm2 a- 2.00g 2.00g # lvcreate -L 1G -i2 -I64 -n lv_data_stripe vg_data # pvs PV VG Fmt Attr PSize PFree /dev/sdb vg_data lvm2 a- 2.00g 508.00m /dev/sdc vg_data lvm2 a- 2.00g 1.50g
Mirror
You can also mirror you data for redundancy reasons which is similar to Raid1
lvcreate -L 500M -m1 --mirrorlog core -n lv_data03 vg_data -m1 = one aditional copy = written two times on the disk --mirrorlog core = log only in memory = rebuild after a reboot (otherwise you need three PVs in the VG, 2 for the data and 1 for the log)
example 1 – (two disks, mirrorlog stored in RAM):
# lvcreate -L 500M -m1 --mirrorlog core -n lv_data01_mirror vg_data Logical volume "lv_data01_mirror" created # pvs PV VG Fmt Attr PSize PFree /dev/sdb vg_data lvm2 a- 2.00g 1.51g /dev/sdc vg_data lvm2 a- 2.00g 1.51g # mkfs.ext4 /dev/vg_data/lv_data01_mirror # mount /dev/vg_data/lv_data01_mirror /mnt/data_mirror # echo "foo" > /mnt/data_mirror/file.txt # vgs VG #PV #LV #SN Attr VSize VFree vg_data 2 1 0 wz--n- 3.99g 3.02g # pvs PV VG Fmt Attr PSize PFree /dev/sdb vg_data lvm2 a- 2.00g 1.51g /dev/sdc vg_data lvm2 a- 2.00g 1.51g # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data01_mirror vg_data mwi-ao 500.00m 100.00
example 2 – (two disks, mirrorlog stored in RAM):
In case you have only two disk but you don’t want to keep the data in the RAM you need to create additional PVs on the same disks. In my opinion 5% of the cilinders is a good starting point
# fdisk -l /dev/sdb /dev/sdc | grep -e "cylinders" -e "dev" Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes /dev/sdb1 1 247 1983996 8e Linux LVM /dev/sdb2 248 261 112455 8e Linux LVM Disk /dev/sdc: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes /dev/sdc1 1 247 1983996 8e Linux LVM /dev/sdc2 248 261 112455 8e Linux LVM # pvcreate /dev/sdb1 /dev/sdb2 /dev/sdc1 /dev/sdc2 # vgcreate vg_data /dev/sdb1 /dev/sdb2 /dev/sdc1 /dev/sdc2 Volume group "vg_data" successfully created # lvcreate -L 1988M -m1 --mirrorlog disk -n lv_data_mirror vg_data Logical volume "lv_data_mirror" created # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data_mirror vg_data mwi-a- 1.94g lv_data_mirror_mlog 100.00 # pvs PV VG Fmt Attr PSize PFree /dev/sdb1 vg_data lvm2 a- 1.89g 0 /dev/sdb2 vg_data lvm2 a- 108.00m 4.00m /dev/sdc1 vg_data lvm2 a- 1.89g 0 /dev/sdc2 vg_data lvm2 a- 108.00m 104.00m
Snapshot Volume
# lvcreate –L100M –s –nlv_data01_snapshot /dev/vg_data/lv_data01 # mount /dev/vg_data/lv_data01_snapshot /data01_snapshot
example:
# lvcreate -L1G -n lv_data01 vg_data # mkfs.ext4 /dev/vg_data/lv_data01 # mkdir /mnt/data # mount /dev/vg_data/lv_data01 /mnt/data/ # echo "foo" > /mnt/data/file.txt # lvcreate -s -n lv_data01_snap -L 100M /dev/vg_data/lv_data01 # mkdir /mnt/data_snap # mount /dev/vg_data/lv_data01_snap /mnt/data_snap/ # df -h | grep data /dev/mapper/vg_data-lv_data01 1022M 47M 924M 5% /mnt/data /dev/mapper/vg_data-lv_data01_snap 1022M 47M 924M 5% /mnt/data_snap # echo "bar" > /mnt/data/file.txt # cat /mnt/data/file.txt bar # cat /mnt/data_snap/file.txt foo # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data01 vg_data owi-ao 1.00g lv_data01_snap vg_data swi-ao 100.00m lv_data01 0.08 # lvresize -L 200M /dev/vg_data/lv_data01_snap Extending logical volume lv_data01_snap to 200.00 MiB Logical volume lv_data01_snap successfully resized # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data01 vg_data owi-ao 1.00g lv_data01_snap vg_data swi-ao 200.00m lv_data01 0.04 # umount /mnt/data_snap/ # lvremove /dev/vg_data/lv_data01_snap Do you really want to remove active logical volume lv_data01_snap? [y/n]: y Logical volume "lv_data01_snap" successfully removed
be carefull with the size of your snapshot. When it reaches 100% you can still write to the disk, but the snapshot is gone
# lvcreate -L 1G -n lv_data vg_data # mkfs.ext4 /dev/vg_data/lv_data # mount /dev/vg_data/lv_data /mnt/data # dd if=/dev/urandom of=/mnt/data/file20 bs=1M count=20 20971520 bytes (21 MB) copied, 2.39865 s, 8.7 MB/s # dd if=/dev/urandom of=/mnt/data/file7 bs=1M count=7 7340032 bytes (7.3 MB) copied, 0.871878 s, 8.4 MB/s # dd if=/dev/urandom of=/mnt/data/file12 bs=1M count=12 # lvcreate -s -L 10M -n lv_data_snap /dev/vg_data/lv_data # mount /dev/vg_data/lv_data_snap /mnt/data_snap/ # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data vg_data owi-ao 1.00g lv_data_snap vg_data swi-ao 12.00m lv_data 0.10 # dd if=/dev/urandom of=/mnt/data/file7 bs=1M count=7 7340032 bytes (7.3 MB) copied, 0.88662 s, 8.3 MB/s # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data vg_data owi-ao 1.00g lv_data_snap vg_data swi-ao 12.00m lv_data 58.89 # dd if=/dev/urandom of=/mnt/data/file4 bs=1M count=4 4194304 bytes (4.2 MB) copied, 0.484525 s, 8.7 MB/s # lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data vg_data owi-ao 1.00g lv_data_snap vg_data swi-ao 12.00m lv_data 58.98 # dd if=/dev/urandom of=/mnt/data/file4 bs=1M count=23 24117248 bytes (24 MB) copied, 2.86702 s, 8.4 MB/s # lvs /dev/vg_data/lv_data_snap: read failed after 0 of 4096 at 0: Input/output error LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_data vg_data owi-ao 1.00g lv_data_snap vg_data Swi-Io 12.00m lv_data 100.00 root@bt:~# ls -l /mnt/data_snap/ ls: reading directory /mnt/data_snap/: Input/output error total 0