How to force fsck after a restart (file system consistency check)

fsck Is a tool for checking and repairing file systems on Linux, macOS and FreeBSD, similar to the CHKDSK Windows tool.
With the old sysvinit and Upstart, users can use a simple command to force a disk check on the next reboot (sudo touch /forcefsck), But this is no longer the case, and most Linux distributions use systemd. This article explains how to force fsck to run at boot in two ways, both of which work with systemd. You will also find a command at the end of this article that shows when fsck last checked the partition.
Use the following steps to force a file system consistency check every time you start your computer. There are also instructions for undoing this action.

Option 1 (only for ext2 / ext3 / ext4 file system): Use tune2fs to force fsck on restart

tune2fs can only force fsck every time the EXT4, EXT3, and EXT2 file systems are restarted. Most Linux distributions use EXT3 / 4 by default, so unless you explicitly specify other file system types, EXT3 / 4 is used in most cases.
tune2fs can trigger a forced fsck with the following command on every restart: -c (Maximum number of counts) option. This option setting will check the number of mounts of the file system, so set it to 1 Fsck runs every time the computer is started. Set as -1 Either 0 Reset this setting (the number of mounts of the file system will be ignored by e2fsck and the kernel). To do this, you need to ensure that the number of passes for the partition you want to check and repair with fsck is greater than 0 Set in /etc/fstab (last row /etc/fstab). The root partition should be set to 1 (Check first), and the other partitions you want to check should be set to 2. example:

# /etc/fstab: static file system information.

/dev/sda1  /      ext4  errors=remount-ro  0  1
/dev/sda5  /home  ext4  defaults           0  2

After doing this, use the following command to force the use of fsck each time you start the EXT4, EXT3 or EXT2 file system:

sudo tune2fs -c 1 /dev/sdXY

You need to replace sdXY With the partition you want to check and repair. You can find this file using a graphics application such as Gparted, which displays all available partitions for each disk, or using a command line tool such as lsblk Either fdisk (sudo fdisk -l).
You can now restart your system and fsck should be forced to run a file system consistency check. When you no longer want to force fsck to run on every boot, run the same command -1 Instead 1:

sudo tune2fs -c -1 /dev/sdXY

You can also use this command to run fsck periodically. For example, you can use the following command to set fsck to run a file system check every 30 boots -c 30 (E.g.: sudo tune2fs -c 30 /dev/sdXY).

Option 2: Use the following command to force fsck on restart fsck.mode=force As a kernel parameter, you can also choose fsck.repair=yes

This should work for many file system types, including ext4 / ext3 / ext2, xfs, ntfs, fat32, exfat, etc. It should also be noted that systemd-fsck Without knowing any details about a particular file system, only the file system checker specific to each file system type (/sbin/fsck.*;E.g. : /sbin/fsck.ext4) .Systemd runs fsck for each file system whose fsck passes are greater than 0 Set in /etc/fstab (last row /etc/fstab), So make sure you edit /etc/fstab if not like this. The root partition should be set to 1 (Check first), and the other partitions you want to check should be set to 2. example:

# /etc/fstab: static file system information.

/dev/sda1  /      ext4  errors=remount-ro  0  1
/dev/sda5  /home  ext4  defaults           0  2

Appears in all partitions /etc/fstab If you are using a bootstrap and the number of passes (last column in fstab) is greater than 0, a check is performed fsck.mode=force Kernel parameters.
Now that we have solved this problem, this is how to force start fsck under systemd boot. You need to add fsck.mode=force As a kernel parameter of the grub configuration file.
This command opens with Nano command line text editor /etc/default/grub So you can edit it:

sudo nano /etc/default/grub

To force fsck every time you start your computer, you need to add fsck.mode=force To GRUB_CMDLINE_LINUX_DEFAULT, at the end of the line but the last quote (").
example:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash fsck.mode=force"

Make sure that you have not made any other edits and that your edits are correct, otherwise your computer may not start!
After making changes, save the file and exit (To save the file and exit Nano, use: Ctrl + O, EnterAnd then Ctrl + X).
You can add a second kernel parameter, called fsck.repair. This has default options preen, It can automatically fix problems that can be safely solved. you can use it fsck.repair=yes fsck answers “yes” to all questions, or “no” to all questions.
Example (force fsck on every restart and answer “yes” to all fsck questions):

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash fsck.mode=force fsck.repair=yes"

Add this to /etc/default/grub Same explanation as before.
After editing /etc/default/grub, Update your Grub2 configuration:

sudo update-grub
  • Fedora or openSUSE
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
  • Arch Linux:
sudo grub-mkconfig -o /boot/grub/grub.cfg

In other Linux distributions, you can try the above command. If they don’t work, search for how to update the Grub2 configuration for your Linux distribution.
When done, reboot the system and fsck should run a file system consistency check at boot time.
To stop forcibly checking the file system on every boot, delete fsck.mode=force (with fsck.repair=yes If you added) /etc/default/grub Configure the file and update grub as described earlier.

How to find out when fsck last checked the file system

You can use tune2fs to find out when fsck last checked the file system:

sudo tune2fs -l /dev/sdXY | grep checked

Replace sdXY With the partitions you want to see during the last check. example:

$ sudo tune2fs -l /dev/sda1 | grep checked
Last checked:             Thu May 23 15:16:23 2019

Source

Sidebar