如何在启动时强制文件系统检查:systemd-fsck RHEL/CentOS 7/8

时间:2020-01-09 10:38:26  来源:igfitidea点击:

在早期的CentOS/RHEL 5和6中,我们曾经使用tune2fs强制对引导和修复文件系统进行文件系统检查。
在那里,我们曾经使用" tune2fs -c 4/dev/disk-name"命令来更改最大安装数,然后在要检查的文件系统下创建一个空文件" forcefsck"。

现在从CentOS/RHEL 7开始,这些方法是不支持的。
因此,创建/forcefsck不会强制在RHEL/CentOS 7/8 Linux启动时检查文件系统。

更新GRUB2以强制在启动时检查文件系统

通过systemd-219-19.el7,在RHEL 7.2中添加了fsck.repair =fsck.mode =的内核命令行选项。

因此,我们可以使用fsck.mode = force来在引导(下一次重新引导)时执行并强制执行文件系统检查。
可以将其与fsck.repair = yes结合使用,以对fsck命令发出肯定的回答(如果发现任何错误)。
因此,使用此" fsck"将在引导阶段在文件系统中执行必要的更正,而无需任何手动干预。

接下来在/etc/sysconfig/grub文件的GRUB_CMDLINE_LINUX下的fsck.mode = forcefsck.repair = yes下添加如下图所示

[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

从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.

在CentOS/RHEL 7/8中重建GRUB2

提示:

以下命令适用于基于UEFI的计算机上的旧式BIOS:~]#grub2-mkconfig -o/boot/efi/EFI/redhat/grub.cfg

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

到此为止,我们已经全部完成了使用systemd-fsck强制引导(下次重新引导)文件系统的操作。
接下来"重启"CentOS/RHEL 7/8 Linux主机以验证步骤

验证配置

重新启动后,使用腻子或者任何其他CLI工具连接到Linux主机。
我们需要检查引导日志,可以使用dmesg或者journalctl -b或者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-2016)
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>

如我们所见,在启动时会调用systemd-fsck来强制执行文件系统检查。
由于没有错误,因此fsck检查顺利进行。