Linux文件系统有可能在各类各样的状况下受到损坏,好比系统崩溃、忽然断电、磁盘断开,或者文件节点 (i-node)不当心被覆盖等等,所以须要按期检查文件系统,而说到检查和修复Linux文件系统,fsck是一款实用的工具。本文将介绍图和借助 fask工具实现文件系统自动检查的方法。node
AD:ide
【51CTO精选译文】日常Linux管理方面最重要的任务之一就是,检查文件系统的完整性。Linux文件系统有可能在各类各样的状况下受到损 坏,好比系统崩溃、忽然断电、磁盘断开,或者文件节点(i-node)不当心被覆盖等等。所以,按期检查文件系统的完整性是个好主意,以便尽可能减少文件系 统受到损坏的风险。而说到检查和修复Linux文件系统,fsck是一款实用的工具。工具
我在本教程中将介绍如何借助fsck工具,实现文件系统的自动检查。spa
系统一启动,就触发文件系统自动检查机制命令行
若是你但愿系统一启动,就自动触发fsck,那么有一些方法能够设置在启动过程当中实现无人值守的fschk,这些方法具体取决于特定的Linux发行版。orm
在Debian、Ubuntu或Linux Mint上,编辑/etc/default/rcS,以下所示。xml
# 启动过程当中,自动修复出现不一致性的文件系统blog
在CentOS上,使用下列内容,编辑/etc/sysconfig/autofsck(要是它没有,就建立)。教程
强制按期检查文件系统ci
若是文件系统很庞大,你能够强制按期检查文件系统,而不是每次系统启动时检查文件系统。为了实现这个操做,先要借助tune2fs命令,找到文件系统配置。下面这个命令行显示了文件系统相关参数的当前值。请注意:/dev/sda1是文件系统超级块所在的分区。
从tune2fs的输出结果中,咱们不只能够看到文件系统的状态(干净与否),还能够看到与文件系统检查有关的一些参数。"Maximum mount count"(最大挂载次数)这个参数是指文件系统检查被触发后的挂载次数。"Check interval"(检查时间间隔)这个参数显示了两次文件系统检查之间的最长时间。在大多数Linux发行版上,这些参数并非默认状况下被设置的,这 意味着并不进行任何按期的文件系统检查。
若是想强制每隔30次挂载就检查文件系统,请运行下面这个命令。
-c max-mount-counts
Adjust the number of mounts after which the filesystem will be checked by e2fsck(8). If max-mount-counts is 0 or -1, the number of times the filesystem is mounted will be disregarded by e2fsck(8) and the kernel. Staggering the mount-counts at which filesystems are forcibly checked will avoid all filesystems being checked at one time when using journaled filesystems.You should strongly consider the consequences of disabling mount-count-dependent checking entirely. Bad disk drives, cables, memory, and kernel bugs could all corrupt a filesystem without marking the filesystem dirty or in error. If you are using journaling on your filesystem, your filesystem will never be marked dirty, so it will not normally be checked. A filesystem error detected by the kernel will still force an fsck on the next reboot, but it may already be too late to prevent data loss at that point.
See also the -i option for time-dependent checking.
若是想强制每隔3个月就检查文件系统,请使用下面这个命令。
如今确认刚添加上去的文件系统检查条件已正确设置。
强制下一次系统重启时,进行一次性的文件系统检查
若是你想在下一次系统重启时触发一次性的文件系统检查,可使用这个命令。
一旦你在相似上面的顶层目录(/)中建立了一个名为forcefsck的空文件,它就会在你下一次重启时,强制进行文件系统检查。系统成功启动后,/forcefsck则会自动被清除。