Linux LVM CheatSheet


Related articles


TL;DR / Кароче:
$ pvcreate /dev/sdb && pvdisplay /dev/sdb
$ vgcreate files-vg /dev/sdb && vgdisplay files-vg
$ lvcreate --extents 38399 -n files-lv files-vg && lvdisplay /dev/files-vg/files-lv

mkfs.ext4 /dev/files-vg/files-lv
mkdir /files && mount /dev/files-vg/files-lv /files && df -Th
cat /etc/fstab >fstab.20230809.bak && echo '/dev/mapper/files--vg-files--lv /files ext4 defaults 0 0' | tee -a /etc/fstab && echo -n "\n\n" && cat /etc/fstab

1. Create the physical volume and give it a meaningful name:
# Create the physical volume (PV)
pvcreate /dev/sdb

# Verify
pvscan
pvs
pvdisplay /dev/sdb 

2. Then the Volume Group (VG) volume and give it a meaningful name:
# Create the volume group (VG)
vgcreate files-vg /dev/sdb

# Verify
vgscan
vgs
vgdisplay files-vg
3. Create the Logical Volume

Important thing here is to take note of the --extents <NUMBER> parameter which grately depends on the output of vgdisplay previously:

# Create the logical volume (LV) from the VG
#  The --extents parameter depends on the PE (Physical Extents) number from the `vgdisplay files-vg` output
lvcreate --extents 38399 -n files-lv files-vg

# Verify
lvscan
lvs
lvdisplay /dev/files-vg/files-lv
4. Format and mount

Format the partition, then mount it to the desired location and update the /etc/fstab file:

# Format the LVM partition
mkfs.ext4 /dev/files-vg/files-lv


# Mount the partition and update /etc/fstab
mkdir /files
mount /dev/files-vg/files-lv /files
echo '/dev/mapper/files--vg-files--lv /files ext4 defaults 0 0' | tee -a /etc/fstab