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 -n opens the filesystem read-only and answers no to every question, so it never writes. It is still only meaningful on an unmounted or read-only-mounted filesystem. Against a mounted read-write filesystem the kernel dirty cache makes e2fsck report inode and block-count errors that do not exist. To check a live root filesystem, remount read-only (mount -o remount,ro /) or boot to rescue.target first.
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 -p is automatic repair - it fixes simple problems (unconnected inodes, bad blocks) without prompting. Use when fsck -n finds problems and you trust the auto-repair.
Read-only / Safexfs_repair -n— xfs_repair -n is read-only. It scans all metadata for inconsistency. Use this to verify filesystem health before any other action.
$ 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 without -n repairs the filesystem. It is not lossless: orphaned inodes are reconnected into lost+found renamed to their inode number, so files lose their original path and name, and quota information may be partially or entirely cleared (xfs_repair warns before exiting if so). Snapshot the block device first - an LVM, EBS or VM-disk snapshot is your only undo. Run only on an unmounted filesystem, and re-check quota configuration afterwards.
$ 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 prints the superblock parameters without writing anything. Use this to see the filesystem parameters, then restore from a backup superblock with e2fsck -b.
Run mke2fs -n /dev/sda1 to see the superblock parameters
Run e2fsck -b 32768 /dev/sda1 to restore from superblock backup at block 32768
If that fails, try other backup locations: 98304, 163840, 229376, 294912
Mount and verify: mount /dev/sda1 /mnt; ls /mnt
If all superblock backups fail, restore from filesystem backup
Recovering from a full journal
Configuration changeumount— A full journal is often the cause of read-only mounts. Unmounting the filesystem is the first step in recovery. If the mount is stuck in read-only mode, umount -l /data (lazy unmount) is the next option, but it is not safe for repair.
$ umount /data
Data-loss riskxfs_repair -L— -L zeroes the log. Use when the log is corrupted and the journal cannot be replayed. This is a last-resort option - any pending metadata changes in the log are lost. Use only when the filesystem refuses to mount due to log corruption.
$ 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
Q1. When is it safe to run fsck on an ext4 filesystem?
Q2. xfs_repair -L zeroes the log, losing any pending metadata changes.
Q3. Which of the following are correct filesystem repair discipline? Select all that apply.
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?
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.