1 of 4

File Systems, NFS and AutofsLesson 1 of 4

XFS, ext4 and VFAT: create, label and mount permanently

Choose a file system, format it, give it a label, and mount it at boot by UUID or label, then check the result before rebooting.

  • mkfs.xfs
  • mkfs.ext4
  • mkfs.vfat
  • blkid
  • xfs_admin
  • e2label
  • fatlabel
  • findmnt
  • findmnt --verify
  • mount -a
  • umount
  • fuser

45 min5 exercises

After this lesson you can

  • Pick between XFS, ext4 and VFAT for a requirement, and create each one.
  • Set a label or read a UUID, and write an /etc/fstab line that uses it.
  • Validate fstab without rebooting, and unmount a busy file system.

The idea

A new partition or logical volume is raw space. To store files it needs a file system: the on-disk structure of names, directories and data. On RHEL you will meet three:

File systemCreated withProperties
XFSmkfs.xfsRHEL default. Fast with big files and many files, grows online, cannot shrink
ext4mkfs.ext4Mature and flexible, can be shrunk (offline), good for small volumes
VFATmkfs.vfat (package dosfstools)Works on every operating system; no Unix owners or permissions

mkfs.* erases whatever was on the device. Check the device name twice, and use lsblk -f first: a device that already shows a file system type has data.

Labels and UUIDs

Device names such as /dev/vdb1 can change from one boot to the next when disks are added or detected in a different order. Two stable identifiers fix that:

  • UUID: a unique ID generated when the file system is created. Always unique. blkid shows it.
  • Label: a name you choose, for example webdata. Easy to read; you must ensure it is unique yourself.

Set a label at creation (mkfs.xfs -L webdata, mkfs.vfat -n EXCHANGE), or later with xfs_admin -L (unmounted), e2label DEV NAME (ext4) or fatlabel DEV NAME (VFAT).

The fstab line

/etc/fstab lists what to mount at boot. Six fields:

UUID=3f1c... /srv/ext4 ext4 defaults 0 0
│            │         │    │        │ └─ fsck order (0 = skip; XFS always 0)
│            │         │    │        └─── dump flag (always 0)
│            │         │    └──────────── mount options
│            │         └───────────────── file system type
│            └─────────────────────────── mount point (must exist)
└──────────────────────────────────────── device: UUID=, LABEL= or a path

defaults means rw,suid,dev,exec,auto,nouser,async. Useful additions: nofail (boot continues if the device is missing), noauto (do not mount at boot), ro, noexec, _netdev (network file systems, wait for the network).

A mistake in fstab can stop a machine from booting normally, so test before you reboot: mount -a (mounts everything listed that is not mounted, and reports errors now) and findmnt --verify (checks syntax, targets and sources).

Try it

sudo mkfs.xfs -L webdata /dev/vdb1
sudo blkid /dev/vdb1
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=32000 blks
data     =                       bsize=4096   blocks=128000, imaxpct=25
log      =internal log           bsize=4096   blocks=16384, version=2
/dev/vdb1: LABEL="webdata" UUID="e0d2c1c4-2b1b-4a6e-9c59-6d8c8e3e4f10" TYPE="xfs" PARTUUID="6f1d8f3b-01"
sudo mkdir -p /srv/webdata
echo 'LABEL=webdata /srv/webdata xfs defaults 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo mount -a
findmnt /srv/webdata
TARGET        SOURCE    FSTYPE OPTIONS
/srv/webdata  /dev/vdb1 xfs    rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota

ext4 by UUID, built without typing the UUID

sudo mkfs.ext4 /dev/vdb2
uuid=$(sudo blkid -s UUID -o value /dev/vdb2)
echo "UUID=$uuid /srv/ext4 ext4 defaults 0 0" | sudo tee -a /etc/fstab
UUID=8a5d6a1b-7c0e-4c3d-b6f1-0f8c8a9a1b44 /srv/ext4 ext4 defaults 0 0

Copying a 36-character UUID by hand invites typos. Command substitution does not.

VFAT has no permissions of its own

sudo mkfs.vfat -n EXCHANGE /dev/vdb3
echo 'LABEL=EXCHANGE /mnt/exchange vfat defaults,umask=000 0 0' | sudo tee -a /etc/fstab
sudo mkdir -p /mnt/exchange && sudo systemctl daemon-reload && sudo mount -a
ls -ld /mnt/exchange
mkfs.fat 4.2 (2021-01-31)
drwxrwxrwx. 2 root root 16384 Jan  1  1970 /mnt/exchange

umask=000 removes no permission bits, giving rwx to everyone. Because VFAT cannot store owners or modes, chmod and chown fail or do nothing; ownership comes only from uid=, gid=, umask=, fmask= and dmask= options.

Check before you reboot

sudo findmnt --verify
sudo mount -a
Success, no errors or warnings detected

With a wrong UUID in the file, findmnt --verify reports the source as unreachable, and mount -a prints can't find UUID=.... At boot, the same mistake on a normal line makes systemd wait and then drop to emergency mode (a root-password prompt). Add nofail for non-essential mounts.

Busy file systems

umount /srv/webdata fails with target is busy when a process has a file open or its shell is inside it. Find the cause with sudo fuser -vm /srv/webdata or lsof +f -- /srv/webdata, cd out, and try again.

