Ever needed to do a fsck on the root filesystem? I was happy to find that an old method of forcing a file system check on reboot still exists on most modern distributions. I tested this successfully on the following operating systems:
CentOS 6 Ubuntu 12.04 Ubuntu 14.04
Before proceeding, it is a really good idea to have a monitor and keyboard hooked up to the server just in case the fsck requires manual intervention due to bad blocks or something, therefore preventing normal boot up!
First, check to see when the last time the server had a file system check ran. This command should be against the device listing for /, such as /dev/sda1, or if using LVM, something like /dev/mapper/ubuntu–vg-root.
[root@web01 ~]# tune2fs -l /dev/mapper/ubuntu--vg-root |grep -iE "last|state" Last mounted on: / Filesystem state: clean Last mount time: Thu Sep 10 23:48:18 2015 Last write time: Sun Mar 1 16:02:06 2015 Last checked: Sun Mar 1 16:02:04 2015
As shown in the output above, a fsck has not been ran in quite some time! So to force a fsck at next boot, simply type:
[root@web01 ~]# touch /forcefsck
Now reboot your server:
[root@web01 ~]# shutdown -r now
Once the server comes back online, confirm the fsck ran by:
[root@web01 ~]# tune2fs -l /dev/mapper/ubuntu--vg-root |grep -iE "last|state" Last mounted on: / Filesystem state: clean Last mount time: Thu Feb 18 18:40:34 2016 Last write time: Thu Feb 18 18:40:32 2016
Finally, check to confirm the system removed the file /forcefsck:
[root@web01 ~]# ls -al /forcefsck ls: cannot access /forcefsck: No such file or directory