Is Linux Fragmented? How to Check and Optimize EXT4 and XFS Drives to Boost I/O Speed

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

The Truth About Linux Fragmentation

“Linux never fragments” is one of the most common myths in the tech world. In reality, file systems like EXT4, XFS, or Btrfs manage blocks very intelligently. They attempt to arrange data contiguously to optimize speed. However, being smart doesn’t mean being perfect.

I once managed a server cluster running SQL databases and 24/7 logging on CentOS 7. After two years, I/O speeds dropped from 150MB/s to less than 40MB/s, even though the disk was only 80% full. Upon inspection, log files were split into more than 5,000 fragments. The HDD head had to jump constantly, causing latency to skyrocket and making the application extremely sluggish.

You should consider defragmenting when:

  • Free space is below 15%, making it hard for the system to find contiguous free blocks.
  • The server performs intensive write/delete tasks, such as Mail or Log servers.
  • The system is running on traditional HDDs.

Preparing the Tools

Most distributions like Ubuntu, Debian, or AlmaLinux come with these tools pre-installed. If not, it only takes a few seconds to install the e2fsprogs (for EXT4) and xfsprogs (for XFS) packages.

For Ubuntu/Debian:

sudo apt update && sudo apt install e2fsprogs xfsprogs -y

For RHEL/CentOS/AlmaLinux:

sudo yum install e2fsprogs xfsprogs -y

Measuring Disk Fragmentation Levels

Don’t rush into running defragmentation commands immediately. This process consumes significant CPU and I/O resources. Check the actual status first to decide if intervention is necessary.

1. For EXT4 Systems

To see a general overview of free space on the disk, use e2freefrag:

sudo e2freefrag /dev/sda1

If you want to closely inspect a specific important file (like a database file), filefrag is your best choice:

sudo filefrag -v /var/lib/mysql/ibdata1

Look at the extents column. If this number reaches the hundreds for a single file, it’s a red flag for performance.

2. For XFS Systems

XFS provides the xfs_db tool for a quick read-only check:

sudo xfs_db -c frag -r /dev/sdb1

Practical experience shows: under 10% is stable, but if this number exceeds 20%, you will notice the system starting to lag.

How to Defragment Safely

A vital note: Never defragment an SSD the same way you would an HDD. Constantly moving data on an SSD only wastes write cycles (P/E cycles) and reduces drive lifespan. For SSDs, we only need to use the fstrim command.

Handling EXT4 with e4defrag

The biggest advantage of e4defrag is its ability to run online. You don’t need to unmount the drive or stop services, making it perfect for servers requiring high uptime.

Check the fragmentation level of a specific directory:

sudo e4defrag -c /var/www/html/

If the “Fragmentation score” exceeds 30, proceed with defragmentation:# Defragment a heavy file sudo e4defrag /var/lib/mysql/data_file.dbf # Defragment the entire partition sudo e4defrag /dev/sda1

Handling XFS with xfs_fsr

XFS uses xfs_fsr (Filesystem Reorganizer). This tool is very smart; it creates a temporary file containing the reorganized data and then swaps it with the old file to ensure data integrity.

# Optimize the entire XFS partition
sudo xfs_fsr /dev/sdb1

Automation and Monitoring

Instead of waiting for the server to hang before taking action, set up a script to check periodically during off-peak hours. Here is how I usually configure a cron job to run at 3 AM.

Create the optimize_disk.sh script:

#!/bin/bash
# Get the fragmentation score of the log directory
SCORE=$(e4defrag -c /var/log | grep "Fragmentation score" | awk '{print $3}')

# Only perform defragmentation if score > 30
if [ "$SCORE" -gt 30 ]; then
    echo "$(date): Score $SCORE is too high. Optimizing..." >> /var/log/defrag_history.log
    e4defrag /var/log
fi

Don’t forget to enable fstrim if you are using an SSD so the system can automatically clean up redundant data blocks:

sudo systemctl enable --now fstrim.timer

System administration is not just about installation but also about maintaining long-term performance. Understanding how file systems operate will help you resolve performance “bottlenecks” that sometimes even RAM or CPU upgrades cannot fix.

Share: