Block devices, filesystems and mounts

Linux · Advanced · 12 min

Adding a volume to a server or recovering a non-booting one requires understanding how disks become directories.

Disks appear as block devices in /dev: /dev/sda, /dev/nvme0n1, /dev/vdb in VMs, with partitions such as /dev/sda1 or /dev/nvme0n1p1. lsblk -f shows the whole tree with filesystem types, labels, UUIDs and mount points; blkid prints UUIDs.

A partition or volume must hold a filesystem before use: mkfs.ext4 or mkfs.xfs create one (and destroy what was there). ext4 is the solid default, XFS is the RHEL default and handles large files well but cannot shrink, Btrfs offers snapshots and checksums.

Mounting attaches a filesystem to a directory: mount /dev/sdb1 /mnt/data; umount detaches it. "target is busy" means a process still uses it (check with lsof or fuser -vm). findmnt shows what is mounted where, with options.

To mount at boot, add a line to /etc/fstab: device, mount point, type, options, dump, fsck order. Always refer to the device by UUID (or label), since /dev/sdX names can change between boots. Useful options: defaults, noatime, ro, and `nofail` for non-essential disks so a missing disk does not stop the boot. Test with findmnt --verify and mount -a before rebooting; a bad fstab can drop the machine into emergency mode.

Special filesystems need no disk: tmpfs lives in RAM, and network filesystems (NFS, SMB) mount remote shares.

Example

lsblk -f                            # devices, filesystems, UUIDs, mount points
findmnt /                           # what is mounted at /, with options
sudo mkfs.ext4 -L data /dev/sdb1    # new filesystem (DESTROYS existing data)
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data
sudo blkid /dev/sdb1                # UUID="3f1c..." TYPE="ext4" LABEL="data"
# /etc/fstab  (by UUID, never /dev/sdX):
# UUID=3f1c...  /mnt/data  ext4  defaults,noatime,nofail  0  2
sudo findmnt --verify               # check fstab before rebooting
sudo systemctl daemon-reload && sudo mount -a   # mount everything listed
sudo umount /mnt/data               # "target is busy"? -> sudo fuser -vm /mnt/data
sudo mount -t tmpfs -o size=1G tmpfs /mnt/scratch   # RAM-backed filesystem

Remember

  • lsblk -f and blkid identify devices, filesystems and UUIDs
  • mkfs creates a filesystem; mount attaches it to a directory
  • fstab by UUID with nofail; verify before rebooting

Watch out for

  • Using /dev/sdb1 in fstab, then a new disk shifts the names and the wrong device (or none) is mounted.

Check yourself

  1. Why should /etc/fstab refer to filesystems by UUID?
  2. What does the `nofail` mount option do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: man7.org/linux/man-pages/man8/mount.8.html