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.
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:
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.