An intro to LVM

Logical Volume Management, an alternative method of managing storage systems on Linux Systems

An intro to LVM

I'm not gonna try to convince you to use LVM. Here, I'm just gonna show you how to use this beautiful level of abstraction.

This is more a cheatsheet than a complete tutorial.

LVM Diagram

Physical Volume (PV)

Description

Physical system (disks, partitions, RAID); used as raw hardware to build the architecture with a higher level of abstraction. LVM writes a header to the hardware, indicating its manageability by LVM.

Create a PV

sudo lvmdiskscan 
# Displays all disks potentially manageable by lvm

sudo pvcreate /dev/sda /dev/sdb
# ⤷ Output pvcreate :
#     Physical volume "/dev/sda" successfully created
#     Physical volume "/dev/sdb" successfully created

Display infos of PVs

sudo pvs
# For more details, use : 
sudo pvdisplay

# ⤷ Output pvs :
#    PV         VG    Fmt  Attr PSize   PFree  
#     /dev/sda        lvm2 ---  200.00g 200.00g
#     /dev/sdb        lvm2 ---  100.00g 100.00g

Volume Group (VG)

Description

LVM combines Physical Volumes into a group. These groups abstract from the underlying characteristics of the physical devices and therefore function as a unified whole that combines the storage capabilities of the Physical Volumes.

Create a VG

sudo vgcreate <NAME_VG> /dev/sda /dev/sdb 
# PV previously tagged

# ⤷ Output vgcreate :
#     Volume group "NAME_VG" successfully created

Display infos of VGs

sudo vgs
# For more details, use : 
sudo vgdisplay

# ⤷ Output vgs :
#      VG         #PV #LV #SN   Attr    VSize     VFree  
#      NAME_VG     2   0   0    wz--n- 299.99g   299.99g

Note: now that our Physical Volumes are associated with a Volume Group, when pvs is run, the VG column now points to the Volume Group name.

Manipulations

Remove a VG

sudo umount /dev/<NAME_VG>/<ALL_LVS>
sudo vgremove <NAME_VG>

Logical Volume (LV)

Description

A Volume Group can now be divided into a number of Logical Volumes. These Logical Volumes are functionally equivalent to a partition on a physical disk, while retaining much more flexibility. These Logical Volumes are the primary components that users and applications use.

Create a LV

sudo lvcreate -L <10G> -n <NAME_LV1> <NAME_VG>
# To create a LV <NAME_LV2> that uses all the remaining space available on <NAME_VG> :
sudo lvcreate -l 100%FREE -n <NAME_LV2> <NAME_VG>

# ⤷ Output lvcreate :
#     Logical volume "NAME_LV1" created.
#     Logical volume "NAME_LV2" created.

Display infos of LVs

sudo lvs
# For more details, use : 
sudo lvdisplay

# ⤷ Output lvs :
#       LV             VG         Attr          Lsize
#    NAME_LV1         NAME_VG    -wi-ao----     10,00g
#    NAME_LV1         NAME_VG    -wi-ao----    100,00g

How to access LVs

Logical Volumes are accessible via /dev/NAME_VG/NAME_LV1 or /dev/mapper/NAME_VG-NAME_LV1; for formatting or mounting, for example.

Create a snapshot

sudo lvcreate –s –L <10G> -n <NAME_SNAP> <NAME_VG>/<NAME_LV1>

Manipulations

Enlarge a LV

sudo lvresize -L <+5G> --resizefs <NAME_VG>/<NAME_LV1>
# Adds 5G to LV and also extends its filesystem

Shrink a LV

df -h # Evaluate the size we can recover
sudo umount /dev/<NAME_VG>/<NAME_LV1> # Unmount the LV
sudo fsck -t ext4 -f /dev/<NAME_VG>/<NAME_LV1> # Check the file system
sudo resize2fs -p /dev/<NAME_VG>/<NAME_LV1> <5G> # Shrink the size of the filesystem
sudo lvresize -L <5G> /dev/<NAME_VG>/<NAME_LV1> # Shrink LV size
sudo fsck -t ext4 -f /dev/<NAME_VG>/<NAME_LV1> # Control the filesystem
sudo mount /dev/<NAME_VG>/<NAME_LV1> /mnt/... # Mount the LV

Remove a LV

sudo umount /dev/<NAME_VG>/<NAME_LV1>
sudo lvremove <NAME_VG>/<NAME_LV1>