在Linux系统中,有时候重启会耗费很是长的时间,若是你进一步检查细节,就会发现绝大部分时间都耗费在磁盘自检(fsck)上了,有时候遇到时间比较紧急的状况,磁盘自检耗费的时间很是长,真的是让人心焦火急的!以下截图所示 node
关于磁盘自检,若是是新手,确定都会有很多疑惑,下面从这几个方面一一讲述,但愿能解答你的疑惑。下面实验版本为Red Hat Enterprise Linux Server release 5.7,请注意不一样版本之间的区别。 linux
为何磁盘须要自检呢? 安全
如今的文件系统已经很是可靠,极少出现问题,可是总有意外或错误出现的几率,例如断电、硬件失败等,因此Linux会使用fsck来检查和修复文件系统。fsck命令(filesystem consistency check),意思是文件系统一致性检查。fsck可以安全、自动修复下面这5类问题:app
未被引用的inode; 测试
难以置信的超大连接数 ui
没有记录在磁盘块映射表中的未用数据块 this
列出的空闲数据块还在某个文中使用; spa
超级块中不正确的汇总信息。.net
一般状况下,硬盘在启动时使用fsck -p来进行检查,它将检查/etc/fstab中列出的全部本地文件系统。大多数系统设置为启动时自动运行fsck,但愿任何错误在系统使用前被检测到,并获得修正。由于使用错误的文件系统可能使得问题变得更加糟糕。因此磁盘自检是有必要的,这也是为何大多数系统将其设置为启动时自动运行fsck(有必定规律,不是每次启动都会作磁盘自检,取决于你的配置,下面阐述),因此没有特殊必要的话,最好不要取消磁盘自检。 unix
何时磁盘才会自检?
上面所述,并非每次重启都会作磁盘自检,那么磁盘自检的规律如何查看呢? 此时须要借助tune2fs命令
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -E "Maximum mount count|Check interval"
Maximum mount count: -1
Check interval: 604800 (1 week)
[root@DB-Server ~]#
如上所示,Check interval表示执行磁盘自检fsck的时间间隔,Maximum mount count表示强制自检的挂载次数,即达到最大挂载次数后,再次开机时就会强制自检。上面信息告诉咱们,磁盘自检的时间间隔为一周,也就是7天。Maximum mount count 值为-1表示禁用这个功能。
如何更改磁盘自检设置?
加入我要将磁盘自检的时间间隔设置为一个月,那么能够以下设置
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -i -E 'mount|check'
Last mounted on: <not available>
Default mount options: user_xattr acl
Last mount time: Mon Jul 4 11:30:54 2016
Mount count: 94
Maximum mount count: -1
Last checked: Sun Jan 4 21:34:24 2015
Check interval: 604800 (1 week)
Next check after: Sun Jan 11 21:34:24 2015
[root@DB-Server ~]# tune2fs -i 30 /dev/sda2
tune2fs 1.39 (29-May-2006)
Setting interval between checks to 2592000 seconds
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -i -E 'mount|check'
Last mounted on: <not available>
Default mount options: user_xattr acl
Last mount time: Mon Jul 4 11:30:54 2016
Mount count: 94
Maximum mount count: -1
Last checked: Sun Jan 4 21:34:24 2015
Check interval: 2592000 (1 month)
Next check after: Tue Feb 3 21:34:24 2015
[root@DB-Server ~]#
若是我要设置磁盘挂载2次就必须进行磁盘自检,那么能够以下设置:
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -i -E 'mount|check'
Last mounted on: <not available>
Default mount options: user_xattr acl
Last mount time: Mon Jul 4 11:30:54 2016
Mount count: 94
Maximum mount count: -1
Last checked: Sun Jan 4 21:34:24 2015
Check interval: 2592000 (1 month)
Next check after: Tue Feb 3 21:34:24 2015
[root@DB-Server ~]# tune2fs -c 2 /dev/sda2
tune2fs 1.39 (29-May-2006)
Setting maximal mount count to 2
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -i -E 'mount|check'
Last mounted on: <not available>
Default mount options: user_xattr acl
Last mount time: Mon Jul 4 11:30:54 2016
Mount count: 94
Maximum mount count: 2
Last checked: Sun Jan 4 21:34:24 2015
Check interval: 2592000 (1 month)
Next check after: Tue Feb 3 21:34:24 2015
[root@DB-Server ~]#
固然,你也能够一块儿设置,以下所示
[root@DB-Server ~]# tune2fs -i 60 -c 10 /dev/sda2
tune2fs 1.39 (29-May-2006)
Setting maximal mount count to 10
Setting interval between checks to 5184000 seconds
You have new mail in /var/spool/mail/root
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -i -E 'mount|check'
Last mounted on: <not available>
Default mount options: user_xattr acl
Last mount time: Mon Jul 4 11:30:54 2016
Mount count: 94
Maximum mount count: 10
Last checked: Sun Jan 4 21:34:24 2015
Check interval: 5184000 (2 months)
Next check after: Thu Mar 5 21:34:24 2015
[root@DB-Server ~]#
如何取消磁盘自检设置?
如何取消、关闭磁盘自检呢?咱们能够有下面几种方式:
1: 使用命令tune2fs -i 0 -c 0 取消磁盘自检,以下所示
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -i -E 'mount|check'
Last mounted on: <not available>
Default mount options: user_xattr acl
Last mount time: Mon Jul 4 11:30:54 2016
Mount count: 94
Maximum mount count: 10
Last checked: Sun Jan 4 21:34:24 2015
Check interval: 5184000 (2 months)
Next check after: Thu Mar 5 21:34:24 2015
[root@DB-Server ~]# tune2fs -i 0 -c 0 /dev/sda2
tune2fs 1.39 (29-May-2006)
Setting maximal mount count to -1
Setting interval between checks to 0 seconds
[root@DB-Server ~]#
[root@DB-Server ~]# tune2fs -l /dev/sda2 | grep -i -E 'mount|check'
Last mounted on: <not available>
Default mount options: user_xattr acl
Last mount time: Mon Jul 4 11:30:54 2016
Mount count: 94
Maximum mount count: -1
Last checked: Sun Jan 4 21:34:24 2015
Check interval: 0 (<none>)
[root@DB-Server ~]#
2:修改/etc/fstab中第六列的值
/etc/fstab分区表中第六列(pass):指明自检顺序。 (0为不自检,1或者2为要自检,若是是根分区要设为1,其余分区只能是2)
[root@DB-Server ~]# more /etc/fstab
LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
/dev/mapper/VolGroup01-LogVol00 /u02 ext3 defaults 0 2
/dev/VolGroup02/LogVol00 /u05 ext3 defaults 1 2
#/dev/VolGroup03/LogVol00 /u06 ext3 defaults 1 1
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
[root@DB-Server ~]# more /etc/fstab
LABEL=/ / ext3 defaults 1 0
LABEL=/boot /boot ext3 defaults 1 0
tmpfs /dev/shm tmpfs defaults 0 0
/dev/mapper/VolGroup01-LogVol00 /u02 ext3 defaults 0 0
/dev/VolGroup02/LogVol00 /u05 ext3 defaults 1 0
#/dev/VolGroup03/LogVol00 /u06 ext3 defaults 1 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda3 swap swap defaults 0 0
You have new mail in /var/spool/mail/root
[root@DB-Server ~]#
关于这二者的优先级,我测试过,即便已经知足了Maximum mount count和Check interval里面的条件,若是在/etc/fstab里面关闭了磁盘自检,那么在重启时,并不会作磁盘自检,也就是说/etc/fstab设置里面的优先级要高一些。
3:使用参数-f 跳过自检
[root@DB-Server ~]# shutdown -rf now
这种方式是临时的,不须要修改系统配置。
4:在/boot/grub/grub.conf 中添加fastboot,以下所示
[root@DB-Server /]# cd /boot
[root@DB-Server boot]# ls
config-2.6.18-274.el5 grub initrd-2.6.18-274.el5.img lost+found symvers-2.6.18-274.el5.gz System.map-2.6.18-274.el5 vmlinuz-2.6.18-274.el5
[root@DB-Server boot]# cd grub/
[root@DB-Server grub]# ls
device.map fat_stage1_5 grub.conf jfs_stage1_5 minix_stage1_5 splash.xpm.gz stage2 vstafs_stage1_5
e2fs_stage1_5 ffs_stage1_5 iso9660_stage1_5 menu.lst reiserfs_stage1_5 stage1 ufs2_stage1_5 xfs_stage1_5
[root@DB-Server grub]# more grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda2
# initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-274.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-274.el5 ro root=LABEL=/ rhgb quiet
initrd /initrd-2.6.18-274.el5.img fastboot
You have new mail in /var/spool/mail/root
[root@DB-Server grub]#
kernel /vmlinuz-2.6.18-274.el5 ro root=LABEL=/ rhgb quiet
initrd /initrd-2.6.18-274.el5.img fastboot
如何强制下次重启磁盘自检?
如何强制系统下次root时,进行磁盘自检?
方法1: 使用tune2fs调整Maximum mount count和Check interval的值,使其下次重启时知足磁盘自检。
方法2:关于这个,在RHEL中,你能够在/etc/rc.sysinit 中看到以下代码(Debian or Ubuntu Linux下查看/etc/init.d/checkfs.sh)以下所示:
因此,你只须要建立一个forcefsck文件,下次重启时,就能强制其进行磁盘自检。
[root@DB-Server /]# touch /forcefsck
[root@DB-Server /]# reboot
Broadcast message from root (pts/1) (Mon Jul 4 14:33:59 2016):
The system is going down for reboot NOW!
重启过程当中,你就会看到磁盘自检。重启后,你会发现刚才生成的forcefsck文件已经不见了。
方法3:使用shutdown相关参数强制磁盘自检
[root@DB-Server /]# man shutdown
# shutdown -rF now
参考资料:
http://www.cyberciti.biz/faq/linux-force-fsck-on-the-next-reboot-or-boot-sequence/