Get to know the fsck command

Situation Normal, All Fscked Up

Article from Issue 188/2016
Author(s):

Learn how to use fsck's capabilities to solve filesystem problems.

The fsck command [1] is often used as a euphemism for a well-known swear word – and not just because it is a four-letter word that starts with f and ends with k. Typically, the command becomes relevant only when a filesystem needs repairing before the computer will finish booting or starting a graphical interface, and the repair work requires human intervention. In such circumstances, users may feel like swearing as they try to cope with their limited knowledge of the command. However, with more knowledge of fsck's background and options, such moments become much less alarming.

An abbreviation of FileSystem Consistency Check, fsck is installed by default on Linux systems as part of the util-linux package. Properly speaking, it is not a separate command at all, but a front end for filesystem-checking commands such as e2fsck, dosfsck, and fsckvfat, all of which work in very similar ways, usually in close association with /etc/fstab [2], which lists the available filesystems. These commands can still be run separately, but, thanks to fsck, users no longer need to remember each of them. Instead, in most cases fsck calls the appropriate command as needed, and the individual commands are only needed in advanced circumstances that few users are likely to encounter.

For convenience, either run fdisk -l (Figure 1) or else run less /etc/fstab before using fsck (Figure 2). Either command gives you a list of partitions on the systems, which can help you ensure that you make all necessary repairs. Make sure you run fsck on the correct drive. Finding the correct drive is essential, because it is possible to bypass fsck's defaults and run on a mounted drive, which can permanently corrupt the drive. Instead, use umount [3] to unmount the filesystem first. Should the filesystem not be unmountable – as often happens with the root partition at boot time – start a Live DVD such as GParted to run fsck.

Figure 2: Opening /etc/fstab provides useful information, including the filesystems, their UUID, and, in the sixth column for each filesystem, the order in which to check them.
Figure 1: Before running fsck, run fsck -l to see a list of available partitions.

If you forget to unmount a drive before checking it, fsck will start to run, but it will warn you before proceeding about what you are doing. Usually, a drive that is used mainly for storage can be checked without being unmounted, as long as you close other applications that are running. However, checking active filesystems – like /root or /usr – can crash your system and even permanently corrupt it. On the whole, you should err on the side of caution and pay attention to fsck's warning about mounted drives, no matter how they are used (Figure 3).

Figure 3: The fsck command warns when you are about to check a mounted drive.

Fsck has a standard command structure of:

[COMMAND] [OPTIONS] [FILESYSTEM]

Filesystems can be added to the command in a comma-separated list, and each can be specified as a device: such as /dev/sda1, a label, or a mountpoint. Add the -A option to check all drives without naming any filesystem in the command, or type the command without any mount point to run fsck one filesystem at a time, in the order they are listed in /etc/fstab.

You can continue running the command, but you do risk making a change that may crash your system. As you work, you might notice that fsck does not include any GNU options, with specific names and two hyphens before them. This lack does not affect functionality but presumably reflects the fact that fsck was written long after the basic GNU/Linux system. Also, the command is intended not for ordinary users. Instead, it is intended for administrators, who in theory do not require built-in guides of GNU options to functionality in their options.

Filesystem Selection Options

About half of fsck's options set how the command selects the filesystems to run upon. The most important of these options is -A, which tries to save time by checking all filesystems listed in /etc/fstab in a single pass instead of by running the command multiple times. By itself, -A begins with the root filesystem, than moves on to others according to fstab's sixth column, pass_no. Filesystems with a pass_no of 1 follow the root system, then those with a pass_no of 2. Filesystems with the same priority are checked in parallel, if possible. By contrast, filesystems with a pass_no of 0 are not checked at all.

One limitation of -A is that it cannot run in parallel on RAID systems or encrypted disks. An even more important one is that it does not detect filesystems not listed in /etc/fstab, which means that it might fail to detect a filesystem that has a problem.

Two other options modify -A. When -P is included, the root filesystem is checked in parallel with the other filesystems. The man page for fsck warns that if -P is used and the root filesystem requires repairs, other commands, such as e2fsck may be corrupted, although normally the practice might save time. By contrast, -R is much safer, because it signals to skip the root filesystem. This is useful if you know that it has already been mounted – as generally happens if a problem emerges while the system is booting.

Additionally, multiple filesystems can be set to run one after another with the -s option. This option is especially useful when fsck runs in interactive mode (see below), asking for your input before performing any repairs.

Repair Options from e2fsck

For repairs, fsck borrows options from the commands for which it fronts. That is especially true in the cases of e2fsck, which repairs the popular ext2/3/4 filesystems.

By default, fsck runs interactively, asking for confirmation before attempting any repairs. However, users can change this behavior to suit themselves. For example, when -p is added to the command, the command repairs (or preens, in the jargon of fsck) problems that can be fixed without the user making any decision. If the option encounters a problem it cannot handle, then it prints a description of the problem and exits. The -a option acts the same way, but it exists only for backward compatibility with older releases. The man page advises that -p be used whenever possible rather than -a.

Neither -p nor -a can be used with -n or -y. With -n, all requests for decisions by the user are answered automatically with "No", whereas with -y, all requests are answered automatically with "Yes". Running -n causes few problems, because any problems are simply printed out as the command runs, and users can simply try running fsck again with other options. However, -y could aggravate the problems, so it should be used carefully.

Options to Change fsck's Behavior

Other options for fsck affect how the command runs rather than where. As in most commands, verbosity (-V) assures users that the command ran successfully or else helps pinpoint problems. Advanced users also have the -r option, which lists such statistics as the exit status, the time fsck took to run, and the user and system CPU time in a comma-separated list.

Usually, the filesystem format can be read from /etc/fstab. However, if you want fsck to run on a filesystem not listed in /etc/fstab or have any other dificulty, you can hard-code its format with -t FILESYSTEM.

All in all, fsck can be a tricky command to run, and adding options can sometimes make the situation worse. Under these circumstances, using -M to skip mounted filesystems can sidestep the most common problems. Similarly, once you have decided on which options to use, you should use -N to simulate the command without actually doing anything, so that you can avoid unexpected, or even fatal results (Figure 4).

Figure 4: The -N option simulates running fsck without actually running it. Here, fsck simulates running the -A command. Notice the use of square brackets.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News