fsck - Checking and Repairing Linux Filesystems

fsck command examples

fsck


"fsck" is used to check and optionally repair one or more of your Linux filesystems. When issuing the "fsck" command you can specify a mount point such as "/", a device "/dev/sda1" or you can specify a UUID. If no filesystems are passed to the command line and the "-A" option is not passed, then fsck will use the entries within the "/etc/fstab" to check. Many distributions of Linux will run a automatic "fsck" after x number of days or x number of re-boots. When running the fsck command, you should always unmount the filesystem first. This will reduce the chances of any corruption occurring.



fsck - Error Codes:


The exit code returned by the fsck command is the sum of the following conditions:


            0    - No errors
	    1    - Filesystem errors corrected
            2    - System should be rebooted
            4    - Filesystem errors left uncorrected
            8    - Operational error
            16   - Usage or syntax error
            32   - Fsck cancelled by user request
            128  - Shared-library error

The exit code returned when multiple filesystems are checked is the bit-wise OR of the exit codes for each filesystem that is checked!


Examples of fsck commands


As mentioned earlier, you should NEVER run fsck on a mounted filesystem. First, we will display all mounted filesystems. This will enable us to work out if we need to unmount any filesystems before running the fsck command.


Display mounted filesystems - mount


In the example below we have used the "mount" command to display all mounted filesystems. We also used the "df" command to display the mount points.



[root@centos ~]# mount | grep "/dev/sd*"
tmpfs on /dev/shm type tmpfs
(rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sdb1 on /test1 type ext3 (rw)
/dev/sdb2 on /test2 type ext3 (rw)
/dev/sdb3 on /test3 type ext3 (rw)
/dev/sdb5 on /test4 type ext3 (rw)
/dev/sdb6 on /test5 type ext3 (rw)
/dev/sdb7 on /test6 type ext3 (rw)
/dev/sdb8 on /test7 type ext3 (rw)
[root@centos ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_centos-lv_root
                       6748936   3193112   3212992  50% /
tmpfs                   515368        80    515288   1% /dev/shm
/dev/sda1               495844     73013    397231  16% /boot
/dev/sdb1               287782     10293    262631   4% /test1
/dev/sdb2               295555     10287    270007   4% /test2
/dev/sdb3               295555     10287    270007   4% /test3
/dev/sdb5               295561     10287    270014   4% /test4
/dev/sdb6               295561     10287    270014   4% /test5
/dev/sdb7               295561     10287    270014   4% /test6
/dev/sdb8               264445     10287    240505   5% /test7

fsck - Basic Check


Now we can unmount the device/filesystem to carry out our "fsck" checks. Note, the command to unmount is "umount" without a "n"



[root@centos ~]# umount /dev/sdb1

[root@centos ~]# fsck /dev/sdb1
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sdb1: clean, 14/74296 files, 19679/297168 blocks

After unmounting the device "/dev/sdb1" we then ran our "fsck". In this example it was found to be clean with no errors reported.



[root@centos ~]# umount /test2
[root@centos ~]# fsck /test2
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sdb2: clean, 11/76608 files, 19964/305232 blocks

In the above example, we run the same commands, however, we specified the name of the filesystem instead of the device.



fsck - Only if filesystem is unmounted


In the following example we ran the "fsck" command, however, we passed an additional parameter of "-M". The "-M" parameter instructs the fsck command to only run a check if the filesystem is not mounted. In our example, we had not unmounted the filesystem therefore no action was taken. If no action was taken against a mounted filesystem, then a return code of "0" is returned. We can verify this by issuing the "echo $?" command.



[root@centos ~]# fsck -M /dev/sdb3
fsck from util-linux-ng 2.17.2
[root@centos ~]# echo $?
0

It is a good idea to get into the habit of using the "-M" option every time you run the fsck as this will safe guard against accidentally running a fsck against a mounted filesystem.


Force a fsck on a clean filesystem



[root@centos ~]# umount /dev/sdb3
[root@centos ~]# fsck /dev/sdb3 -f
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
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/sdb3: 11/76608 files (0.0% non-contiguous), 19964/305232 blocks

In the above example, the "-f" option was used to force the fsck to carry out its check/repairs.


fsck - Automatically fix errors


By specifying the "-y" parameter, fsck will attempt to automatically fix/repair any errors. The "-y" automatically answers "y" to any responses that you would normally have to reply to.



[root@centos ~]# fsck -y /dev/sdb3 
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sdb3: clean, 11/76608 files, 19964/305232 blocks

fsck - run but do not fix any errors


By using the "-n" parameter, we will run the fsck, however, it will not attempt to make any fixes to our filesystems. Not all filesystems will accept the "-n" option. Sometimes you may need to run the "-N" option which will carry out no fixes, it will only report what would be done. This is because not all filesystem checkers will report errors. For example "fsck.reiserfs" will not report any corruption with the "-n" option!



[root@centos ~]# fsck -n /dev/sdb3 
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sdb3: clean, 11/76608 files, 19964/305232 blocks

Common types of filesystems supported by fsck


In the example below taken from my "Ubuntu 12.04 LTS" system we can see that there are numerous types of fsck checkers available. The number may differ depending on what distribution of Linux you are using.



fsck.ext2      fsck.ext4dev   fsck.msdos     fsck.vfat
fsck.btrfs     fsck.ext3      fsck.jfs       fsck.nfs       fsck.xfs
fsck.cramfs    fsck.ext4      fsck.minix     fsck.reiserfs 

As with many Linux commands there are numerous options available that can be passed to the "fsck" command. To see all the available options issue the "man fsck" command.