7

Usually, fsck is executed automatically at startup after every 30th reboot or so. Sometimes, it can be really slow, and can delay bootup by more than half an hour.

Is there any way to run fsck at shutdown instead? (I don't mind if shutdown is delayed. Bootup time is more important.)

I would like this for the Debian or Scientific Linux distros.

3 Answers3

5

While you can check some things during shutdown, you cannot effectively run fsck on your root filesystem. You see, fsck requires that the filesystem is not mounted before it can attempt repair. This is only possible during boot, while still running from initramfs.

Dennis Kaarsemaker
  • 18,793
  • 2
  • 43
  • 69
  • No. It's [difficult](http://serverfault.com/q/405248) to repair a root filesystem at shutdown time, but it's almost surely possible. One option might be: Remount the filesystem read-only, then call a custom tool which you could write in C. The tool could run `/sbin/fsck`, then wait for it to finish, then call `sync()`, then call `reboot(RB_POWER_OFF)` — all in immediate succession. Note: I'm no kernel whiz; you should pay a qualified expert to check this comment before relying on it. See [this link](http://chat.stackexchange.com/transcript/message/25792356#25792356) for more ideas. – unforgettableidSupportsMonica Dec 04 '15 at 17:28
1

For Debian you can use AutoFsck but the package is no longer supported (last update 2010). So keep that in mind.

The other way would be to put a FSCK/bash script in your shutdown script. For SE linux/centos etc I would use chkconfig level 0 (halt) and 6(reboot). chkconfig --levels 0 rc.local-shutdown on Put the script in /etc/rc.d/init.d/. (level 6 for reboot)

For Debian I would put the create the scripts in /etc/rc6.K00scriptname and /etc/rc0.d/K00scriptname,

As for the script, you can use something like this:

#!/bin/bash
NETFS="nonfs,nosmbfs,nocifs,nocodafs,noncpfs,nosysfs,nousbfs,noshfs,nofuse,nofuseblk"

if (( `tune2fs -l /dev/sda2 | awk '/Mount count/{print $3}'` > `tune2fs -l /dev/sda2 | awk '/Maximum mount count/{print $4-1}' ))
then /sbin/fsck -A -T -C -a -t $NETFS 2>/dev/null
fi

This code was not written by me, but by someone on archlinux forums. I have not tried the code. So keep that in mind too.

Or you can just do something simple like /sbin/fsck -A -T -C -a -- -f inside the script.

Sc0rian
  • 1,011
  • 7
  • 16
1

The problem with fsck on shutdown is that, you see when you are shutting down, you are unmounting the filesystem. Even if the fs is dirty, unmounting a fs doesn't force it to check it's integrity. While, you are mounting it, you are performing a fsck to check whether the metadata for the data is correct and whether fs has proper information for the fs as it reports it has. This can take place while the fs is going to be mounted. Because the code to check the fs integrity comes to play when the fs is going to be mounted.

This is I would guess the reason that we can do a lazy umount even if the fs is dirty but there is no lazy or forced umount when the fs is dirty ;)

Usually journalling fs shouldn't take long time to come up unless it is heavily screwed. xfs_repair is pretty slick and can be done online while btrfs is well, another story.

Soham Chakraborty
  • 3,534
  • 16
  • 24