Disks, partitions and mounts
Linux names every disk as a block device in /dev: sda, sdb, nvme0n1. A partition table slices a disk into partitions, a filesystem such as ext4 turns a partition into a place for files, and mount attaches it to a folder of the one directory tree that starts at /. Follow a new, empty disk from lsblk to sfdisk, mkfs.ext4, mount, a copy, df, /etc/fstab and umount.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01lsblk shows sdb with a child sdb1. What is sdb1?
sdb is the whole second disk; the number names a partition on it, so sdb1 is its first partition.
02Which of these commands can destroy data if you give it the wrong device name?
mkfs.ext4 writes a new, empty filesystem over the partition, erasing what it held (sfdisk is just as dangerous for the partition table). lsblk, df and blkid only read. Always check the name with lsblk first.
03After sudo mount /dev/sdb1 /mnt/data, you copy a file into /mnt/data. Where are its bytes stored?
You only name a path. The kernel sees that /mnt/data is the mount point of sdb1, so everything below that folder is read from and written to sdb1.
04Why does a line in /etc/fstab usually name the filesystem by UUID instead of /dev/sdb1?
The kernel hands out sda, sdb ... in the order it finds disks, which can change between boots. The filesystem's UUID (shown by blkid) belongs to the filesystem itself.
05You run sudo umount /mnt/data. What happens to notes.txt, which you copied there?
umount only detaches the filesystem from the tree. The file stays on sdb1 and appears again under whatever folder you next mount sdb1 on.
Glossary
- Block device
- Storage the kernel reads and writes in fixed-size blocks, such as a disk (/dev/sda) or one of its partitions (/dev/sda1).
- Partition
- A slice of a disk, recorded in the disk's partition table. Linux names them by adding a number: sdb1 is the first partition of sdb.
- Partition table
- A small table at the start of a disk listing its partitions. GPT is the modern kind; it keeps a backup copy at the end of the disk.
- Filesystem
- The structures written into a partition (for ext4: a superblock, inode tables and bitmaps) that keep track of files, folders and free space.
- Mount
- Attaching a filesystem to a folder of the directory tree, so the files on it appear under that folder.
- Mount point
- The folder a filesystem is mounted on, such as /mnt/data. Its own contents are hidden while something is mounted there.
- /etc/fstab
- The file that lists the filesystems to mount at boot: one line each, with six fields (what, where, type, options, dump, check order).
- UUID
- A unique id stored in a filesystem. blkid prints it; fstab uses it because it does not change when disk names do.
Notes and sources
- The machine is an example with two disks, sda and sdb. Real lsblk output often also lists loop devices and the CD drive (sr0), and the card leaves out the sizes because they depend on your disk.
- sfdisk and mkfs destroy what is on the device you name. Run lsblk first, make sure the name is the new, empty disk, and never point them at a mounted partition.
- NVMe drives are named like nvme0n1, with partitions nvme0n1p1. Disk names can change between boots when disks are added or removed, which is why /etc/fstab uses the UUID.
- A mistake in /etc/fstab can stop the machine from booting. After editing, run sudo findmnt --verify and then sudo mount -a to test it; on systemd distributions run sudo systemctl daemon-reload too. The nofail option lets the boot carry on if the disk is missing.
- The kernel keeps freshly written data in memory for a moment and writes it to the disk soon after. umount (or sync) makes sure all of it has reached the disk.
- /mnt is the standard place for mounts you make by hand (Filesystem Hierarchy Standard 3.0). Desktops mount USB sticks under /media or /run/media instead.
- Linux man-pages: lsblk(8), list block devices
- Linux man-pages: sfdisk(8), script-oriented partitioning
- Linux man-pages: mke2fs(8), create an ext2/ext3/ext4 filesystem (mkfs.ext4)
- Linux man-pages: mount(8), mount a filesystem
- Linux man-pages: umount(8), unmount filesystems
- Linux man-pages: df(1), report file system space usage
- Linux man-pages: fstab(5), static information about the filesystems
- Linux man-pages: blkid(8), locate and print block device attributes
- Linux man-pages: findmnt(8), find a filesystem (and --verify for fstab)
- Linux man-pages: sd(4), driver for SCSI disk drives (sda, sdb)
- The Linux kernel documentation: Linux allocated devices
- The Linux kernel documentation: ext4 Data Structures and Algorithms
- UEFI Specification 2.10: GUID Partition Table (GPT) Disk Layout
- Filesystem Hierarchy Standard 3.0: /mnt
- ArchWiki: Device file (block device names, NVMe naming)
- ArchWiki: fstab
Quiz Unlocks after the last step.