How to Force fsck on Ubuntu Linux Reboot

How to Force fsck on Ubuntu Linux Reboot

Fsck command is used to check filesystem consistency in Linux and Unix-like operating systems. On the system, fsck runs either automatically or manually. The filesystem shouldn’t be mounted while running the fsck. We can force fsck to run on reboot for both root and non-root filesystems.

In this tutorial, we learn how to force fsck on reboot to check for filesystem errors.

Force fsck on System Boot on systemd

Many modern Linux distributions are now adopted to systemd system. The traditional way of using tune2fs works only in sysvinit or upstart systems.

For systemd-based Linux distributions, add fsck.mode=force to your grub configuration file as a kernel parameter. It’s important to note that systemd-fsck doesn’t know anything about the filesystem, so it just runs file system checkers specific to each filesystem type (/sbin/fsck.*). If you have an ext4 filesystem, for example, systemd-fsck will run /sbin/fsck.ext4.

You need to add fsck.mode=force to GRUB_CMDLINE_LINUX_DEFAULT, at the end of the last quote (“).

For example:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash fsck.mode=force"

You can edit the grub configuration using the nano command.

$ sudo nano /etc/default/grub

Output:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
 # For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'
 GRUB_DEFAULT=0
 GRUB_TIMEOUT=5
 GRUB_DISTRIBUTOR=lsb_release -i -s 2> /dev/null || echo Debian
 GRUB_CMDLINE_LINUX_DEFAULT="quiet"
 GRUB_CMDLINE_LINUX=""
 # Uncomment to enable BadRAM filtering, modify to suit your needs
 # This works with Linux (no patch required) and with any kernel that obtains
 # the memory map information from GRUB (GNU Mach, kernel of FreeBSD …)
 # GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"
 # Uncomment to disable graphical terminal (grub-pc only)
 # GRUB_TERMINAL=console
 # The resolution used on graphical terminal
 # note that you can use only modes which your graphic card supports via VBE
 # you can see them in real GRUB with the command `vbeinfo'
# GRUB_GFXMODE=640x480
 # Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
 # GRUB_DISABLE_LINUX_UUID=true
 # Uncomment to disable generation of recovery mode menu entries
 # GRUB_DISABLE_RECOVERY="true"
 # Uncomment to get a beep at grub start
 # GRUB_INIT_TUNE="480 440 1"

If you want to automatically repair problems that are easily fixed, you can add the option fsck.repair=yes to the grub configuration file. The yes in fsck.repair indicates that the system accepts “yes” as an answer to all fsck questions; use no if you want the system to accept “no” as an answer.

You need to edit the grub configuration file as before and add fsck.repair=yes or fsck.repair=no to the GRUB_CMDLINE_LINUX_DEFAULT.

For fsck.repair=yes

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

For fsck.repair=no

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

Make sure that you do not make any additional changes and the changes you made are correct, or your system will fail to boot. After you have made the changes, you can save the nano file and exit.

After you’ve finished editing and saved your file, run the following command to update the Grub2 configuration for Linux distribution.

$ sudo update-grub

You must now reboot the system, and fsck will perform a filesystem consistency check on boot.

If you do not want fsck to perform a filesystem consistency check on each boot, you must delete any additional steps you have added to the grub configuration file.

For systemd to perform single time force fsck during the boot:

  • Boot to the Grub menu
  • While entry is highlighted, press ‘e’ to edit the commands
  • Press “End” button to move cursor to the last
  • Add a space and then add the kernel parameter fsck.mode=force
  • Press Ctrl + x to close and boot the system

Force fsck on System Boot on sysvinit or Upstart system

There are two ways to perform force fsck in old Linux Distribution: using tune2fs and forcefsck.

If a file system is defined as corrupt or after a particular number of boots or time, fsck executes at boot time. Only EXT4, EXT3, and EXT2 filesystems can be forced to perform fsck on each reboot.

The tune2fs allows adjusting of various tunable filesystem parameters on Linux. We will use this command for our purpose.

The following command tune2fs command sets the filesystem (/dev/sda1) to perform fsck every system reboot:

$ sudo tune2fs -c 1 /dev/sda1

Also make sure in the /etc/fstab file, the pass the last, 6th column ([PASS]) is greater than zero.

  • 0 – Do not check.
  • 1 – The file systems to be checked first. Usually root partition set this value
  • 2 – The file systems which are checked last and possibly in parallel.

If you want to force fsck to run the filesystem check after 10 boots, set the value of -c to 10.

$ sudo tune2fs -c 10 /dev/sda1

To stop the fsck to run on each boot, you can replace the value of -c with -1 and run the command.

$ sudo tune2fs -c -1 /dev/sda1

The tune2fs tool can be used to check the current mount count, maximum mount count, the time it was last checked, and interval.

$ sudo tune2fs -l /dev/sda1 | grep -i 'last checked\|mount count\|check interval'

Output:

 $ sudo tune2fs -l /dev/sda1 | grep -i 'last checked|mount count|check interval'
 Mount count:              31
 Maximum mount count:      -1
 Last checked:             Fri May  8 08:08:15 2020
 Check interval:           0 ()

The “Maximum mount count” is the maximum number of mounts before the filesystem is reviewed. The fsck will never run if the maximum value is 0 or -1. The “check interval” refers to the amount of time that passes between two filesystem checks.

It’s possible to run the fsck after a certain number of mounts, for example, if you want to run fsck after every 10 mounts, you can use the following command.

$ sudo tune2fs -c 10 /dev/sda1

Output:

 $ sudo tune2fs -c 10 /dev/sda1
 tune2fs 1.45.6 (20-Mar-2020)
 Setting maximal mount count to 10
$ sudo tune2fs -l /dev/sda1 | grep -i 'last checked|mount count|check interval'
 Mount count:              31
 Maximum mount count:      10
 Last checked:             Fri May  8 08:08:15 2020
 Check interval:           0 ()

Using forcefsck

In older Linux distributions just simply creating a file named forcefsck under the root directory (/) partition will perform force fsck on the root partition.

$ sudo touch /forcefsck

Check to see if the file is created.

$ sudo ls /forcefsck

Output:

$ sudo touch /forcefsck
$ sudo ls /forcefsck
/forcefsck
$ 

Now you can perform a reboot or on your next reboot, the system will perform fsck on the root filesystem.

Normally /forcefsck file is removed automatically after the fsck, if not removed you can manually delete that file.

Conclusion

In this tutorial, we learned how to force fsck on root and non-root filesystems in Linux. Thanks for reading, please give your suggestion in the comment section

Be the first to comment

Leave a Reply

Your email address will not be published.


*