The fsck(FileSystem ChecK) utility verifies the integrity of many types of filesystems and, if possible, repairs problems it finds. Repairs can destroy data, particularly on nonjournaling filesystems, such as ext2, so by default fsck asks you for confirmation before making each repair.
Note: You can run fsck with option -N(no write) on a mounted filesystem. But DO NOT run fsck on a mounted filesystem, more likely you will harm the filesystem if do so. This is because when fsck is rearranging the underlying structure through the raw device, another process could change a disk block using the block device, resulting in a corrupted filesystem.
Depends on the fsck option in /etc/fstab and the last time the filesystem was checked, during booting, system runs fsck to check whether the filesystem is in consistent state.
Here are just few examples:
Check a filesystem
To check a filesystem named /data, you can either run fsck directly to the filesystem.
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/md1 has gone 835 days without being checked, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/md1: 30827/30539776 files (2.7% non-contiguous), 41314443/61048976 blocks
Or to its filesystem device
Check all umounted filesystem
The -A option let fsck to check filesystems listed in fstab, the -R option causes fsck not to check the root filesystem.
Check and fix filesystem without asking.
#fsck /data -y
By default fsck asks for confirmation before making repair, the option -y is to say yes to all repairs.
The opposite option is -n, which is to avoid attempting to repair.
Note that not all filesystem-specific checkers implement -y option. In particular fsck.minix(8) and fsck.cramfs(8) does not support the -y option as of this writing.
Check only specified filesystem type
#fsck -AR -t ext3 -y
The command above will check and repair all ext2 filesystems listed in /etc/fstab.
Further more, you can specify a list of filesystems to the option -t, like
fslist is a The fslist parameter is a comma-separated list of filesystems and options specifiers. The options specifiers must have the format opts=fs-option
#fsck -AR -t ext3,opts=ro
The example above causes fsck to check rdonly ext3 filesystems in /etc/fstab
Turn on verbose output
#fsck -V /data
Force to check a filesystem even it's in clean state
The option -f forces fsck to check a filesystem even it's in clean state.
Fsck exit code
The exit code returned by fsck is the sum of the following conditions:
0 - No errors
1 - File system errors corrected
2 - System should be rebooted
4 - File system errors left uncorrected
8 - Operational error
16 - Usage or syntax error
32 - Fsck canceled by user request
128 - Shared library error
The exit code returned when multiple file systems are checked is the bit-wise OR of the exit codes for each file system that is checked.