LPIC 104.2 Maintain the integrity of filesystems

Weight: 2

Key Knowledge Areas:

  • Verify the integrity of filesystems
  • Monitor free space and inodes
  • Repair simple filesystem problems

Terms and Utilities:

  • du
  • df
  • fsck
  • e2fsck
  • mke2fs(see mkfs)
  • debugfs
  • dumpe2fs
  • tune2fs
  • XFS tools (such as xfs_metadump and xfs_info)

Verify the integrity of filesystems

fsck is used to check and optionally repair one or more Linux file systems.

  • Different fsck utilities work for different filesystems.
root@myhost:/sbin# ll *fsck*  
# dosfsck used for all varieties of the DOS/FAT filesystem
-rwxr-xr-x 1 root root  59472 May 26 20:21 fsck.fat*
lrwxrwxrwx 1 root root      8 May 26 20:21 fsck.msdos -> fsck.fat*  
lrwxrwxrwx 1 root root      8 May 26 20:21 dosfsck -> fsck.fat*  
lrwxrwxrwx 1 root root      8 May 26 20:21 fsck.vfat -> fsck.fat*

# e2fsck checks all those ext filesystems
-rwxr-xr-x 1 root root 256952 Oct 30  2015 e2fsck*
lrwxrwxrwx 1 root root      6 Oct 30  2015 fsck.ext2 -> e2fsck*  
lrwxrwxrwx 1 root root      6 Oct 30  2015 fsck.ext3 -> e2fsck*  
lrwxrwxrwx 1 root root      6 Oct 30  2015 fsck.ext4 -> e2fsck*  
lrwxrwxrwx 1 root root      6 Oct 30  2015 fsck.ext4dev -> e2fsck*

-rwxr-xr-x 1 root root  44184 May 27 07:31 fsck*
-rwxr-xr-x 1 root root  35608 May 27 07:31 fsck.cramfs*
-rwxr-xr-x 1 root root  76896 May 27 07:31 fsck.minix*
-rwxr-xr-x 1 root root    333 Feb  5  2016 fsck.nfs*
  • For XFS filesystem, need use xfs_repair on its own.
root@myhost:/sbin# xfs_repair  
The program 'xfs_repair' is currently not installed. You can install it by typing:  
apt-get install xfsprogs  
  • Auto fsck check on system start-up will be applied to those last column none-zero items in /etc/fstab:
    /dev/sda2 / ext3 acl,user_xattr,usrquota,grpquota 1 1
    All filesystems that have a 1 in the column are checked first in parallel, then any that have a 2 in the column in parallel, and so on.

  • If system is shutdown properly with shutdown or halt command, then fsck doesn't need to check anything.

  • Always use fsck on read-only or unmounted partitions. If working on root filesystem, make sure you do it under single-user mode.

Check if any disk errors1
The badblocks utility can help check if there's any error blocks on your disk:

[root@myCentOS ~]# badblocks -v /dev/sda4
Checking blocks 0 to 10485759  
Checking for bad blocks (read-only test):  
Pass completed, 0 bad blocks found. (0/0/0 errors)  

You can use redirection to save the output to a file on disk:
[root@myCentOS ~]# badblocks -v /dev/sda4 > badsectors.txt

Step2: Employ e2fsck (for ext2/ext3/ext4 file systems) or fsck command with the badsectors.txt file:
[root@myCentOS ~]# e2fsck -l badsectors.txt /dev/sda4
The -l option tells the command to add the block numbers listed in the file specified to the list of bad blocks.

Tuning Filesystems

tune2fs adjust tunable filesystem parameters on ext2/ext3/ext4 filesystems.

  • Set mount count to 0 (zero): tune2fs –c 0 /dev/sda1

    -c max-mount-counts
    If max-mount-counts is 0 or -1, the number of times the filesystem is mounted will be disregarded by e2fsck(8) and the kernel.

  • Set reserved filesystem blocks(space available only to the root user) can also be associated with a system group users: tune2fs -g admins /dev/sda1

    -g group
    Set the group which can use the reserved filesystem blocks. The group parameter can be a gid or a group name. If a group name is given, it is converted to a gid before it is stored in the superblock.


The debugfs program is an interactive file system debugger. It can be used to examine and change the state of an ext2, ext3, or ext4 file system.

Monitor free space and inodes

Using du

  • List how many disk space used by /home folder
    du –sh /home

  • Exclude any sub-folder in different filesystem
    du –shx /home
    -x skip directories on different file systems

Using df2

  • List disk utilization statistics expressed in KB: df -k

  • List inode3 utilization:

[root@myCentOS sbin]# df -i
Filesystem     Inodes IUsed  IFree IUse% Mounted on  
/dev/sda2      589824 30513 559311    6% /
devtmpfs       122831   371 122460    1% /dev  
tmpfs          125041     1 125040    1% /dev/shm  
tmpfs          125041   432 124609    1% /run  
tmpfs          125041    13 125028    1% /sys/fs/cgroup  
/dev/sda1       65536   343  65193    1% /boot
tmpfs          125041     1 125040    1% /run/user/1000  
  • List disk space usage:
[root@myCentOS sbin]# df -h
Filesystem      Size  Used Avail Use% Mounted on  
/dev/sda2       8.8G  1.2G  7.2G  14% /
devtmpfs        480M     0  480M   0% /dev  
tmpfs           489M     0  489M   0% /dev/shm  
tmpfs           489M  6.7M  482M   2% /run  
tmpfs           489M     0  489M   0% /sys/fs/cgroup  
/dev/sda1       248M  102M  134M  44% /boot
tmpfs            98M     0   98M   0% /run/user/1000