茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671411 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00017.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]fsck 指令範例教學,Linux 檢查與修復硬碟檔案系統的工具

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]fsck 指令範例教學,Linux 檢查與修復硬碟檔案系統的工具

fsck 指令範例教學,Linux 檢查與修復硬碟檔案系統的工具

Linux 的 fsck 指令可以用來檢測或修復檔案系統,這裡蒐集了許多實用的範例。

fsck 是 Linux 系統中常會使用到的硬碟檢測工具,它可以檢查檔案系統是否有錯誤,並且嘗試修復它,通常 Linux 系統每間隔一段時間就會自動使用 fsck 檢查一次檔案系統,而在平常如果檔案系統出問題時,管理者也會需要用到這個指令來處理這類的問題。

以下是各種 fsck 的使用範例,透過這些例子可以很快了解如何使用 fsck 來檢查自己的硬碟資料,並且修正錯誤。

在執行 fsck 時,請先確定硬碟是處於沒有被掛載(mount)的狀態下,以避免資料損毀的風險。

磁碟分割區的檔案系統檢查

首先以 parted 察看磁碟分割表:

parted /dev/sda 'print'

輸出為


Number Start End Size Type File system Flags
1 1049kB 106MB 105MB primary fat16 diag
2 106MB 15.8GB 15.7GB primary ntfs boot
3 15.8GB 266GB 251GB primary ntfs
4 266GB 500GB 234GB extended
5 266GB 466GB 200GB logical ext4
6 467GB 486GB 18.3GB logical ext2
7 487GB 499GB 12.0GB logical fat32 lba

若要檢查 /dev/sda6 的檔案系統,則執行:

fsck /dev/sda6

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 95/2240224 files, 3793506/4476416 blocks

下面這些是各種 fsck 的傳回值所代表的意義:

  • 0:沒有錯誤。
  • 1:檔案系統錯誤已修正。
  • 2:系統需要重新開機。
  • 4:檔案系統錯誤未修正。
  • 8:操作錯誤。
  • 16:使用方式錯誤。
  • 32:使用者取消執行。
  • 128:共享函式庫(shared library)錯誤。

不同的檔案系統

fsck 在執行時會依照不同的檔案系統,呼叫對應的內部檢查指令,這些對應的內部指令一般都放在 /sbin 下面。


cd /sbin
ls fsck*

輸出為


fsck fsck.ext2 fsck.ext4 fsck.minix fsck.nfs
fsck.cramfs fsck.ext3 fsck.ext4dev fsck.msdos fsck.vfat

從這些 fsck 的內部指令就可以看出來它支援哪些檔案系統,如果遇到不支援的檔案系統,它就會出現錯誤訊息,例如 ntfs:

fsck /dev/sda2

輸出為


fsck from util-linux 2.20.1
fsck: fsck.ntfs: not found
fsck: error 2 while executing fsck.ntfs for /dev/sda2

一次檢查所有的檔案系統


如果想要一次檢查系統中所有的檔案系統,可以在執行 fsck 時加上 -A 參數,這樣它就會依照 /etc/fstab 中的 fs_passno 所指定的順序來檢查(如果 fs_passno 的值為 0 則跳過)。

下面這個是 /etc/fstab 的內容:


##
proc /proc proc nodev,noexec,nosuid 0 0
## / was on /dev/sda5 during installation
/dev/sda5 / ext4 errors=remount-ro 0 1
## /mydata was on /dev/sda6 during installation
/dev/sda6 /mydata ext2 defaults 0 2
## /backup was on /dev/sda7 during installation
/dev/sda7 /backup vfat defaults 2 3

其中最後一個欄位就是 fs_passno,當執行

fsck -A

的時候,就會依照 fs_passno 數值的順序來檢查。在檢查所有的檔案系統時,建議加上 -R 參數,跳過根目錄所在的檔案系統。

fsck -AR -y

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 95/2240224 files, 3793506/4476416 blocks
dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN
/dev/sda7: 8 files, 50/1463400 clusters

