Because running fsck to repair a file system which is mounted for read/write operations can potentially cause severe data corruption/loss, the file system is normally checked while unmounted, mounted read-only, or with the system in a special maintenance mode that limits the risk of such damage.
When checking the file system of the root partition, it’s not an option to unmount it. So basically we have two solutions: have it checked when booting or mount it read-only.
File system check during the next boot
Linux checks while booting whether the file /forcefsck exists. If it finds this file, a file system check is started.
In order to create this file, you can either shutdown using:
or create the file manually e.g. using:
File system check on read-only root partition
First you should get in single user mode:
# init 1
Wait until you reach run-level 1. Then remount the partition as read-only:
# mount -n -o remount,ro /dev/sda1
(replace /dev/sda1 by the mount device of your root partition)
Now we can check the file system:
# fsck -fp /dev/sda1
-f: means that we want to force a check.
-p: means that all errors should be automatically repaired.
If this command fails, you can also try the following:
fsck -y /dev/sda1
After the check we can switch back to read/write mode:
# mount -n -o remount,rw /dev/sda1
Now we can switch back to our “normal” run-level:
# init 3
(for command line)
# init 5
That’s it !