Skip to content

Linux Foundations / Understand the system underneath ยท Step 10 of 38

Lesson: Filesystem RepairSwitch
โ† Linux Foundations

System administration

Filesystem Repair

Check and repair a damaged filesystem with fsck without making things worse.

Power cuts, crashes and failing hardware can leave a filesystem inconsistent. The tool for checking and repairing is fsck.

The golden rule

Do not run a repair on a mounted filesystem. The kernel is changing it while the tool changes it too, which can cause serious damage.

Check first, and unmount if needed:

$ findmnt /dev/sdb1
$ sudo umount /dev/sdb1

No output from findmnt means it is not mounted there.

Running fsck

fsck is a front end. For ext4 it calls e2fsck:

$ sudo fsck /dev/sdb1
fsck from util-linux 2.41
e2fsck 1.47.2 (1-Jan-2025)
data: clean, 12/327680 files, 42000/1310720 blocks

If the filesystem is marked clean, e2fsck skips the full check. Force one with -f:

$ sudo e2fsck -f /dev/sdb1
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
data: 12/327680 files (0.0% non-contiguous), 42000/1310720 blocks

Useful e2fsck options:

  • -n opens read-only and answers "no" to every question. Good for a first look.
  • -p fixes problems that are safe to fix automatically.
  • -y answers "yes" to everything. Convenient, but it may discard data you wanted to rescue.

Files that lost their directory entry are placed in lost+found.

Other filesystem types

XFS does not use fsck for repair (fsck.xfs normally just exits successfully without checking). Use xfs_repair on the unmounted filesystem, and xfs_repair -n to check without changing. Btrfs has its own tools, such as btrfs scrub and btrfs check. Read their documentation before using any repair option.

The root filesystem

You cannot unmount / while the system runs from it. Boot from a live USB or rescue mode and run the check from there. On systemd systems you can also request a check at boot with the kernel parameter fsck.mode=force.

Before you repair

If the data matters and there is no backup, copy it first, ideally an image of the whole partition. If the disk is making odd noises or logging I/O errors, check its health (for example smartctl from smartmontools) and stop writing to it. fsck repairs structures, not hardware.

Try it

Practise on a file, which is completely safe:

$ truncate -s 100M fs.img
$ mkfs.ext4 fs.img
$ e2fsck -f -n fs.img

You should see the five passes and a summary line.

Check your understanding

Filesystem Repair

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.