這裡的 -y 參數,請參考下面的教學。

檢查指定類型的檔案系統

在使用 -A 參數時,可以配合 -t 參數,指定要檢查的檔案系統類型,在這種情況下, fsck 就只會針對該類型的檔案系統進行檢查,如果不是屬於該類型的檔案系統則會被忽略。

如果只要檢查 ext2 這個類型的檔案系統,則執行:

fsck -AR -t ext2 -y

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 11/2240224 files, 70327/4476416 blocks

由於所有的分割區中,只有 /dev/sda6 是屬於 ext2 的檔案系統,所以 fsck 只會檢查這個分割區。

如果要排除某檔案系統類型,可以使用 no 來排除指定的類型,例如:

fsck -AR -t noext2 -y

就會排除 ext2 這個類型,輸出為


fsck from util-linux 2.20.1
dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN
/dev/sda7: 0 files, 1/1463400 clusters

跳過已掛載的檔案系統


使用 -M 參數可以讓 fsck 自動跳過系統上已經掛載的檔案系統,這樣可以避免不小心處理到已經掛載的分割區。

我們可以使用

mount | grep "/dev/sd*"

查看系統上各個檔案系統掛載的狀況



/dev/sda5 on / type ext4 (rw,errors=remount-ro)
/dev/sda6 on /mydata type ext2 (rw)
/dev/sda7 on /backup type vfat (rw)

根據這裡的資訊, /dev/sda7 是一個已經被掛載的檔案系統,如果這時候對這個分割區執行

fsck -M /dev/sda7

就不會有任何動作,而其傳回值為 0

echo $?

輸出為

0

避免輸出標題資訊

預設的狀況下, fsck 在一開始執行的時候,會輸出一些基本的訊息,例如

fsck from util-linux 2.20.1

如果要避免這類的輸出,可以加上 -T 參數:

fsck -TAR

輸出為


e2fsck 1.42 (29-Nov-2011)
/dev/sda6 is mounted. e2fsck: Cannot continue, aborting.
dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN
/dev/sda7: 8 files, 50/1463400 clusters

強制檢查 Clean 的檔案系統


每當檔案系統被掛載成讀寫模式(rw)時,檔案系統狀態會被設定為 not-clean,而在檔案系統被成功卸載後,其狀態就會被設定回 clean。所以檔案系統沒有被成功卸載或者運作中途系統被中斷等,狀態就會維持在 not-clean,當檔案系統下次被檢查時,就可以由檔案系統狀態得知檔案系統是否有被正常卸載。

在預設的狀況下, fsck 只會對狀態為 not-clean 的檔案系統進行檢查,如果碰到狀態為 clean 的檔案系統,就會很快地跳過:

fsck /dev/sda6

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6: clean, 95/2240224 files, 3793503/4476416 blocks

如果想要讓它檢查 clean 的檔案系統,可以加上 -f 參數:

fsck -f /dev/sda6

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda6: 95/2240224 files (7.4% non-contiguous), 3793503/4476416 blocks

修正所有偵測到的錯誤


假設 /dev/sda6 中有一些檔案出問題:


mount /dev/sda6 /mydata
cd /mydata
ls -li

輸出為


ls: cannot access test: Input/output error
total 72
49061 -rw-r--r-- 1 root root 8 Aug 21 21:50 1
49058 -rw-r--r-- 1 root root 36864 Aug 21 21:24 file_with_holes
49057 -rw-r--r-- 1 root root 8192 Aug 21 21:23 fwh
11 drwxr-xr-x 2 root root 49152 Aug 19 00:29 lost+found
2060353 ?rwSr-S-wT 16 root root 4096 Aug 21 21:11 Movies
? -????????? ? ? ? ? ? test

這裡的 Moviestest 的檔案屬性有問題。

如果加上 -y 參數,則在偵測到錯誤時,所有的問題都會以 yes 回答,進行修復:

