TExcelTAD
Home › Linux Server Administration › Disks, Mounts & /etc/fstab

Disks, Mounts & /etc/fstab

Linux has one directory tree. A disk, a partition, a logical volume or a network share becomes part of it by being mounted on a directory. Servers typically keep data off the root filesystem (/var/lib/postgresql on its own volume, /var/log separate so runaway logs can't fill /), so mounting and /etc/fstab are daily admin work. Mounting needs root and real block devices, so the lsblk/mount transcripts are sample sessions; /etc/fstab itself can be checked here.

From disk to mounted filesystem

bash
$ lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
NAME    SIZE TYPE FSTYPE MOUNTPOINTS
sda      40G disk
├─sda1    1M part
├─sda2    2G part ext4   /boot
└─sda3   38G part ext4   /
sdb     100G disk                         ← a new, empty data disk
$ sudo parted -s /dev/sdb mklabel gpt mkpart data ext4 1MiB 100%
$ sudo mkfs.ext4 -L data /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 26213888 4k blocks and 6553600 inodes
...
$ sudo blkid /dev/sdb1
/dev/sdb1: LABEL="data" UUID="9b1e7c44-2d3a-4c5e-8f60-1a2b3c4d5e6f" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="data" PARTUUID="5c7e…"
$ sudo mkdir -p /srv/data && sudo mount /dev/sdb1 /srv/data
$ findmnt /srv/data
TARGET    SOURCE    FSTYPE OPTIONS
/srv/data /dev/sdb1 ext4   rw,relatime

Which filesystem? ext4 is the safe default on Debian/Ubuntu; XFS is RHEL's default and good for large files and parallel I/O (it can grow but never shrink); Btrfs and ZFS add snapshots and checksums at the cost of more to learn. For a cloud VM, the provider's block volumes (EBS, Persistent Disk, Managed Disks) appear as ordinary disks like sdb or nvme1n1.

/etc/fstab, field by field

A mount by hand is gone after a reboot. /etc/fstab lists the mounts to make at boot (systemd turns each line into a .mount unit):

text
# <source>                                <target>   <type> <options>                     <dump> <pass>
UUID=9b1e7c44-2d3a-4c5e-8f60-1a2b3c4d5e6f /srv/data  ext4   defaults,noatime,nofail       0      2
Field Notes
source use UUID= or LABEL=, never /dev/sdb1: device names can change between boots (add a disk, and sdb becomes sdc). LVM paths like /dev/vg0/data are stable
target the directory; it must exist
type ext4, xfs, tmpfs, nfs, swap…
options comma-separated, see below
dump always 0 today
pass fsck order at boot: 1 for /, 2 for other local filesystems, 0 for none (tmpfs, network, swap)

Options worth knowing:

Option Effect
nofail boot continues if this disk is missing; without it, a detached data volume drops the server into emergency mode, unreachable over SSH
noatime don't write an access time on every read: fewer writes
nodev,nosuid,noexec no device files, no setuid, no executing binaries from this filesystem: good for /tmp, /var/tmp, upload directories
ro read-only
x-systemd.automount mount on first access instead of at boot (network shares)
_netdev a network filesystem: wait for the network
x-systemd.device-timeout=10s how long to wait for the device before giving up

Checking fstab before you reboot

A bad fstab line is one of the classic ways to make a server unbootable. findmnt --verify parses the file and checks every entry. Here it checks a file with a mistake many people make when they comment out half a line:

bash · try it yourself
cat > fstab <<'EOF'
# <source>     <target>  <type>  <options>                                 <dump> <pass>
tmpfs          /tmp      tmpfs   defaults,size=1G,mode=1777,nodev,nosuid   0      0
/dev/vg0/logs  /var/log  ext4    defaults,nodev,nosuid,noexec              0      2
/srv/data                ext4    defaults                                  0      2
EOF
findmnt --verify --verbose --tab-file fstab 2>&1 | grep -v 'cannot detect'

0 parse errors, 1 error, 5 warnings
/tmp
   [ ] target exists
   [ ] VFS options: nodev,nosuid
   [ ] FS options: size=1G,mode=1777
   [ ] do not check tmpfs source (pseudo/net)
   [ ] do not check tmpfs FS type (pseudo/net)
/var/log
   [ ] target exists
   [ ] VFS options: nodev,nosuid,noexec
   [W] unreachable source: /dev/vg0/logs: No such file or directory
ext4
   [E] unreachable on boot required target: No such file or directory
   [ ] FS options: 0
   [W] unreachable source: /srv/data: No such file or directory
   [W] defaults seems unsupported by the current kernel

Read it entry by entry. /tmp is fine. For /var/log, the sandbox has no LVM volume /dev/vg0/logs, which is only a warning here (on the server it would exist). The last line is the real bug: the target is missing, so every field shifted left. findmnt now thinks the mount point is called ext4, the type is defaults, and the options are 0. On a real server, systemctl daemon-reload would show a similar complaint, and the next boot would stop in emergency mode.

The safe routine on a server (sample session):

bash
$ sudo cp /etc/fstab /etc/fstab.bak
$ sudoedit /etc/fstab
$ sudo findmnt --verify                 # 0 errors?
Success, no errors or warnings detected
$ sudo systemctl daemon-reload          # regenerate the .mount units
$ sudo mount -a                         # mount everything not yet mounted: errors show NOW, not at boot
$ findmnt /srv/data
TARGET    SOURCE    FSTYPE OPTIONS
/srv/data /dev/sdb1 ext4   rw,noatime

mount -a is the rehearsal for the next boot. If it fails, you are still logged in and can fix it.

Network filesystems and swap

text
nfs1:/exports/media   /srv/media   nfs4   _netdev,nofail,x-systemd.automount,x-systemd.idle-timeout=600   0 0
/swapfile             none         swap   sw                                                              0 0

A swap file is created with fallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile. Small cloud VMs often ship without swap; a little swap gives the kernel room before the OOM killer steps in.

Test yourself

Why use UUID= instead of /dev/sdb1 in /etc/fstab?

  1. UUIDs are faster
  2. Device names can change between boots; the UUID identifies the filesystem itself
  3. fstab doesn't accept /dev paths
  4. UUIDs encrypt the disk

A cloud data volume was detached, and the server no longer boots to SSH. Which fstab option would have prevented that?

  1. noatime
  2. nofail
  3. defaults
  4. pass 0

What is the best way to test an fstab change before rebooting?

  1. Reboot and hope
  2. findmnt --verify, then systemctl daemon-reload and mount -a while you are still logged in
  3. cat /etc/fstab
  4. df -h