Fragmentation?

Deron Meranda deron.meranda at gmail.com
Fri Mar 18 23:42:27 UTC 2005


On Fri, 18 Mar 2005 11:55:36 -0800 (PST), Jesse Hawkins
<jhawk24821 at yahoo.com> wrote:
> I am pretty new to linux, so this might be a
> rediculous question to some: Do you have/need to
> defrag a linux hard disk?

Defragmentation is generally not necessary under Linux's
ext2/ext3 filesystems.  If fragmentation ever becomes an
issue at all (which is very rare), it's mostly likely under these
conditions:
   1. The filesystem is frequently near full capacity
   2. There is a lot of writing and deleting going on.
For #2, it's especially troublesome for files that are written
a little bit at a time (they "grow" slowly), such as log files--
files written all in one shot are fairly imune to fragmentation.

That being said FC3 does come with a couple tools which
may help diagnose particularly rare worst cases.

  filefrag /path/to/some/file

The filefrag command tells you how "fragmented" any particular
file may be and if it is optimal or not.  I don't think there is any
command which gives you an "average fragmentation" over the
whole filesystem.


   e2fsck -D /dev/hda99   (or LVM device path)

The -D option of e2fsck (fsck) will reorganize the directory entries
so they are sorted, optimally indexed, and compressed.  You'll
want to do this while the filesytem is not mounted (perhaps in
single-user boot mode).  This could be useful if you have a filesystem
that has very large directories (thousands of entries) and also has
a lot of churn to them.

Finally, if one file is quite fragmented, usually just copying it will
result in a copy which is not fragmented.  Then you delete the
original and put the copy in it's place.  You only want to do this
if you know the file is not currently in use or open by some process.

   cp -p  a_fragmented_file  a_copy
   rm -f  a_fragmented_file
   mv  a_copy  a_fragmented_file

Also if you have a bunch of files, you can use a bulk-copy method
like tar (use "star" if you use SElinux so security context attributes
are preserved).


I guess the only way to perfectly optimize a fileystem using only
the tools provided under FC3 would have to be something like:

  1. Create a new empty filesystem (at least as large as the
      amount of used space in the old one).  For this example,
      lets say they are
            /dev/hdb1 - /old
            /dev/hdb2 - /new
  2. Unmount /old
  3. Perform an fsck with -D on it:   fsck -D /dev/hdb1
  4. Mount /old as read-only:  mount -o ro /dev/hdb1 /old
  5. Copy the whole filesystem,
       star -C /old -c -xattr -M | star -C /new -x -p -xattr -no-fsync
  6. Unmount both /old and /new
  7. Remount the new filesystem in place of the old one
      (don't forget to update the /etc/fstab file).
  8. If your fstab file used filesystem labels (you see LABEL=xxxx) then
      you'll need to relabel the filesystems to.  Read the man for e2label


But again, the normal user should never need to worry about
fragmentation.
-- 
Deron Meranda




More information about the fedora-list mailing list