Ubuntu – How to make ext3 converted to ext4 support e4defrag

defragext3ext4

I have two ext3 filesystems that are very old and have been 99% full for a long time, leading to fragmentation. Also, they've been used for running VMs with dynamic disk images and other fragmentation-causing workloads.

I recently converted the filesystems to ext4 using the instructions here, specifically by running:

# tune2fs -O extents,uninit_bg,dir_index /dev/DEV
# e2fsck -fDC0 /dev/DEV

However, the filesystems still don't support e4defrag:

Failed to defrag with EXT4_IOC_MOVE_EXT ioctl:Operation not supported   [ NG ]

I'm guessing the tune2fs command lacks some option, but googling didn't help me figure out what that might be.

Best Answer

I had the same problem. I found this which had the answer:

https://wiki.archlinux.org/index.php/Ext4#Migrating_files_to_extents

Migrating files to extents

Warning: Do NOT use the following method with Mercurial repository that have been cloned locally, as doing so will corrupt the repository. It might also corrupt other hard link in the filesystem.

Even though the filesystem is now converted to ext4, all files that have been written before the conversion do not yet take advantage of the new extents of ext4, which will improve large file performance and reduce fragmentation and filesystem check time. In order to fully take advantage of ext4, all files would have to be rewritten on disk. A utility called e4defrag is being developed and will take care of this task; however, it is not yet ready for production.

Fortunately, it is possible to use the chattr program, which will cause the kernel to rewrite the file using extents. It is possible to run this command on all files and directories of one partition (e.g. if /home is on a dedicated partition): (Must be run as root)

find /home -xdev -type f -print0 | xargs -0 chattr +e
find /home -xdev -type d -print0 | xargs -0 chattr +e

It is recommended to test this command on a small number of files first, and check if everything is going all right. It may also be useful to check the filesystem after conversion.

Using the lsattr command, it is possible to check that files are now using extents. The letter 'e' should appear in the attribute list of the listed files.

--

Note: I had assumed e4defrag could convert the files to use extents. The failed ioctl is the one that does that afaik, so I am a little confused as to why it doesn't work, but chattr +e did work for me. After you have chattr +e the files, you can then use e4defrag on them. Obviously make sure you have a backup etc just in case (I would have thought this is safe, but you never know - there is a warning here about hardlinks but I find no other reference to known issues with hardlinks).

Related Question