fsck -y /dev/sda6

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inode 2060353 is a unknown file type with mode 0137642 but it looks
like it is really a directory.
Fix? yes
Pass 2: Checking directory structure
Entry 'test' in / (2) has deleted/unused inode 49059. Clear? yes
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda6: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda6: 96/2240224 files (7.3% non-contiguous), 3793508/4476416 blocks

不要修復任何錯誤


如果不想修復任何的問題,只想得到問題的資訊,可以加上 -n,讓所有的問題都以 no 回答。

假設 /dev/sda6 的資料有些問題:


mount /dev/sda6 /mydata
cd /mydata
ls -lrt

輸出為


total 64
drwxr-xr-x 2 root root 49152 Aug 19 00:29 lost+found
?--xrwx-wx 16 root root 4096 Aug 21 21:11 Movies
?-----x-wx 1 root root 8192 Aug 21 21:23 fwh
-rw-r--r-- 1 root root 36864 Aug 21 21:24 file_with_holes
-rw-r--r-- 1 root root 8 Aug 21 21:50 1

如果只想要得到問題的資訊,而不想直接修復,則執行:

fsck -n /dev/sda6

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inode 2060353 is a unknown file type with mode 0173 but it looks
like it is really a directory.
Fix? no
Inode 2060353, i_blocks is 8, should be 0. Fix? no
Pass 2: Checking directory structure
Inode 2060353 (/Movies) has invalid mode (0173).
Clear? no
Inode 49057 (/fwh) has invalid mode (013).
Clear? no
Entry 'fwh' in / (2) has an incorrect filetype (was 1, should be 0).
Fix? no
Pass 3: Checking directory connectivity
Unconnected directory inode 65409 (???)
Connect to /lost+found? no
'..' in ... (65409) is ??? (2060353), should be (0).
Fix? no
Unconnected directory inode 2076736 (???)
Connect to /lost+found? no
Pass 4: Checking reference counts
Inode 2 ref count is 4, should be 3. Fix? no
Inode 65409 ref count is 3, should be 2. Fix? no
Inode 2060353 ref count is 16, should be 15. Fix? no
Unattached inode 2060354
Connect to /lost+found? no
Pass 5: Checking group summary information
Block bitmap differences: -(164356--164357) -4149248
Fix? no
Directories count wrong for group #126 (1, counted=0).
Fix? no
/dev/sda6: ********** WARNING: Filesystem still has errors **********
/dev/sda6: 96/2240224 files (7.3% non-contiguous), 3793508/4476416 blocks

自動修復錯誤


如果要讓 fsck 自動修復偵測到的問題,可以加上 -a 參數:

fsck -a -AR

這樣就會自動修復那些比較沒有風險的問題。如果遇到需要管理者確認的問題, fsck 就會輸出這樣的訊息:

fsck -a /dev/sda6

輸出為


fsck from util-linux 2.20.1
/dev/sda6 contains a file system with errors, check forced.
/dev/sda6: Inode 2060353 is a unknown file type with mode 0173 but it looks
like it is really a directory.
/dev/sda6: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.
(i.e., without -a or -p options)

並且傳回 4

echo $?

輸出為

4

如果遇到這樣的狀況,可以使用上面介紹的 -y 參數:

fsck -y /dev/sda6

輸出為


fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/sda6 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Inode 2060353 is a unknown file type with mode 0173 but it looks
like it is really a directory.
Fix? yes
Pass 2: Checking directory structure
Inode 49057 (/fwh) has invalid mode (013).
Clear? yes
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences: -(164356--164357)
Fix? yes
Free blocks count wrong for group #5 (0, counted=2).
Fix? yes
Free blocks count wrong (682908, counted=682910).
Fix? yes
/dev/sda6: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda6: 95/2240224 files (7.4% non-contiguous), 3793506/4476416 blocks

參考資料: The Geek StuffDebian WikiMTE


原文出處:fsck 指令範例教學,Linux 檢查與修復硬碟檔案系統的工具 - G. T. Wang
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]用 fsck 檢查、修復 linux 檔案系統
用fsck檢查、修復linux檔案系統

今天突然發現開機時會顯示正在fsck filesystem,隨著錯誤訊息說明,查看該檔案,
Routine check of drives:/dev/sda1...

Press ESC to skip                 10%(stage 5/5,)

進入終端機裡,
frle@frle-desktop:~$ cat /var/log/fsck/checkfs
Log of fsck -C3 -R -A -a
Sat Oct 25 08:43:53 2008

fsck 1.40.8 (13-Mar-2008)
/dev/sda5 contains a file system with errors, check forced.
/dev/sda5: Unattached inode 147721

/dev/sda5: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.
    (i.e., without -a or -p options)
fsck died with exit status 4

Sat Oct 25 08:43:59 2008
------------------------------------------------------------------------

frle@frle-desktop:~$ fsck --help
fsck 1.40.8 (13-Mar-2008)
fsck.ext3: 不適用的選項 -- h
Usage: fsck.ext3 [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]
        [-I inode_buffer_blocks] [-P process_inode_size]
        [-l|-L bad_blocks_file] [-C fd] [-j external_journal]
        [-E extended-options] device

Emergency help:
 -p                   Automatic repair (no questions)
 -n                   Make no changes to the filesystem
 -y                   Assume "yes" to all questions
 -c                   Check for bad blocks and add them to the badblock list
 -f                   Force checking even if filesystem is marked clean
 -v                   Be verbose
 -b superblock        Use alternative superblock
 -B blocksize         Force blocksize when looking for superblock
 -j external_journal  Set location of the external journal
 -l bad_blocks_file   Add to badblocks list
 -L bad_blocks_file   Set badblocks list

---------------------------------------------------------------------------

-p 自動修復檔案系統,不回答任何問題
-n 不要真正執行,只看執行時會做那些動做
-y 自動回答 "yes" 給每次詢問
-c 檢查出壞區塊並且紀錄在清單中
-f 連被標示的區塊都檢查

硬碟有問題!趕快用 fsck 指令修復,※僅管理者的身份才有權限能執行喔,
frle@frle-desktop:~$ sudo fsck -c /dev/sda5
[sudo] password for frle:
fsck 1.40.8 (13-Mar-2008)
e2fsck 1.40.8 (13-Mar-2008)
/dev/sda5 is mounted.

WARNING!!!  Running e2fsck on a mounted filesystem may cause
SEVERE filesystem damage.

Do you really want to continue (y/n)? yes

/dev/sda5: recovering journal
Checking for bad blocks (read-only test): done
/dev/sda5: Updating bad block inode.
Pass 1: Checking inodes, blocks, and sizes
Inode 261125, i_size is 1507331, should be 1523712.  Fix? yes

Inode 261125, i_blocks is 2960, should be 2984.  Fix? yes

Pass 2: Checking directory structure
Entry '4E2F0A0Fd01' in /frle/.mozilla/firefox/3o0bvab4.default/Cache (261123)
has deleted/unused inode 261409.  Clear? yes

Entry '38126154d01' in /frle/.mozilla/firefox/3o0bvab4.default/Cache (261123)
has deleted/unused inode 261410.  Clear? yes

Entry '785B9565d01' in /frle/.mozilla/firefox/3o0bvab4.default/Cache (261123)
has deleted/unused inode 261408.  Clear? yes

Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Unattached inode 147721
Connect to /lost+found? yes

Inode 147721 ref count is 2, should be 1.  Fix? yes

Unattached inode 148268
Connect to /lost+found? yes

Inode 148268 ref count is 2, should be 1.  Fix? yes

Unattached inode 148275
Connect to /lost+found? yes

Inode 148275 ref count is 2, should be 1.  Fix? yes

Unattached inode 148277
Connect to /lost+found? yes

Inode 148277 ref count is 2, should be 1.  Fix? yes

Unattached inode 148279
Connect to /lost+found? yes

Inode 148279 ref count is 2, should be 1.  Fix? yes

Unattached inode 148287
Connect to /lost+found? yes

Inode 148287 ref count is 2, should be 1.  Fix? yes

Pass 5: Checking group summary information
Block bitmap differences:  -(590351--590352) -591886 +591890 +(591933--591935)
+(591937--591938) +(591944--591946) -592443 -592445 -594424 -596009 -(596016--596018)
-(596026--596027) -(596029--596035) -(596037--596051) -(596064--596079) -(596081--596101)
-(596105--596139) -(596259--596260) -(596272--596279) -(596305--596310) -(596354--596355)
-(597220--597223) -597231 -(597773--598015) -598053 -598111 -598129 -600086 -602162
-602164 -(602166--602169) -(602175--602185) -602420 -602457 -(602464--602480)
-(602752--602755) -(602757--602763) -(602771--602776) -(602790--602801) -602803
-(602806--602817) -(602842--602873) -(602906--602969) -(603034--603161) -(603290--603545)
-(603802--604159) -604211 -(604220--604221) -(604232--604234) -(604237--604246)
-604249 -(604254--604257) -(604259--604313) -(604763--604766) -(604775--604790)
-(604807--604838) -(604871--604934) -605012 -(605127--605254) -(605511--605766)
-(606290--606297) -(606302--606305) -606309 -(606314--606324) -(608339--608340)
-608343 -(608346--608348) -(608350--608354) -(608356--608385) -608527 -610313 -610315
-610320 -(611399--611401) -(611406--611408) -(611411--611413) -(611438--611444)
-613319 -613327 -613379 -615027 -615029 -616132 -616485 -(616533--616534) -616536
-(616538--616539) -(616541--616542) -616649 -621187 +(621189--621190) -622192
-(622194--622197) +(622198--622206) +(622218--622224) +622242 +(622244--622247)
+622249 -(622250--622268) -630110 -630477 -(635972--635973) -(636662--636663)
-637175 -(638197--638198) -638756 -(638804--638805) -638808 -638819 -638856 -638861
-638867 -638883 -638998 -(639011--639015) -639017 -(639019--639022) -639024 -(639101--639104)
-(639111--639112) -639120 -639138 -639217 -639261 -(639323--639324) -639329 -639331
-(639333--639335) -(639338--639339) -(639341--639342) -639346 -(639840--639844)
-639852 -(639854--639855) -(639857--639861) -639863 -639873 -(639879--639885) -(639911--639949)
-(639952--640122) -(640128--640378) -640540 -640543 -(640856--641274) -(641276--641469)
-(641836--642187) -(642196--642211) -(642260--642291) -(642356--642419) -(642548--642675)
-(642936--643189) -(643700--644211) -(644600--645151) -(645162--645170) -(645177--645270)
-(645299--645300) -(645323--645325) -(645328--645357) -(645494--648237) -(653373--653884)
+(1050633--1050670) +(1066497--1066500) +(1066508--1066512) +(1066633--1066640) +(1066675--1066701)
+(1066714--1066736) +(1066746--1066753) +1162947 +1162960 +1162963 +1162969 -1179907
-1179928 -(1179995--1179997) -1179999 -(1180001--1180540) -(1180544--1180764) -(1180772--1181695)
-(1181959--1181960) -(1183574--1183743) -(1184504--1184801) -(1191088--1191089) -1200384
Fix? yes

Free blocks count wrong for group #15 (32510, counted=32511).
Fix? yes

Free blocks count wrong for group #18 (11968, counted=13929).
Fix? yes

Free blocks count wrong for group #19 (6401, counted=12855).
Fix? yes

Free blocks count wrong for group #20 (13, counted=2189).
Fix? yes

Free blocks count wrong for group #21 (1, counted=1034).
Fix? yes

Free blocks count wrong for group #22 (0, counted=7).
Fix? yes

Free blocks count wrong for group #23 (0, counted=7).
Fix? yes

Free blocks count wrong for group #24 (1, counted=9800).
Fix? yes

Free blocks count wrong for group #25 (45, counted=24003).
Fix? yes

Free blocks count wrong for group #26 (2, counted=5525).
Fix? yes

Free blocks count wrong for group #31 (23717, counted=24140).
Fix? yes

Free blocks count wrong for group #32 (27342, counted=27234).
Fix? yes

Free blocks count wrong for group #33 (31043, counted=32057).
Fix? yes

Free blocks count wrong for group #34 (30951, counted=32502).
Fix? yes

Free blocks count wrong for group #35 (17100, counted=29804).
Fix? yes

Free blocks count wrong for group #36 (29062, counted=31226).
Fix? yes

Free blocks count wrong (1948086, counted=2016753).
Fix? yes

Inode bitmap differences:  -147440 -147481 -147493 -(147495--147502) -(147504--147522)
-147758 -147765 -147856 -147882 -(147901--147902) -(147904--147905) -147907 -147913
-148028 -148079 -148085 -(148105--148106) -148115 -(148153--148156) -148163 -148215
+148220 -148237 +(261403--261407)
Fix? yes

Free inodes count wrong for group #15 (8159, counted=8160).
Fix? yes

Free inodes count wrong for group #18 (6764, counted=6816).
Fix? yes

Free inodes count wrong for group #20 (8071, counted=8081).
Fix? yes

Directories count wrong for group #20 (36, counted=34).
Fix? yes

Free inodes count wrong for group #25 (7992, counted=7991).
Fix? yes

Free inodes count wrong for group #26 (8151, counted=8152).
Fix? yes

Directories count wrong for group #26 (5, counted=4).
Fix? yes

Free inodes count wrong for group #32 (7878, counted=7873).
Fix? yes

Free inodes count wrong (609663, counted=609721).
Fix? yes

/dev/sda5: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sda5: 2279/612000 files (12.0% non-contiguous), 425119/2441872 blocks
yfrle@frle-desktop:~$


記得下次要加 y 的參數,才不會那麼辛苦。

原文出處:用fsck檢查、修復linux檔案系統 @ 一些有的沒的 :: 隨意窩 Xuite日誌
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Linux 上處理壞軌硬碟的兩三事

Linux 上處理壞軌硬碟的兩三事

養動物園或是管理伺服器的人,難免都會碰到一些硬碟故障的問題。這篇文章略為整理一些在 Linux 上處理壞軌硬體的一些軟體知識與技巧。針對讀者為中等程度以上的使用者,文中僅僅提供參考資料,不說明實際操作或指令明細,請自行參考相關文件。作者以最大善意盡力提供參考資訊,但文中如有疏漏或錯誤造成資料遺失,作者不負任何責任。

文中說明均以 Linux Extended file system 為主,操作平臺是 Debian GNU/Linux. 所提及工具授權均為 FLOSS 。假設錯誤硬碟為 /dev/sdb,所有指令需要以 root 執行。

章節結構

  • 錯誤類型。
  • 壞軌測試。
  • 預先偵測。
  • 備份與資料回復。
  • 排除壞軌磁區。
  • 送修與資料清除。

錯誤類型

這裡討論的硬碟資料損毀,不外乎物理性傷害,摔落、靜電、停電、過熱、原件老化等等。這些傷害會造成邏輯性 (logical error) 上或物理 (physical error) 性的錯誤。

所謂的邏輯性錯誤,就是硬碟離線時,暫存資料來不及寫入,導致資料錯誤異常。所幸大部分的檔案系統都會有一個檔案系統狀態 (Filesystem state) 的值,若是最後使用未順利完成卸載 (umount),下次掛載時就會提示要求作
fsck 檢查。如果是 日誌式檔案系統ext3/ ext4,則會試著回復未完成的操作。這種錯誤偶爾會破壞檔案,fsck/e2fsck 可能會將錯誤檔案移到 lost+found 目錄下,造成困擾,但問題不大。

物理性錯誤指的是硬體磁盤受到損壞,該段 磁區無法讀取,也就是 壞軌。肯定導致資料遺失,最常見的是系統吐出 I/O Error,無法讀取該檔案。實際的症狀是 讀取變慢、聽到 Spin-up 時的卡卡聲( Clicking sound),甚至是系統當機等問題。


當壞軌開始產生時,通常代表硬碟壽命將至,得趕快開始更換健康新硬碟。這篇文章主要討論的是壞軌這種物理性錯誤的處理辦法。

壞軌測試

最基本的測試方法是利用 badblocks (8) 來檢測硬碟是否存在壞軌,這個指令不分檔案系統。若是含有重要資料的硬碟,應該先作預設的非破壞性唯讀測試 (non-destructive read-only test),以避免寫入測試時異常,造成磁碟重置離線。

badblocks -vs /dev/sdb

上述只要一發現壞軌,請第一時間拔下備份吧。若是可承受資料損失,可以使用 “-n” 的非破壞性寫入式測試。這基本上會嘗試寫入資料,再三確認硬碟運作正常。

badblocks -nvs /dev/sdb

預先偵測

不過 badblocks 需要在離線時才能執行。很多時候,伺服器是無法忍受長時間的離線檢查的,必須要能夠線上檢測硬碟健康狀況才行。目前市面所有 ATA 硬碟都已經支援 S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology),S.M.A.R.T. 基本上提供一個界面讓管理者可以查詢幾個重要的 參數

透過讀取這些參數,管理者能夠預先警覺
硬碟錯誤。根據 Google 的調查十萬顆硬碟後的 研究報告 ( Failure Trends in a Large Disk Drive Population),S.M.A.R.T. 的數值的確反應硬碟錯誤率 – 六十天內,出現 S.M.A.R.T. 掃描錯誤的硬碟是其他硬碟的 39 倍高。

在 Linux 上,可以使用 smartmontools 中的 smartctl (8) 來查詢硬碟的 S.M.A.R.T. 狀態。指令如下

smartctl --attributes /dev/sdb

根據 Google 的研究以及 Bad block HOWTO for smartmontools 指出,與硬碟失效關係最高的數值是 Reallocated Sectors Count/Reallocations event count, Current Pending Sector Count, Uncorrectable Sector Count。這幾組數據代表硬碟發現該磁區已經損毀,它會重新從備用磁區分配一位置取代該損壞磁區,或磁區已發現問題尚未替換或無法替換。這通常可以視為磁區損耗 (surface wear) 的狀況。當你發現這些值不為 0 時,就該開始準備備份資料並作壞軌檢查了。


除了使用 smartctl 手動檢測外,也可以設定 smarted,讓系統自動觀測 S.M.A.R.T. 狀態,有錯誤會顯示於桌面訊息或發送郵件。smartd 的設定可以參考 檢測硬碟狀態 – smartmontools 一文。S.M.A.R.T. 也支援 Self-Test,可以讓硬碟作效能與功能檢查,詳情請見 smartctl (8).

備份與資料回復

在發現壞軌之際,應該第一優先備份資料。如果還能夠掛起檔案系統,則趕緊利用 rsync, tar 等工具備份檔案。或者利用 dd 將整顆硬碟資料或分割區存檔,未來置換到新硬碟上

dd if=/dev/sdb of=sdb.img

若要確保資料正確複製,可使用 dcfldd 來複製,它會邊複製邊作 hash 確保資料複製正確。不過由於壞軌處常常無法讀取,這會造成 dd 執行失敗。此時可以改用
dd_rescue, 它可以略過無法讀取的部分,儘可能複製其他健康的資料。

不過天有不測風雲,人有旦夕禍福。萬一資料損壞的區域不是資料區域,而是存放重要的系統資訊,那就需要額外的處置。建議無論任何時候,都不要在壞硬碟上直接操作,永遠先備份,再於備份碟上進行處理。以下略談三種情境的可用工具

排除壞軌區域

通常如果你的硬碟還在保固範圍,一般原廠均會更換健康的硬碟給你。但如果硬碟已經超過保固,壞軌硬碟仍堪用,可以用來存一些低度重要的資料。

對於這些壞軌,可以用 sector slipping 或叫做 defect mapping 的機制來跳過不用。基本上分成兩種形式,一是透過 硬碟控制器或是透過檔案系統的 bad block table 來記錄。

傳統的 (1990 年代中期以前的硬碟)是可以進行 低階格式化 (
Low-Level Formatting) 的,透過原廠的指令來重新定義物理儲存格式、重新定義 CHS、若是硬碟中有壞軌,可以在低階格式化時避開不用。

由於低階格式化常因為操作錯誤而 損壞 (bricked) 硬碟,新款硬碟已經不提供使用者透過 指令作低階格式化的工具。取而代之的是透過 hard-drive defect management 功能,以 Hard drive defects table – P & G Lists 來記錄 Bad Sector Mapping。所謂 Primary defect list 是代表出廠時已經產生缺陷的磁區列表,而 Grown defect list 則是使用中產生缺陷的磁區列表。


當硬碟發現壞軌時,便會自動將該磁區列入 G-List 中,並從備用的磁區中替換。因此現在已經沒有所謂低階格式化,目前所謂低階格式化大多指的是 Zero-filled,對磁區填入空值,強迫硬碟重新分配損壞磁區,這也是前述 S.M.A.R.T. 所謂 Reallocated Sectors。

所以你可以做的事情是對壞軌區域寫入空值,迫使它重置。你可以直接透過一開始的 badblocks 所產生之數值直接覆寫錯誤區塊,範例請見 Bad block HOWTO for smartmontools

上述 HOWTO 中詳述了,怎麼計算 e2fsck 吐出來數值,人工計算有點麻煩。以下介紹如何處理利用工具自動處理。

不保留資料

如果確認整顆硬碟資料都不需要保留,可以直接下達一下 dd 指令,重寫整顆硬碟。

dd if=/dev/zero of=/dev/sdb

或者在格式化時下達 -c -c 參數,讓 mkfs.ext3 順便檢查壞軌。

mkfs.ext3 -c -c /dev/sdb1

保留資料

如果硬碟已有資料,並想保留此資料,可以用 badblocks 檢查,並生成 badblocks list,再將 badblocks 吐出的列表,餵給 e2fsck -l.。但是必須確認執行 badblocks 時,指派了正確的 block size,可用 dumpe2fs -h 確認 block size.然後指示給 badblocks。

上述操作有點複雜,比較保險的做法是直接讓 e2fsck 用 badblocks 去作壞軌檢查,指令如下

e2fsck -k -c  -c /dev/sdb1

如此 e2fsck 會將壞軌寫入檔案系統列表中,可以用下述指令查詢目前壞軌列表

dumpe2fs -b /dev/sdb1

其他做法


本節前述可用低階格式化重置硬碟磁區分配,但是新款硬碟已少提供工具程式給終端使用者。有些工具透過反組譯硬碟韌體的方式取得 vendor specific ATA commands。另外,像是 Seagate 等品牌,則在硬碟上留有 TTL serial communication界面,於是你可以透過終端機連上硬碟韌體,以 Diagnostic Commands 進行一些偵錯以及重新格式化的動作。

送修與資料清除

爲了避免 陳冠希裸照事件發生,送修前最好確保資料已經完全刪除。

就如前述所說,若只是進行快速格式化,硬碟資料也可能被 回復。事實上,根據 Peter Gutmann 的在 90 年代的
研究,他可以用 磁力顯微鏡取得複寫過三十五次以上的磁區資料。而美國 National Institute of Standards and Technology 的研究 “Guidelines for Media Sanitization” Craig Wright, Dave Kleiman, Shyaam Sundhar R.S. 的論文
Overwriting Hard Drive Data: The Great Wiping Controversy 指出,新型硬碟只需要複寫一次即可。

爲了達到資料保密措施,各國軍方也因此設定有不同的資料清除 。總之,無論你的資料是否重要,在 Linux 上,建議使用這兩種 wipe, scrub 指令之一來抹除資料,這兩種工具支援上述的各國標準。你也可下載使用 DBAN 製作開機片來清除硬碟資料。

References



原文出處:Linux 上處理壞軌硬碟的兩三事 – Rex's blah blah blah
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|