Home Blog Certs Knowledge Base About

LVM

LVM (Logical Volume Manager): flexible disk management with Physical Volumes โ†’ Volume Groups โ†’ Logical Volumes. Supports online resize, snapshots, and spanning multiple disks.

LVM concepts

Three-layer model
LayerCommand prefixDescription
PV โ€” Physical Volumepv*A disk or partition initialised for LVM. Building block.
VG โ€” Volume Groupvg*Named pool aggregating one or more PVs. Total storage.
LV โ€” Logical Volumelv*Virtual partition carved from the VG. Formatted and mounted.
Key LVM terms
TermDescription
PE โ€” Physical ExtentSmallest allocatable unit on a PV (default 4 MB)
LE โ€” Logical ExtentMatching unit on an LV (maps to PEs)
Thin provisioningAllocate LVs larger than physical space (over-commit)
CoW snapshotPoint-in-time copy โ€” only changed blocks are stored

Setup: PV โ†’ VG โ†’ LV

Create and format
CommandDescription
pvcreate /dev/sdb /dev/sdcInitialise disks as Physical Volumes
vgcreate data-vg /dev/sdb /dev/sdcCreate Volume Group spanning both PVs
lvcreate -L 50G -n web-lv data-vgCreate 50 GB Logical Volume
lvcreate -l 100%FREE -n data-lv data-vgUse all free space in the VG
lvcreate -l 80%VG -n data-lv data-vgUse 80% of total VG capacity
mkfs.ext4 /dev/data-vg/web-lvFormat with ext4
mkfs.xfs /dev/data-vg/web-lvFormat with XFS
mount /dev/data-vg/web-lv /var/wwwMount the Logical Volume

Typical fstab entry using device mapper path:

/dev/data-vg/web-lv  /var/www  ext4  defaults,noatime  0  2

Inspecting LVM

pv* / vg* / lv* display commands
CommandDescription
pvsOne-line summary of all PVs
pvdisplay /dev/sdbDetailed PV info (PE size, total/free PEs)
pvscanScan for PVs on all block devices
vgsOne-line summary of all VGs
vgdisplay data-vgDetailed VG info (free space, PE count)
lvsOne-line summary of all LVs
lvdisplay /dev/data-vg/web-lvDetailed LV info (size, path, type)
lsblkBlock device tree โ€” shows LVM hierarchy
dmsetup lsDevice mapper mappings (underlying LVM)

Extending & resizing

Grow LV and filesystem
CommandDescription
vgextend data-vg /dev/sddAdd a new disk to the Volume Group
lvextend -L +20G /dev/data-vg/web-lvGrow LV by 20 GB
lvextend -L 100G /dev/data-vg/web-lvSet LV to exactly 100 GB
lvextend -l +100%FREE /dev/data-vg/web-lvUse all remaining free space in VG
lvextend -r -L +20G /dev/data-vg/web-lvExtend LV and resize filesystem in one step (-r flag)
resize2fs /dev/data-vg/web-lvResize ext4 to fill LV (online, no unmount needed)
xfs_growfs /var/wwwResize XFS to fill LV (must be mounted, use mount point)
btrfs filesystem resize max /mntResize Btrfs to fill LV (online)
Shrink LV (ext4 only โ€” XFS cannot shrink)
CommandDescription
umount /var/wwwStep 1: unmount
e2fsck -f /dev/data-vg/web-lvStep 2: force check
resize2fs /dev/data-vg/web-lv 30GStep 3: shrink filesystem to 30 GB
lvreduce -L 30G /dev/data-vg/web-lvStep 4: shrink LV to match
mount /dev/data-vg/web-lv /var/wwwStep 5: remount

Shrink order matters: always shrink filesystem first, then LV. Doing it in reverse destroys the filesystem.

Snapshots

LVM snapshots
CommandDescription
lvcreate -s -n snap-lv -L 5G /dev/data-vg/web-lvCreate snapshot (5 GB CoW buffer)
mount -o ro /dev/data-vg/snap-lv /mnt/snapMount snapshot read-only for backup
lvs -aList all LVs including snapshots (snap% shows usage)
lvconvert --merge /dev/data-vg/snap-lvRollback origin to snapshot state
lvremove /dev/data-vg/snap-lvDelete snapshot without rolling back

Snapshot fills up when CoW buffer is exhausted โ†’ it becomes invalid. Allocate at least 10โ€“20% of original volume size, or use thin provisioned snapshots for better flexibility.

Moving & removing

Move, rename, remove
CommandDescription
pvmove /dev/sdbMigrate data from /dev/sdb to other PVs (online)
pvmove /dev/sdb /dev/sddMigrate to specific target PV
vgreduce data-vg /dev/sdbRemove PV from VG (after pvmove)
lvrename data-vg web-lv app-lvRename a Logical Volume
vgrename data-vg prod-vgRename a Volume Group
lvremove /dev/data-vg/web-lvDelete LV (unmount first)
vgremove data-vgDelete VG (all LVs must be removed first)
pvremove /dev/sdbRemove PV label (must be removed from VG first)