Safe offline ext2/ext3/ext4 defragmentation

I have a very large RAID6 array (11TB) with an ext4 par­ti­tion that, due to par­tic­u­lar use cases, has become dis­gust­ingly frag­mented (~40% non-contiguous accord­ing to fsck). Sadly, as much as ext4 is designed to resist frag­men­ta­tion issues, my par­ti­tion has been hav­ing sub­stan­tial per­for­mance issues.

The ext4 defrag pro­gram e4defrag would be an ideal solu­tion to my prob­lems but it is not yet sta­ble enough for pro­duc­tion use.

Putting some thought into the mat­ter, I have came up with a tech­nique for defrag­ment­ing my par­ti­tion using only sta­ble tools. My process is very slow and requires sub­stan­tial peri­ods of down­time but pre­lim­i­nary results are good.

At present, I have 2TB of free space, which means that I can copy files off my frag­mented par­ti­tion and then copy them back to decrease the frag­men­ta­tion of indi­vid­ual files; alter­na­tively, the appli­ca­tion shake can be used to accom­plish a sim­i­lar result. However, the copy/recopy solu­tion will only work if the free space on my par­ti­tion is not frag­mented. Running e2freefrag I found that the free space on my par­ti­tion is mon­strously fragmented.

However, clever use of resize2fs can almost com­pletely defrag­ment the free space of a par­ti­tion. If you unmount the par­ti­tion, shrink it to a min­i­mum size and the expand it, the vast major­ity of the free space will be moved to a con­tigu­ous region at the end of the partition.

If the par­ti­tion is /dev/md1 and is mounted at /mnt/fragmented, your file sys­tem can be defrag­mented with the fol­low­ing set of commands:

$ umount /mnt/fragmented
$ fsck -f /dev/md1
$ resize2fs -M -p /dev/md1
$ resize2fs -p /dev/md1
$ mount /dev/md1 /mnt/fragmented
$ shake -o 0 -S 0 /mnt/fragmented

This will result in some defrag­men­ta­tion of your par­ti­tion but it is likely that the process will need to be repeated mul­ti­ple times to achieve a sub­stan­tial degree of defragmentation.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

*