How to Repair Corrupted XFS Filesystem with xfs_repair

In this guide, we will explain how to repair corrupted XFS filesystem using the xfs_repair utility.

Originally developed by Silicon Graphics, the XFS file system is a robust, high-performance journaling filesystem first introduced to the Linux kernel in 2001. Over time, its popularity has grown significantly, and by 2014, XFS was adopted by major Linux distributions. Today, XFS is the default filesystem in Red Hat-based distributions like RHEL, CentOS, and Rocky Linux. Known for its exceptional speed and robustness, XFS excels at handling large files efficiently.

Despite its reliability, XFS is not immune to filesystem corruption. Common causes of such issues include ungraceful shutdowns, NFS write errors, power outages, and hardware problems such as bad blocks on a drive. Filesystem corruption can lead to severe problems, including corrupted files or even an unbootable system if critical boot files are affected.

Several tools are available to diagnose and fix filesystem errors. One such tool is the fsck (Filesystem Check) command, which checks the overall health of a filesystem. It identifies potential and existing errors, repairs them, and generates a report. The fsck command is pre-installed in most Linux distributions, requiring no additional setup.

Another valuable tool is xfs_repair, a system utility designed specifically for the XFS filesystem. Highly scalable, xfs_repair efficiently scans and repairs large filesystems with numerous inodes, making it ideal for managing XFS filesystems effectively.

1) Simulate File corruption

In this tutorial, we will simulate filesystem corruption on an XFS filesystem to demonstrate the repair process. For this example, we will use an 8GB external USB drive as our block device, identified as /dev/sdb1, as shown in the command below.

$ lsblk | grep sd

linux-lsblk-command

The first step is to format it to xfs filesystem using the mkfs command.

$ sudo mkfs.xfs -f /dev/sdb1

This displays the output shown

format-partition-with-xfs-filesystem

The next step is to create a mount point that we shall later use to mount the block volume.

$ sudo mkdir /mnt/data

Next, mount the partition using the  mount command.

$ sudo mount /dev/sdb1  /mnt/data

You can verify if the partition was correctly mounted as shown.

$ sudo mount | grep /dev/sdb1

Mount-xfs-filesystem-linux

Our partition is now successfully mounted as an xfs partition. Next, we are going to simulate filesystem corruption by trashing random filesystem metadata blocks using the xfs_db command.

But before that, we need to unmount the partition.

$ sudo umount /dev/sdb1

Next, corrupt the filesystem by running the command below to trash random filesystem metadata blocks.

$ sudo xfs_db -x -c blockget -c "blocktrash -s 512109 -n 1000" /dev/sdb1

bad-sectors-xfs-filesystem-linux

2) Repair Corrupted XFS filesystem using xfs_repair

To repair the file system using xfs_repair command, use the syntax:

$ sudo xfs_repair /dev/device

But before we embark on repairing the filesystem, we can perform a dry run using the -n flag as shown. A dry run provides a peek into the actions that will be performed by the command when is it executed.

$ sudo xfs_repair -n /dev/device

For our case, this translates to:

$ sudo xfs_repair -n /dev/sdb1

Dry-run-xfs-repair-linux

From the output, we can see some metadata errors and inode inconsistencies. The command terminates with a brief summary of the steps the actual command would have carried out. The corrective measures that would have been applied in steps 6 and 7 have been skipped.

xfs-repair-command-output

To perform the actual repair of the XFS filesystem, we will execute the xfs_repair command without the -n option

$ sudo xfs_repair /dev/sdb1

The command detects the errors and inconsistencies in the filesystem.

xfs-repair-filesystem-linux

And performs remediation measures to the inodes and rectifies any other errors. The output provided shows that the command completes its tasks successfully.

Fixing-xfs-filesystem-errors

For more xfs_repair options visit the man page.

$ man xfs_repair

man-xfs-repair-command-linux

Conclusion

The xfs_repair utility is a powerful tool for restoring XFS filesystems to a stable state after corruption. With this guide, you can confidently diagnose and repair a corrupted XFS partition. Feel free to post your queries and feedback in below comments section.

Read Also : How to Monitor Linux System with Glances Command

Leave a Comment

Your email address will not be published. Required fields are marked *