How to defragment an ext3 filesystem

In general ext3 filesystems have very low fragmentation, so you don't need to defragment. This is because the kernel places files on the hard drive intelligently, reducing any fragmentation. Ext3 fragmentations is generally less than 10%. But if you use and ext3 filesystem to store large files, add and deleting them, on a mostly full partition, fragmentations can become very high. Greater than 50%. This is how to you can defragment a partitions.

=Tools=
 * Shake - http://www.vleu.net/shake/

=Checking Fragmentation=

Display partition's fragmentation percentage

 * unmout the partition
 * Execute: fsck -nvf /dev/sdb2

fsck from util-linux-ng 2.16 e2fsck 1.41.9 (22-Aug-2009) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information
 * Example output of a partition with 79.5% fragmentation:

1955 inodes used (0.08%) 1554 non-contiguous files (79.5%) 0 non-contiguous directories (0.0%) 3706004 blocks used (72.40%) 0 bad blocks 1 large file
 * 1) of inodes with ind/dind/tind blocks: 1660/1224/0

1825 regular files 121 directories 0 character device files 0 block device files 0 fifos 0 links 0 symbolic links (0 fast symbolic links) 0 sockets

1946 files

Display files that are fragmented

 * Execute: shake -pvv /mnt/ext3_partition/ | awk '{ printf("%06d,%s\n", $4, $8) }' | sort > fragmented_file_list.txt
 * Wait a long time, for it to complete
 * When done, view fragmented_file_list.txt

000001,/mnt/ext3_partition/file1.avi 000002,/mnt/ext3_partition/file2.avi 004597,/mnt/ext3_partition/file3.avi
 * Example output:
 * First column is # of fragments
 * Second column is the file that is fragmented

=Defragmenting=


 * Install shake
 * Make sure the partition is mounted with the "user_xattr" option
 * To check execute: mount
 * Example output: /dev/sdb2 on /mnt/ext3_partition type ext3 (rw,noatime,user_xattr)
 * If you don't have that option add it to /etc/fstab
 * Example: /dev/sdb2 /mnt/ext3_partition ext3 auto,noatime,user_xattr 0 0
 * Run shake to defragment
 * Execute: shake --old=0 --bigsize=0 /mnt/ext3_partition
 * Wait a long time for the process to finish

Optimizing the defragmentation

 * Make as much free space as possible.
 * You wont be able to do a good defragmentation without empty space to move blocks. 15% or greater is recommended.
 * Keep a log of fragmentation, before and after, to show the improvements
 * Execute before: shake -pvv /mnt/ext3_partition/ | awk '{ printf("%06d,%s\n", $4, $8) }' | sort > fragmented_file_list-before.txt
 * Execute after: shake -pvv /mnt/ext3_partition/ | awk '{ printf("%06d,%s\n", $4, $8) }' | sort > fragmented_file_list-after.txt
 * And compare
 * Running shake multiple times, can remove more fragmentation

=References=
 * http://www.webupd8.org/2009/10/defragmenting-linux-ext3-filesystems.html
 * http://blog.timmattison.com/archives/2011/08/10/how-to-defragment-a-linux-system/