File System Consistency Check (FSCK)

What is fsck?

Short for file system consistency check, fsck refers to the specific program used on Linux, Unix, and their other variants. It is used to check for any errors existing in the file system.

Technically, it is the front-end for many filesystem checkers (fsck.fstype) available on Linux. Based on the file system, the checker is searched first in the /sbin, then in the /etc/fs and /etc, and finally in the directories recorded in the PATH setting variable.

Understanding fsck

What is fsck

Ideally, fsck is a Linux utility. It checks for any unresolved issues or errors in the file systems, fixes the potential errors, and generates reports.

Available with the Linux distribution by default, this specific utility program does not need any specific installation procedures or steps to follow to use it.

You can start using the tool and exploiting its functionalities once the terminal is loaded.

The fsck program is pretty much the same as the ScanDisk utility available in Microsoft Windows and operates in two specific modes, namely:

It may also check the errors but not repair them. Instead, it may display them as regular output.

A few examples of using the fsck program to check a file system could be as follows:

Typically, on most devices, the fsck program runs at boot time under specific circumstances such as:

Usually, this program works in parallel on file systems stored on different physical disk drives. The primary intention is to reduce the time taken to check all these files.

If there is an -A option or any filesystem specified on the command line, fsck will serially check filesystems in /etc/fstab by default. This is the same as the mixture of the -A and -S options.

The program returns different exit codes. These are all sums of unique numbers that represent different condition values as follows:

While checking multiple file systems, the exit code returned is typically the bitwise OR of the exit codes of the individual file systems that the program has checked.

You can use fsck utility program in Linux under different situations such as:

However, there are some prerequisites to using fsck utility program such as:

Once you have them, you can use the basic fsck syntax in the following pattern:

The file system can be anything such as:

You may also use specific options at the end of the command based on the file system.

fsck Command

The fsck commands help in checking and repairing the errors in inconsistent file systems interactively. These commands should be run prior to mounting a file system to read the device file on which the file system exists.

Usually, the fsck command displays information about the inconsistencies in a file system, but if the file is consistent, it will simply display the following information in its report:

The commands are typically quite conservative in nature when it comes to repairing the existing errors in the file system. It usually tries to abstain from any action that may result in a potential loss of data from the files.

This means that, unless it is unavoidable as it is in some cases, the command will not recommend destroying a damaged file.

However, it is always recommended that you allow the fsck command to do the required rectification so that there are no inconsistent files in your system, loading which may even result in a system crash.

The command will check all of the file systems if you do not mention a file system in the FileSystem parameter. Ideally, the check attribute is set to True for the file systems that are listed in the /etc/filesystems file.

Such checks can be initiated with the command:

check=true

If there are multiple file systems to check, you can do so by grouping them in the /etc/filesystems file and changing the check attribute of it by using the following command:

check=Number

If you want minor issues in the file systems to be fixed, the necessary command for it is:

fsck -p

While the default file-checking command is fsck, the specific command for checking a specific file system is:

fsck /dev/hd1

fsck Options

The fsck Linux utility allows using different options with it that are both file-system specific and otherwise. Ideally, the options and arguments that are not understood are passed on to the checker for the specific file system.

Here are some of the common options used in the fsck utility program along with a brief description of each:

Read Also:  What is Flash Drive? Lifespan, Function, Pros, Cons & More

How to Run fsck Manually?

The fsck utility program can be run manually on an unmounted file system, if there is any system error on the disk by a superuser or the system administrator.

However, it is not supposed to run on a mounted filesystem because it may display an error message or even corrupt the data on the file system.

The process to follow includes entering the command /etc/fsck -n in the UNIX shell prompt and pressing Enter. You will be able to see all the inconsistencies in the file system without repairing them.

Count the number of errors, and if you find that it is more than 20, there are some serious issues with the disk.

However, if it is less than 20, re-run the fsck command and include the -y option.

This means that all the questions asked by fsck will be affirmative, and therefore the errors will be fixed, allowing the boot process to continue.

If there are any unreferenced file, these will be kept in the lost+found directory on the disk.

At this point, you should pay specific attention to messages like the following:

REBOOT HP-UX; DO NOT SYNC (USE reboot -n).

In that case, you will have to reboot the controller by entering: reboot -n. The controller will then reboot, and everything will be fine.

If you do not get any such message, enter “^D” at the prompt. This will continue running the bcheckrc program to finish the booting process.

What is the Use of fsck?

The fsck utility program is mainly used to check the file systems and repair the inconsistencies in them interactively.

It works on the data structures directly, and it is also used to check the unmounted, mounted read-only file systems, or the systems in special maintenance mode.

Typically, different fsck commands are used to check different types of inconsistencies in the file systems, which include and are not limited to the following:

The fsck commands also perform size checks in the following conditions that are not compressed file systems:

Apart from that, it can also perform specific directory checks in the following conditions:

Ideally, these orphaned files or directories are reconnected in the root directory of the file system by putting them in the lost+found subdirectory if you allow it.

Read Also:  What is Logical Drive? (Explained)

The name is assigned in the i-node number. If you do not allow it, then the command will request authorization to destroy the file.

It is also used to check the following inconsistencies in the superblock:

Finally, apart from the messages, the fsck command will also record the output of the different checks done by it and repair the existing error through the exit value, as mentioned earlier.

Is It Safe to Run fsck?

Typically, the fsck utility program tries its best and most valiantly not to lose data during its operation, therefore. it is quite safe to use.

However, this is not a miracle tool and therefore it depends on the type of fsck command you are using for a specific file and the damage. If the damage is reasonably ordinary, this tool is reasonably safe.

Typically, there are a few particular commands such as e2fsck for ext2/ext3/ext4 file systems, that can slow down things when the -f option is used since they will check even clean file systems.

And when the -y option is used, it will fix the errors detected automatically. It may even perform a few tasks that were not advised by the expert administrator.

This means that there is always some risk involved in running fsck commands.

Therefore, it is not a good idea to run the automatic thing always or when the underlying hardware such as the CPU, RAM, hard drive, or disk controller is damaged. It will inevitably result in data loss.

So, never, ever run fsck without inspecting the issues in the kernel and eliminating the root cause.

How Do You Turn Off fsck?

You can turn off or disable the fsck utility program on a hard disk partition by editing the /etc/fstab file if the operating system does not use SystemD.

The two values to look for are, dump and pass, where dump signifies that the partition is discarded and Pass will have it checked.

To turn it off once:

To permanently disable it on a disk partition:

Does fsck Run in the Background?

Yes, the fsck utility program can run in the background, though it is designed to run at boot time and most users think it cannot run in the background.

Derived from the conventional disk-based fsck, it typically runs by taking snapshots and running traditional algorithms.

However, it cannot update the on-disk image or obtain kernel-level locks.

Can fsck Cause Data Loss?

Yes, there is a high chance of losing data from a disk when you use the fsck utility program while the disk is active.

You should always unmount the target disk before using this program to check and repair errors in it.

When Should You Fix Errors Using fsck?

You should ideally use fsck to fix errors in the hard disk when the system fails to boot or the drive is acting in a way that it is not supposed to.

You may also use it when the files are corrupted on a particular disk.

Conclusion

So, through this article, you now know what the fsck utility program on Linux is and how to use it to your advantage to keep a check on your file systems and even repair the potential issues existing in them.

Simply have root permission and run the commands listed in this article to run this useful tool on your system.