22

...and how do I find out?

Say I am about to reboot a server. I would like to minimize downtime, so thinking about wrapping reboot in an alias that says "hang on buddy, you're going to hit a fsck on boot".

Next question.. what's the best way to say "lets do it next time?" set the last check date?

I know tune2fs can set a bunch of parameters, but how would I get em?

Tom Newton
  • 4,021
  • 2
  • 23
  • 28

8 Answers8

20

If all you want to do is avoid an fsck, adding the -f option to shutdown should help with that. shutdown -F to force fsck.

tune2fs -l /dev/foo will tell you the interesting bits of information.

Here's a start at extracting just what you need:

mount -l -t ext3,ext2 | cut -d' ' -f1 | xargs -n1 tune2fs -l | egrep -i 'state|mount|check'

If the "Next check after" date is in the past, there will be an fsck.

If the filesystem state is not clean, there will be an fsck. (this could also happen if there's a problem with the system during reboot/shutdown that prevents a clean unmount)

If the mount count has reached the maximum mount count, there will be an fsck.

freiheit
  • 14,334
  • 1
  • 46
  • 69
  • Thanks - if I ussue shutdown -f, will the next reboot get the fsck instead? – Tom Newton Jun 19 '09 at 07:27
  • 1
    Assuming your system currently meets one of the criteria for getting an fsck, yes, the next reboot would get the fsck instead. "shutdown -f" creates a file that tells the system startup to skip fsck, but the system startup also removes that file. – freiheit Jun 19 '09 at 15:08
  • Consider using `ext4,ext3,ext2` these days. – rudolfbyker Jul 10 '20 at 10:44
10

Using tune2fs -l /path/to/device:

  • If "Mount count" will be greater than "Maximum mount counts" then use -c to change the max or -C to change the count
  • If "Last Checked" is not recent enough for the "Check interval" use -i to change the interval or -T to change the last checked
brian-brazil
  • 3,904
  • 1
  • 20
  • 15
6

the other option is you can manually make it skip fsck checks at boot by updating the 6th field in your /etc/fstab:

/dev/sda2  /  ext3  defaults,errors=remount-ro 0       1

This is the similar to what most fstabs will have 1 means that it should be checked and is a root file system, 2 means it should be checked but will be done in parallel with other file systems and 0 means skip checking

this is also true no matter the file system

Brendan
  • 914
  • 6
  • 5
2

If you don't want to guess - do this:

touch /fastboot

and you'll be certain to avoid slow checks (or worse - a total failue if a check fails and the reboot halts)

1

I use the following perl script to check when the next fsck will occur:

#!/usr/bin/perl -w

use strict;

my $mountcount = 0;
my $maxmount = 0;
my $dev;
my $mountpoint;
my $fstype;
my $debug = 0;

my $cmd = "mount";

open MOUNT, sprintf ( "%s |", $cmd ) or die sprintf ( "ERROR: Cannot execute %s - %s\n", $cmd, $! );
while ( <MOUNT> ) {
    chomp;
    ( $dev, undef, $mountpoint, undef, $fstype, undef ) = split;
    printf "Found device %s\n", $dev if ( $debug > 0 );

    if ( $fstype =~ /^ext/i ) {
        $cmd = sprintf "dumpe2fs -h %s 2>&1", $dev;
        open DUMPE2FS, sprintf ( "%s |", $cmd ) or die sprintf ( "ERROR: Unable to execute %s - %s\n", $cmd, $! );
        while ( <DUMPE2FS> ) {
            chomp;
            if ( /^Mount count:/ ) {
                ( undef, $mountcount ) = split /:/;
                $mountcount =~ s/\s*//g;
                printf "Mount count = %s\n", $mountcount if ( $debug > 0 );
            }
            if ( /^Maximum mount count:/ ) {
                ( undef, $maxmount ) = split /:/;
                $maxmount =~ s/\s*//g;
                printf "Maximum mount count = %s\n", $maxmount if ( $debug > 0 );
            }
        }
        close DUMPE2FS;

        printf "Filesystem %s, mounted on %s will be checked in %s mounts\n", $dev, $mountpoint, $maxmount - $mountcount;
    }
}
close MOUNT;

I have it run in my ~/.bashrc so I always know when my filesystems will be checked, though I use ext4 now which doesn't suffer from extended fsck times, it's still nice to know.

Typical output is something like:

Filesystem /dev/sda1, mounted on / will be checked in 5 mounts
user@localhost ~ $
RivieraKid
  • 111
  • 5
0

I regularly use tunefs to reset boot counts and time before doing a mid-day reboot, to prevent fsck. The difference in boot time is stunning. Afterwords I reset them to allow the next boot to fsck.

kmarsh
  • 3,103
  • 15
  • 22
0

Here is a script that checks your filesystems for fsck (source):

for x in $(df -t ext4 -t ext3 -t ext2 | tr -s ' ' | cut -d " " -f1 | grep -v "^$" | tail -n +2); do mmc=$(tune2fs -l $x | grep 'mount count' | tr -s ' ' | cut -d ' ' -f4) ; mc=$(tune2fs -l $x | grep 'Mount count' | tr -s ' ' | cut -d ' ' -f3) ; if [ `expr $mmc - $mc` -le 0 ] ; then fsck="0" ; else fsck="1"; fi ; CT=`date +%s` ; LCT=`date -d "\`tune2fs -l $x | grep "Last checked" | tr -s ' '| cut -d" " -f3-\`" +%s` ; CI=`tune2fs -l $x | grep "Check interval"| tr -s ' '| cut -d" " -f3` ; if [ `let $CT-$LCT` -gt `let $CI*3600*24` ] && [ $CI -gt 0 ] || [ $fsck -eq 1 ];  then echo "There will be forced fsck for $x"; else echo "There will be no fsck for $x" ; fi ; done
# Alternative script from friend with sed
# mount -t ext2,ext3,ext4|while read i j; do tune2fs -l $i|sed -n '/[Mm]ount count/{s/.*: *//;p}'|(read c; read m; [ $m -gt 0 -a $m -le $c ] && echofsck,count,$i); c="$(tune2fs -l $i|sed -n '/Next check/{s/.*r: *//;p}')"; [ -z "$c" ] || ([ `date +%s` -ge `date -d"$c" +%s` ] && echo fsck,time,$i); done
tshepang
  • 377
  • 2
  • 12
Avyd
  • 1
  • 1
    Please dont just post links to external sites - copy over useful information and provide the link for reference. – Frederik Oct 16 '13 at 15:16
0

I thought about how to get a list of filesystems more comfortably:

for D in $(mount -l -t ext3,ext2 | cut -d' ' -f1 ) ; do \
echo --- $D; tune2fs -l $D | egrep -i 'mount (count|time)|check' ; done \
| awk '
  /^--- / { mydev=$2; checked=0 }
  /^Mount count:/ { mycnt=$3 }
  /^Maximum mount count:/ { if (mycnt >= $3) checked=1 }
  /^Next check after:/ {
    cmd=("date +%s -d\"" $4 FS $5 FS $6 FS $7 FS $8"\"");
    cmd | getline mydate; close(cmd);
    if ((mydate <= (systime()+120)) || (checked == 1)) print mydev;
  }
'

I guess it could be done more elegantly but anyway here it is. If one pipes this into xargs -r df -h one could also quickly see how large the filesystems are.

Note that in the code above the time of "Next check" is compared to (NOW + 120 seconds) assuming that it takes some time until your system gets up again.

HTH

xebeche
  • 341
  • 3
  • 11