歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux 磁盤自檢介紹

Linux 磁盤自檢介紹

日期:2017/3/3 11:36:31   编辑:Linux技術

在Linux系統中,有時候重啟會耗費非常長的時間,如果你進一步檢查細節,就會發現絕大部分時間都耗費在磁盤自檢(fsck)上了,有時候遇到時間比較緊急的情況,磁盤自檢耗費的時間非常長,真的是讓人心焦火急的!如下截圖所示關於磁盤自檢,如果是新手,肯定都會有不少疑惑,下面從這幾個方面一一講述,希望能解答你的疑惑。下面實驗版本為RedHatEnterpriseLinuxServerrelease5.7,請注意不同版本之間的區別。為什麼磁盤需要自檢呢?現在的文件系統已經非常可靠,極少出現問題,但是總有意外或錯誤出現的概率,例如斷電、硬件失敗等,所以Linux會使用fsck來檢查和修復文件系統。fsck命令(filesystemconsistencycheck),意思是文件系統一致性檢查。fsck能夠安全、自動修復下面這5類問題:

未被引用的inode;難以置信的超大鏈接數沒有記錄在磁盤塊映射表中的未用數據塊列出的空閒數據塊還在某個文中使用;超級塊中不正確的匯總信息。

通常情況下,硬盤在啟動時使用fsck-p來進行檢查,它將檢查/etc/fstab中列出的所有本地文件系統。大多數系統設置為啟動時自動運行fsck,希望任何錯誤在系統使用前被檢測到,並得到修正。因為使用錯誤的文件系統可能使得問題變得更加糟糕。所以磁盤自檢是有必要的,這也是為什麼大多數系統將其設置為啟動時自動運行fsck(有一定規律,不是每次啟動都會做磁盤自檢,取決於你的配置,下面闡述),所以沒有特殊必要的話,最好不要取消磁盤自檢。什麼時候磁盤才會自檢?上面所述,並不是每次重啟都會做磁盤自檢,那麼磁盤自檢的規律如何查看呢?此時需要借助tune2fs命令

[root@DB-Server~]#tune2fs-l/dev/sda2|grep-E"Maximummountcount|Checkinterval"
Maximummountcount:-1
Checkinterval:604800(1week)
[root@DB-Server~]#

如上所示,Checkinterval表示執行磁盤自檢fsck的時間間隔,Maximummountcount表示強制自檢的掛載次數,即達到最大掛載次數後,再次開機時就會強制自檢。上面信息告訴我們,磁盤自檢的時間間隔為一周,也就是7天。Maximummountcount值為-1表示禁用這個功能。

如何更改磁盤自檢設置?加入我要將磁盤自檢的時間間隔設置為一個月,那麼可以如下設置

[root@DB-Server~]#tune2fs-l/dev/sda2|grep-i-E'mount|check'
Lastmountedon:<notavailable>
Defaultmountoptions:user_xattracl
Lastmounttime:MonJul411:30:542016
Mountcount:94
Maximummountcount:-1
Lastchecked:SunJan421:34:242015
Checkinterval:604800(1week)
Nextcheckafter:SunJan1121:34:242015
[root@DB-Server~]#tune2fs-i30/dev/sda2
tune2fs1.39(29-May-2006)
Settingintervalbetweenchecksto2592000seconds
[root@DB-Server~]#tune2fs-l/dev/sda2|grep-i-E'mount|check'
Lastmountedon:<notavailable>
Defaultmountoptions:user_xattracl
Lastmounttime:MonJul411:30:542016
Mountcount:94
Maximummountcount:-1
Lastchecked:SunJan421:34:242015
Checkinterval:2592000(1month)
Nextcheckafter:TueFeb321:34:242015
[root@DB-Server~]#

如果我要設置磁盤掛載2次就必須進行磁盤自檢,那麼可以如下設置:

[root@DB-Server~]#tune2fs-l/dev/sda2|grep-i-E'mount|check'
Lastmountedon:<notavailable>
Defaultmountoptions:user_xattracl
Lastmounttime:MonJul411:30:542016
Mountcount:94
Maximummountcount:-1
Lastchecked:SunJan421:34:242015
Checkinterval:2592000(1month)
Nextcheckafter:TueFeb321:34:242015
[root@DB-Server~]#tune2fs-c2/dev/sda2
tune2fs1.39(29-May-2006)
Settingmaximalmountcountto2
[root@DB-Server~]#tune2fs-l/dev/sda2|grep-i-E'mount|check'
Lastmountedon:<notavailable>
Defaultmountoptions:user_xattracl
Lastmounttime:MonJul411:30:542016
Mountcount:94
Maximummountcount:2
Lastchecked:SunJan421:34:242015
Checkinterval:2592000(1month)
Nextcheckafter:TueFeb321:34:242015
[root@DB-Server~]#

當然,你也可以一起設置,如下所示

[root@DB-Server~]#tune2fs-i60-c10/dev/sda2
tune2fs1.39(29-May-2006)
Settingmaximalmountcountto10
Settingintervalbetweenchecksto5184000seconds
Youhavenewmailin/var/spool/mail/root
[root@DB-Server~]#tune2fs-l/dev/sda2|grep-i-E'mount|check'
Lastmountedon:<notavailable>
Defaultmountoptions:user_xattracl
Lastmounttime:MonJul411:30:542016
Mountcount:94
Maximummountcount:10
Lastchecked:SunJan421:34:242015
Checkinterval:5184000(2months)
Nextcheckafter:ThuMar521:34:242015
[root@DB-Server~]#
如何取消磁盤自檢設置?

如何取消、關閉磁盤自檢呢?我們可以有下面幾種方式:

1:使用命令tune2fs-i0-c0取消磁盤自檢,如下所示

[root@DB-Server~]#tune2fs-l/dev/sda2|grep-i-E'mount|check'
Lastmountedon:<notavailable>
Defaultmountoptions:user_xattracl
Lastmounttime:MonJul411:30:542016
Mountcount:94
Maximummountcount:10
Lastchecked:SunJan421:34:242015
Checkinterval:5184000(2months)
Nextcheckafter:ThuMar521:34:242015
[root@DB-Server~]#tune2fs-i0-c0/dev/sda2
tune2fs1.39(29-May-2006)
Settingmaximalmountcountto-1
Settingintervalbetweenchecksto0seconds
[root@DB-Server~]#
[root@DB-Server~]#tune2fs-l/dev/sda2|grep-i-E'mount|check'
Lastmountedon:<notavailable>
Defaultmountoptions:user_xattracl
Lastmounttime:MonJul411:30:542016
Mountcount:94
Maximummountcount:-1
Lastchecked:SunJan421:34:242015
Checkinterval:0(<none>)
[root@DB-Server~]#

2:修改/etc/fstab中第六列的值/etc/fstab分區表中第六列(pass):指明自檢順序。(0為不自檢,1或者2為要自檢,如果是根分區要設為1,其他分區只能是2)

[root@DB-Server~]#more/etc/fstab
LABEL=//ext3defaults11
LABEL=/boot/bootext3defaults12
tmpfs/dev/shmtmpfsdefaults00
/dev/mapper/VolGroup01-LogVol00/u02ext3defaults02
/dev/VolGroup02/LogVol00/u05ext3defaults12
#/dev/VolGroup03/LogVol00/u06ext3defaults11
devpts/dev/ptsdevptsgid=5,mode=62000
sysfs/syssysfsdefaults00
proc/procprocdefaults00
LABEL=SWAP-sda3swapswapdefaults00
[root@DB-Server~]#more/etc/fstab
LABEL=//ext3defaults10
LABEL=/boot/bootext3defaults10
tmpfs/dev/shmtmpfsdefaults00
/dev/mapper/VolGroup01-LogVol00/u02ext3defaults00
/dev/VolGroup02/LogVol00/u05ext3defaults10
#/dev/VolGroup03/LogVol00/u06ext3defaults10
devpts/dev/ptsdevptsgid=5,mode=62000
sysfs/syssysfsdefaults00
proc/procprocdefaults00
LABEL=SWAP-sda3swapswapdefaults00
Youhavenewmailin/var/spool/mail/root
[root@DB-Server~]#
關於這兩者的優先級,我測試過,即使已經滿足了Maximummountcount和Checkinterval裡面的條件,如果在/etc/fstab裡面關閉了磁盤自檢,那麼在重啟時,並不會做磁盤自檢,也就是說/etc/fstab設置裡面的優先級要高一些。

3:使用參數-f跳過自檢

[root@DB-Server~]#shutdown-rfnow

這種方式是臨時的,不需要修改系統配置。

4:在/boot/grub/grub.conf中添加fastboot,如下所示

[root@DB-Server/]#cd/boot
[root@DB-Serverboot]#ls
config-2.6.18-274.el5grubinitrd-2.6.18-274.el5.imglost+foundsymvers-2.6.18-274.el5.gzSystem.map-2.6.18-274.el5vmlinuz-2.6.18-274.el5
[root@DB-Serverboot]#cdgrub/
[root@DB-Servergrub]#ls
device.mapfat_stage1_5grub.confjfs_stage1_5minix_stage1_5splash.xpm.gzstage2vstafs_stage1_5
e2fs_stage1_5ffs_stage1_5iso9660_stage1_5menu.lstreiserfs_stage1_5stage1ufs2_stage1_5xfs_stage1_5
[root@DB-Servergrub]#moregrub.conf
#grub.confgeneratedbyanaconda
#
#Notethatyoudonothavetorerungrubaftermakingchangestothisfile
#NOTICE:Youhavea/bootpartition.Thismeansthat
#allkernelandinitrdpathsarerelativeto/boot/,eg.
#root(hd0,0)
#kernel/vmlinuz-versionroroot=/dev/sda2
#initrd/initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
titleRedHatEnterpriseLinuxServer(2.6.18-274.el5)
root(hd0,0)
kernel/vmlinuz-2.6.18-274.el5roroot=LABEL=/rhgbquiet
initrd/initrd-2.6.18-274.el5.imgfastboot
Youhavenewmailin/var/spool/mail/root
[root@DB-Servergrub]#

kernel/vmlinuz-2.6.18-274.el5roroot=LABEL=/rhgbquiet

initrd/initrd-2.6.18-274.el5.imgfastboot如何強制下次重啟磁盤自檢?

如何強制系統下次root時,進行磁盤自檢?

方法1:使用tune2fs調整Maximummountcount和Checkinterval的值,使其下次重啟時滿足磁盤自檢。

方法2:關於這個,在RHEL中,你可以在/etc/rc.sysinit中看到如下代碼(DebianorUbuntuLinux下查看/etc/init.d/checkfs.sh)如下所示:

所以,你只需要創建一個forcefsck文件,下次重啟時,就能強制其進行磁盤自檢。

[root@DB-Server/]#touch/forcefsck
[root@DB-Server/]#reboot
Broadcastmessagefromroot(pts/1)(MonJul414:33:592016):
ThesystemisgoingdownforrebootNOW!
重啟過程中,你就會看到磁盤自檢。重啟後,你會發現剛才生成的forcefsck文件已經不見了。

方法3:使用shutdown相關參數強制磁盤自檢

[root@DB-Server/]#manshutdown

#shutdown-rFnow

參考資料:

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/

Copyright © Linux教程網 All Rights Reserved