Notes
Block devices
Disks appear as block devices under slash dev: sda for classic disks, nvme zero n one for NVMe drives, xvd or nvme names in the cloud. lsblk draws them as a tree with their partitions and where each is mounted. A partition is a slice of a disk, described in a table called GPT on modern systems. blkid shows each partition's filesystem type and its UUID, a unique ID you will use in a moment.
$ lsblk NAME SIZE TYPE MOUNTPOINT sda 50G disk ├─sda1 1G part /boot └─sda2 49G part / nvme1n1 100G disk $ sudo fdisk -l /dev/sda $ blkid UUIDs and types
Filesystems
A filesystem organises a partition into files, folders and inodes. We made a two hundred megabyte file, attached it as a loop device, the trick that turns a file into a disk, formatted it as ext four and mounted it. d f showed a hundred and seventy two megabytes, a hundred and fifty eight available. ext four is the general-purpose default on Ubuntu; xfs, the Red Hat default, handles very large files and filesystems well.
$ truncate -s 200M disk.img $ losetup -f --show disk.img /dev/loop0 $ mkfs.ext4 -q /dev/loop0 $ mount /dev/loop0 /mnt/lab $ df -h /mnt/lab 172M size, 158M available ext4: general purpose xfs: big files, RHEL default
fstab
A mount made by hand is gone after reboot. To make it permanent, add a line to fstab: which filesystem, where, its type and options. Always use the UUID, because device names like sdb can change between boots, especially in the cloud. Add nofail for disks the system can live without, and test with mount minus a before rebooting. As episode two showed, a bad fstab line is a classic way to make a server stop booting.
UUID=3f1c... /data ext4 defaults,nofail 0 2 use UUID, not /dev/sdb: device names can change nofail: boot even if the disk is missing $ sudo mount -a test it BEFORE rebooting
When it fills
What happens when a disk fills? We wrote three hundred megabytes of zeros into our two hundred megabyte disk. dd stopped after a hundred and sixty seven mebibytes with no space left on device, and d f showed zero available, one hundred percent. Databases stop accepting writes, logs stop, and apps fail in odd ways. ext four keeps about five percent reserved for root, so root processes can sometimes still write, which helps recovery but can confuse diagnosis.
$ dd if=/dev/zero of=fill
bs=1M count=300
175 MB (167 MiB) copied
... No space left on device
$ df -h /mnt/lab
172M size 168M used 0 avail
100%
root still had ~5% reserved LVM
LVM, the logical volume manager, adds a flexible layer. Disks become physical volumes, which pool into a volume group, from which you carve logical volumes that act like partitions. We pooled two three hundred megabyte disks into one volume group of five hundred and ninety two megabytes; a little goes to LVM's own records. Our sandbox lacked the device mapper module needed to create the logical volume, so that step runs in your lab VM, not here.
$ pvcreate /dev/loop1 /dev/loop2
$ vgcreate labvg /dev/loop1
/dev/loop2
$ vgs labvg
labvg 592.00m 592.00m free
(two 300 MB disks)
$ lvcreate -L 400M -n data labvg
$ mkfs.ext4 /dev/labvg/data Grow without downtime
LVM's big win is growing storage without downtime. Add a new disk, make it a physical volume, extend the volume group, then extend the logical volume with minus r, which also grows the filesystem inside. The database keeps running the whole time. One warning: xfs filesystems can grow but never shrink, so do not over-allocate. In the cloud you usually just resize the volume itself, then grow the partition and filesystem.
$ pvcreate /dev/sdc
$ vgextend datavg /dev/sdc
$ lvextend -r -L +50G
datavg/data
-r also grows the filesystem
(resize2fs or xfs_growfs)
online: no unmount, no reboot
xfs can grow, never shrink RAID
RAID combines disks for speed, safety or both. RAID zero stripes data across disks: fast, but losing one disk loses everything. RAID one mirrors: it survives one disk failure at half the space. RAID five and six use parity to survive one or two failures. RAID ten mirrors and stripes, fast and safe. Linux software RAID uses mdadm, with status in proc mdstat. And remember: RAID protects against a dead disk, not against a deleted table. It is not a backup.
- 0 stripe: fast, no safety
- 1 mirror: survives 1 disk, half the space
- 5 parity: survives 1 disk
- 6 parity: survives 2 disks
- 10 mirrors striped: fast and safe, half the space
- Linux: mdadm, /proc/mdstat
- RAID is NOT a backup
In the cloud
In the cloud, disks are virtual volumes, like Amazon's EBS, but the layers inside the server are the same. To grow one, modify the volume in the console or with Terraform, then inside the server grow the partition with growpart and the filesystem with resize two f s or xfs grow f s, all online. Snapshots back volumes up, and performance depends on the volume type and the input-output limits you pay for, which part eight covers.
- modify volume: 100 → 200 GB (online)
- $ sudo growpart /dev/nvme0n1 1
- $ sudo resize2fs /dev/nvme0n1p1 or xfs_growfs / snapshots for backup
- performance: size and type (gp3 IOPS, throughput)
Common mistakes
Mistakes. Device names instead of UUIDs in fstab. No nofail on optional disks. One partition for both the system and the data, so a runaway log stops the OS. No alert until the disk is already full; alert at eighty percent and on the growth rate. Treating RAID as a backup. And growing the cloud volume but forgetting the partition and filesystem, so nothing changes.
- /dev/sdb in fstab, not UUID
- no nofail on extra disks
- one partition for / and data
- no alert before 100%
- RAID treated as a backup
- growing the volume but not the filesystem
Say it like this
In the interview: first I find what grew, with du on the data folder, logs and write-ahead logs. I free space safely, by rotating logs or letting the database clean up its own files, never by deleting them by hand. Then I grow the storage online: lvextend with minus r on LVM, or resize the cloud volume and grow the partition and filesystem. Finally I add alerts on usage and on growth rate, so it never surprises us again.
- "First find what grew: du on the data dir, logs, WAL. Free
- space safely: rotate logs, clear
- old WAL via the DB, not rm.
- Then grow it online: LVM lvextend
- -r, or resize the cloud volume, growpart and resize2fs. Then an alert on usage and growth rate."