Fsck: How to Check and Repair a Filesystem

File system consistency check, or fsck

Every operating system needs a mechanism to store and recover data. This mechanism is called the filesystem. 

But the odds of a filesystem failing increase over time for one reason or another. If your filesystem goes corrupt, you might not be able to access certain parts of your data. 

The good news is that inconsistencies can be checked for and repairs carried out accordingly. You can use the fsck system utility to verify your filesystem’s integrity. 

In this brief post, we’ll walk you through using the utility and repairing disk errors.

What is fsck?

File system consistency check, or fsck for short, typically runs at boot time if it detects that the filesystem is in a certain condition. It is designed to run automatically when:

  1. The filesystem was automatically mounted a certain number of times without being checked; or
  2. The filesystem’s state doesn’t align with the data that was scheduled to be written, and it is subsequently marked “dirty.”

The nice thing about fsck is that you can run it manually if you think there’s a problem with your filesystem. Bear in mind you will need superuser privileges and unmount the filesystem before using the utility.

Running the utility is a good idea when:

  • The attached drive won’t work as expected.
  • You notice an input/output error and that the files are corrupted.
  • Your operating system doesn’t boot.

When you run fsck, it interacts with another filesystem-specific fsck utility that the utility’s authors intentionally designed. But regardless of the filesystem type, the utility works in three modes:

  • Checks for errors and allows the user to resolve individual issues interactively.
  • Checks for errors and attempts to fix them.
  • Checks for errors, doesn’t attempt to fix them, but displays them on standard output.

The Options Available for fsck

As mentioned earlier, you need root or superuser access to use this utility. But it’s noteworthy that you can use the tool with different arguments. Here are some of the most helpful options: 

Option What It Does
-A Instructs fsck to check all filesystems as mentioned in /etc/fstab
-C Displays a progress bar
-l Removes the possibility of other programs using the partition when fsck is running 
-M Skips checking mounted filesystems
-N Displays possible changes without making any 
-P Checks filesystems parallelly, root included
-R Skips checking root filesystem (making it useful only when using -A)
-r Supplies stats for every device being checked
-T Skips displaying the title
-t Allows you to specify the filesystem types to check
-V Displays a description of what fsck is doing

Filesystem-specific Options

There are some options that fsck doesn’t understand and passes to the filesystem-specific checker. The options and arguments that come after “–” are treated as filesystem-specific.

Here are the options that most filesystem-specific checkers support:

Option What It Does
-a Repairs the filesystem automatically without prompting. While some filesystem checkers support this option, the only reason e2fsck supports it is for backward compatibility. The option is mapped to the -p option. 
-n Causes the fsck to avoid attempting to fix any problems and only report them to stdout in some filesystem-specific checkers. But this is not how this option works with all fs-specific checkers, and the checker on your machine may do some things differently.
-r Repairs the filesystem interactively. Using this option is not good if you’re running multiple fsck’s in parallel. Bear in mind that this option is redundant in e2fsck since it repairs interactively by default. It supports the option only for backward compatibility.
-y Results in the fsck to attempt to fix any corruptions automatically. But not every filesystem-specific checker supports this option. 

 

Since fsck cannot tell which options are arguments and which aren’t, it’s important to note that these arguments must not be used with other arguments. 

The fsck command is a system utility and cannot process and send over complicated options to an fsck. Passing complex arguments and options almost always ends with you not getting the desired result. And if this is the case, you’re likely doing something you shouldn’t be with the utility. 

As you might be able to guess, the options that work with various filesystem-specific fsck’s are not standardized. So, if you don’t know what to do, take a look at the manual page of the fs-specific checker on your machine.

fsck’s Environment Variables

The following environment variables impact the fsck system utility’s behavior:

Environment Variable What It Does
FSCK_FORCE_ALL_PARALLEL Setting this environment variable leads to the utility attempting to run all filesystems parallelly. This attempt will not be disrupted even if the filesystems appear on the same device. For this reason, setting this environment variable is especially useful in high-end filesystems and RAID systems. Bear in mind that the fs_passno value is still used.
FSCK_MAX_INST It limits the number of filesystem checkers that can run simultaneously. This way, machines with several disks don’t run too many instances of fsck at once, circumventing CPU and memory overloads. If the value is set to zero, there are no limits to the number of processes the machine can run. Zero is the default value.
PATH This variable helps find the filesystem checkers on the machine. It begins by searching certain system directories: /sbin, /sbin/fs.d, /sbin/fs, /etc/fs, and /etc. Later, the directories in PATH are searched.
FSTAB_FILE It allows the admin to override the location of the /etc/fstab file. This environment variable can be helpful for developers testing fsck.

 

Running fsck to Check and Repair a Filesystem in Linux 

Before you can run fsck, you must double-check that the partition you intend to use fsck on is unmounted. We are using a secondary drive /dev/sdb mounted in /mnt to illustrate how to use fsck. 

If we try to run fsck when the drive is still mounted, like so:

# fsck /dev/sdb

The terminal returns the text: 

/dev/sdb is mounted.

e2fsck: Cannot continue, aborting.

But this is not the result you should expect on your machine. It might corrupt your filesystem data instead of aborting the operation. 

To avoid any potential damage, begin by unmounting the partition like so:

# umount /dev/sdb

You can now safely run fsck with the first command we tried in this section. When the command runs successfully, it is bound to return an exit code. There are many possible exit codes, each with a unique meaning. 

You can check the command’s manual to find these, but we’ve put together a table with the codes and their meanings for your convenience. 

Exit Code Meaning
0 No errors
1 Filesystem errors corrected
2 The system should be rebooted
4 Filesystem errors left uncorrected
8 Operational error
16 Usage or syntax error
32 Checking canceled by user request
128 Shared-library error

Now that we’ve finished the checking part and know the issue, it’s time to get into repairing the filesystem.

There’s a chance that your filesystem has more than one error. In such cases, it’s best to let fsck automatically correct the errors. Using the utility this way is as simple as running:

# fsck -y /dev/sdb

In the command above, the -y flag automatically answers “yes” to any prompts that fsck tries to send you when it wants to correct an error. If you don’t have root permissions and want to accomplish this automatic error correction, you can run the following command on any filesystem:

$ fsck -AR -y

If you need to check the root partition, you will need to either run fsck in rescue mode or force it to load when your system boots since you can’t run the utility when the partition is mounted. 

Running fsck on System Boot

To run fsck when the system boots, you must create a file named “forcefsck” in the root partition with this command:

# touch /forcefsck

You can then reboot your system or schedule a reboot. The machine will run fsck on the next boot. 

Bear in mind that if many used inodes are connected to your system, the utility can take hours to finish its job. So, you might need to schedule bootup carefully.

When the system finally boots, check that the file you created still exists by running:

# ls /forcefsck

If the file hasn’t been deleted, delete it manually, else the utility will run on every boot.

Running fsck in Rescue Mode

To run fsck in rescue mode, you will need to take some extra steps. Stop any critical services that may be running, then run the following command:

# reboot

This will prepare your system for rebooting, and your system will eventually boot. When it does, press and hold the Shift button to make the grub menu appear. Highlight the “Advanced options” button and hit enter, then select the “Recovery mode” option. 

The “Recovery Menu” will appear. Here, you must select the fsck option, then confirm that you want to remount your filesystem. You will then see a black processing screen with white text that will keep you informed about the progress. 

When fsck finishes running, you can boot back to the operation by choosing the “resume” option.Â