Linux › Section D · Disks and filesystems › Topic D.2

Filesystem types, creating, and checking filesystems

A partition is only an empty run of sectors until you put a filesystem on it. This topic compares the filesystems Linux uses, shows how mkfs builds one and how fsck checks it, and explains why both commands deserve respect.

What a filesystem does

A filesystem is a database laid over a block device. It turns a flat run of numbered blocks into named files and nested directories, and records each file's owner, permissions, timestamps and the blocks that hold its data. Most filesystems are implemented in the kernel, and a layer called the Virtual File System (VFS) gives them all the same interface, so ls and cp work identically on ext4, XFS or a USB stick's FAT. FUSE (Filesystem in Userspace) lets ordinary programs implement filesystems too. Not every filesystem stores data on a disk: /proc and /sys are filesystems that present kernel information, as a later card shows.

The common types

ext4 is the current member of Linux's native extended family. ext2 had no journal, ext3 added one, and ext4 added extents and much larger limits; it is the default on Debian and Ubuntu. XFS handles large files and parallel workloads well and has been the default on Red Hat Enterprise Linux since version 7. Btrfs is a copy-on-write filesystem with snapshots, checksums and compression; Fedora Workstation has used it by default since Fedora 33 (2020), as does openSUSE. For exchange with other systems there are vfat (FAT32, also required for the EFI system partition), exfat for large flash media, ntfs for Windows disks and iso9660 for optical discs.

Journals and why they matter

Updating a file touches several structures at once: the data blocks, the file's metadata and the free-space map. If power fails halfway, they disagree. A journaling filesystem such as ext4 or XFS first writes a description of the pending metadata change to a journal, then applies it. After a crash, the kernel replays or discards the unfinished entries during mount, which takes seconds instead of a full scan of the disk. Btrfs reaches the same goal differently: it never overwrites live data in place, so a crash leaves either the old or the new version intact. None of this replaces backups or a clean shutdown.

Creating a filesystem with mkfs

mkfs is a front end. mkfs -t ext4 /dev/sdX2 runs mkfs.ext4, which is the same program as mke2fs; likewise there are mkfs.xfs, mkfs.btrfs and mkfs.vfat, each with its own manual page. Defaults are sensible, so most of the time you add only a label with -L. Warning: mkfs overwrites the structures that locate existing files, so running it on the wrong partition destroys that partition's data in a second. Before formatting, run lsblk -f to see what each partition already contains, and wipefs /dev/sdX2, which with no options only lists any existing signatures. For ext filesystems, mke2fs -n shows what would be done without writing anything.

Worked example: formatting a data partition

Task: put ext4 on the second partition of a new disk, /dev/sdX2, and label it data. 1. Run lsblk -f /dev/sdX and confirm sdX2 has the expected size and an empty FSTYPE column. 2. Run wipefs /dev/sdX2; no output means no old signature was found. 3. Run mkfs.ext4 -L data /dev/sdX2 as root. 4. Read the output: it reports the number of blocks and inodes, the new filesystem UUID and a list of block numbers holding superblock backups, which are worth noting down. 5. Run blkid /dev/sdX2 to confirm the reply shows LABEL="data", the UUID and TYPE="ext4". The filesystem now exists but is not yet usable until it is mounted, the subject of the next topic.

Superblocks, blocks and inodes

The superblock is the filesystem's master record: its size, block size, inode count, state and features. Because losing it would lose everything, mke2fs writes backup copies across the disk. ext4 defaults come from /etc/mke2fs.conf: 4,096-byte blocks and, for ordinary sizes, one inode per 16,384 bytes of space (filesystems under 512 MB use a denser ratio). Every file needs one inode, and the inode count is fixed when the filesystem is created. If you expect millions of tiny files, choose a smaller bytes-per-inode value with -i, or give an exact count with -N, at creation time; the topic "Inside a traditional filesystem: inodes and links" looks at inodes closely.

Worked example: counting inodes

How many inodes does mkfs.ext4 create on a 100 GiB partition with default settings? 1. Size in bytes: \(100 \times 2^{30} = 107{,}374{,}182{,}400\). 2. One inode per 16,384 bytes: \(107{,}374{,}182{,}400 \div 16{,}384 = 6{,}553{,}600\) inodes. 3. Check the ratio: \(2^{30} \div 2^{14} = 2^{16} = 65{,}536\) inodes per GiB, and 100 of those is 6,553,600. 4. Interpretation: this partition can never hold more than about 6.5 million files and directories, however small. A mail or cache server storing ten million small files could use mkfs.ext4 -i 4096, which gives four times as many inodes.

Checking with fsck

fsck checks and repairs a filesystem, again as a front end: on ext4 it runs e2fsck. Warning: never check a mounted, writable filesystem, because the kernel keeps changing it underneath the checker and real damage can follow. Unmount it first, or boot a rescue system for the root filesystem. Useful e2fsck options are -n (open read-only and answer no to everything), -p (preen: fix safe problems automatically, as boot scripts do), -f (force a check even if the filesystem looks clean) and -y (answer yes to everything, a last resort). If the main superblock is damaged, -b with a backup block number can restore it. Recovered nameless files go into lost+found.

Other filesystems and special types

XFS and Btrfs have their own tools. fsck.xfs does nothing and exits successfully; to check or repair XFS, unmount it and run xfs_repair. Btrfs offers btrfs scrub, which verifies checksums on a mounted filesystem, and btrfs check for offline checking. Some filesystems hold no disk data at all: proc on /proc shows processes, sysfs on /sys shows devices, tmpfs on /run keeps files in memory and swap, devtmpfs fills /dev, squashfs is a compressed read-only image (snap packages use it), and overlay merges directories, which containers rely on. The topic "Linux directory hierarchy essentials" covers where these appear.

Common mistakes to avoid

First, formatting the wrong device: check with lsblk -f and blkid, because mkfs asks nothing on a partition with no recognised filesystem and wipes it at once. Second, running fsck on a mounted filesystem. Third, expecting fsck to check XFS: use xfs_repair. Fourth, answering yes to every fsck question on a badly damaged disk before imaging it; try -n first. Fifth, accepting default inode settings for a workload of millions of tiny files and running out of inodes while space remains. Sixth, thinking a journal replaces backups: it protects metadata consistency, not deleted or overwritten files.

Key takeaways

A filesystem turns blocks into files and directories, and the VFS gives every type one interface. ext4 (Debian, Ubuntu), XFS (RHEL) and Btrfs (Fedora Workstation, openSUSE) are the main native choices; vfat, exfat, ntfs and iso9660 serve exchange. Journals make crash recovery fast. mkfs -t type, or mkfs.type, creates a filesystem and destroys what was there; mke2fs -n is a dry run. ext4 defaults to 4 KiB blocks and one inode per 16,384 bytes, fixed at creation. fsck, which runs e2fsck for ext4, checks unmounted filesystems; use xfs_repair for XFS.

Flashcards

Flashcard

Filesystem

A structure that turns a block device's blocks into named files and directories

Also stores metadata.

Test your understanding

Question 1 of 5

What really happens when you run mkfs -t ext4 /dev/sdX2?

Question 2 of 5

With default ext4 settings, about how many inodes does a 10 GiB filesystem get?

Question 3 of 5

You want to check the ext4 filesystem on /dev/sdb1, which is mounted read-write at /data. What should you do first?

Question 4 of 5

An administrator runs fsck on an XFS filesystem and it reports success in a fraction of a second. Why?

Question 5 of 5

Which statement about filesystem journals is correct?

← Previous topicNext topic →