For those who aren't familiar, the script below is a wrapper to fsck.xfs - a script that is supposed to repair an XFS journaling filesystem upon boot-up of a Linux server.

Out of the box, it does nothing, thus leaving many admins to author their own hacked-together solutions, often in response to an immediate, urgent need. Usually, these type of quick fixes are kept under wraps from the powers that be, or rather, anyone who might be in any way the slightest bit script-literate.

Unfortunately for whomever wrote the script, Phil uncovered his admin's ruse and sent this script our way.


# Run xfs_repair 3 times, attempting to remount the 
# filesystem between each run. After this, we run it one last time by 
# dropping the log.
#
# This is terrible. We must never tell anyone that we did this. It's a 
# hideous hack for the xfs metadata corruption bug that we've been seeing.

echo "Attempting repair of xfs filesystem"
if [[ "$*" =~ (/dev/[a-zA-Z0-9]+) ]]; then
  filesystem=${BASH_REMATCH[1]}
  # 3 attempts to fix any possible issues
  for attempt in {1..3}; do
    /bin/umount "${filesystem}"
    /sbin/xfs_repair "${filesystem}"
    /bin/mount "${filesystem}"
  done
  # Drop the log now
  /bin/umount "${filesystem}"
  /sbin/xfs_repair -L "${filesystem}"
else
  echo "error: Couldn't parse filesystem from command"
fi
	
# Clear the ring buffer (see #XXXXXXX)
dmesg -c >/dev/null
	
# We always succeed. Just like the real fsck.xfs exit 0