LinuxCommandLibrary

e4defrag

Defragment ext4 filesystem files

TLDR

Defragment the filesystem

$ e4defrag [/dev/sdXN]
copy

See how fragmented a filesystem is
$ e4defrag -c [/dev/sdXN]
copy

Print errors and the fragmentation count before and after each file
$ e4defrag -v [/dev/sdXN]
copy

SYNOPSIS

e4defrag [options] file|directory...
e4defrag -c [options] file|directory...

PARAMETERS

-c
    Checks the current fragmentation level of the specified files or directories without performing defragmentation. It reports the number of fragmented files and their total fragmentation score.

-v
    Enables verbose output, showing more details about the defragmentation process, including the progress and the fragmentation statistics for each file.

-h
    Displays a brief help message and exits.

-V
    Prints the version information for e4defrag and exits.

DESCRIPTION

e4defrag is a utility designed to defragment individual files or directories on an ext4 filesystem. Unlike traditional offline defragmentation tools that operate on the entire filesystem, e4defrag works online, meaning it can be run while the filesystem is mounted and in use. Its primary purpose is to improve read/write performance for fragmented files by consolidating their data blocks into contiguous regions on disk.

While fragmentation on modern filesystems and SSDs is less of a concern, it can still impact performance, especially for frequently modified large files or databases on traditional hard disk drives (HDDs). The command uses the EXT4_IOC_MOVE_EXT ioctl, a kernel-level operation, to efficiently move extents (contiguous block groups) of a file. It does not reclaim unused blocks or shrink the filesystem; it only rearranges existing file data.

CAVEATS

e4defrag is specifically designed for ext4 filesystems and will not work on other filesystem types (e.g., XFS, Btrfs, ext3). It requires a Linux kernel that supports the necessary online defragmentation ioctls (e.g., EXT4_IOC_MOVE_EXT), typically available since kernel 2.6.28 or later. While it can improve performance for highly fragmented files, it does not reclaim unused space for the filesystem or reduce its overall size; it only rearranges existing data. For filesystems on Solid State Drives (SSDs), defragmentation is generally unnecessary and can even reduce the lifespan of the SSD due to increased write amplification. Always ensure you have backups before performing significant disk operations.

KERNEL REQUIREMENTS

The functionality of e4defrag relies on specific kernel support for online ext4 defragmentation, notably the EXT4_IOC_MOVE_EXT ioctl. This feature became stable and widely available in Linux kernels from version 2.6.28 onwards. Running e4defrag on older kernels or kernels without this support will likely result in errors or non-functional behavior.

MECHANISM OF OPERATION

Unlike traditional block-level defragmenters that operate on the entire disk, e4defrag works by moving 'extents' (contiguous blocks of data) within a file. It identifies fragmented files and attempts to relocate their extents to consolidate them into larger, contiguous extents. This process is performed online, meaning the file can remain open and in use during defragmentation, although performance might be temporarily impacted by the I/O activity.

WHEN TO USE?

While fragmentation is less critical on modern Linux systems, e4defrag can still be beneficial in specific scenarios:

  • On traditional spinning Hard Disk Drives (HDDs) that store frequently modified large files (e.g., video files, large databases, virtual machine images).
  • To improve sequential read/write performance for applications that heavily rely on contiguous file access.
  • When fragmentation levels are exceptionally high (which e4defrag -c can report) and causing noticeable performance degradation.
It is generally not recommended for SSDs, as it increases writes unnecessarily and offers little to no performance benefit due to the nature of SSDs.

HISTORY

e4defrag was introduced as part of the e2fsprogs utilities, which provide a suite of tools for managing ext2, ext3, and ext4 filesystems. Its development specifically targeted the ext4 filesystem, leveraging new kernel features that allow for online extent-based defragmentation. The core functionality relies on the EXT4_IOC_MOVE_EXT ioctl, which was added to the Linux kernel around version 2.6.28-2.6.30. This command provides a user-space interface to this kernel capability, making online defragmentation of individual files a reality for ext4, addressing some fragmentation concerns prevalent on traditional spinning hard drives without requiring filesystem unmounting.

SEE ALSO

fsck(8), tune2fs(8), mke2fs(8), dumpe2fs(8), xfs_fsr(8)

Copied to clipboard