Repair a Linux filesystem using fsck


On Linux, “File System Consistency checK” (fsck) can be used to check and repair filesystems.

# fsck /dev/sda1

This will check the sda1 partition. An important thing to note is that fsck cannot be used on mounted partitions. It can damage a filesystem that is not unmounted. To check the home folder that resides on another partition, sda2, use the commands:

# umount /home
# fsck /dev/sda2

Note: root/superuser permission is required to execute “fsck”

You can also use “fsck” to check external drive, such as a USB drive or SD card. For example:

# umount /dev/sdb1
# fsck /dev/sdb1

If you are not sure of the partition number, use the command

# fdisk -l

to list the partitions in the system.

During the filesystem check, if errors are detected, “fsck” can attempt repair of the filesystem:

# fsck -a /dev/sda1

Similarly, using the -y flag:

# fsck -y /dev/sda1

Check all filesystems

To check every filesystems in a server use the -A flag:

# fsck -A

All entries in /etc/fstab are read and checked for errors. Use -A with the -R and -y flag to prevent fsck from scanning the root filesystem and fix all errors:

# fsck -AR -y

Exclude check on mounted filesystem

As mentioned, fsck cannot repair mounted filesystems. If using the -A flag to scan all the filesystems, and some are mounted, to avoid checking these filesystems use the -M flag to prevent fsck from checking mounted filesystems.

For example, running the command

# fsck -M /dev/sdc1

returns return code 0 (“no error”). No scan will be done when the filesystem is mounted.

Specifying the filesystem type

To check filesystems of a specific type use the -t flag:

# fsck -t ext4 /dev/sdc1

Alternatively, you can combine with the -A flag to scan all filesystems of the specific type:

# fsck -A -t ext4 -y

Checking filesystems during server boot process

The OS configuration file /etc/fstab contains all the mountable partitions and their relevant mount options that are processed during server boot. The last column (column 6), aka the fsck “PASS” column, is used by Linux to determine whether fsck should be executed during boot, prior to filesystem mounting, to check the filesystem and in which order each partition in /etc/fstab should be checked.

Possible values for column 6, the PASS column in fstab are 0,1 and 2.

0 – do not check filesystem

1 – check filesystem, highest priority, root “/” partition

2 – check filesystem, lower priority

Example /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Sep 29 21:42:56 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel00-root /                       ext3    defaults        0 1
UUID=73dbc220-7f9a-4861-b4da-fc223442ac10 /boot ext4    defaults        0 2
UUID=30B3-0666          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
/dev/mapper/rhel00-home /home                   ext4    defaults        0 2
/dev/mapper/rhel00-swap swap                    swap    defaults        0 2

To execute a filesystem check every time a server is booted, create an empty “forcefsck” file in the “/” folder. This will notify Linux to do a “fsck” check during server bootup:

# touch /forcefsck

The following command instructs Linux to execute “fsck” after every 30 bootups:

# tune2fs -c 30 /dev/sdaX

To specify the number of days between Linux executing “fsck” during bootup:

# tune2fs -i 10d /dev/sdaX

Redhat Customer Portal on fsck
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/fsck-fs-specific.html

Leave a comment