Block devices
A disk is cut into partitions, and file systems sit on them
Block devices are split into partitions; file systems live on partitions or on logical volumes. lsblk draws the tree.
Storage starts as a block device: a disk. This forty gigabyte disk is split into three partitions: a small one for the EFI firmware, one for boot, and a large one given to LVM. LVM then carves that into logical volumes: the root file system and swap. lsblk shows this whole tree, and with dash f, the type and unique ID of each file system. Everything you mount is one of these leaves.
Block devices · Going deeper · 1 of 2
GPT replaced MBR: more partitions, any size, a backup copy
GPT allows 128 partitions and disks beyond the 2 TiB MBR limit, and keeps a backup of the table at the end of the disk.
The partition table at the start of a disk records where each partition begins and ends. The old MBR format allows four primary partitions and cannot address beyond two tebibytes. GPT allows a hundred and twenty-eight, has no practical size limit, and keeps a second copy at the end of the disk in case the first is damaged. parted creates a GPT label and partitions, and can be scripted.
Block devices · Going deeper · 2 of 2
XFS or ext4: mostly your distribution’s default
XFS is RHEL’s default and cannot shrink; ext4 is Ubuntu’s and can; vfat is for EFI and removable media.
Which file system? On Red Hat systems, XFS; on Ubuntu, ext4. Both journal their changes and recover quickly after a crash. The practical difference: ext4 can be shrunk while unmounted, XFS cannot shrink at all. vfat is what the EFI partition and USB sticks use, Btrfs adds snapshots, and tmpfs lives entirely in memory.
Adding storage
Five steps from a raw disk to a mount that survives reboot
Partition, mkfs, make a directory, mount, and add the line to /etc/fstab; then test with mount -a.
Adding storage always takes the same five steps. Partition the disk. Make a file system on the partition. Make an empty directory to be the mount point. Mount it, which lasts until the next reboot. Add a line to the file system table, so it is mounted at every boot. Then test that line straight away with mount dash a, because a mistake in that file can stop the next boot.
fstab
Six fields per line, and name the file system by UUID
An fstab line gives what, where, type, options, dump and check order; mount by UUID because device names can change.
Each line of the file system table has six fields. What to mount, where, the type, the options, and two numbers for backup and checking order. Use the UUID for the first field, not the device name: device names depend on the order disks are detected, and adding one can shift them all. The UUID is stored inside the file system and never moves. Labels and LVM names are stable too, and options such as nofail let the machine boot even if this disk is missing.
LVM
Pool the disks, then cut volumes from the pool
Physical volumes form a volume group; logical volumes are cut from it and can span disks and grow online.
LVM adds a layer between disks and file systems. Each disk, or partition, becomes a physical volume. Physical volumes are pooled into a volume group, made of small extents, four mebibytes each. Logical volumes are cut from the pool, regardless of which disk the space is on: this logs volume spans both. pvcreate, vgcreate and lvcreate build it; pvs, vgs and lvs show it.
Growing a volume
A full file system, fixed in one command while in use
lvextend -r grows a logical volume and its file system together, online; XFS can grow but never shrink.
Here is why LVM is worth it. The logs volume is ninety-one per cent full. One command, lvextend dash r, adds four gibibytes to the volume and grows the file system on it, while it stays mounted and in use. If the group has no free space, add a disk to it first with vgextend. Forget dash r, and the volume grows but the file system does not.
Growing a volume · Going deeper · 1 of 3
Swap holds idle memory so RAM can do useful work
Swap stores inactive memory pages on disk; some use is normal, constant swapping means the machine needs more memory.
Swap is disk space the kernel uses for memory. On a busy server, programs use most of the memory and the rest caches files. Pages that nobody has touched for a while can be moved to swap, freeing RAM for cache or for programs that need it. You add swap with mkswap and swapon, and an fstab line of type swap. A little swap in use is fine. Constant swapping in and out is a server short of memory.
Growing a volume · Going deeper · 2 of 3
NFS joins a server’s folder to your tree; autofs mounts it on demand
NFS mounts a remote directory; autofs mounts it on first access and unmounts it when idle.
Storage can also come from another machine. An NFS server exports a directory. A client mounts it like a local disk, by hand or in the file system table. autofs is smarter: it watches a directory and mounts only when someone steps into it, then unmounts after a while idle. With a wildcard map, one line gives every user their own home folder from the server.
Disk quotas · Going deeper · 3 of 3
Cap what each user may store, with a warning line first
A quota sets a soft limit with a grace period and a hard limit per user or group on one file system.
Lab
From a blank disk to a permanent, growing mount
GPT, LVM, XFS and ext4, mounts by UUID and label, an online extension, new swap and an automounted NFS share.
In this lab you give a virtual machine a second disk and take it all the way: partition table, LVM, two file systems, permanent mounts, an online extension, extra swap and, if you have a second machine, NFS with autofs. Then you reboot and prove it all came back. Press the down arrow for the steps.
Lab · Going deeper
From a blank disk to a permanent, growing mount
Add a 10 GiB virtual disk to your VM (it appears as /dev/vdb or /dev/sdb; check with lsblk). Take a snapshot of the VM first: fstab mistakes stop boots.
Add a 10 GiB virtual disk to your VM (it appears as /dev/vdb or /dev/sdb; check with lsblk). Take a snapshot of the VM first: fstab mistakes stop boots. Steps. Look before you cut. lsblk -f; blkid; findmnt --real. Confirm which device is the new, empty one. Partition. parted -s /dev/vdb mklabel gpt mkpart lvm 1MiB 6GiB mkpart data 6GiB 100%; lsblk /dev/vdb. Build LVM. pvcreate /dev/vdb1; vgcreate vg1 /dev/vdb1; lvcreate -n data -L 2G vg1; pvs; vgs; lvs. Make two file systems. mkfs.xfs /dev/vg1/data; mkfs.ext4 -L archive /dev/vdb2. Mount them permanently. One fstab line by UUID for /data, one by LABEL=archive for /archive. mkdir both, systemctl daemon-reload, mount -a, findmnt --verify. Grow online. Fill /data with fallocate -l 1500M /data/big; lvextend -r -L +1G /dev/vg1/data; df -h /data. Add swap. lvcreate -n swap -L 512M vg1; mkswap; add it to fstab; swapon -a; swapon --show; free -h. Mount over the network. If you have a second VM: export a directory with NFS, mount it by hand, then configure autofs so it mounts on first cd. Prove it. Reboot. /data, /archive and the swap must all be back, and /data must show 3 GiB. Check: After a reboot, findmnt /data and findmnt /archive both answer, swapon --show lists the new swap, and df -h /data shows the grown size.
Steps
- Look before you cut. lsblk -f; blkid; findmnt --real. Confirm which device is the new, empty one.
- Partition. parted -s /dev/vdb mklabel gpt mkpart lvm 1MiB 6GiB mkpart data 6GiB 100%; lsblk /dev/vdb.
- Build LVM. pvcreate /dev/vdb1; vgcreate vg1 /dev/vdb1; lvcreate -n data -L 2G vg1; pvs; vgs; lvs.
- Make two file systems. mkfs.xfs /dev/vg1/data; mkfs.ext4 -L archive /dev/vdb2.
- Mount them permanently. One fstab line by UUID for /data, one by LABEL=archive for /archive. mkdir both, systemctl daemon-reload, mount -a, findmnt --verify.
- Grow online. Fill /data with fallocate -l 1500M /data/big; lvextend -r -L +1G /dev/vg1/data; df -h /data.
- Add swap. lvcreate -n swap -L 512M vg1; mkswap; add it to fstab; swapon -a; swapon --show; free -h.
- Mount over the network. If you have a second VM: export a directory with NFS, mount it by hand, then configure autofs so it mounts on first cd.
- Prove it. Reboot. /data, /archive and the swap must all be back, and /data must show 3 GiB.
Lesson complete
You finished “Disks and file systems”
Nicely done.