TExcelTAD
Home › Operating Systems: Linux & Windows › Linux File Systems

Linux File Systems

Linux supports dozens of file system formats, and a running system usually has ten or more mounted at the same time. Some live on disks (ext4, XFS, Btrfs), some only in RAM (tmpfs), and some are generated on the fly by the kernel (proc, sysfs). This lesson shows how the kernel juggles them and how the popular on-disk formats differ.

The Virtual File System (VFS)

Programs never talk to ext4 or XFS directly. They call open(), read(), write() and stat(), and the kernel's Virtual File System layer turns those generic calls into calls on the right driver:

text
   cat, cp, python, nginx ...           (user space)
 ------------------------------------------------------
   system calls: open read write stat getdents mkdir
 ------------------------------------------------------
   VFS: path lookup, dentry cache, inode cache, page cache, permissions
 ------------------------------------------------------
   ext4 | xfs | btrfs | vfat | ntfs3 | tmpfs | proc | sysfs | nfs | overlay
 ------------------------------------------------------
   block layer + device drivers  (only for disk-backed file systems)

The VFS defines a common object model: a superblock per mounted file system, inodes for files, dentries (directory entries) for names, and file objects for open handles. Each driver fills in a table of functions ("how do I read a directory?", "how do I allocate a block?"). That is why ls works the same on a USB stick, a network share and /proc.

Mounts: one tree, many file systems

The kernel keeps a mount table. To decide which driver handles a path, it finds the mount point that is the longest prefix of that path. The program below does the same thing with a small, realistic table:

python · try it yourself
mounts = {
    "/":         ("ext4",  "/dev/nvme0n1p2"),
    "/boot/efi": ("vfat",  "/dev/nvme0n1p1"),
    "/home":     ("btrfs", "/dev/nvme0n1p3"),
    "/proc":     ("proc",  "proc"),
    "/sys":      ("sysfs", "sysfs"),
    "/tmp":      ("tmpfs", "tmpfs"),
    "/media/usb":("exfat", "/dev/sda1"),
}

def which_fs(path):
    best = max((m for m in mounts if path == m or path.startswith(m.rstrip("/") + "/")), key=len)
    fstype, source = mounts[best]
    return f"{path:28} -> {fstype:6} ({source}, mounted on {best})"

for p in ["/etc/hostname", "/home/ada/photo.jpg", "/boot/efi/EFI/ubuntu/grubx64.efi",
          "/proc/cpuinfo", "/tmp/build.log", "/media/usb/song.mp3"]:
    print(which_fs(p))
/etc/hostname                -> ext4   (/dev/nvme0n1p2, mounted on /)
/home/ada/photo.jpg          -> btrfs  (/dev/nvme0n1p3, mounted on /home)
/boot/efi/EFI/ubuntu/grubx64.efi -> vfat   (/dev/nvme0n1p1, mounted on /boot/efi)
/proc/cpuinfo                -> proc   (proc, mounted on /proc)
/tmp/build.log               -> tmpfs  (tmpfs, mounted on /tmp)
/media/usb/song.mp3          -> exfat  (/dev/sda1, mounted on /media/usb)

You can ask the real kernel which file system type holds a path with stat -f (file system status). This runs in the course's Linux sandbox, which is a container:

bash · try it yourself
for p in /proc /sys /dev /dev/shm; do
  printf '%-9s %s\n' "$p" "$(stat -f -c '%T' "$p")"
done
findmnt -n -o FSTYPE /
/proc     proc
/sys      sysfs
/dev      tmpfs
/dev/shm  tmpfs
overlay

The root is overlay: containers stack read-only image layers with one writable layer on top using OverlayFS (see Virtualization & Containers). On a normal laptop the same command prints ext4, btrfs or xfs.

On a real machine you would inspect disks and mounts like this (sample output, yours will differ):

bash
ada@ubuntu:~$ lsblk -f
NAME        FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
nvme0n1
├─nvme0n1p1 vfat   FAT32       6C1E-2A47                             945.1M     7% /boot/efi
├─nvme0n1p2 ext4   1.0         3c70a1f8-24c5-4ad1-a6ca-ca36277b21b2   61.3G    52% /
└─nvme0n1p3 btrfs        home  a9d2c3e4-55f1-4b0e-9b7a-0d3c8e1f2a6b  288.4G    37% /home
ada@ubuntu:~$ df -hT -x tmpfs
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/nvme0n1p2 ext4   137G   69G   61G  53% /
/dev/nvme0n1p3 btrfs  465G  172G  289G  38% /home
/dev/nvme0n1p1 vfat   1.1G   72M  945M   8% /boot/efi
ada@ubuntu:~$ cat /etc/fstab
UUID=3c70a1f8-24c5-4ad1-a6ca-ca36277b21b2 /         ext4  defaults,errors=remount-ro 0 1
UUID=6C1E-2A47                            /boot/efi vfat  umask=0077                 0 1
UUID=a9d2c3e4-55f1-4b0e-9b7a-0d3c8e1f2a6b /home     btrfs defaults,compress=zstd     0 0
ada@ubuntu:~$ sudo mount /dev/sda1 /mnt     # attach a USB stick by hand
ada@ubuntu:~$ sudo umount /mnt

