35

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?

4 Answers 4

31

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
3
  • 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, 2014 at 21:25
  • 4
    @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, 2014 at 9:36
  • 1
    fsck checks what is on the disk/storage, but if mounted then there will be stuff in progress (half written). That is why you will see errors, they are not real errors, they are false errors. Ignore them. Mar 26, 2016 at 10:12
10

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.

6

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.
12
  • 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, 2012 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, 2012 at 13:39
  • 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, 2012 at 14:11
  • 1
    @KentFredric Ah! Yes, indeed. I see what you mean now. I edited accordingly, thanks. Let's delete the comments to remove clutter.
    – terdon
    Feb 6, 2016 at 19:18
  • 1
    Here's an accelerated alternative that doesn't use system() at all, and uses Path::Iterator::Rule for traversal and uses FS_IOC_FIEMAP ioctls to extract the number of extents instead of a system(filefrags) call, which should be much faster due to the lack of elf startup overhead. gist.github.com/kentfredric/ede160396eb96aea37dc Feb 7, 2016 at 9:38
1

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.

8
  • Would you mind detailing your answer? I tried getting that information with e4defrag -c, but it didn't work.
    – Peltier
    Sep 15, 2012 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, 2012 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, 2012 at 13:23
  • Note that it takes a lot of time, whereas fsck is very fast.
    – Peltier
    Sep 15, 2012 at 13:23
  • Just tried it again with the device, it only says "Done.".
    – Peltier
    Sep 15, 2012 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .