Defragmenting EXT4 file systems with e4defrag (coming soon to a distribution near you)


If you have been around the systems engineering field you have probably read about file system fragmentation at some point. This typically occurs when files are randomly updated over time, and the blocks that comprise the file get scattered over different areas of the disk. This causes the drives to perform more work since the drive heads have to perform additional seeks vs. being able to sequentially read data off of a given platter. Anytime you do sequential I/O vs. random I/O you are better off.

Several file systems provide tools to defragment file systems, and it looks like the EXT4 engineers are planning to come out with a similar tool for EXT4. The tool will be called e4defrag, and a test version of the tool is available in the e2fsprogs git repository. This utility will be super handy when it’s stable, and should assist with getting every last bit of performance out of your EXT4 file system.

Before I provide an overview of this tool I need to state that this tool is currently being released for testing, and there is the possibility of data corruption if you use it!! Do not use this utility on anything you need to keep around! This isn’t my opinion, this is the opinion from the developers themselves:

$ e4defrag -c /mnt

This is a release only for testing, and bugs may
which could corrupt your data. Please invoke
with "-test" if you wish to use it at this time.
Usage : e4defrag [-v] file...| directory...| device...
: e4defrag -c file...| directory...| device...

With that said, e4defrag can be used to defragment a file, a directory or a file system that has been placed on a device. To see how fragmented your file system is you can run e4defrag with the “-c” option:

$ e4defrag -c -test /mnt

now/best ratio
1. /mnt/mail/domaintable.db 2/1 50.00%
2. /mnt/mail/virtusertable.db 2/1 50.00%
3. /mnt/mail/mailertable.db 2/1 50.00%
4. /mnt/file 72148/1 0.77%
5. /mnt/elinks.conf 1/1 0.00%

Total/best extents 74010/1860
Fragmentation ratio 3.84%
Fragmentation score 30.72
[0-30 no problem: 31-55 a little bit fragmented: 55- needs defrag]
This directory(/mnt) does not need defragmentation.

Nifty! The utility will print the files that are fragemented, the number of extents that are associated with the file, and the fragmentation ratio. Here is the full description of the fields from the source code:

struct frag_statistic_ino {
        int now_count;          /* the file's extents count of before defrag */
        int best_count;         /* the best file's extents count */
        __u64 size_per_ext;     /* size(KB) per extent */
        float ratio;            /* the ratio of fragmentation */
        char msg_buffer[PATH_MAX + 1];  /* pathname of the file */
};

To defragment a fragmented file, directory or device you can run e4defrag without the “-c” option:

$ e4defrag -test /mnt

This will take some time, and the result will hopefully be files that aren’t scattered all through out the hard drive. While this tool isn’t ready for primetime, it’s nice to know that it will be available down the road. You are on your own if you decide to use e4defrag. I provide zero warranties or assurances on the information provided. You are seriously putting your data at risk if you choose to ignore the various warnings provided here.

This article was posted by Matty on 2011-11-04 10:13:00 -0400 -0400