/etc/fstab lists what to mount at boot; using UUIDs instead of /dev/sda1 keeps it working when disks are detected in a different order.

The disk file systems

ext4 XFS Btrfs
Default on Debian, Ubuntu RHEL, Rocky, Alma, Fedora Server Fedora Workstation, openSUSE
Design Journaling, extents, descended from ext2/ext3 Journaling, extents, built for parallel I/O Copy-on-write B-trees
Max file / volume 16 TiB / 1 EiB 8 EiB / 8 EiB 16 EiB / 16 EiB
Shrink online No (offline only) Never Yes
Snapshots No (use LVM) No (use LVM) Built in, instant
Checksums Metadata only Metadata only Data and metadata
Compression No No zstd, lzo, zlib
Multi-disk (RAID) Via mdadm/LVM Via mdadm/LVM Built in (RAID 1/10 solid; 5/6 not recommended)
Strength Simple, mature, fast fsck Huge files, many threads Snapshots, rollback, integrity

Others you will meet:

  • tmpfs: files in RAM (and swap). Used for /tmp on many distributions, /run and /dev/shm. Fast, gone after a reboot.
  • vfat / exfat: FAT32 and exFAT for USB sticks, SD cards and the EFI System Partition, readable everywhere.
  • ntfs3: an in-kernel NTFS driver (since Linux 5.15) for Windows disks.
  • ZFS: a copy-on-write file system and volume manager from Solaris; popular on servers, shipped outside the kernel tree because of its license.
  • nfs / cifs (SMB): network file systems.
  • squashfs: compressed, read-only images (Snap packages, live USBs).

Creating a file system is called formatting and uses mkfs (sample session; it erases the partition):

bash
ada@ubuntu:~$ sudo mkfs.ext4 -L data /dev/sdb1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 26214144 4k blocks and 6553600 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done

Note "6553600 inodes": ext4 decides the number of inodes at format time. A disk full of millions of tiny files can run out of inodes while it still has free space (df -i shows it). XFS and Btrfs allocate inodes dynamically and do not have this problem.

Copy-on-write and snapshots

Btrfs never overwrites a block in place. A change writes new blocks and then updates the parent pointers, all the way up to the root. A snapshot is simply a second root that still points to the old blocks, so it costs nothing until files change:

python · try it yourself
store = {}                     # block id -> content (blocks are never modified)
next_id = 0

def write_block(data):
    global next_id
    next_id += 1
    store[next_id] = data
    return next_id

live = {"a.txt": write_block("v1 of a"), "b.txt": write_block("v1 of b")}
snapshot = dict(live)          # a snapshot copies the pointers, not the data
print("blocks after snapshot:", len(store))

live["a.txt"] = write_block("v2 of a")       # copy-on-write: new block, old one kept
print("blocks after editing a.txt:", len(store))
print("live    :", {name: store[b] for name, b in live.items()})
print("snapshot:", {name: store[b] for name, b in snapshot.items()})
shared = set(live.values()) & set(snapshot.values())
print("blocks shared by both:", sorted(shared))
blocks after snapshot: 2
blocks after editing a.txt: 3
live    : {'a.txt': 'v2 of a', 'b.txt': 'v1 of b'}
snapshot: {'a.txt': 'v1 of a', 'b.txt': 'v1 of b'}
blocks shared by both: [2]

This is how openSUSE and Fedora can roll back a broken system update: they snapshot / before installing packages (sample session):

bash
ada@fedora:~$ sudo btrfs subvolume snapshot -r / /.snapshots/before-upgrade
Create a readonly snapshot of '/' in '/.snapshots/before-upgrade'
ada@fedora:~$ sudo btrfs filesystem df /
Data, single: total=180.00GiB, used=171.82GiB
Metadata, DUP: total=3.00GiB, used=1.94GiB

Which one should I use?

For a desktop or a simple server, the distribution's default is the right answer: ext4 for predictability, Btrfs if you want snapshots and compression. Choose XFS for very large files and heavy parallel writes (databases, media storage). Use exFAT for removable drives shared with Windows and macOS.

Learn more: File System Concepts · The Linux Directory Tree · Bash: Disk & Memory

Test yourself

What does the kernel's VFS layer do?

  1. Formats disks
  2. Gives every file system the same system-call interface and routes each call to the right driver
  3. Encrypts files
  4. Replaces the page cache

Which Linux file system can never be shrunk?

  1. ext4
  2. Btrfs
  3. XFS
  4. tmpfs

Why does a Btrfs snapshot take almost no space when it is created?

  1. It is compressed
  2. It shares all blocks with the live file system; only later changes use new blocks
  3. It is stored in RAM
  4. It only saves file names