Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

At boot, fsck sometimes reports the frgmentation for a disk: "5.3% non-contiguous".

How can I get this information myself? Is there a specific fsck invocation?

share|improve this question

4 Answers 4

up vote 5 down vote accepted

You can safely run fsck.ext2 on an ext2 ext3 or ext4 filesystem even if it's mounted like this

sudo fsck.ext2 -fn /dev/sdXY

If the drive is mounted, it will output a lot of errors, but the -n argument tells it not to touch the drive.

$ sudo fsck.ext2 -fn /dev/sda1
e2fsck 1.42 (29-Nov-2011)
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
Ubuntu_Rescue: 291595/1222992 files (0.2% non-contiguous), 1927790/4882432 blocks
share|improve this answer
    
Very cool, thanks! –  Peltier Oct 11 '13 at 7:34
    
Thanks! fsck.ext2 found several errors on my home partition. Now I would like to repair it. This begs the question: how do I repair my home partition, which I need to keep mounted in order to login? –  Michael Jun 18 at 21:25
    
@Michael If your drive is mounted, running a check on it will always report errors. Anyway, if you want check to the filesystem on next reboot, run this sudo tune2fs -C 16000 /dev/sda1 and reboot. –  Rucent88 Jun 27 at 9:36

Yes, there is a specific fsck invocation (as root, or sudo):

$ fsck /dev/sdXY

Replace X and Y with the correct parameters for your case (e.g. /dev/sda1).

DO NOT RUN THIS ON A MOUNTED PARTITION! It can seriously damage your filesystem.

For some more information on Linux and defragmenting, see my answer here.


If you do not need to specifically run fsck, but just want to check your drive's fragmentation, the following script (taken from here) should do the trick:

#!/usr/bin/perl -w

#this script search for frag on a fs
use strict;

#number of files
my $files = 0;
#number of fragment
my $fragments = 0;
#number of fragmented files
my $fragfiles = 0;

#search fs for all file
open (FILES, "find " . $ARGV[0] . " -xdev -type f -print0 |");

$/ = "\0";

while (defined (my $file = <FILES>)) {
        open (FRAG, "-|", "filefrag", $file);
        my $res = <FRAG>;
        if ($res =~ m/.*:\s+(\d+) extents? found/) {
                my $fragment = $1;
                $fragments += $fragment;
                if ($fragment > 1) {
                        $fragfiles++;
                }
                $files++;
        } else {
                print ("$res : not understand for $file.\n");
        }
        close (FRAG);
}
close (FILES);

print ( $fragfiles / $files * 100 . "% non contiguous files, " . $fragments / $files . " average fragments.\n"); 

You can then run it on the directory (or mount point) in question:

$ frag_check.pl $HOME/
  1.32410691664555% non contiguous files, 1.05380668862427 average fragments.
share|improve this answer
    
That's just calling fsck on the partition, and as we both know, it will destroy a mounted partition. Is there any way to use fsck in a read-only, tell-me-how-fragmented-my-disk-is way? –  Peltier Sep 15 '12 at 13:33
    
Not as far as I know, @Peltier. I was thinking you can boot into a lower runlevel, unmount the relevant disk and run fsck. In any case, seriously, you can almost certainly ignore fragmentation on most linux systems. –  terdon Sep 15 '12 at 13:39
    
@Peltier, see my updated answer. –  terdon Sep 15 '12 at 14:04
    
I appreciate the effort, but this is going to be extremely slow. I'll wait for a few days to see if anyone has a better solution to offer. –  Peltier Sep 15 '12 at 14:08
1  
Yeah, it is slow. Mind you it took ~2-3mins on my 500GB /home/ partition. Again, I stress, drive fragmentation is not really a problem any more. Especially if you are using an EXT3-4 filesystem. –  terdon Sep 15 '12 at 14:11

The modern ext family of filesystems take balancing and contiguity into consideration when allocating files. In ext3 and ext4 balancing is generally taken care of by applying journal entries in order, and in ext4 specifically, by pre-allocating file-system extents to increase contiguous blocks. Chasing fragmentation levels lower than 20% might not be worth your time, especially if your system is accessing a few files repeatedly. I suggest pursuing these tactics for increasing read speed for a single disk system:

  • increase ram: this increases filesystem cache

  • increase block size: by moving from 1k towards 4k block sizes, you get better performance on reads for files near or larger than your block size

  • if you are pedantic, you can organize large files and small files into partitions by their block size, you might only want to do this if you stored photos, videos and source code in separate directories

  • increase your read-ahead setting using hdparm and/or laptop-mode utility

  • backup and restore your filesystem in a linear fashion such as with tar

If you have multiple disks and are using raid striping and/or LVM, you other factors to consider, such as stripe size.

share|improve this answer

You can use a tool like e2defrag to get this information.

Do mind that because of the nature of Linux file systems, defragging is normally unnecessary.

share|improve this answer
    
Would you mind detailing your answer? I tried getting that information with e4defrag -c, but it didn't work. –  Peltier Sep 15 '12 at 13:19
    
@Peltier Point it to an Ext3 or Ext4 moint point after the -c option as the last argument. And... "didn't work"? What is the error you get? –  gertvdijk Sep 15 '12 at 13:20
    
It says "Done". I didn't, however, point it directly to the device, but to the mount point. I am trying again with the device. –  Peltier Sep 15 '12 at 13:23
    
Note that it takes a lot of time, whereas fsck is very fast. –  Peltier Sep 15 '12 at 13:23
    
Just tried it again with the device, it only says "Done.". –  Peltier Sep 15 '12 at 13:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.