Any Linux user must have experienced file corruptions and booting failures at least once in their life. Being an I.T professional for many years, I have encountered this problem many times. Linux comes with a handy command called 'fsck' to save you. Fsck stands for File System Consistency Checker.
When and Where to use fsck?
A common reason of file corruption is an unforeseen power off of the computer due to hardware failure. Improper shutdown, accidental system -file deletion, and viruses can cause file corruption and an unstable system. Some file corruptions are less harmful and users can continue working. However, it is important to run a “fsck” once in a while as a regular practice.
How to use fsck?
Checks system files and asks for operator confirmation at each file error detected. It can continue with repair or proceed without any changes.
Non interactive mode
It can continue the repair without any user interaction .In this mode, all the files detected with errors get repaired automatically. If a system file is detected, the system boots to single user mode and asks for user's confirmation to manually repair files.
While Running fsck, be smart!
You should always run fsck in the single user mode to prevent any further file corruptions. The simple reason is when running in multiuser mode, files change constantly thereby leading to further corruptions.
If you cannot boot into single mode, you should unmount each partition and run fsck. You cannot un mount root/usr file system . In case the root/usr file system is corrupt, you can boot from a Linux cd and run the fsck .
Running in single user mode
Unmount the file system:
$ init 1
$ umount /home
$ fsck /dev/sda1
$ fsck /home
$ fsck /dev/sda1
$ e2fsck -y /dev/sda1
Running in rescue mode using boot cd
Insert installation cd and reboot the pc:
$ shutdown -r now
After booting, use cd type following at the boot prompt
Boot: Linux rescue nomount
Since we passed nomount option , kernel will not initialize any filesystem or its related device file. So, we need to create it manually.
Now, create new node for disk and partition 1.
# mknod /dev/sda
# mknod /dev/sda1
# fsck /dev/sda1
# e2fsck -y /dev/sda1
Note : You need to reboot the system after running fsck.
Always unmount the device for the files system you want to run the fsck. Suppose, you want to check file consistency in hard disk 1 (/dev/sda1):
This is what happens when you run the fsck without unmounting file system:
As a regular practice, run a “fsck” occasionally in your Linux box. Remember that “prevention is better than the cure”.