Defragmenting XFS file systems on Ubuntu 13.04

After using the XFS file system for a while, you might see it slow down a bit.  If so, you may need to defragment the file system.  Doing so is very easy but does require root access and the xfsdump package:

sudo aptitude install xfsdump

Now that we have the xfsdump package, we can use the xfs_db utility to determine the current fragmentation percentage:

sudo xfs_db -r /dev/sda6
xfs_db> frag
actual 95343, ideal 85809, fragmentation factor 10.00%
xfs_db> quit

As you can see, the estimated fragmentation percentage is 10%. Not bad but let’s get that down:

 sudo xfs_fsr -v /dev/sda6
/home start inode=0
extents before:2 after:1 DONE ino=138
extents before:19 after:1 DONE ino=100725
extents before:17 after:1 DONE ino=103896
extents before:3 after:1 DONE ino=474184
extents before:2 after:1 DONE ino=474183

We can have XFS defrag all the mounted file systems automatically by creating a crontab job in /etc/cron.daily/

#!/usr/bin/env bash
/bin/date >> /var/log/xfs.log
/usr/sbin/xfs_fsr -v 2>&1 >> /var/log/xfs.log
/bin/date >> /var/log/xfs.log
Share Button