Growing Volumes & LVM Snapshots
The two reasons most admins are glad they used LVM: a full volume can be grown while the service keeps running, and a volume can be snapshotted in a second before a risky upgrade or for a consistent backup. Both are root-only operations on block devices, so the transcripts are sample sessions (try them on a practice VM); the space accounting is modelled in Python.
Growing a volume online
A filesystem lives inside the LV, so growing is two steps: grow the LV, then grow the filesystem. lvextend -r (--resizefs) does both:
$ df -h /var/lib/postgresql
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_data-lv_pg 15G 14G 204M 99% /var/lib/postgresql
$ sudo vgs vg_data
VG #PV #LV #SN Attr VSize VFree
vg_data 2 2 0 wz--n- 39.99g 19.99g
$ sudo lvextend -r -L +10G vg_data/lv_pg
Size of logical volume vg_data/lv_pg changed from 15.00 GiB (3840 extents) to 25.00 GiB (6400 extents).
Logical volume vg_data/lv_pg successfully resized.
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/mapper/vg_data-lv_pg is mounted on /var/lib/postgresql; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/mapper/vg_data-lv_pg is now 6553600 (4k) blocks long.
$ df -h /var/lib/postgresql
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_data-lv_pg 25G 14G 10G 58% /var/lib/postgresql| Filesystem | Grow online | Shrink |
|---|---|---|
| ext4 | yes (resize2fs, called by lvextend -r) |
only unmounted (resize2fs then lvreduce, in that order) |
| XFS | yes (xfs_growfs /mountpoint, called by lvextend -r) |
never: back up, recreate, restore |
| Btrfs | yes (btrfs filesystem resize) |
yes, online |
Shrinking is where people lose data: reduce the filesystem first, then the LV, never the other way round. lvreduce -r does it in the right order; better still, don't hand out space you might want back.
When the VG itself is full, add a disk first (pvcreate, vgextend). On a cloud VM you can usually grow the existing virtual disk instead:
# Sample session: the cloud volume behind /dev/sdb was resized from 20G to 40G in the provider's console
$ sudo pvresize /dev/sdb
Physical volume "/dev/sdb" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
$ sudo lvextend -r -l +100%FREE vg_data/lv_pgWithout LVM, on a single-partition cloud disk: growpart /dev/nvme0n1 1 (from cloud-guest-utils) then resize2fs /dev/nvme0n1p1 or xfs_growfs /.
Snapshots: a frozen view in a second
An LVM snapshot is a new LV that shows the origin volume as it was when the snapshot was taken. Creating one is instant because nothing is copied up front: classic ("thick") snapshots use copy-on-write. When a block of the origin is about to change for the first time, its old content is first copied into the snapshot's own space. The snapshot therefore only needs room for the blocks that change while it exists, and if that room runs out, the snapshot becomes invalid and is useless.
def snapshot_usage(snap_mib, writes, block_mib=4):
"""writes: list of (hour, set of blocks written). COW copies each block once."""
copied = set()
for hour, blocks in writes:
new = blocks - copied
copied |= new
used = len(copied) * block_mib
pct = 100 * used / snap_mib
state = "INVALID (overflowed)" if used > snap_mib else f"{pct:5.1f}% used"
print(f"hour {hour}: wrote {len(blocks):>3} blocks, {len(new):>3} new to COW -> {state}")
if used > snap_mib:
break
# a 1 GiB snapshot of a database volume; the same hot blocks are rewritten every hour,
# then a nightly batch job rewrites a big table
hot = set(range(0, 40))
writes = [(1, hot), (2, hot), (3, hot | set(range(40, 60))), (4, set(range(100, 400)))]
snapshot_usage(snap_mib=1024, writes=writes)hour 1: wrote 40 blocks, 40 new to COW -> 15.6% used
hour 2: wrote 40 blocks, 0 new to COW -> 15.6% used
hour 3: wrote 60 blocks, 20 new to COW -> 23.4% used
hour 4: wrote 300 blocks, 300 new to COW -> INVALID (overflowed)
Rewriting the same blocks costs nothing after the first copy; a bulk write of new blocks fills the snapshot quickly. Size snapshots for the changes you expect while they exist, keep their life short, and watch Data% in lvs:
$ sudo lvcreate -s -n lv_pg_snap -L 2G vg_data/lv_pg
Logical volume "lv_pg_snap" created.
$ sudo lvs vg_data
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 owi-aos--- 25.00g
lv_pg_snap vg_data swi-a-s--- 2.00g lv_pg 3.41Two uses: backups and rollbacks
Consistent backups. Copying a live database's files gives a torn copy. With a snapshot: briefly pause or checkpoint the application (or rely on the database's crash recovery, which most handle), snapshot, resume, then back up from the snapshot at leisure:
$ sudo lvcreate -s -n pg_backup -L 5G vg_data/lv_pg
$ sudo mkdir -p /mnt/pg_backup && sudo mount -o ro /dev/vg_data/pg_backup /mnt/pg_backup
$ sudo rsync -aAX /mnt/pg_backup/ backup@vault1:/backups/web1/pg/
$ sudo umount /mnt/pg_backup && sudo lvremove -y vg_data/pg_backup
Logical volume "pg_backup" successfully removed.(XFS refuses to mount a snapshot next to its origin because both carry the same UUID; add -o ro,nouuid.)
Rollback before a risky change. Snapshot, upgrade, and if it goes wrong, merge the snapshot back into the origin:
$ sudo lvcreate -s -n before_upgrade -L 5G vg_data/lv_pg
$ # ... the upgrade goes wrong ...
$ sudo systemctl stop postgresql && sudo umount /var/lib/postgresql
$ sudo lvconvert --merge vg_data/before_upgrade
Merging of volume vg_data/before_upgrade started.
vg_data/lv_pg: Merged: 100.00%
$ sudo mount /var/lib/postgresql && sudo systemctl start postgresqlA snapshot is not a backup: it lives in the same VG, on the same disks. Lose the disk, lose both. Thin provisioning (lvcreate --type thin-pool, then thin volumes and thin snapshots) makes many snapshots cheap and has no fixed per-snapshot size, but the pool itself can fill up, so monitor it just as closely.
Test yourself
What does lvextend -r -L +10G vg_data/lv_pg do?
- Creates a 10G snapshot
- Grows the LV by 10 GiB and then resizes the filesystem inside it, online
- Shrinks the LV
- Moves the LV to another disk
A classic LVM snapshot of 2 GiB shows Data% at 100. What does that mean?
- The origin is full
- More than 2 GiB of origin blocks changed since the snapshot; it is now invalid
- The snapshot finished copying
- Nothing, it grows automatically
Which filesystem cannot be shrunk?
- ext4
- XFS
- Btrfs
- tmpfs