LVM: Resize and Snapshot Live Filesystems

LVM (Logical Volume Manager) is a layer between physical disks and filesystems that lets you:

  • Resize filesystems while they’re mounted.
  • Take snapshots for backup.
  • Pool multiple physical disks into one big logical disk.
  • Move data between disks without unmounting.

Most server installs use LVM by default. If you’ve ever seen /dev/mapper/vg0-root, that’s LVM.

The three layers

  1. Physical Volume (PV) — a real disk or partition, prepared for LVM use.
  2. Volume Group (VG) — a pool made up of one or more PVs.
  3. Logical Volume (LV) — a slice of the VG that you put a filesystem on.

Mental model: PVs are bricks → VG is a pile of bricks → LVs are houses you build from the pile.

Install LVM tools

sudo apt install lvm2          # Debian/Ubuntu
sudo dnf install lvm2          # Fedora/RHEL

See what’s there

sudo pvs                       # physical volumes (brief)
sudo pvdisplay                  # detailed
sudo vgs                       # volume groups
sudo vgdisplay
sudo lvs                       # logical volumes
sudo lvdisplay
lsblk                          # also shows LVM hierarchy

Create LVM from scratch

Start with a fresh disk like /dev/sdb:

# 1. Make the disk a PV (or partition it first if you want)
sudo pvcreate /dev/sdb

# 2. Create a volume group
sudo vgcreate data_vg /dev/sdb

# 3. Create logical volumes from the VG
sudo lvcreate -L 50G -n logs_lv data_vg
sudo lvcreate -L 100G -n db_lv data_vg
sudo lvcreate -l 100%FREE -n bulk_lv data_vg     # use the rest

# 4. Format
sudo mkfs.ext4 /dev/data_vg/logs_lv
sudo mkfs.ext4 /dev/data_vg/db_lv
sudo mkfs.xfs /dev/data_vg/bulk_lv

# 5. Mount
sudo mkdir -p /var/log/svc /var/db /data
sudo mount /dev/data_vg/logs_lv /var/log/svc
# add to /etc/fstab to persist

Grow a filesystem (no downtime)

Most common LVM operation. Source of LVM’s value.

# 1. Add space — either extend the PV or add a new disk to the VG
sudo vgextend data_vg /dev/sdc

# 2. Grow the LV
sudo lvextend -L +20G /dev/data_vg/db_lv
# OR use all free space
sudo lvextend -l +100%FREE /dev/data_vg/db_lv

# 3. Grow the filesystem (online, while mounted)
sudo resize2fs /dev/data_vg/db_lv      # ext4
sudo xfs_growfs /var/db                 # XFS — pass mount point

Snapshots: a point-in-time copy

LVM snapshots are copy-on-write — they only use disk space for blocks that change after the snapshot was taken. Great for backups: snapshot, back up the snapshot at leisure, delete it.

# 1. Create a snapshot
sudo lvcreate -L 5G -s -n db_snap /dev/data_vg/db_lv

# 2. Mount it (read-only is safest)
sudo mkdir -p /mnt/snap
sudo mount -o ro /dev/data_vg/db_snap /mnt/snap

# 3. Back up from the snapshot
tar czf /backup/db.tar.gz -C /mnt/snap .

# 4. Tear down
sudo umount /mnt/snap
sudo lvremove /dev/data_vg/db_snap

Important: snapshots fill up if writes exceed their size. Make snapshots big enough or use --monitor to grow automatically.

Move data to a different disk (online)

# Move all extents off /dev/sdb to other disks in the VG
sudo pvmove /dev/sdb

# Once empty, remove from VG
sudo vgreduce data_vg /dev/sdb

# Wipe and remove
sudo pvremove /dev/sdb

Shrink (more risky)

Shrinking ext4 requires unmount. XFS cannot shrink at all.

# Always back up first
sudo umount /var/db
sudo e2fsck -f /dev/data_vg/db_lv          # filesystem check
sudo resize2fs /dev/data_vg/db_lv 50G       # shrink filesystem first
sudo lvreduce -L 50G /dev/data_vg/db_lv     # then shrink LV
sudo mount /var/db

Remove things

sudo lvremove /dev/data_vg/old_lv
sudo vgremove data_vg                 # only if no LVs in it
sudo pvremove /dev/sdb                # cleanup PV header

Real-world workflow: out of disk space on /

(Cloud server, root is on LVM, you increased the disk size in the cloud console.)

# 1. Refresh the kernel's view of the disk
sudo growpart /dev/sda 3

# 2. Extend the PV
sudo pvresize /dev/sda3

# 3. Grow the LV (assuming root is /dev/mapper/ubuntu--vg-ubuntu--lv)
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

# 4. Grow the filesystem
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv      # or xfs_growfs /

df -h /

Common mistakes

  • Forgetting to grow the FILESYSTEM after growing the LV — extra space exists but isn’t usable.
  • Snapshot too small for write volume — fills up and gets invalidated.
  • Removing PVs before they’re empty — data loss.
  • Trying to shrink XFS — not supported. You must back up, recreate, restore.

What to learn next

That covers storage. Next chunk: security and hardening — how to lock down a server before someone else does.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *