Skip to content

Instantly share code, notes, and snippets.

@kanmeiban
Last active October 11, 2023 18:27
Show Gist options
  • Save kanmeiban/f4daa71028ad6dce10133f01c946f3bf to your computer and use it in GitHub Desktop.
Save kanmeiban/f4daa71028ad6dce10133f01c946f3bf to your computer and use it in GitHub Desktop.
Fixing corrupt XFS filesystem with dirty log that cannot be mounted

Situation

Linux root partition with XFS that cannot be mounted because of dirty and inconsistent log. Log can be neither repaired nor cleared:

$ sudo xfs_repair /dev/nvme0n1p7
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
Log inconsistent (didn't find previous header)
failed to find log head
zero_log: cannot find log head/tail (xlog_find_tail=5)
ERROR: The log head and/or tail cannot be discovered. Attempt to mount the
filesystem to replay the log or use the -L option to destroy the log and
attempt a repair.

$ sudo mount /dev/nvme0n1p7 /mnt
# says something like
# Cannot mount, run xfs_repair first

$ sudo xfs_repair -L /dev/nvme0n1p7
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
Log inconsistent (didn't find previous header)
failed to find log head
zero_log: cannot find log head/tail (xlog_find_tail=5)
Log inconsistent (didn't find previous header)
failed to find log head

fatal error -- failed to clear log%

How to fix

  1. Attach an external disk
  2. sudo xfs_copy -d /dev/nvme0n1p7 /run/media/manjaro/PortableSSD/mariana_xfs.img - errors out without -d, takes ~2h for ~500GB filesystem
  3. xfs_repair -L /run/media/manjaro/PortableSSD/mariana_xfs.img - this succeeds on the copied image
  4. mount /run/media/manjaro/PortableSSD/mariana_xfs.img /mnt
  5. cp /mnt/home/mariana /run/media/manjaro/PortableSSD/mariana - backup your files

If you need to fix the original filesystem, you can try xfs_metadump/xfs_mdrestore combo using the repaired image to repair the original broken file system or use again xfs_copy to replace the original broken filesystem with the repaired copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment