Rocky 9: judging and repairing a damaged XFS
Test scope
Section titled “Test scope”Reproduced in a Rocky Linux 9.8 privileged container (host kernel 5.14.0-687.54.1.el9_8) with a 512 MB loop disk: a healthy filesystem, repair refused while mounted, a -n dry run after unmounting, zeroing the first four bytes to fake a bad superblock, xfs_repair recovery, then remount and read. Log: plans/content-expansion/evidence/logs/rocky-xfs-repair.log.
Repairing a production volume (backups, snapshots, downtime) is out of scope; this page covers command behaviour and the order of decisions.
Decision order: can it be repaired at all?
Section titled “Decision order: can it be repaired at all?”Three questions, in this order:
- Is it still mounted? If so, it cannot be repaired;
- What does
-nreport? It separates a minor issue from a superblock repair; - Do you have a backup or snapshot? If not, take one before touching anything.
While mounted, repair is refused outright
Section titled “While mounted, repair is refused outright”sudo xfs_repair /dev/loop0xfs_repair: /dev/loop0 contains a mounted and writable filesystem
fatal error -- couldn't initialize XFS libraryThe kernel still holds cache and journal state for that filesystem; repairing under a live mount only produces messier metadata. Stop the workload and unmount, or repair a snapshot on another host.
Dry run: -n reads but never writes
Section titled “Dry run: -n reads but never writes”sudo umount /mnt/xsudo xfs_repair -n /dev/loop0Phase 7 - verify link counts...No modify flag set, skipping filesystem flush and exiting.The value of a dry run is getting the problem list first; archive it in production before deciding on a real repair.
What a bad superblock looks like
Section titled “What a bad superblock looks like”sudo dd if=/dev/zero of=/dev/loop0 bs=4 count=1 conv=notruncsudo mount /dev/loop0 /mnt/xmount: /mnt/x: wrong fs type, bad option, bad superblock on /dev/loop0, ...XFS magic is the first four bytes, XFSB; zeroed, the kernel no longer recognises a filesystem and mount reports bad superblock.
Repair: recover from a backup superblock
Section titled “Repair: recover from a backup superblock”sudo xfs_repair /dev/loop0Phase 7 - verify and correct link counts...Note - stripe unit (0) and width (0) were copied from a backup superblock.doneXFS keeps superblock copies across the disk and repairs the primary from a backup; the Note says geometry was copied from the backup, so RAID volumes should correct stripe settings as suggested.
Always mount and verify afterwards
Section titled “Always mount and verify afterwards”sudo mount /dev/loop0 /mnt/xcat /mnt/x/a.txthelloSuccess means it mounts and key files read back; done alone does not prove the data is intact.
Common traps
Section titled “Common traps”- Repairing under a live mount: refused; unmount first or use a snapshot;
- Treating
-nas a repair: it writes nothing; - Ignoring the stripe note: geometry restored from a backup may lose RAID settings and hurt performance.
Practice: reproduce a bad-superblock repair
Section titled “Practice: reproduce a bad-superblock repair”sudo dnf install -y xfsprogstruncate -s 512M /var/tmp/x.imgL=$(sudo losetup -f --show /var/tmp/x.img)sudo mkfs.xfs -q "$L"sudo mkdir -p /mnt/x && sudo mount "$L" /mnt/xecho hello | sudo tee /mnt/x/a.txt >/dev/null && syncsudo umount /mnt/xsudo dd if=/dev/zero of="$L" bs=4 count=1 conv=notruncsudo xfs_repair "$L" | tail -3sudo mount "$L" /mnt/x && cat /mnt/x/a.txtChecks:
mountreportsbad superblockbefore the repairxfs_repairoutput ends withdone- After repair,
cat /mnt/x/a.txtprintshello xfs_repair -n "$L"printsNo modify flag set
常见问题
Why is repair refused while mounted?
Repair changes metadata while the mounted kernel still holds cached state and journal assumptions, producing an inconsistent result. Unmount first, or repair a snapshot on another host.
How is `-n` different from a real repair?
`-n` scans and reports without writing; a real repair writes changes. In production, dry-run and archive the output before deciding.
Is the data guaranteed after a repair?
No. Repair restores a usable filesystem structure; data in damaged areas may end up as orphaned files in lost+found. Mount and read key data afterwards, and restore from backup when needed.
Next steps
Section titled “Next steps”- Growing storage: LVM growth
- Post-install checks: 7 checks after install
- Log rotation: logrotate in practice