在Linux系统中,有时候重启会耗费非常长的时间,如果你进一步检查细节,就会发现绝大部分时间都耗费在磁盘自检(fsck)上了,有时候遇到时间比较紧急的情况,磁盘自检耗费的时间非常长,真的是让人心焦火急的!如下截图所示

关于磁盘自检,如果是新手,肯定都会有不少疑惑,下面从这几个方面一一讲述,希望能解答你的疑惑。下面实验版本为Red Hat Enterprise Linux Server release 5.7,请注意不同版本之间的区别。

 

为什么磁盘需要自检呢?

现在的文件系统已经非常可靠,极少出现问题,但是总有意外或错误出现的概率,例如断电、硬件失败等,所以Linux会使用fsck来检查和修复文件系统。fsck命令(filesystem consistency check),意思是文件系统一致性检查。fsck能够安全、自动修复下面这5类问题:

未被引用的inode;

难以置信的超大链接数

没有记录在磁盘块映射表中的未用数据块

列出的空闲数据块还在某个文中使用;

超级块中不正确的汇总信息。

通常情况下,硬盘在启动时使用fsck -p来进行检查,它将检查/etc/fstab中列出的所有本地文件系统。大多数系统设置为启动时自动运行fsck,希望任何错误在系统使用前被检测到,并得到修正。因为使用错误的文件系统可能使得问题变得更加糟糕。所以磁盘自检是有必要的,这也是为什么大多数系统将其设置为启动时自动运行fsck(有一定规律,不是每次启动都会做磁盘自检,取决于你的配置,下面阐述),所以没有特殊必要的话,最好不要取消磁盘自检。

 

什么时候磁盘才会自检?

上面所述,并不是每次重启都会做磁盘自检,那么磁盘自检的规律如何查看呢? 此时需要借助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.pc-freak.net/blog/changing-setting-33-times-standard-fsck-file-system-check-debian-linux-desktop-systems/

http://www.cyberciti.biz/faq/linux-force-fsck-on-the-next-reboot-or-boot-sequence/

www.cyberciti.biz/faq/linux-unix-bypassing-fsck/

最新文章

  1. mybatis Oracle 批量插入,批量更新
  2. Camera中对焦模式总结
  3. git——学习笔记(三)分支管理
  4. 网络流(最大流):CodeForces 499E Array and Operations
  5. Linux--根文件系统的挂载过程分析
  6. Revit 2015 API 的全部变化和新功能
  7. 安装mono和jexus,运行asp.net程序
  8. 【.NET】SQL链接字符串
  9. 第十九节: 结合【表达式目录树】来封装EF的BaseDal层的方法
  10. Ubuntu16.04重新安装MySQL数据库
  11. eclipse svn不显示提交人、提交时间的问题
  12. Xmind8 破解
  13. tomcat优化之安装并配置apr库
  14. c# 调apicontroller
  15. wget 报错 OpenSSL: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failur
  16. elasticsearch备忘
  17. Extjs 下拉框
  18. Ubuntu 16.09下iptables通过raw表实现日志输出和调试
  19. Node.js综述
  20. 探究JS中的连等赋值问题

热门文章

  1. C语言 第六章 多重循环练习
  2. 从底层开发谈WebGIS中实现地理长度固定的可视窗口的思路和方法
  3. Easyui datagrid行内【添加】、【编辑】、【上移】、【下移】
  4. AnagularJs之directive
  5. gitlab工作流程简介
  6. 【原创】HDFS介绍
  7. ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法,先分组,然后在组内排名,分组计算,主表与附表一对多取唯一等
  8. SQL转换全角/半角函数
  9. [WCF编程]10.操作:回调操作
  10. ASP.NET MVC4实现TinyMCE 4.0.20自定义上传功能