Situation: You detect XFS metadata corruption (kernel logs show XFS errors, mount failures, or xfs_repair suggestions). As an SRE you must preserve data, minimize downtime, and follow safe repair/restore steps.1) Immediate hardware checks (before touching filesystem)- Check SMART for the underlying disk(s): smartctl -a /dev/sdX; run short/long self-test if suspicious.- Inspect dmesg/journalctl for I/O errors, controller/driver errors, timeouts.- Verify cabling/hot-swap status, RAID controller logs, and battery-backed cache state.- If any hardware fault is suspected, isolate the device (fail the RAID member, replace) before any write operations.2) Online vs offline considerations- XFS metadata repair cannot be done safely online. xfs_repair must operate on an unmounted filesystem (or on a device mapper device).- You may mount filesystem read-only to extract files while planning repairs: mount -o ro,nouuid /dev/mapper/vg-lv /mnt/recover- Do not attempt xfs_repair on a mounted filesystem (except using -n on a device to simulate — still must be unmounted for real repair).3) Safe unmount procedure- Notify stakeholders, pause writers, enter maintenance mode.- Stop services using the FS (systemctl stop <svc>, stop containers).- Find and kill remaining processes: fuser -vm /mountpoint or lsof +f -- /mountpoint; gracefully terminate; avoid lazy (-l) unmount for repair — filesystem must be cleanly unmounted.- Umount: umount /mountpoint (or umount -f only for emergency, but avoid unless necessary).- Confirm: mount | grep /mountpoint or lsblk to ensure device is not mounted.4) Block-level backup BEFORE ANY REPAIR (critical)- Create a full block-level image of the device and store off-host or on separate storage. Prefer ddrescue for resilience to read errors:bash
# example using ddrescue (recommended)
ddrescue --no-split --logfile=/tmp/ddrescue.log /dev/mapper/vg-lv /backup/images/vg-lv.img /backup/images/vg-lv.map
- Or use dd with pv and compression if device healthy:bash
dd if=/dev/mapper/vg-lv | pv | gzip -c > /backup/images/vg-lv.img.gz
- Verify image integrity (checksums): sha256sum image > image.sha256- If using LVM, create an LVM snapshot first to avoid long downtime:bash
lvcreate --size 10G --snapshot --name lv_snap /dev/vg/lv
# mount snapshot read-only and dd from /dev/vg/lv_snap
- For large volumes, stream backup to a remote host (netcat/ssh) to avoid filling local disk.5) Run xfs_repair safely- First run non-destructive check:bash
xfs_repair -n /dev/mapper/vg-lv
- Review output carefully. If it reports only metadata inconsistencies that xfs_repair can fix, run repair. Use parallel option for speed:bash
xfs_repair -P /dev/mapper/vg-lv
- If the log is corrupted and xfs_repair refuses, you can zero the log as last resort (data-risking):bash
xfs_repair -L /dev/mapper/vg-lv
- Note: -L can lose recent metadata (inode/link) but may be necessary to recover mountability.6) Post-repair validation- Run xfs_repair -n again to confirm no remaining issues.- Mount read-only, run filesystem checks, and run application-level validations.- Remount read-write in maintenance and gradually bring services back.7) Restore path if repair fails- If xfs_repair cannot restore or results are unacceptable: - Restore from block-level image: dd or use ddrescue to write back to the block device (ensure target is correct):bash
dd if=/backup/images/vg-lv.img of=/dev/mapper/vg-lv bs=4M status=progress
- If you backed up via an LVM snapshot or have file-level backups (rsync, Borg, backups), restore files rather than raw blocks: - Mount the snapshot or image loopback: losetup -f --show /backup/images/vg-lv.img; kpartx -av /dev/loopX; mount -o ro /dev/mapper/... - Copy data to new clean LV or newly provisioned storage with rsync preserving attributes. - If only partial recovery possible, use file-level salvage from readonly mounts and rebuild services.8) Preventive follow-up- Replace failing hardware, run full filesystem scrubs on schedule.- Implement regular block-level snapshots and file-level backups, verify restores.- Document incident, root cause, and update runbooks for future incidents.Key principles: never run destructive fixes before taking a complete block-level backup; prefer non-destructive checks first; validate hardware health; use LVM snapshots to minimize downtime and enable safe backups; -L is last-resort.