Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Storage and Filesystems

Filesystems, mkfs, mount and /etc/fstab

Create ext4, XFS and Btrfs filesystems with mkfs, mount them by UUID, make mounts permanent in /etc/fstab, check and repair with fsck and grow them safely.

A partition is raw space. A filesystem turns it into files and directories by keeping track of names, sizes, permissions and where the data blocks are. Choosing, creating, mounting and maintaining filesystems is core storage work.

Common filesystems

Filesystem Strengths Notes
ext4 Mature, reliable, general purpose; shrinkable Default on Debian and Ubuntu
XFS Great with large files and parallel I/O; grow only Default on Red Hat family
Btrfs Snapshots, checksums, compression, subvolumes Needs more care and knowledge
ZFS Pools, snapshots, strong integrity Separate module, license differs
tmpfs Lives in RAM /tmp, /run
vfat, exfat Compatible with other systems and firmware EFI partition, USB sticks
NFS, CIFS Network filesystems Mount remote shares

Most servers do well with ext4 or XFS.

Creating a filesystem

Terminal
sudo mkfs.ext4 -L data /dev/sdb1
sudo mkfs.xfs -L logs /dev/sdc1
sudo mkfs.btrfs -L pool /dev/sdd1

mkfs erases the filesystem it replaces. Double-check the device with lsblk -f first; a device that already shows a filesystem and mount point probably holds data.

Mounting

Terminal
sudo mkdir -p /data
sudo mount /dev/sdb1 /data
sudo mount -o ro,noexec /dev/sdb1 /mnt/check     # options
sudo mount UUID=1111-aaaa /data
findmnt /data
sudo umount /data                                # or the device name

If umount says the target is busy, find what uses it:

Terminal
sudo lsof +f -- /data
sudo fuser -vm /data

Useful options: ro read-only, noexec forbid running programs, nosuid ignore setuid bits, nodev ignore device files, noatime skip access-time writes. The last four are also hardening choices for /tmp and data volumes.

Making mounts permanent: /etc/fstab

TEXT
# device          mount   type  options           dump pass
UUID=1111-aaaa    /data   ext4  defaults,noatime  0    2
LABEL=logs        /var/log xfs  defaults          0    2
/swapfile         none    swap  sw                0    0

Fields: device, mount point, type, options, dump (almost always 0) and pass (fsck order: 1 for root, 2 for others, 0 to skip).

Get the UUID with blkid or lsblk -f. Then test before rebooting:

Terminal
sudo mount -a                 # mounts everything in fstab; errors show now
findmnt --verify              # checks syntax and options
sudo systemctl daemon-reload  # systemd regenerates mount units from fstab

A wrong line can stop the machine booting into emergency mode. Add nofail for non-critical disks so a missing drive does not block boot, and keep a console or rescue option available.

Checking and repairing

Terminal
sudo umount /data
sudo fsck -n /dev/sdb1              # dry run, report only
sudo fsck.ext4 -f /dev/sdb1
sudo xfs_repair /dev/sdc1           # XFS (unmounted)
sudo e2label /dev/sdb1 newname      # change ext label

Never run fsck on a mounted, writable filesystem; it can corrupt it. The root filesystem is checked at boot or from rescue media.

Growing a filesystem

After enlarging a partition or logical volume, grow the filesystem to match:

Terminal
sudo resize2fs /dev/sdb1          # ext4, can run mounted
sudo xfs_growfs /data             # XFS, give the mount point, can run mounted
sudo btrfs filesystem resize max /data

Shrinking is possible for ext4 (unmounted, shrink the filesystem before the partition) but not for XFS. Prefer LVM for flexible sizes (LVM).

Inodes and usage

Each file needs an inode. A filesystem can be "full" because it ran out of inodes while having free blocks, typically with millions of tiny files.

Terminal
df -h          # blocks
df -i          # inodes

The whole sequence

flowchart LR
    A["Partition"] --> B["mkfs"]
    B --> C["mount test"]
    C --> D["blkid UUID"]
    D --> E["fstab line"]
    E --> F["mount -a + findmnt --verify"]

From raw partition to a boot-safe mount.

A stand-in: validate fstab lines

This model checks sample lines for typical mistakes: wrong field count, device names instead of UUIDs and a bad fsck pass number.

Python Stand-in: lint fstab

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: usage report

This model turns df -h-style numbers into warnings, the kind of check a monitoring script performs.

Python Stand-in: filesystem usage alerts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Running mkfs on a device that already holds data.
  • Putting an untested line in fstab and rebooting a remote server.
  • Resizing the partition but forgetting resize2fs or xfs_growfs.
  • Running fsck on a mounted filesystem.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command tests all /etc/fstab entries without rebooting?

    mount -a mounts everything listed and reports errors at once.
  2. 2. Which filesystem cannot be shrunk?

    XFS can only be grown.
  3. 3. What does the nofail option do?

    It prevents a missing non-critical disk from blocking boot.
  4. 4. What can cause 'No space left on device' while df -h shows free blocks?

    Check df -i; many tiny files can use all inodes.

Summary

  • Partition, mkfs, mount, then add a UUID-based line to /etc/fstab.
  • Test with mount -a and findmnt --verify before rebooting.
  • Use fsck only on unmounted filesystems; grow with resize2fs or xfs_growfs.
  • Watch both block and inode usage.

Next: LVM: Logical Volumes