LVM Explained
Traditional disk partitions are rigid — once you set a size, changing it is painful. LVM (Logical Volume Manager) adds a flexible abstraction layer: pool multiple disks together, carve logical volumes of any size, resize them live, and take snapshots. It's how production servers manage storage without downtime.
The Three Layers of LVM
Physical Disks: /dev/sda /dev/sdb /dev/sdc
| | |
Physical Volumes (PV): [PV: sda] [PV: sdb] [PV: sdc]
\ | /
\ | /
Volume Group (VG): [ vg_data (3TB pool) ]
/ | \
/ | \
Logical Volumes (LV): [lv_root] [lv_home] [lv_data]
(50GB) (200GB) (2.7TB)
| | |
Filesystems: ext4 ext4 xfs
Physical Volume (PV)
A disk or partition marked for LVM use. LVM writes metadata to the disk to claim it. Any block device works: /dev/sda, /dev/sdb1, even RAID arrays or loop devices.
Volume Group (VG)
One or more PVs pooled together into a single storage pool. The VG is divided into fixed-size "physical extents" (PE, default 4MB each). Think of it as one big virtual disk.
Logical Volume (LV)
A slice of the VG that looks like a regular partition to the OS. Create a filesystem on it, mount it, resize it — all without caring which physical disk the data lives on.
Setting Up LVM from Scratch
# Step 1: Create Physical Volumes
pvcreate /dev/sdb /dev/sdc
# Physical volume "/dev/sdb" successfully created.
# Physical volume "/dev/sdc" successfully created.
pvdisplay /dev/sdb # detailed info
pvs # quick summary: PV, VG, size, free
# Step 2: Create a Volume Group
vgcreate vg_data /dev/sdb /dev/sdc
# Volume group "vg_data" successfully created
vgdisplay vg_data # details
vgs # quick summary
# Step 3: Create Logical Volumes
lvcreate -L 100G -n lv_home vg_data # fixed size
lvcreate -l 100%FREE -n lv_data vg_data # use all remaining space
lvdisplay /dev/vg_data/lv_home
lvs # quick summary
# Step 4: Create filesystem and mount
mkfs.ext4 /dev/vg_data/lv_home
mkdir /home
mount /dev/vg_data/lv_home /home
# /etc/fstab entry:
/dev/vg_data/lv_home /home ext4 defaults 0 2
Resizing Volumes — Live and Without Downtime
How do you grow a partition that's running out of space?
With LVM, you extend the logical volume then resize the filesystem — usually without unmounting. No data migration, no reinstall, no downtime for reads. Just two commands.
# Extend a logical volume by 50GB
lvextend -L +50G /dev/vg_data/lv_home
# Resize the filesystem to use new space (ext4):
resize2fs /dev/vg_data/lv_home
# resize2fs: 1.46.5
# old: 26214400 blocks new: 39321600 blocks
# Or do both in one command:
lvextend -L +50G -r /dev/vg_data/lv_home
# -r = resize filesystem automatically
# For XFS (XFS can only grow, not shrink):
xfs_growfs /mountpoint
# Shrink a volume (must unmount first, ext4 only):
umount /home
e2fsck -f /dev/vg_data/lv_home # check filesystem first
resize2fs /dev/vg_data/lv_home 80G # shrink fs to 80GB
lvreduce -L 80G /dev/vg_data/lv_home # then shrink LV
Adding a New Disk to an Existing VG
# New disk arrived: /dev/sdd
pvcreate /dev/sdd
vgextend vg_data /dev/sdd
# Now the VG has more free space — extend LVs as needed
vgs
# VG #PV #LV SSize SFree
# vg_data 3 2 3.00t 1.00t ← new disk added free space
# Optionally: move data off an old disk (pvmove)
pvmove /dev/sdb /dev/sdd # migrate all data from sdb to sdd
# Then you can remove sdb from the VG:
vgreduce vg_data /dev/sdb
pvremove /dev/sdb
LVM Snapshots
What is an LVM snapshot and how does it work?
A snapshot is an instant, space-efficient copy of an LV at a point in time. It doesn't copy data immediately — it uses copy-on-write: only when the original LV changes does the old data get saved to the snapshot. Snapshots let you back up a live database or test risky changes with an instant rollback option.
# Create a snapshot (needs free space in VG for changed blocks)
lvcreate -s -L 10G -n lv_home_snap /dev/vg_data/lv_home
# -s = snapshot, -L = max COW storage, -n = name
# Mount and use snapshot (read-only or read-write)
mount -o ro /dev/vg_data/lv_home_snap /mnt/snapshot
# Use case: backup while original is live
tar czf /backup/home.tar.gz /mnt/snapshot
umount /mnt/snapshot
# Restore from snapshot:
lvconvert --merge /dev/vg_data/lv_home_snap
# Merges snapshot back into origin on next activation
# Remove snapshot when done:
lvremove /dev/vg_data/lv_home_snap
Thin Provisioning
Thin provisioning lets you create logical volumes larger than the physical space available, banking on the assumption that not all space will be used at once. Common in cloud environments and VMs. If actual usage exceeds real space, you get out-of-space errors — so monitor usage carefully.
# Create a thin pool (real storage: 100GB)
lvcreate -L 100G --thinpool tp_main vg_data
# Create thin volumes that "promise" more than the pool
lvcreate -V 500G --thin -n lv_vm1 vg_data/tp_main
lvcreate -V 500G --thin -n lv_vm2 vg_data/tp_main
# Total promised: 1TB, actual pool: 100GB — works if not all used
# Monitor thin pool usage (critical!):
lvs -a vg_data
# Data%: 45.00 ← 45% of pool used, add more space if near 100%