Home Linux Admin Introduction Directory Tree Disks/File Systems Memory Mgmt Monitoring Startup/ShutDown Logging in/out User Accounts Backups Processes Cron Packages Books

File Systems: Part 3 -- Checking FileSystems


Contents

This chapter discusses file systems in Linux.

Checking FileSystems

Suppose we are editing a file and do a save. The save may not automatically update the contents on the hard disk. A copy of the data is saved in memory and then at a later time the contents are written out to the disk. We can refer to these 2 states as in-memory and on-disk states. These 2 states will differ by a small amount and usually be very close. If the filesystem is detacted then the states are synchronized. The "syncing" will copy the in-memory contents to the on-disk state. When executing the "umount" command the command will not happen if a process is accessing the file system and until the writing is done to the disk. There is a utility called "sync" to do this manually also. The "sync" utility is also called automatically during the shutdown process.

Recall that each filesystem has a superblock that contains certain information. Included in that data is a flag that has 2 values clean and dirty. If a file is mounted then the flag is marked dirty and upon un mounting the flag is marked clean. That is assuming the file was unmounted without any issues. During boot each filesystem is checked and if the flag is dirty then the "fsck" utility is run.

We can manually run "fsck" on a filesystem before mounting it. If the system is dirty then fsck will check it else it will exit. Also the filesystem must be unmounted. The command "fsck" is run as:

fsck [-t fstype] [-y] [-A] [-f] [other options] {device | mount point }

-t fstype  the type of the filesystem (ext2, ext3, dos, reiserfs, etc).
This is required if the filesystem type is not in the file system table.
-y  answer 'yes' to all fsck's questions automatically.
-A  automatically check all the filesystems indicated in the file
     system table (fstab)
-f  force the check even if the filesystem is marked 'clean'.
    Normally fsck will quit immediately if the filesystem is 'clean'.

Let's try the command on our flash drive. We need
to make sure that the "sbin" is in our path. The "fsck" is
similar to "mkfs" in that it runs another program for that
filesystem so that "fsck" will run "fsck.ext2" as an example.

We give the device name.

root@ajkumar08-PC:/home/ajay# fsck /dev/sdb1
fsck from util-linux 2.36.1
e2fsck 1.46.2 (28-Feb-2021)
FLASH_1: clean, 14/1916928 files, 164375/7667708 blocks
root@ajkumar08-PC:/home/ajay#

The entry in the fstab file is.
LABEL=FLASH_1 /home/ajay/FLASH ext4 defaults 0 0

So we can use the mount point also.

root@ajkumar08-PC:/home/ajay# fsck /home/ajay/FLASH
fsck from util-linux 2.36.1
e2fsck 1.46.2 (28-Feb-2021)
FLASH_1: clean, 14/1916928 files, 164375/7667708 blocks
root@ajkumar08-PC:/home/ajay#

We can also use the "fsck.ext2" command directly.

root@ajkumar08-PC:/home/ajay# fsck.ext4  /dev/sdb1
e2fsck 1.46.2 (28-Feb-2021)
FLASH_1: clean, 14/1916928 files, 164375/7667708 blocks
root@ajkumar08-PC:/home/ajay#

The "fsck" didn't really run because it found the system to be
"clean". However we can force it to run wiht the "-f" option.

root@ajkumar08-PC:/home/ajay# fsck -f  /dev/sdb1
fsck from util-linux 2.36.1
e2fsck 1.46.2 (28-Feb-2021)
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
FLASH_1: 14/1916928 files (0.0% non-contiguous), 164375/7667708 blocks
root@ajkumar08-PC:/home/ajay#
fsck errors
If errors are encountered during the checking process then "fsck" will prompt us to see if we want to fix them. We can use the "-y" option to avoid the prompts and state yes to all the prompts. A possible error can be due to an orphaned inode and "fsck" may fix this by creating a new file in the "lost+found" folder ( for ext systems ). Once "fsck" is done we should decide what to do with the new files in the "lost+found" folder as this folder has limited space and we should keep it empty for future recovered files.
Checking on Boot
The filesystems are checked at startup and if they are clean then we know that "fsck" will skip the test. If a system was not determined to be clean then it will be checked. Journaled filesystems can be checked faster by replaying the journal. However a complete check of ext3 and ext4 systems will be done periodically. This is a parameter called check interval.
root@ajkumar08-PC:/home/ajay#  dumpe2fs -h /dev/sda1 | egrep -i 'count|check'
dumpe2fs 1.46.2 (28-Feb-2021)
Inode count:              9707520
Block count:              38822144
Reserved block count:     1941107
Mount count:              2
Maximum mount count:      -1
Last checked:             Sun Feb 12 18:58:34 2023
Check interval:           0 ()
Checksum type:            crc32c
Checksum:                 0xf91900d4
Journal features:         journal_incompat_revoke journal_64bit journal_checksum_v3
Journal checksum type:    crc32c
Journal checksum:         0x348816ca
root@ajkumar08-PC:/home/ajay#

This can be changed using the "tune2fs" utility.

Change last checked to now.
tune2fs -T now /dev/sda1

Change the mount count to 6
tune2fs -C 6 /dev/sda1

Change the check interval to 90 days.
tune2fs -i 90d /dev/sda1

TO DO: