xfs_repair command: Tutorial & Examples

Troubleshooting XFS filesystems

The xfs_repair command is a crucial tool in any Linux administrator's toolkit. It's specifically designed to repair an XFS filesystem, which is a high-performance journaling filesystem created by Silicon Graphics. This command is of high importance due to its ability to salvage data from damaged or corrupted XFS partitions. This tutorial will focus on explaining what the xfs_repair command does, how it works, its importance, potential problems it can solve, and how to use it through various examples.

Understanding the xfs_repair command

The xfs_repair command, as the name suggests, is used to repair XFS filesystems. It's typically used when a filesystem has been shut down uncleanly, and the filesystem's internal logs are not sufficient to restore it to a consistent state. This situation can arise due to a variety of reasons, such as a power failure or a kernel crash.

The way xfs_repair works is by scanning the filesystem metadata, identifying inconsistencies, and resolving them. It's important to note that xfs_repair does not operate on a mounted filesystem. The filesystem must be unmounted or mounted as read-only for the command to work.

Importance of xfs_repair

The xfs_repair command is especially significant because it allows users to recover data from a damaged XFS filesystem, which could otherwise lead to data loss. It's a vital tool for system administrators, especially in environments where data integrity is of utmost importance.

Problems xfs_repair can solve

The xfs_repair command can help recover from situations where the filesystem has been corrupted due to various reasons, including but not limited to:

Using xfs_repair

Before running xfs_repair, it's important to ensure that the target filesystem is unmounted. You can unmount a filesystem using the umount command.

Here's an example of how to use xfs_repair:

sudo umount /dev/sdb1
sudo xfs_repair /dev/sdb1

This sequence of commands will unmount the filesystem located at /dev/sdb1, then run xfs_repair on it.

In certain cases, xfs_repair might not be able to repair a filesystem in the first attempt. In such scenarios, you can use the -L flag to force the command to zero the log:

sudo xfs_repair -L /dev/sdb1

Typical output

Here's an example of what the output of xfs_repair might look like:

Phase 1 - find and verify superblock...
Phase 2 - using internal log
- zero log...
- scan filesystem freespace and inode maps...
- found root inode chunk
Phase 3 - for each AG...
- scan and clear agi unlinked lists...
- process known inodes and perform inode discovery...
- agno = 0
- agno = 1
- agno = 2
- agno = 3
- process newly discovered inodes...
Phase 4 - check for duplicate blocks...
- setting up duplicate extent list...
- check for inodes claiming duplicate blocks...
- agno = 0
Phase 5 - rebuild AG headers and trees...
- reset superblock...
Phase 6 - check inode connectivity...
- resetting contents of realtime bitmap and summary inodes
- traversing filesystem ...
- traversal finished ...
- moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
done

This output shows the different phases that xfs_repair goes through when repairing an XFS filesystem. The command first verifies the superblock, then uses the internal log to scan the filesystem's freespace and inode maps. It then scans and clears the AGI unlinked lists, processes known inodes, checks for duplicate blocks, rebuilds AG headers and trees, checks inode connectivity, and finally verifies and corrects link counts.

Common command line parameters

  • -L: Forces the command to zero the log.
  • -n: No modify; checks for problems but does not modify the filesystem.
  • -v: Verbose output; provides more detailed information during the repair process.

Cheatsheet

  • To unmount:

    sudo umount /dev/sdX
    
  • To repair:

    sudo xfs_repair /dev/sdX
    
  • To force zero log:

    sudo xfs_repair -L /dev/sdX
    

Real-world use cases

  • Recovering a corrupted filesystem after an unexpected power loss.
  • Fixing a filesystem that has been improperly unmounted during system crashes.
  • Restoring data from a filesystem that shows signs of hardware failure.

Troubleshooting and common errors

  • Error: "Filesystem is mounted": Ensure the filesystem is unmounted before running xfs_repair.
  • Error: "Can't repair this filesystem": This might indicate severe corruption, and it may be necessary to restore from a backup.

Performance considerations

Using xfs_repair on large filesystems can take time, especially if there are many inconsistencies to resolve. Running the command during low-traffic hours is advisable to minimize the impact on system performance.

Security considerations

Be cautious when using the -L option, as this will zero the logs and may result in data loss if the command is not executed correctly. Always ensure you have backups before performing repairs.

Tips and best practices

  • Always try to unmount the filesystem gracefully before running xfs_repair.
  • Keep regular backups of important data to minimize loss during filesystem repairs.
  • Familiarize yourself with the man page for xfs_repair to understand all available options.

See also

The text above is licensed under CC BY-SA 4.0 CC BY SA