Partitions and LVM

Linux · Advanced · 14 min

Growing a full volume without downtime is a routine but nerve-wracking task unless you know LVM.

A disk is divided into partitions described by a partition table. GPT is the modern standard (large disks, many partitions, required for UEFI boot); the older MBR is limited to 2 TiB. Create partitions with fdisk, gdisk or scriptable parted, then let the kernel re-read the table (partprobe).

LVM (Logical Volume Manager) adds a flexible layer between disks and filesystems:

  • physical volumes (PV): disks or partitions initialised with pvcreate,
  • a volume group (VG): a pool built from one or more PVs with vgcreate,
  • logical volumes (LV): virtual block devices carved from the pool with lvcreate, used like partitions at /dev/vgname/lvname.

The payoff: when a volume fills up, add a disk to the VG (vgextend) and grow the LV while mounted with lvextend -r, where -r also resizes the filesystem. ext4 and XFS can grow online; only ext4 can shrink, and only unmounted. pvs, vgs and lvs summarise the layout.

Snapshots (lvcreate -s) freeze a point-in-time view, handy before an upgrade or for consistent backups; they consume space as the origin changes, so remove them afterwards.

In the cloud you often grow the disk itself: resize it in the provider, then growpart the partition and resize the PV or filesystem. Always take a backup before touching partition tables.

Example

sudo fdisk -l /dev/sdb                    # current partition table
sudo parted /dev/sdb --script mklabel gpt mkpart data 1MiB 100%
# LVM: physical volumes -> volume group -> logical volumes
sudo pvcreate /dev/sdb1 /dev/sdc1
sudo vgcreate vgdata /dev/sdb1 /dev/sdc1
sudo lvcreate -n lvapp -L 50G vgdata
sudo mkfs.xfs /dev/vgdata/lvapp
sudo pvs; sudo vgs; sudo lvs              # summaries
# Out of space? Add a disk and grow online:
sudo pvcreate /dev/sdd1 && sudo vgextend vgdata /dev/sdd1
sudo lvextend -r -L +20G /dev/vgdata/lvapp   # -r grows the filesystem too
sudo lvcreate -s -n before-upgrade -L 5G /dev/vgdata/lvapp   # snapshot
# Cloud disk resized in the console instead:
sudo growpart /dev/nvme0n1 1 && sudo resize2fs /dev/nvme0n1p1   # (xfs_growfs for XFS)

Remember

  • GPT partition tables; MBR only for legacy
  • LVM: PV -> VG -> LV, grow online with lvextend -r
  • Snapshots before risky changes; remove them afterwards

Watch out for

  • Extending the logical volume but forgetting to grow the filesystem, so df shows no change.

Check yourself

  1. You ran `lvextend -L +20G /dev/vg/lv` and df still shows the old size. What is missing?
  2. What is a volume group in LVM?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: man7.org/linux/man-pages/man8/fdisk.8.html

Builds on

Unlocks

Nothing builds on this yet.