Tuesday, January 8, 2013

fsck : file system consistency check


fsck is used to fix damaged file systems or inconsistent file systems. It is generally run when a filesystem fails to mount. In Linux, filesystems are usually marked "dirty" or "clean" and fsck checks only the dirty filesystems during the next startup.

Before running fsck, the file system should be unmounted. file system cannot be repaired while running. To repair root partition, the system should be booted into single  user mode.

fsck format is - fsck -t <File System Type>  <File System Mount/Device>

The <File System Mount/Device> can be specified by any of the following

  1. A device name (e.g /dev/sda1, /dev/mapper/VolGroup-logical_volume_vz)
  2. A mount point (e.g. /var, /opt, /home)
  3. A label (e.g. LABEL=home)
  4. UUID specifier (e.g. UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd )
If no filesystems are specified on the command line, and the -A  option is not  specified,  fsck  will  default  to  checking  filesystems  in /etc/fstab serially.  This is equivalent to the -As options.


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
Generally fsck is run automatically at boot time if the file system becomes inconsistent due to non-graceful shutdown such as crash or power loss.

In /etc/fstab we can specify the order in which fsck should be run on partitions.

UUID=fffff7aa-57b8-40aa-baa4-588c4eff7651         /          ext4      defaults        1  1
UUID=8b5a0a93-1dd3-4394-bb3e-0032a77201fa   /boot    ext4      defaults        1  2
/dev/VolGroup/logical_volume_vz                           /vz        ext4      defaults        0  2
/dev/VolGroup/logical_volume_music                     /music   ext4      defaults        0  2
/dev/VolGroup/logical_volume_opt                         /opt        ext4      defaults        0  2 

The last column (6th column marked in Green) is the fsck option.
  • 0 = Do not check.
  • 1 = First file system (partition) to check; / (root partition) should be set to 1.
  • 2 = All other filesystems to be checked.

Let us see how to run fsck on an ext4 filesystem


1) First determine the filesystem type from /etc/fstab or mount command, like one as follows

           /dev/mapper/VolGroup-logical_volume_opt    on  /opt   type ext4 (rw)

2) This filesystem need to be unmounted. Before unmounting check if the filesystem in being used by any other process. This can be done using either fuser or lsof command

           [root@dhcppc5 ~]# fuser -m /opt
           /opt:                 1474m  7785c

    -m     name specifies a file on a mounted file system or a block device that is mounted.

So there are two processes - 1474 and 7785, using the /opt  mount point.

lsof /opt too shall help in identifying the processes using /opt partition.

The identified processes - 1474 and 7785 can be stopped gracefully.
Else the identified processes can killed forcefully as follows

fuser -km /opt kills all processes accessing the file system /opt in any way.

[root@dhcppc5 ~]# fuser -km /opt
/opt:                 1474m  7785c  8062c  8065c

[root@dhcppc5 ~]# fuser -m /opt
[root@dhcppc5 ~]# 

After the processes using the mount point are stopped, unmount the filesystem

[root@dhcppc5 ~]# umount  /opt

3) Now run fsck on  /opt

[root@dhcppc5 ~]# fsck -t ext4 /dev/mapper/VolGroup-logical_volume_opt
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/VolGroup-logical_volume_opt: clean, 803/327680 files, 61242/1310720 blocks
You have new mail in /var/spool/mail/root

By default fsck tries to skip the clean file system to do a quicker job.
That's what happened in the above case. However, we can force fsck to check the filesystem using -f option

[root@dhcppc5 ~]# fsck -t ext4 /dev/mapper/VolGroup-logical_volume_opt -f
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/mapper/VolGroup-logical_volume_opt: 803/327680 files (0.9% non-contiguous), 61242/1310720 blocks

Thus we observe that fsck is a five stage process.


How to force fsck on the next reboot


Method 1:

Just touch forcefsck file on the partition on which fsck need to be run on next reboot.

Say, I want to run fsck on root(/) partition on next reboot. Let us demonstrate as follows.

As superuser(root), change to / directory
# cd /

Create a file called forcefsck
# touch /forcefsck

Reboot the system
# reboot

The file forcefsck will be removed automatically when the process is finished. 

Method 2:

Using shutdown command with -F option will force fsck on reboot

# shutdown -rF now

Checking fsck Boot frequency


fsck is set to run after every "n" boots. To know how many boots have passed since last fsck check, run the following

# dumpe2fs -h /dev/sda2 | grep -i 'mount count'
dumpe2fs 1.41.12 (17-May-2010)
Mount count:              2
Maximum mount count:      -1 

# tune2fs -l /dev/sda2 | grep -i count | grep -i mount
Mount count:              2
Maximum mount count:      -1

To change the frequency of fsck checks - say, run fsck every 20 boots

# tune2fs -c 20 /dev/sda2

Howto bypass/skip fsck?


Using shutdown command as follows

# shutdown -rf now

Set boot without fsck using Linux kernel option - /boot/grub/grub.conf

Put "fastboot" at the end of kernel line, like follows

kernel /vmlinuz-2.6.32-042stab068.8 ro root=UUID=fffff7aa-57b8-40aa-baa4-588c4eff7651 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM fastboot

Skip fsck from /etc/fstab file

Set the last column(6th column) to 0 in /etc/fstab, so that the file system shall not be checked

/dev/VolGroup/logical_volume_opt    /opt       ext4      defaults      0 0

Using tune2fs

# tune2fs -i 0 /dev/sda2

No comments:

Post a Comment