Skip to main content
RunBook Academy

LinuxXIV · FilesystemsOperations

Filesystem check, repair, and recovery with fsck and xfs_repair

Intermediate⏱ ~10 minbashfsck.ext4xfs_repairmountdd

What you'll learn

  • Run fsck and xfs_repair safely on an unmounted filesystem
  • Distinguish metadata corruption from data loss
  • Recover from a corrupted superblock or journal
  • Plan filesystem maintenance windows and snapshot-before-fix discipline

Prerequisites

Practice

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

Filesystem corruption is rare but happens: unclean shutdown, bad disk, kernel bug, hardware failure. The recovery process is different for ext4 vs XFS, but the discipline is the same: unmount first, check second, repair last, and always have a backup.

The discipline

fsck for ext4

Read-only / Safefsck -n
$ fsck.ext4 -n /dev/sda1
e2fsck 1.47.0 (5-Feb-2023)
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
/dev/sda1: 12345/6553600 files (0.2% non-contiguous), 234567/26214400 blocks

A common mistake is to treat -n as a licence to check anything, including a mounted production root. It is not. Read-only means e2fsck will not write; it does not mean the answer is true. The kernel is mutating the same metadata while e2fsck reads it, so e2fsck sees a torn view and reports phantom corruption. An on-call engineer who acts on that output escalates a healthy host into an unnecessary emergency unmount.

Read-only / Safefsck -p
$ fsck.ext4 -p /dev/sda1
e2fsck 1.47.0 (5-Feb-2023)
/dev/sda1: clean, 12345/6553600 files, 234567/26214400 blocks

xfs_repair for XFS

Read-only / Safexfs_repair -n
$ xfs_repair -n /dev/sda1
Phase 1 - find and verify superblock...
Phase 2 - scan filesystems for bad magic numbers...
Phase 3 - scan inodes for bad magic numbers...
done
Data-loss riskxfs_repair
$ xfs_repair /dev/sda1
Phase 1 - find and verify superblock...
Phase 2 - scan filesystems for bad magic numbers...
Phase 3 - scan inodes for bad magic numbers...
Phase 4 - scan directory entries...
Phase 5 - rebuild directory tree...
Phase 6 - rebuild link counts...
Phase 7 - rebuild freespace information...
Maximum filesystem writebacks: 0
done

Common recovery scenarios

Recovering from a corrupted superblock

Read-only / Safemke2fs -n
$ mke2fs -n /dev/sda1
mke2fs 1.47.0 (5-Feb-2023)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
  1. Run mke2fs -n /dev/sda1 to see the superblock parameters
  2. Run e2fsck -b 32768 /dev/sda1 to restore from superblock backup at block 32768
  3. If that fails, try other backup locations: 98304, 163840, 229376, 294912
  4. Mount and verify: mount /dev/sda1 /mnt; ls /mnt
  5. If all superblock backups fail, restore from filesystem backup

Recovering from a full journal

Configuration changeumount
$ umount /data
Data-loss riskxfs_repair -L
$ xfs_repair -L /dev/sda1
Phase 1 - find and verify superblock...
Phase 2 - scan filesystems for bad magic numbers...
Phase 7 - rebuild freespace information...
Maximum filesystem writebacks: 0
done

Knowledge check

Knowledge check · 5 questions

  1. Q1. When is it safe to run fsck on an ext4 filesystem?

  2. Q2. xfs_repair -L zeroes the log, losing any pending metadata changes.

  3. Q3. Which of the following are correct filesystem repair discipline? Select all that apply.

  4. Q4. A colleague says plain xfs_repair (no -L) is conservative and cannot lose data, so no snapshot is needed before running it on a corrupt production volume. What is wrong with that reasoning?

  5. Q5. You run fsck.ext4 -n against the mounted read-write root filesystem of a busy host and it reports dozens of inode and block-count errors. What is the correct next step?

Passing score: 75%. Answers are checked in this browser.