How to force file system check on boot: systemd-fsck RHEL/CentOS 7/8

Earlier with CentOS/RHEL 5 and 6 we used to use tune2fs to force file system check on boot and repair file system. There we used to change maximum mount count using tune2fs -c 4 /dev/disk-name command and then creating an empty file forcefsck under the file system to be checked. Now starting with CentOS/RHEL 7 those methods are not supported. Hence creating /forcefsck will not force file system check on boot with RHEL/CentOS 7/8 Linux.

 

Lab Environment

I have created a Virtual Machine using Oracle VirtualBox installed on Linux server. I have installed CentOS 8 on this virtual machine but the same steps will work on RHEL 7/8 or CentOS 7.

In this article we will cover below topics:

  • Creating the files /forcefsck and /fsckoptions doesn't work on CentOS/RHEL 7/8 Linux.
  • How to perform filesystem check on next reboot at startup stage and repair file system, using systemd-fsck?
  • How to perform non-interactive force fsck during reboot stage to repair file system, as by default the fsck will await user input for every error?

 

Step 1: Update GRUB2 to force file system check on boot

With systemd-219-19.el7 the kernel command line option of fsck.repair= and fsck.mode= was added in RHEL 7.2.

So we can use fsck.mode=force to perform and force file system check on boot (next reboot). This can be combined with fsck.repair=yes to answer yes to fsck command out (if any errors found). So with this fsck will perform necessary correction in the file system at boot stage without any manual intervention.

Next append fsck.mode=force and fsck.repair=yes under GRUB_CMDLINE_LINUX in /etc/sysconfig/grub file as shown below

[root@centos-8 ~]# cat /etc/sysconfig/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet fsck.mode=force fsck.repair=yes"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true

 

From man page of systemd-fsck

KERNEL COMMAND LINE
       systemd-fsck understands one kernel command line parameter:

fsck.mode=
           One of "auto", "force", "skip". Controls the mode of operation. The default is "auto", and ensures that
           file system checks are done when the file system checker deems them necessary.  "force" unconditionally
           results in full file system checks.  "skip" skips any file system checks.

fsck.repair=
           One of "preen", "yes", "no". Controls the mode of operation. The default is " preen", and will
           automatically repair problems that can be safely fixed.  "yes " will answer yes to all questions by fsck
           and "no" will answer no to all questions.

 

Step 2: Rebuild GRUB2 in CentOS/RHEL 7/8

[root@centos-8 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
done

We are all done here to force file system check on boot (next reboot) using systemd-fsck. Next reboot your CentOS/RHEL 7/8 Linux host to verify the steps

 

Step 3: Verify the configuration

Post reboot connect to your Linux host using putty or any other CLI tool. You need to check the boot logs which you can either check using dmesg or journalctl -b or journalctl --boot

[root@centos-8 ~]# journalctl --boot

Jan 17 03:11:55 centos-8.example.com dracut-cmdline[195]: Using kernel command line parameters: BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-80.11.2.el8_0.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet fsck.mode=force fsck.repair=yes

<Output trimmed>

Jan 17 03:11:57 centos-8.example.com systemd[1]: Starting File System Check on /dev/mapper/rhel-root...
Jan 17 03:11:57 centos-8.example.com systemd[1]: Reached target Remote File Systems (Pre).
Jan 17 03:11:57 centos-8.example.com systemd[1]: Reached target Remote File Systems.
Jan 17 03:11:57 centos-8.example.com systemd-fsck[485]: e2fsck 1.44.3 (10-July-2018)
Jan 17 03:11:57 centos-8.example.com systemd-fsck[485]: Pass 1: Checking inodes, blocks, and sizes
Jan 17 03:11:57 centos-8.example.com systemd-fsck[485]: Pass 2: Checking directory structure
Jan 17 03:11:58 centos-8.example.com kernel: random: crng init done
Jan 17 03:11:58 centos-8.example.com kernel: random: 7 urandom warning(s) missed due to ratelimiting
Jan 17 03:11:58 centos-8.example.com systemd-fsck[485]: Pass 3: Checking directory connectivity
Jan 17 03:11:58 centos-8.example.com systemd-fsck[485]: Pass 4: Checking reference counts
Jan 17 03:11:58 centos-8.example.com systemd-fsck[485]: Pass 5: Checking group summary information
Jan 17 03:11:58 centos-8.example.com systemd-fsck[485]: /dev/mapper/rhel-root: 199444/889440 files (0.1% non-contiguous), 1190592/3555328 blocks
Jan 17 03:11:58 centos-8.example.com systemd[1]: Mounting /sysroot...

<Output trimmed>

Here as you see systemd-fsck was called at called to force file system check at boot stage. Here since there were no errors the fsck check went on smoothly.

 

Lastly I hope the steps from the article to force file system check on boot (next reboot) in RHEL/CentOS 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References:
How to force fsck during the boot in RHEL7?

 

Related Searches: linux file system check on boot. disk check runs at every startup. Creating the files /forcefsck and /fsckoptions doesn't work on CentOS/RHEL 7/8 Linux. How to force filesystem check during boot time, using systemd? How to answer yes, to all questions by fsck automatically during boot time? shutdown force fsck in CentOS/RHEL 7/8. How to repair file system on next reboot in Linux.

1 thought on “How to force file system check on boot: systemd-fsck RHEL/CentOS 7/8”

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.