In other two articles How to repair a xfs filesystem, and xfs_repair command examples I discussed how to use the xfs_repair and how to check and fix a xfs filesystem. I'm not going to repeat everything again, but just give some tips:

When repairing a problematic XFS filesystem, especially with lots of data on it. Before you run xfs_repair '-L' option to force log zeroing, which could potentially zero dirty log that has metadata changes. Hense data lost could occur.

Option 1, duplicate xfs metadata and repair xfs filesystem with it

Using xfs_metadump and xfs_mdrestore, you can create a metadata image of the affected partition and perform the forced repair on the image rather than the partition itself. The benefits of this is the ability to see the damage that comes with a forced repair before performing it on the partition.

To do this, you'll need an extra hard drive/partition/lv or a USB drive. Mount it to a special mount point

mkdir -p /mnt/xfs_meta_repair
mount /dev/sdb1 /mnt/xfs_meta_repair

Once mounted, run xfs_metadump to create a copy of the partition metadata, assume you have a corrupt partition located at /dev/mapper/vg0-lv_database:

xfs_metadump /dev/mapper/vg0-lv_database /mnt/xfs_meta_repair/database.metadump

Next, you'll want to restore the metadata in to an image so that we can perform a repair and measure the damage.

xfs_mdrestore /mnt/xfs_metarepair/database.metadump /mnt/xfs_metarepair/database.img

Finally, we can perform the repair on the image:

xfs_repair -L /mnt/xfs_metarepair/database.img

After the repair has completed and you've assessed the output and potential damage, you can determine as to whether you'd like to perform the repair against real partition.

To run the repair against the partition, simply run:

xfs_repair -L /dev/mapper/vg0-lv_database

Option 2, duplicate the whole XFS filesystem and repair it

If you have enough space, you can just copy the problematic xfs filesystem and work on it. xfs_copy provides this function for you.

xfs_copy can copy the whole xfs filesystem to a device or an image, for example:

To copy to

Copy XFS filesystem from a device to device

# xfs_copy /dev/disk/by-vdisk/c5t7 /dev/disk/by-vdisk/c5t6

Copy XFS filesystem from a device to a file image

The target can also be the name of a regular file, in which case an image of the source XFS filesystem is created in that file. If the file does not exist, xfs_copy creates the file. The length of the resulting file is equal to the size of the source filesystem.

However, if the file is created on an XFS filesystem, the file consumes roughly the amount of space actually  used  in  the source  filesystem  by  the filesystem and the XFS log.  The space saving is because xfs_copy seeks over free blocks instead of copying them and the XFS filesystem supports sparse files efficiently.

# xfs_copy /dev/disk/by-vdisk/c5t7 /c5t6/c5t7xfsimage

It takes time, depends on the data on the device and device speed. Once it's done. Do the same as the option one, but pointing to the image/new device.

xfs_repair -L /c5t6/c5t7xfsimage

or

xfs_repair -L /dev/disk/by-vdisk/c5t6

After the repair has completed and you've assessed the output and potential damage, you can determine as to whether you'd like to perform the repair against real partition.

The benefit of xfs_copy is that you can actually use the duplicated copy for production, leave the original one for further debugging with Vendor.