Hi, I'm Brett

Marketing Specialist

Should I Defrag XFS?


Introduction

XFS is a high performance filesystem, meant for high amounts of IO and will take a beating with varying sized files, and has the ability to scale quite easily. While XFS provides many benefits to IO by allowing striped allocation, etc. etc. it can become fragmented. Which is likely why you’re here. The highest risk items to disk fragmentation are random operations that don’t write in order, which is one reason why pre-allocating space for torrents is a huge benefit to performance. Additionally, it reduces the movement of the arm inside the physical drive, by writing things in order, rather than parts of files sitting at random locations.

Individual file fragmentation occurs when a single file has been broken into multiple pieces (called extents on extent-based file systems). While disk file systems attempt to keep individual files contiguous, this is not often possible without significant performance penalties. File system check and defragmentation tools typically only account for file fragmentation in their “fragmentation percentage” statistic.

Usenet

Usenet is a huge offender for this. It grabs files and begins to extract them on-the-fly or extracts after the download of the compressed files is completed. While article caching can reduce the amount of fragmentation, hard disks are the limiting factor. More on that here.

Torrent Client Impacts

Should a torrent client be running on the system, preallocation can reduce the fragmentation by allocating disk space prior to initializing download.

Checking Fragmentation Levels

XFS

To get started, you’ll need xfs_progs. This allows us to talk to the filesystem. On Debian or Ubuntu, you can run sudo apt-get install xfsprogs and it’ll grab you everything you need for what’s talked about here. You’ll also need root permissions.

sudo su -
lsblk # This will give you a readout of what is going on, IE what partitions exist and the the md device they're associated with.
xfs_db -c frag -r /dev/***  # this will give you the extents per file. If it's 2-3+, I'd proceed to the How to fix section.
https://images2.imgbox.com/94/a6/1liWyHNH_o.png

where *** is to be replaced with the md127 device, as it’s mapped to /.

How to fix

XFS

Crontab for defragging XFS: do sudo su, type apt-get install xfsprogs then docrontab -e then paste in line below.

00 2 1-7,15-21 * * [ `date +\%a` = Fri ] && xfs_fsr

For the curious, 00 2 1-7,15-21 * * means this will run every day at 2am (server time) between 1-7 and everyday between 15-21, but [ `date +\%a` = Fri ] sets the day of the week to run it on.

Crontab Guru is a great tool that can help with this.

Or you could run it manually as root. I’d generally recommend using a screen session if possible. You can do this by running sudo screen -S defrag xfs_fsr. This should run for ~2 hrs and can be run several times over.

xfs_fsr manpage