LVM: PVs, VGs & LVs
A partition is fixed: once /var/lib/postgresql sits on /dev/sdb1, growing it means downtime and careful repartitioning. The Logical Volume Manager (LVM) puts a layer between disks and filesystems, so storage can be pooled, grown online, moved between disks and snapshotted. Most installers (Ubuntu Server's "use LVM" default, RHEL) set it up for the root disk too. LVM commands need root and block devices: the transcripts are sample sessions from a practice VM with two extra 20 GB disks (or loop devices, see A Practice Server).
Three layers
| Layer | What it is | Commands |
|---|---|---|
| PV (physical volume) | a disk or partition initialised for LVM, cut into extents (4 MiB by default) | pvcreate, pvs, pvdisplay |
| VG (volume group) | a pool of extents from one or more PVs | vgcreate, vgextend, vgs |
| LV (logical volume) | a block device carved out of a VG's extents, which you format and mount | lvcreate, lvextend, lvs |
/dev/sdb (20G) /dev/sdc (20G) ← PVs
\ /
vg_data (≈40G) ← VG: one pool of 4 MiB extents
/ | \
lv_pg 15G lv_logs 5G free ≈20G ← LVs → /dev/vg_data/lv_pg, mounted at /var/lib/postgresqlKeeping free space in the VG is deliberate: you hand it out when a volume needs it, instead of guessing sizes on day one.
Building it
$ sudo pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
$ sudo vgcreate vg_data /dev/sdb /dev/sdc
Volume group "vg_data" successfully created
$ sudo lvcreate -n lv_pg -L 15G vg_data
Logical volume "lv_pg" created.
$ sudo lvcreate -n lv_logs -L 5G vg_data
Logical volume "lv_logs" created.
$ sudo mkfs.ext4 -q -L pgdata /dev/vg_data/lv_pg
$ sudo mkdir -p /var/lib/postgresql
$ echo '/dev/vg_data/lv_pg /var/lib/postgresql ext4 defaults,noatime 0 2' | sudo tee -a /etc/fstab
$ sudo systemctl daemon-reload && sudo mount -a
$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/sdb vg_data lvm2 a-- <20.00g 0
/dev/sdc vg_data lvm2 a-- <20.00g <19.99g
$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
vg_data 2 2 0 wz--n- 39.99g 19.99g
$ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_logs vg_data -wi-ao---- 5.00g
lv_pg vg_data -wi-ao---- 15.00g<20.00g is LVM saying "slightly less than 20 GiB": a little of each disk is used for LVM's own metadata, and sizes round down to whole extents. The /dev/vg_data/lv_pg path (also /dev/mapper/vg_data-lv_pg) is stable, so it is fine in /etc/fstab.
Extents, the unit of everything
All sizes in LVM are whole extents. lvcreate -L 15G asks for 15 GiB = 3840 extents of 4 MiB; -l 50%FREE or -l 100%FREE asks in extents directly. This model allocates LVs from a VG the way the default (linear) allocator does: from the first PV with free extents, spilling over to the next:
EXTENT_MIB = 4
pvs = {"/dev/sdb": 5119, "/dev/sdc": 5119} # free extents per PV (<20 GiB each)
layout = []
def lvcreate(name, size_gib=None, pct_free=None):
free_total = sum(pvs.values())
need = int(size_gib * 1024 // EXTENT_MIB) if size_gib else free_total * pct_free // 100
if need > free_total:
print(f"lvcreate {name}: Volume group \"vg_data\" has insufficient free space ({free_total} extents): {need} required.")
return
left = need
for pv in pvs:
take = min(left, pvs[pv])
if take:
pvs[pv] -= take; left -= take
layout.append((name, pv, take))
print(f"lvcreate {name}: {need} extents = {need * EXTENT_MIB / 1024:.2f} GiB")
lvcreate("lv_pg", size_gib=15)
lvcreate("lv_logs", size_gib=5)
lvcreate("lv_big", size_gib=25)
lvcreate("lv_cache", pct_free=50)
print()
for name, pv, n in layout:
print(f"{name:<9} {pv:<9} {n:>5} extents")
print("VG free:", sum(pvs.values()), "extents =", f"{sum(pvs.values()) * EXTENT_MIB / 1024:.2f} GiB")lvcreate lv_pg: 3840 extents = 15.00 GiB
lvcreate lv_logs: 1280 extents = 5.00 GiB
lvcreate lv_big: Volume group "vg_data" has insufficient free space (5118 extents): 6400 required.
lvcreate lv_cache: 2559 extents = 10.00 GiB
lv_pg /dev/sdb 3840 extents
lv_logs /dev/sdb 1279 extents
lv_logs /dev/sdc 1 extents
lv_cache /dev/sdc 2559 extents
VG free: 2559 extents = 10.00 GiB
lv_logs spans both disks. That is the trade-off of a linear VG over several disks: losing either disk loses that LV (and LVM striping, -i 2, makes every LV depend on all disks). LVM is not redundancy. Put the PVs on RAID (mdadm), use LVM's own RAID LVs (lvcreate --type raid1), or rely on the cloud provider's replicated block storage.
Day-to-day commands
| Task | Command |
|---|---|
| Overview | pvs, vgs, lvs (-o +devices shows which PVs an LV uses) |
| Add a disk to the pool | pvcreate /dev/sdd && vgextend vg_data /dev/sdd |
| Empty a disk before removing it | pvmove /dev/sdb (online), then vgreduce vg_data /dev/sdb |
| Rename | lvrename vg_data lv_pg lv_postgres (update fstab!) |
| Activate after attaching disks to another machine | vgchange -ay vg_data |
| Remove | umount, then lvremove vg_data/lv_logs |
Growing a volume and taking snapshots are the two features you will use most; they get their own lesson: Growing Volumes & LVM Snapshots.
Test yourself
Put the LVM layers in order from disk to filesystem.
- LV → VG → PV
- PV → VG → LV
- VG → PV → LV
- PV → LV → VG
Why keep free extents in the volume group instead of giving everything to LVs at once?
- LVM needs 50% free
- You can grow whichever volume runs out later, online, instead of guessing sizes up front
- Free space makes disks faster
- Snapshots are impossible otherwise
A linear VG spans two disks, and one disk fails. What happens to an LV with extents on both?
- Nothing, LVM mirrors data
- The LV is damaged: LVM alone provides no redundancy
- LVM moves the data automatically
- Only reads fail