Fsck – The ultimate File System Check utility

I have already mentioned about fsck in couple of my previous articles.

https://kernelcraft.wordpress.com/2011/12/29/the-fsck-way-lostfound/
https://kernelcraft.wordpress.com/2011/12/25/tuning-tune2fs-linux-file-system/

So, what is fsck?

Fsck is the short form for File System Check and is a system utility that is used to check and repair inconsistencies in Unix and Unix-based file systems. A filesystem can be a device name (e.g. /dev/hdc1, /dev/sdb2), a mountpoint (e.g. /, /usr, /home), or an ext2 label or UUID specifier (e.g. UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd or LABEL=root). Normally, the Fsck program will try to handle filesystems on different physical disk drives in parallel to reduce the total amount of time needed to check all of the filesystems.

Fsck can be run in 2 modes – Interactive and Non-Interactive modes.

In Interactive mode, fsck examines the file system and stops at each error it finds in the file system and gives the problem description and ask for user response whether to correct the problem or continue without making any change to the file system.

In Non-Interactive mode, fsck tries to repair all the problems it finds in a file system without stopping for user response useful in case of a large number of inconsistencies in a file system but has the disadvantage of removing some useful files which are detected to be corrupt .

How do you run fsck:

Running fsck on a mounted file system can result in data corruption. The two options are:

1) Change the running state of the system to single user mode and unmount the file system

# init 1 (Changing the run level to single user mode)
# umount /home (unmount the /home file system that is mounted on /dev/sda2)
# fsck /dev/sda2 (Run fsck)

What if you need to run fsck on the root / file system ?

2) Rescue Mode using installation CD ( to run fsck on root /)

Insert the Installation CD into the drive and reboot your system:

# shutdown -r now
After booting from the Installation CD and presented with the installation command prompt type:

#linux rescue nomount

Once you are at the system command prompt you need to run mknod. Because we started Rescue Mode with the “nomount” option, no file systems were initialized and no device files were created. If we try to run fsck on a file system it will fail. We need to use mknod to create the block or character special file.

To use mknod we need to know the Minor and Major numbers of the device.

# ls -l /dev/sda
8 0

# ls -l /dev/sda2
8 2

# mknod /dev/sda b 8 0
# mknod /dev/sda2 b 8 2

Run fsck and force the check and attempt to automatically repair:

-y — cause the fs-specific fsck to always attempt to fix any detected filesystem corruption automatically.

-f — force a check even if reported in a clean state

-v — Produce verbose output, including all file system-specific commands that are executed.

# fsck -yvf /dev/sda2

Advertisements