Disks, Partitions & LVM
From raw blocks to a mounted filesystem, a full disk at 3 a.m., and a volume you can grow without downtime.
An interactive Linux lesson: 24 steps, about 35 minutes, on a live simulation in your browser.
web01 runs a shop. Customers upload avatars and invoices into /srv/uploads, and today that directory sits on the same filesystem as the operating system. One flood of uploads and the whole machine is out of space. Two empty 20G disks have been attached to fix that.
lsblk lists block devices as a tree. Read each row left to right: NAME, SIZE, TYPE, and where it is mounted. sda is a 40G disk. Indented under it are two part rows: sda1, 1G, mounted on /boot/efi, and sda2, 39G, mounted on /. sdb and sdc are disks with nothing under them and no mount point.
What you will learn
The storage stack
- Read lsblk as a stack: Storage is a stack: a block device, cut into partitions, a filesystem written on one, mounted on a directory. lsblk draws that stack.
- Disks are files in /dev: /dev/sdb is the whole disk as one long row of numbered blocks. /dev/sdb1 is a slice of that row.
- Mount a blank disk: A disk gives you blocks. A filesystem is the index that turns blocks into files. No index, nothing to mount.
Filesystems and mounts
- Cut a partition
- mkfs writes the index: mkfs writes an empty index onto a device. It creates a filesystem and destroys whatever filesystem was there before.
- Mount over a directory with files in it: Mounting makes a directory show the root of another filesystem. What the directory held before is hidden, not deleted and not moved.
- Break it: umount says target is busy
- Drill: mount it again
Where did the space go
- df counts filesystems, du counts files: df answers: how full is this filesystem. du answers: how big is this directory. df shows you the problem, du finds it.
- Break it: the disk fills up: A full filesystem fails every write on it and nothing outside it. Separate filesystems are firewalls for disk space.
- Find what filled it, then free it
Surviving a reboot
- /etc/fstab: mounts that survive a reboot: A mount typed by hand lasts until reboot. /etc/fstab is the list mounted at boot, and it should name a filesystem by UUID, not by device letter.
- Break it: a bad line in fstab: fstab is checked when it is used, not when it is saved. Test every edit with mount -a while you still have a shell.
LVM: a pool between disk and filesystem
- The partition cannot grow: A partition is a fixed slice of one disk. LVM puts a pool between disks and filesystems, so a volume can grow and can span disks.
- Physical volume, volume group: PV: a disk given to LVM. VG: a pool made of PVs. LV: a slice of the pool that behaves like a partition you can resize.
- Carve a logical volume
- Same three steps on top
Growing it live
- Full again: add a disk to the pool
- Extend the volume, then look at df: lvextend grows the container. The filesystem inside still ends where it used to, until you grow that too.
- resize2fs: the step everyone forgets: Three layers, three grow commands: vgextend the pool, lvextend the volume, resize2fs the filesystem. Skip the last and df does not move.
- Swap, briefly
- Drill: five more gigabytes
Recap & playground
- Cheat sheet
- Playground