LinuxCommandLibrary

e2freefrag

Report file system free space fragmentation

TLDR

Check how many free blocks are present as contiguous and aligned free space

$ e2freefrag [/dev/sdXN]
copy

Specify [c]hunk size in kilobytes to print how many free chunks are available
$ e2freefrag -c [chunk_size_in_kb] [/dev/sdXN]
copy

SYNOPSIS

e2freefrag [-n] [-v] device [blocks...]

PARAMETERS

-n
    Dry-run mode: scan and report without freeing blocks.

-v
    Verbose mode: print details on each block processed.

device
    Required: path to the block device (e.g., /dev/sda1).

blocks...
    Optional: space-separated list of block numbers to free; scans all if omitted.

DESCRIPTION

The e2freefrag command is a low-level utility from the e2fsprogs package used to free fragmented blocks in ext2, ext3, and ext4 filesystems. These fragments are blocks marked as allocated in the block bitmap but not referenced by any inode, indirect block, or other metadata structures. Such orphans can accumulate after filesystem crashes, aggressive e2fsck repairs (e.g., with -y), or manual edits.

It scans specified block numbers (or the entire filesystem if none provided) to verify if they are truly unused, then clears their allocation bits to reclaim space. This reduces internal fragmentation, potentially improving performance and allowing better space utilization for new allocations.

e2freefrag operates directly on the raw block device, bypassing the kernel's filesystem layer, making it powerful but risky. It is essential for recovery scenarios where free space appears insufficient despite ample unused blocks. Always run on unmounted filesystems with root privileges, and consider a dry run first. Pair it with tools like debugfs to inspect blocks beforehand.

CAVEATS

Run as root on unmounted filesystems only; direct bitmap modification risks corruption if blocks are in-use. Verify blocks with debugfs or dumpe2fs first. No journaling protection on ext3/4.

TYPICAL USAGE

After e2fsck -y /dev/sda1, run e2freefrag -n -v /dev/sda1 to preview, then without -n.

BLOCK INSPECTION

Use debugfs -R "blockdump N" /dev/sda1 (N=block#) or hexdump to confirm fragments before freeing.

HISTORY

Developed by Theodore Ts'o as part of e2fsprogs (introduced ~1.19 in 2000) for ext2; extended for ext3/4 support in later versions to address fragmentation post-fsck repairs.

SEE ALSO

debugfs(8), e2fsck(8), dumpe2fs(8), tune2fs(8)

Copied to clipboard