延伸 linux 檢查硬碟有無壞軌 這篇文章

這應該是我們當 Linux 重開機後最不想看到的畫面,這可說是睛天霹靂,就壞軌了~啦

2013-03-08 09 59 09

fsck

fsck 是最常看到要用的檢查修復指令,我們可能也會看到有 e2fsck、fsck.ext2、fsck.ext3、fsck.ext4 這些指令,那到底有何不同,是不用想太多啦,我們只要用 fsck 時它會自已去根據檔案系統的不同選擇是要用那個。

fsck /dev/sdb1

fsck 掃描完後的代碼表示意思 (用 echo $? 得知)
0 – No errors
1 – File system errors corrected
2 – System should be rebooted
4 – File system errors left uncorrected
8 – Operational error
16 – Usage or syntax error
32 – Fsck canceled by user request
128 – Shared library error

badblocks

在未出現上面訊息前,我們可以用 badblocks 來掃描測試

非破壞性的唯讀測試

badblocks -sv -b 1024 -c 1 /dev/sdb1 -o badblock1.txt

結果

Checking blocks 0 to 511999
Checking for bad blocks (read-only test): done
Pass completed, 0 bad blocks found.

非破壞性的寫入測試

badblocks -svn -b 1024 -c 1 /dev/sdb1 -o badblock.txt

結果

Checking for bad blocks in non-destructive read-write mode
From block 0 to 511999
Checking for bad blocks (non-destructive read-write test)
Testing with random pattern: done
Pass completed, 0 bad blocks found.

 

參數說明:
-s : 顯示進度
-v : verbose
-b 1024 : block size (預設是 1024) ,用以下查看

tune2fs -l /dev/sdb1 | grep 'Block size:'
或
dumpe2fs   /dev/sdb1 | grep 'Block size:'

-c 1 : 同一磁區檢查 1 次讀不到就判定是 badblock
-o badblock.txt : 將壞軌資訊輸出成檔案

另外 badblocks 可以執行多次,然後合併輸出的檔案為一檔

cat badblock1.txt badblock2.txt badblock3.txt | sort | uniq > badblock.final.txt

那如果我用 -c3 跟掃了三次的作法呢,我尚未測試比較過

mke2fs

如果硬碟裡的資料不要了,就可以拿 badblocks 掃出來有壞軌的檔案交給 mke2fs 重新格式化,隱藏/標記壞軌

mk2efs -j -l badblocks.final.txt /dev/sdb1

參數說明 :
-j : 格式化為有 journal 功能
-l badblock.final.txt : 標記有壞軌的地方,根據 badblocks 掃出來的

mke2fs  格式化時也能檢查壞軌,它會用 (badblocks -b 1024 -X -s /dev/sdb1 511999 檢查)

mke2fs -cv -j /dev/sdb1

-c : 檢查壞軌,如果 -c -c 則會進行較完整一點的檢查
-v : verbose

e2fsck

e2fsck 拿 badblock 檔標記壞軌

e2fsck -f -y -l badblock.final.txt /dev/sdb1

參數說明 :
-f : force
-y : yes
-l : 拿 badblocks 掃的檔

e2fsck 也可以檢查壞軌,並標記

e2fsck -cDfty -C 0 /dev/sdb1

-c : 跑 badblocks 指令,如果 -c -c 才是用非破壞性的檢查
-D : Optimize directories in filesystem
-f : Force checking even if the file system seems clean
-t : Print timing statistics for e2fsck
-y : Assume an answer of ‘yes’
-C 0 : print a completion bar

smartmontools (SMART Health Status)

http://smartmontools.sourceforge.net/badblockhowto.html

隨時監控硬碟狀態

Superblock

如果遇到

/dev/sda2: Input/output error
mount: /dev/sda2: can’t read superblock

可以參考 Linux: Recover Corrupted Partition From A Bad Superblock

dumpe2fs /dev/sda2 | grep superblock
Primary superblock at 0, Group descriptors at 1-6
Backup superblock at 32768, Group descriptors at 32769-32774
Backup superblock at 98304, Group descriptors at 98305-98310
fsck -b 32768 /dev/sda2

dd

用 dd 來備份磁區

dd if=/dev/sdb1 of /dev/sdc1
or
dd if=/dev/sdb1 of=/path/file.image

如果 dd 不行備,就用 dd_rescue (跳過壞軌)

debugfs

查看 block 是否被標記了

debugfs /dev/sdb1
debugfs: testb 1233
Block 1233 marked in use

 

參考來源

http://people.debian.org.tw/~chihchun/2011/05/30/manage-bad-blocks-on-linux/
http://moogoo78.blogspot.tw/2011/10/linux.html
http://talk.away.com.tw/main/modules/weblog/details.php?blog_id=156
http://jamyy.dyndns.org/blog/2013/01/4415.html

 

Related posts 相關文章
linux 的 dd + nc 線上備份主機硬碟
More...
Linux 測試硬碟讀寫速度
More...
Linux 指令 dd – 可以產生大檔及備份硬碟磁區
More...
Linux 發生 Journal has aborted 錯訊,是壞軌嗎!?
More...

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。