Performing fsck on non-root filesystem is fairly straight forward. But, for root filesytem, you cannot perform fsck when it is mounted.
This quick tutorial explains how to force filesytem check for a root filesystem.
Root Filesystem
In this example, /dev/sda1 partition is the root filesystem that is mounted as /
# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 63G 41G 19G 69% /
If you run a fsck to check your root filesystem, you’ll get the following error message, as /dev/sda1 is mounted, and your cannot perform fsck on a mounted filesystem.
# fsck /dev/sda1 fsck from util-linux-ng 2.17.2 e2fsck 1.41.12 (17-May-2010) /dev/sda1 is mounted. e2fsck: Cannot continue, aborting.
If you are performing a fsck on a non-root file system, you can just unmount that partition and perform fsck.
But in this case, we like to perform fsck on a root filesystem. So, what is the solution?
Also, if you are new to fsck, refer to this: 10 Linux Fsck Command Examples to Check and Repair Filesystem
Tune2fs Output Before Reboot
Before we reboot the system, let us check when was the last time fsck performed a check on the root filesystem.
For this, use tune2fs command, and grep for “check” as shown below.
# tune2fs -l /dev/sda1 | grep -i check Last checked: Mon Nov 24 12:39:44 2015 Check interval: 15552000 (6 months) Next check after: Sun May 22 13:39:44 2016
As we see from the above output:
- Last checked: indicates the last time the root filesystem check happened. This happened on Nov.
- Check internal: indicates how often the root filesystem will be checked. In this example, it will wait for another 6 months before fsck will be executed on this filesystem during reboot.
- Next check after: indicates the date and time after which when you perform a reboot, the filesystem will be checked. This will happen only after May.
But, in our case, we don’t want to wait until May. We want to perform root filesystem fsck check now.
Create /forcefsck File to Force Check Root Filesystem
So, to force a root filesystem check, one of the easy way is to reboot the system, and force fsck to perform the filesystem when the system is starting up before the root filesystem is mounted by the kernel.
To force the fsck during reboot, first create an empty file called forcefsck under / as shown below.
# cd / # touch forcefsck # ls -l /forcefsck -rw-r--r--. 1 root root 0 Mar 9 20:15 /forcefsck
Now reboot the system:
# reboot
Tune2fs Output After Reboot
If you have console access, you can see that fsck will be performing the check on the / filesystem during the reboot.
After the check, fsck will automatically delete the /forcefsck file that we created earlier. After the reboot, you won’t see this file anymore.
# ls -l /forcefsck ls: cannot access /forcefsck: No such file or directory
Now, if you execute tune2fs, you’ll see the “Last checked” field was updated with the current timestamp. This confirms that fsck performed the root filesystem check during the reboot.
# tune2fs -l /dev/sda1 | grep -i check Last checked: Wed Mar 09 20:30:04 2016 Check interval: 15552000 (6 months) Next check after: Mon Sep 05 21:30:04 2016
You’ll also see the value of “Next check after” changed accordingly. i.e 6 months from the time we rebooted the system.
Comments on this entry are closed.
Here what is the role of /forcefsck file. Please explain.
dipanjan mukherjee – The OS checks for that file “forcefsck” in root / and if exists it will check disk. Very simple and can be created by a script or by default or can be touched like above example touch forcefsck, it doesn’t always work. File systems are checked every 20 or 30 restarts, etc or specified restart count.
# tune2fs -c 50 /dev/hda1
Will change fsck check count to 50
If /forcefsk exists, mountall (who is responsible for mounting filesystems during boot) will be called with –force_fsck as paramaeter, causing it to run a filesystem check. This is controlled by mountall.conf.
This is interesting. Thanks for this tip. The examples help also thank you.
I wouldn’t rely on /forcefsk in the root. That isn’t the best option but scheduling is and you know it will run the next time around after a reboot. The script that checks if /forcefsk exists should be verified first. I never rely on it or I double check that it exists first and it runs at boot time.
Nice Post, I found this some time ago and found your help a life saver!
Today I happened to stumble past it again, and had a FYI about doing a fsck in recovery.
You can force the partition to unmount by using “umount” in recovery like this:
# umount -dfrv /dev/sda1
It might throw a message not able to verify unmount — with a ref to mtab.
But, it works, just run this after:
e2fsck -f -v -y -C 0 /dev/sda1 — and it will run like a champ!
Hope this helps.