Going further

xfs_info /srv/webdata shows geometry and tune2fs -l DEV the ext4 superblock. xfs_repair and fsck repair unmounted file systems; XFS is never fsck-ed at boot, which is why its fstab pass is 0. mount -o remount,ro /srv/webdata changes options in place. systemd also creates .mount units from fstab lines; systemctl list-units --type=mount shows them, and x-systemd.automount mounts on first access. Prefer UUID for anything non-removable and a label when a human has to recognise the volume.

Practice

0 of 5 done

Do these on your own system. They get harder. Check each result, then tick it off.

  1. Warm-up

    Which file system, and why?

    Choose a file system for each case and justify it in one line: (a) a general-purpose server volume that may grow; (b) a USB stick shared with Windows and macOS; (c) a volume that must be shrunk later.

    Check your result

    (a) XFS: default, grows online; (b) VFAT (or exFAT): understood by every OS; (c) ext4: it can be shrunk, offline. XFS cannot shrink.

    Hint

    Think about portability and resizing. RHEL's default is XFS.

    Solution
    (a) XFS   - default on RHEL, scales well, can be extended while mounted
    (b) VFAT  - readable and writable on Windows, macOS and Linux
    (c) ext4  - can be reduced (unmounted); XFS can only grow
  2. Practice

    XFS with a label, mounted by label

    On partition /dev/vdb1 (500 MiB) create an XFS file system labelled webdata. Mount it on /srv/webdata permanently by label. Verify with findmnt and mount -a.

    Check your result

    blkid /dev/vdb1 shows LABEL="webdata" TYPE="xfs". findmnt /srv/webdata lists it. sudo mount -a prints nothing.

    Hint

    mkfs.xfs -L webdata /dev/vdb1. fstab: LABEL=webdata /srv/webdata xfs defaults 0 0.

    Solution
    sudo mkfs.xfs -L webdata /dev/vdb1
    sudo mkdir -p /srv/webdata
    echo 'LABEL=webdata /srv/webdata xfs defaults 0 0' | sudo tee -a /etc/fstab
    sudo systemctl daemon-reload
    sudo mount -a
    findmnt /srv/webdata
  3. Practice

    ext4, mounted by UUID

    On logical volume /dev/vgdata/lvext (or partition /dev/vdb2) create an ext4 file system, read its UUID, and mount it on /srv/ext4 permanently using the UUID. Use blkid to get the value rather than typing it.

    Check your result

    findmnt /srv/ext4 shows ext4. grep ext4 /etc/fstab shows a UUID= line identical to blkid -s UUID -o value DEVICE.

    Hint

    mkfs.ext4 DEVICE, blkid -s UUID -o value DEVICE. Use command substitution to build the fstab line.

    Solution
    sudo mkfs.ext4 /dev/vdb2
    sudo mkdir -p /srv/ext4
    uuid=$(sudo blkid -s UUID -o value /dev/vdb2)
    echo "UUID=$uuid /srv/ext4 ext4 defaults 0 0" | sudo tee -a /etc/fstab
    sudo systemctl daemon-reload
    sudo mount -a
    findmnt /srv/ext4
  4. Practice

    VFAT with a label

    Create a VFAT file system labelled EXCHANGE on /dev/vdb3 and mount it on /mnt/exchange so that every user can read and write the files. Persist it. Explain why chmod on the files does nothing here.

    Check your result

    findmnt /mnt/exchange shows vfat with umask=000 (or uid/gid/fmask/dmask). Any user can create a file there. VFAT has no Unix owners or modes, so permissions come from mount options.

    Hint

    mkfs.vfat -n EXCHANGE /dev/vdb3. Options: defaults,umask=000.

    Solution
    sudo dnf install -y dosfstools
    sudo mkfs.vfat -n EXCHANGE /dev/vdb3
    sudo mkdir -p /mnt/exchange
    echo 'LABEL=EXCHANGE /mnt/exchange vfat defaults,umask=000 0 0' | sudo tee -a /etc/fstab
    sudo systemctl daemon-reload
    sudo mount -a
    findmnt /mnt/exchange
    touch /mnt/exchange/hello && ls -l /mnt/exchange
  5. Challenge

    Prove fstab is right before rebooting

    Run the checks that tell you /etc/fstab is safe: syntax and device check, mount everything, and show the final list of the three new mounts. Then introduce a deliberate mistake (wrong UUID), observe how each check reports it, and fix it. State what happens at boot if you reboot with the mistake in place.

    Check your result

    findmnt --verify reports Success, no errors or warnings detected for the correct file and an error such as unreachable on boot required source for the bad one. A required mount that fails at boot drops the system to emergency mode unless the entry has nofail.

    Hint

    findmnt --verify --verbose, mount -a. The option nofail allows boot to continue when the device is absent.

    Solution
    sudo findmnt --verify
    sudo mount -a
    sudo cp -a /etc/fstab /root/fstab.good
    sudo sed -i 's#^UUID=[^ ]* /srv/ext4#UUID=00000000-0000-0000-0000-000000000000 /srv/ext4#' /etc/fstab
    sudo findmnt --verify
    sudo mount -a
    sudo cp -a /root/fstab.good /etc/fstab
    sudo systemctl daemon-reload
    sudo findmnt --verify