Checking and Repairing an XFS File System

Use the xfs_repair command to check file system consistency and to perform a file system repair.

If you can't mount an XFS file system, you can use the xfs_repair command to check its consistency and repair any issues. The command replays the journal log and shows any inconsistencies that might have resulted from the file system not being cleanly unmounted. Unless the file system has an inconsistency, you don't need to perform any repairs, as the journal is replayed every time that you mount an XFS file system.

The following procedure provides the usual steps to take when checking and repairing a file system with a corrupted journal.

  1. Backup the file system metadata using the xfs_metadump command.

    A metadata image can be helpful in root cause analysis and for identification of software bugs. For example, you can run:

    sudo xfs_metadump /dev/sdb /tmp/sdb-metadata-image

    Only run this command on an unmounted or frozen file system, or the dump file could be corrupted or inconsistent. See also Freezing and Unfreezing an XFS File System.

    You can compress the metadata image file and use it when working with a support team to resolve file system issues. You can use the xfs_mdrestore command to restore a metadata image to a file system. See the xfs_metadump(8) and xfs_mdrestore(8) manual pages for more information.

  2. Backup file system data, if possible.

    If you can mount the file system and you don't have a suitable backup, you can use the xfsdump command to back up the existing file system data. However, note that the command might fail if the file system's metadata has become corrupted.

    For example, to backup the file system mounted at /mnt onto a device at /dev/sdc, you could run:

    sudo xfsdump -l 0 -L "Full backup of /mnt $(date)" -f /dev/sdc /mnt

    See Backing Up and Restoring an XFS File System for more information.

  3. Check file system integrity.
    XFS replays the journal every time the file system is mounted. Therefore. the first file system integrity check is to decide whether the journal is corrupt. If the journal is corrupt, performing a dry run of the repair can help show any corruption and what can be done to remedy the issue.
    1. Replay the file system journal by mounting and unmounting the file system. For example, run:
      sudo mount /dev/sdb /mnt
      sudo umount /dev/sdb /mnt

      If you can't mount the file system and a structure needs cleaning error is returned, the journal is corrupt.

    2. Perform a file system consistency check to review the changes that a repair would perform.
      sudo xfs_repair -n /dev/sdb

      The xfs_repair -n command displays output to indicate changes that would be made to the file system in the case where it would need to complete a repair operation. No changes are made to the file system by running this command.

  4. Use the xfs_repair command to perform the file system repair.
    1. Run the xfs_repair -v command to get verbose output while the repair runs.

      For example, you can run:

      sudo xfs_repair -v /dev/sdb
    2. Reset the journal as a final resort.

      If the journal log has become corrupted, you can reset the log by specifying the -L option to xfs_repair. For example:

      sudo xfs_repair -L /dev/sdb

      Attention:

      Resetting the log can leave the file system in an inconsistent state, resulting in data loss and data corruption. Unless you're experienced with debugging and repairing XFS file systems by using the xfs_db command, we recommend that you instead re-create the file system and restore its contents from a backup.

  5. Mount and validate the file system.

    For example, run:

    sudo mount /dev/sdb /mnt

If you can't mount the file system or you don't have a suitable backup, running the xfs_repair command is the only viable option, unless you're experienced in using the xfs_db command.

xfs_db provides an internal command set for debugging and repairing an XFS file system manually. The commands enable you to perform scans on the file system, and display its data structures. If you specify the -x option to enable expert mode, you can change the data structures.

sudo xfs_db [-x] device

For more information, see the xfs_db(8) and xfs_repair(8) manual pages, and run the help command within xfs_db.