Linux LVM — Logical Volume Management Guide
In this tutorial, you'll learn about Linux LVM. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Linux Logical Volume Manager (LVM) gives you flexible storage management that traditional partitions cannot match — resizing, snapshots, striping, and pooling multiple disks into a single logical hierarchy.
Hook
LVM lets you treat physical disk space as a pool you can allocate, resize, and snapshot without downtime. By the end of this guide you will create PVs, VGs, LVs, resize them, and use snapshots for backups. DodaZIP uses LVM snapshots for consistent backup snapshots across its compression cluster.
Why LVM Matters
Standard partitions lock you into fixed sizes. When /home fills up but /var has free space, you cannot easily reshuffle. LVM decouples physical disks from logical volumes, letting you grow, shrink, or move storage while the system runs.
LVM Architecture
flowchart LR A[Physical Volumes
/dev/sda1, /dev/sdb1] --> B[Volume Group
vg_data] B --> C[Logical Volume
lv_home] B --> D[Logical Volume
lv_var] B --> E[Logical Volume
lv_docker] C --> F[Filesystem
ext4 / XFS] style B fill:#f90,color:#fff
Three layers: Physical Volumes (PV) are partitions or whole disks. A Volume Group (VG) pools PVs into one storage pool. Logical Volumes (LV) are carved from the VG and hold filesystems.
Creating Physical Volumes
# Identify available disks
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
# Initialize disks as PVs
sudo pvcreate /dev/sdb /dev/sdc
# Verify
sudo pvs
sudo pvdisplay
Expected output:
$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 100.00g 100.00g
/dev/sdc lvm2 --- 200.00g 200.00g
Creating a Volume Group
# Create VG with both PVs
sudo vgcreate vg_data /dev/sdb /dev/sdc
# Verify
sudo vgs
sudo vgdisplay
Creating Logical Volumes
# Create 50GB LV for home
sudo lvcreate -L 50G -n lv_home vg_data
# Create LV using all remaining space
sudo lvcreate -l 100%FREE -n lv_docker vg_data
# Verify
sudo lvs
# Create filesystem
sudo mkfs.ext4 /dev/vg_data/lv_home
sudo mkfs.xfs /dev/vg_data/lv_docker
# Mount
sudo mount /dev/vg_data/lv_home /mnt/home
Expected output:
$ sudo lvs
LV VG Attr LSize Pool Origin Data%
lv_home vg_data -wi-ao---- 50.00g
lv_docker vg_data -wi-ao---- 250.00g
Resizing Logical Volumes
LVM allows online resizing — no reboot needed.
Grow an LV
# Check current size
df -h /mnt/home
# Extend LV by 20GB
sudo lvextend -L +20G /dev/vg_data/lv_home
# Resize filesystem (ext4)
sudo resize2fs /dev/vg_data/lv_home
# For XFS
sudo xfs_growfs /mnt/home
Shrink an LV
# Shrink requires unmount (ext4 only)
sudo umount /mnt/home
sudo e2fsck -f /dev/vg_data/lv_home
sudo resize2fs /dev/vg_data/lv_home 40G
sudo lvreduce -L 40G /dev/vg_data/lv_home
sudo mount /dev/vg_data/lv_home /mnt/home
XFS does not support shrinking. Plan XFS volumes carefully.
Snapshots
Snapshots capture LV state at a point in time for consistent backups.
# Create snapshot (use 5GB CoW storage)
sudo lvcreate -L 5G -s -n lv_home_snap /dev/vg_data/lv_home
# Mount snapshot read-only for backup
sudo mkdir /mnt/snap
sudo mount -o ro /dev/vg_data/lv_home_snap /mnt/snap
# Backup
sudo tar czf /backup/home-$(date +%F).tar.gz /mnt/snap
# Remove snapshot
sudo umount /mnt/snap
sudo lvremove /dev/vg_data/lv_home_snap
Thin Provisioning
Thin provisioning lets you overcommit storage — allocate 200GB total from a 100GB pool.
# Create thin pool
sudo lvcreate -L 100G -T vg_data/thin_pool
# Create thin volumes
sudo lvcreate -V 50G -T vg_data/thin_pool -n thin_home
sudo lvcreate -V 150G -T vg_data/thin_pool -n thin_logs
# Monitor pool usage
sudo lvs -o+data_percent
Expected output:
$ sudo lvs -o+data_percent
LV VG Attr LSize Pool Data%
thin_pool vg_data twi-aot--- 100.00g 12.50
thin_home vg_data Vwi-aot--- 50.00g thin_pool
thin_logs vg_data Vwi-aot--- 150.00g thin_pool
Adding Disks to a VG
# Add new disk to existing VG
sudo pvcreate /dev/sdd
sudo vgextend vg_data /dev/sdd
# Move data between PVs
sudo pvmove /dev/sdb /dev/sdd
# Remove old PV
sudo vgreduce vg_data /dev/sdb
Common Errors
1. lvresize Fails with "Can't Shrink XFS"
XFS cannot be shrunk. Use ext4 for volumes that need shrinking or create smaller volumes from the start.
2. Snapshot Runs Out of Space
When snapshot CoW space fills, the snapshot becomes invalid. Monitor with lvs -o+data_percent. Allocate 10-20% of source LV size.
3. Volume Group Not Found at Boot
The initramfs may not include LVM tools. Run sudo update-initramfs -u after creating LVM volumes.
4. pvcreate Fails on Partitioned Disk
Use a whole disk or create a partition with fdisk type 8e (Linux LVM). Wipe existing signatures with wipefs.
5. lvextend But df Shows Old Size
The filesystem resize step is separate from lvextend. Run resize2fs or xfs_growfs after extending the LV.
6. Cannot Remove PV That is Using LVs
Check pvdisplay -m to see which LVs use the PV. Move data with pvmove first.
7. Thin Pool Overprovisioning Causes Freeze
When thin pool fills, all thin LVs become read-only. Set dmeventd monitoring or add --errorwhenfull n to avoid freezes.
Practice Questions
1. What are the three layers of LVM? Physical Volumes (PV), Volume Groups (VG), Logical Volumes (LV).
2. How do you extend an LV and its filesystem in one command?
sudo lvextend -L +10G -r /dev/vg_data/lv_home — the -r flag resizes the filesystem automatically.
3. What is the difference between a snapshot and a thin volume? A snapshot captures another LV's state at a point in time using copy-on-write. A thin volume is a regular LV that allocates space from a thin pool on demand.
4. Can you shrink an XFS filesystem? No. XFS only supports growing. Use ext4 for volumes that may need shrinking.
5. Challenge: You have three 500GB disks. Create a VG with all three, allocate a 200GB LV for data, a 50GB snapshot space, and a thin pool using the remaining space. Show the complete command sequence.
Answer: pvcreate /dev/sd{b,c,d}, vgcreate vg_main /dev/sd{b,c,d}, lvcreate -L 200G -n lv_data vg_main, lvcreate -L 50G -s -n lv_data_snap vg_main/lv_data, lvcreate -L 1050G -T vg_main/thin_pool, lvcreate -V 500G -T vg_main/thin_pool -n thin_data1.
Mini Project: LVM Backup Strategy
Create a script that snapshots an LV, mounts it, runs a backup, and cleans up:
#!/bin/bash
# lvm_backup.sh — Snapshot and backup an LV
# Usage: sudo ./lvm_backup.sh /dev/vg_data/lv_home /backup
LV_PATH="$1"
BACKUP_DIR="${2:-/backup}"
SNAP_NAME="${LV_PATH##*/}_snap"
SNAP_SIZE="${3:-5G}"
if [ ! -e "$LV_PATH" ]; then
echo "Error: LV $LV_PATH not found"
exit 1
fi
sudo lvcreate -L "$SNAP_SIZE" -s -n "$SNAP_NAME" "$LV_PATH"
SNAP_PATH="${LV_PATH%/*}/${SNAP_NAME}"
sudo mkdir -p /mnt/snap
sudo mount -o ro "$SNAP_PATH" /mnt/snap
sudo mkdir -p "$BACKUP_DIR"
sudo tar czf "$BACKUP_DIR/backup-$(date +%F-%H%M).tar.gz" -C /mnt/snap .
sudo umount /mnt/snap
sudo lvremove -f "$SNAP_PATH"
echo "Backup complete: $BACKUP_DIR/backup-$(date +%F-%H%M).tar.gz"
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Updated 2026-06-24.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro