Linux have a lot of utilities to manage, diagnose, check and correct different type of file systems. these utilities provide a lot of features especially Linux native file systems like ext2,ext3,ext4 etc. One of the most popular tool is fsck
tool. In this tutorial we will look general usage of fsck
Syntax
Fsck have similar syntax with other Linux tools but have some difference like adding file system specific options to the end of command as we can see below.
fsck [-lsAVRTMNP] [-r [fd]] [-C [fd]] [-t fstype] [filesystem...] [--] [fs-specific-options]
Help
$ fsck -h

fsck Phases
During the fsck following phases are evaluated. There 5 phases which will check different functionality of the file system and disk drive.
phase 1
– Check Blocks and Sizesphase 2
– Check Path namesphase 3
– Check Connectivityphase 4
– Check Reference Countsphase 5
– Check Cylinder Groups
List Partitions
To work with fsck and fix the disks and partitions we need to know and list the partitions and disks. There are different tools those can be used to list partitions. In this example we will use parted to list partitions and file systems.
$ sudo parted -l

Warning
While interacting with tools related file system be cautious. Because mounted file systems are error prone. Prefer using these tools on a unmounted partitions for more safety.
Check Partition
Fsck provides sub tools for different file systems. File system is an operating system level system used to store files, folders, pictures and data into storage like HDD, SSD in a convenient manner. Ext3 is different then ext4 and they have generally different tools to work. Fsck will automatically select related tool according to file system type.
$ sudo fsck /dev/vdb1

Exit Codes
While using fsck there will be some errors, warnings or messages about the operations. Below are some of them
- 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
Check Specific File System
In previous example we have used fsck in a generic way that fsck decided the file system of the specified partitions and selected file system accordingly. There is a way to specifically decided the file system. We have following options . After writing fsck
tabbing will list available options like below.
$ fsck

Check All File Systems
Another option to effectively check file systems is check them in one command. But there are some configuration to select partitions. fsck will look /etc/fstab
file to check file systems. If a file system is not listed in fstab it will be not checked. Let’s look our current fstab content with the following command
$ cat /etc/fstab

And now we will use -A
options to check all file systems listen in fstab file.
$ sudo fsck -A
As we can see from above screenshot vda1
partition is all ready mounted and can not checked.
Do Not Check Mounted File Systems
Checking mounted file systems can create problems. So preventing check of mounted file system by default is the best practice. -M
option can be used to prevent mounted file systems to prevent problems.
$ fsck -M /dev/vdb1

Do Not Display Title
While checking file systems fsck will provide some information called title in the begging of the check. This information can be problem if we only want actual operation data provided fsck. This title information can be skipped with -T
option.
$ fsck -T /dev/vdb1

Automatically Fix Problems
While checking file system there may be a lot of errors to fix. For each error one by one a question will be asked to us whether we want to fix this error. If there are 10.000 errors to fix this will be very hard job. Fsck provides a simple work around for this situation. -y
option can be provided to automatically fix the errors without answering all of them one by one.
$ fsck -y /dev/vdb1

Do Not Fix Just Report
Some time we may need to only get report about errors and corrupts. Fsck provides options for only reporting purposes without fixing errors. -n
option provides this feature.
$ fsck -n /dev/vdb1
