[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Linux defragmenter? (ext2/3 defrag)

Robert Storey wrote:

On Tue, 19 Aug 2003 03:03:36 -0700
Paul Johnson <baloo@ursine.ca> wrote:

On Mon, Aug 18, 2003 at 08:23:04PM -0500, Joel Konkle-Parker wrote:
I just realized that I've never heard of a hard drive defragmenter
for Linux (ext2/ext3). Do I really live under a rock, or are they
really not used? If not, why?
My understanding is that ext2 and ext3 do a good job at keeping
themselves from getting fragmented.

This question gets asked a lot. Linux filesystems defragment themselves automatically - human intervention isn't needed. But to make it work properly, you do need to be sure that you keep some empty space (about 20%) on your partitions. The command "df -h" should tell you how much space is being used. Note that even with a Windows defragmenter, you still have this issue of needing empty space.
I haven't read about how any other filesystems used with the Linux kernel work, but the popular ext2 (and ext3 journaling extensions) use of inodes scattered across your partition(s) does a very good job of file management in a way that for most file operations avoids fragmentation when there is enough free space to write contiguous files.

The exception is for files that are constantly appended to and not rewritten. Almost all the files in /var/log and /var/spool fall into this case and back up the argument to put at least /var on it's own partition for servers. If anyone has some insights on filesystems for linux that offer better performance and minimized file fragmentation for continually growing files like log and mailbox files, I'd appreciate hearing about it. I thought I had read an opinion once that the journaling option of ext3 might more of a performance hit than it's worth for the /var tree and that you might want to do a filesystem check on that tree anyway after a crash.

Even with high fragmentation, people don't complain about serious slowdowns. Maybe that is why you don't hear people talking about defrag for ext2/3 file systems, and the defrag tool I knew about, e2defrag, is very out of date. I hate to even mention it here for fear it might spur someone's interest. I don't recomend you use it. Use these 'defrag tools' instead:

To defrag a directory (just the directory structure, not the files in the directory) and keep the contents of the directory available for access as long as possible try this:

mkdir newdir
ln currentdir/* newdir/
mv currentdir/ olddir/
mv newdir/ currentdir/
rm -rf olddir/

Replace ln with cp -ar if you want to have the contents of the directory also try to reconsolidate themselves on the disk, or with mv if you don't have the free space to copy the files and/or dont care if the file is inaccessable longer. From what I had read, using e2defrag required that the partition was unmounted, so the above commands are better in that sense.

Another ext2/3 'defrag' utility is tar:

cd /tmp
tar -czf mailspool.tgz -C /var/spool mail/
cd /var/spool
rm -rf mail
tar -xzf /tmp/mailspool.tgz

With all of these commands, let the user beware. Double-check yourself before committing to a rm -rf call. For example, if you're messing with /var/spool/mail, your mail programs should be stopped. Even better you should be in single user mode. Be sure you are really loosing performance and understand what you are about to do before wasting an hour trying to gain a couple seconds back. Using tar to copy an entire partition to a tar file on another partition should result in the best gains.

I have yet to experiance a situation where I've felt I need to defrag my ext2/3 partitions. The only reason the thought had crossed my mind for me to research a while ago this in the first place was my conditioning from using HPFS, NTFS and FAT(32) file systems.


Reply to: