1. Overview

Fragmentation occurs when parts of a file are scattered across a disk, causing inefficient read/write operations and reducing performance. It’s more likely to occur in large files that are frequently modified. To mitigate this problem, defragmentation tools reorganize file fragments, aligning them contiguously on disk to improve access speed and overall system efficiency.

Ext4 file systems are inherently designed to reduce fragmentation through advanced techniques such as extent-based allocation, which groups contiguous blocks of data together. Typically, ext4 file systems experience minimal fragmentation, making extensive defragmentation processes less critical.

In this tutorial, we’ll see how to effectively use e4defrag, a command-line tool for defragmenting ext4 file systems. We’ll look at when this tool is necessary to maintain system performance despite the inherent efficiency of ext4.

2. e4defrag

e4defrag performs online defragmentation, which means it defragments files while the system is running and the file system is mounted and in use. To be clear, we can safely defragment a file while another program is using it.

In most cases, e4defrag comes with our Linux distribution. Let’s check its version:

$ e4defrag
e4defrag 1.46.5 (30-Dec-2021)

If it’s missing, we can install it on all major distributions. It’s part of the e2fsprogs package:

  • Debian → sudo apt update && sudo apt install e2fsprogs
  • Fedora → sudo dnf install e2fsprogs
  • openSuse → sudo zypper refresh && sudo zypper install e2fsprogs
  • Gentoo → sudo emerge –sync && sudo emerge sys-fs/e2fsprogs
  • Arch Linux → sudo pacman -Sy e2fsprogs

e4defrag can defragment a single file, a single directory, or an entire file system. But first, let’s see how to check the fragmentation level of our files.

2.1. Check the Fragmentation Level

To get an accurate report of the fragmentation level with the -cv options, we need to use sudo even on files we own. The following tests are informative only, as they don’t perform any defragmentation.

Let’s check the fragmentation of a file:

$ sudo e4defrag -cv /media/francesco/[...]/doc2009.inc
 Fragmentation score				85
 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag]
 This file (/media/francesco/[...]/doc2009.inc) needs defragmentation.

In this case, e4defrag clearly indicates that this file needs defragmentation.

Now let’s check a directory:

$ sudo e4defrag -cv '/home/francesco/'
<Fragmented files>                             now/best       size/ext
1. /home/francesco/snap/telegram-desktop/common/.cache/mesa_shader_cache/index
                                                69/1              4 KB
2. /home/francesco/.zoom/data/zoomus.enc.db
                                                16/1              4 KB
3. /home/francesco/.config/opera/Session Storage/000016.log
                                                13/1              4 KB

 Fragmentation score				1
 This directory (/home/francesco/) does not need defragmentation.

Finally, let’s examine an entire block device, in this case, an LVM logical volume mounted as the root filesystem. The piping of mount and grep is just to make sure we’ve chosen the right device:

$ mount | grep /dev/mapper/vgmint-root
/dev/mapper/vgmint-root on / type ext4 (rw,relatime,errors=remount-ro)

$ sudo e4defrag -cv /dev/mapper/vgmint-root
Fragmentation score				1
 This device (/dev/mapper/vgmint-root) does not need defragmentation.

As in these cases, it’s common for e4defrag to tell us that there is no need to defragment our directories or disks.

2.2. Defragment a File or Directory

As for the defragmentation, we don’t need sudo on the files or directories we own.

Earlier, we saw that doc2009.inc had a high fragmentation score. Let’s defragment it:

$ e4defrag -v /media/francesco/[...]/doc2009.inc
ext4 defragmentation for /media/francesco/[...]/doc2009.inc
[1/1]/media/francesco/[...]/doc2009.inc:	100%  extents: 2 -> 1	[ OK ]
 Success:			[1/1]

After that, the fragmentation level of this file is zero.

Now let’s defragment our home directory. It takes some time because, in this case, it contains 166713 files, some of them very heavy:

$ e4defrag -v ~
ext4 defragmentation for directory(/home/francesco)
[87632/166713]/home/francesco/VirtualBox VMs/[...]/2024-01-04T07-36-56-796921000Z.sav:	100%  extents: 21 -> 21	[ OK ]
[87633/166713]/home/francesco/VirtualBox VMs/[...]/2024-01-04T07-26-35-941169000Z.sav:	100%  extents: 52 -> 18	[ OK ]
	Success:			[ 146854/166713 ]
	Failure:			[ 19859/166713 ]
	Total extents:			175047->170480
	Fragmented percentage:		  2%->0%

The log details each file, with self-explanatory statistics at the end. In this case, our home directory has reduced its fragmentation from 2% to zero.

2.3. Defragment an Entire File System

If we specify a device or mount point to e4defrag, it only defragments files within the specified device or mount point. For example, if we want to defragment the root directory /, e4defrag only processes files directly within the root file system. It won’t extend its operation to files in other nested mount points, such as /mnt or /media, even though they’re technically under /. This ensures that e4defrag‘s action is limited to the selected device or directory, avoiding unintended defragmentation of files in different devices or partitions.

That said, let’s check the device we want to defragment:

$ mount | grep "on / type ext4"
/dev/sda1 on / type ext4 (rw,relatime)

Let’s defragment it:

$ sudo e4defrag -v /dev/sda1
ext4 defragmentation for device(/dev/sda1)
    Success:                [ 264269/349296 ]
	Failure:                [ 85027/349296 ]
	Total extents:          273250->267225
	Fragmented percentage:  0%->0%

In this case, defragmentation was completely unnecessary.

3. Conclusion

In systems where large, frequently modified files are common, such as databases, video editing suites, or servers that handle large amounts of changing data, fragmentation can have a significant impact on performance.

In this article, we’ve seen how e4defrag can defragment our files, directories, or entire file systems. e4defrag also provides us with statistics to help us decide whether defragmentation is necessary or not.