[心得] Linux 上處理壞軌硬碟的兩三事

看板Linux作者 (Rex Tsai)時間14年前 (2011/05/30 17:37), 編輯推噓8(800)
留言8則, 8人參與, 最新討論串1/1
網頁: http://people.debian.org.tw/~chihchun/2011/05/30/manage-bad-blocks-on-linux/ [2]Linux 上處理壞軌硬碟的兩三事 有養動物園或是管理伺服器的人,難免都會碰到一些硬碟故障的問題。這篇文章略為整理 一些在 Linux 上處理壞軌硬體的一些軟體知識與技巧。針對讀者為中等程度以上的使用者 ,文中僅僅提供參考資料,不說明實際操作或指令明細,請自行參考相關文件。作者以最 大善意盡力提供參考資訊,但文中如有疏漏或錯誤造成資料遺失,作者不負任何責任。 文中說明均以 Linux [3]Extended file system 為主,操作平臺是 [4]Debian GNU/Linux . 所提及工具授權均為 [5]FLOSS 。假設錯誤硬碟為 /dev/sdb,所有指令需要以 root 執 行。 章節結構 ? 錯誤類型。 ? 壞軌測試。 ? 預先偵測。 ? 備份與資料回復。 ? 排除壞軌磁區。 ? 送修與資料清除。 = 錯誤類型 = 這裡討論的硬碟資料損毀,不外乎物理性傷害,摔落、靜電、停電、過熱、原件老化等等 。這些傷害會造成邏輯性 (logical error) 上或物理 (physical error) 性的錯誤。 所謂的邏輯性錯誤,就是硬碟離線時,暫存資料來不及寫入,導致資料異常。大部分的檔 案系統都會有一個檔案系統狀態 (Filesystem state) 的值,若是上次使用未順利 umount ,下次掛載時就會提示要求作 [6]fsck 檢查。如果是[7]日誌式檔案系統如 [8]ext3/[9] ext4,則會試著回復未完成的操作。這種錯誤偶爾會破壞檔案,fsck/e2fsck 可能會將錯 誤檔案移到 lost+found 目錄下,造成困擾,但問題不大。 物理性錯誤指的是硬體磁盤受到損壞,該段[10]磁區無法讀取,也就是[11]壞軌。肯定導 致資料遺失,最常見的是系統吐出 I/O Error,無法讀取該檔案。實際的症狀是[12]讀取 變慢、聽到 [13]Spin-up 時的卡卡聲([14]Clicking sound),甚至是系統當機等問題。 當壞軌開始產生時,通常代表硬碟壽命將至,得趕快開始更換健康新硬碟。這篇文章主要 討論的是壞軌這種物理性錯誤的處理辦法。 = 壞軌測試 = 最基本的測試方法是利用 [15]badblocks (8) 來檢測硬碟是否存在壞軌,這個指令不分檔 案系統。若是含有重要資料的硬碟,應該先作預設的非破壞性唯讀測試 (non-destructive read-only test),以避免寫入測試時異常,造成磁碟重置離線。 badblocks -vs /dev/sdb 上述只要一發現壞軌,請第一時間拔下備份吧。若是可承受資料損失,可以使用 “-n” 的 非破壞性寫入式測試。這基本上會嘗試寫入資料,再三確認硬碟運作正常。 badblocks -nvs /dev/sdb = 預先偵測 = 不過 badblocks 需要在離線時才能執行。很多時候,伺服器是無法忍受長時間的離線檢查 的,必須要能夠線上檢測硬碟健康狀況才行。目前市面所有 [16]ATA 硬碟都已經支援 [17]S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology), S.M.A.R.T.? 基本上提供一個界面讓管理者可以查詢幾個重要的[18]參數。 透過讀取這些參數,管理者能夠預先警覺[19]硬碟錯誤。根據 Google 的調查十萬顆硬碟 後的[20]研究報告 ([21]Failure Trends in a Large Disk Drive Population), S.M.A.R.T. 的數值的確反應硬碟錯誤率 – 六十天內,出現 S.M.A.R.T. 掃描錯誤的硬碟 是其他硬碟的 39 倍高。 在 Linux 上,可以使用 [22]smartmontools 中的 smartctl (8) 來查詢硬碟的 S.M.A.R.T. 狀態。指令如下 smartctl --attributes /dev/sdb 根據 Google 的研究以及 [23]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 的設定可以參考 [24]檢測硬碟狀態 – smartmontools 一文。S.M.A.R.T. 也支援 Self-Test,可以讓硬碟作效能與功能檢查,詳 情請見 smartctl (8). = 備份與資料回復 = 在發現壞軌之際,應該第一優先備份資料。如果還能夠掛起檔案系統,則趕緊利用 [25] rsync, [26]tar 等工具備份檔案。或者利用 [27]dd 將整顆硬碟資料或分割區存檔,未來 置換到新硬碟上 dd if=/dev/sdb of=sdb.img 若要確保資料正確複製,可使用 [28]dcfldd 來複製,它會邊複製邊作 [29]hash 確保資 料複製正確。不過由於壞軌處常常無法讀取,這會造成 dd 執行失敗。此時可以改用 [30] dd_rescue, 它可以略過無法讀取的部分,儘可能複製其他健康的資料。 不過天有不測風雲,人有旦夕禍福。萬一資料損壞的區域不是資料去,而是重要的系統資 訊,那就需要額外的處置。建議無論任何時候,都不要在壞硬碟上直接操作,永遠先備份 ,再於備份碟上進行處理。以下略談三種情境的可用工具 ? 若不幸你的 [31]MBR 爛掉以至於 partition table 消失,可以嘗試使用[32] [33] testdisk 來猜測各 partition table 的位址,或許可以救回 partition table 資料 。 ? 若是檔案系統的 superblock 損壞,會看到 “can’t read superblock” 訊息,ext? 系列檔案系統通常有多個 superblock 備份,你可以從備份的 superblock 進行復原 工作 ([34]Recover Corrupted Partition From A Bad Superblock, [35]硬?[36] ext2/3文件系?superblock?坏修复??)。 ? 若你手忙腳亂已經把硬碟重新格式化,那麼只能透過 [37]foremost 或 [38]Sleuth Kit 慢慢的揀出硬碟中仍可辨識的檔案。祝你幸運。 = 排除壞軌區域 = 通常如果你的硬碟還在保固範圍,一般原廠均會更換健康的硬碟給你。但如果硬碟已經超 過保固,壞軌硬碟仍堪用,可以用來存一些低度重要的資料。 對於這些壞軌,可以用 [39]sector slipping 或叫做 [40]defect mapping 的機制來跳過 不用。基本上分成兩種形式,一是透過[41]硬碟控制器或是透過檔案系統的 bad block table 來記錄。 傳統的 (1990 年代中期以前的硬碟)是可以進行[42]低階格式化 ([43]Low-Level Formatting) 的,透過原廠的指令來重新定義物理儲存格式、重新定義 [44]CHS、若是硬 碟中有壞軌,可以在低階格式化時避開不用。 由於低階格式化常因為操作錯誤而[45]損壞 (bricked) 硬碟,新款硬碟已經不提供使用者 透過[46]指令作低階格式化的工具。取而代之的是透過 [47]hard-drive defect management 功能,以 [48]Hard drive defects table – P & G Lists 來記錄 [49]Bad Sector Mapping。所謂 Primary defect list 是代表出廠時已經產生缺陷的磁區列表,而 Grown defect list 則是使用中產生缺陷的磁區列表。 當硬碟發現壞軌時,便會自動將該磁區列入 G-List 中,並從備用的磁區中替換。因此現 在已經沒有所謂低階格式化,目前所謂低階格式化大多指的是 Zero-filled,對磁區填入 空值,強迫硬碟重新分配損壞磁區,這也是前述 S.M.A.R.T. 所謂 Reallocated Sectors 。 所以你可以做的事情是對壞軌區域寫入空值,迫使它重置。你可以直接透過一開始的 badblocks 所產生之數值直接覆寫錯誤區塊,範例請見 [50]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 等品牌,則在硬碟上留有 [51]TTL[52]serial communication 的[53]界面, 於是你可以透過終端機連上硬碟韌體,以 [54]Diagnostic Commands 進行一些偵錯以及重 新格式化的動作。 = 送修與資料清除 = ?了避免[55]陳冠希裸照事件發生,送修前最好確保資料已經完全刪除。就如前述所說, 若只是進行快速格式化,硬碟資料也可能被[56]回復。事實上,根據 [57]Peter Gutmann 的在 90 年代的[58]研究,他可以用[59]磁力顯微鏡取得複寫過三十五次以上的磁區資料 。而美國 [60]National Institute of Standards and Technology 的研究 [61] “Guidelines for Media Sanitization” 或[62] Craig Wright, Dave Kleiman, Shyaam Sundhar R.S. 的論文 [63]Overwriting Hard Drive Data: The Great Wiping Controversy 指出,新型硬碟只需要複寫一次即可。各國軍方也因此設定有不同的資料清 除[64]標 [65]準。 總之,無論你的資料有多重要,在 Linux 上,建議使用這兩種 [66]wipe, [67]scrub 指 令之一來抹除資料,或者用 [68]DBAN 開機來清除硬碟資料。 References ? [70]Bad block HOWTO for smartmontools ? [71]The PC Guide: Hard Disk Formatting and Capacity References: [1] http://people.debian.org.tw/~chihchun [2] http://people.debian.org.tw/~chihchun/2011/05/30/manage-bad-blocks-on-linux/ [3] http://en.wikipedia.org/wiki/Extended_file_system [4] http://www.debian.org/ [5] http://en.wikipedia.org/wiki/Free_and_open_source_software [6] http://bit.ly/mcXTDs [7] http://en.wikipedia.org/wiki/Journaling_file_system [8] http://en.wikipedia.org/wiki/Ext3 [9] http://en.wikipedia.org/wiki/Ext4 [10] http://en.wikipedia.org/wiki/Disk_sector [11] http://en.wikipedia.org/wiki/Bad_sector [12] http://en.wikipedia.org/wiki/Access_time [13] http://en.wikipedia.org/wiki/Spin-up [14] http://en.wikipedia.org/wiki/Click_of_death#Hard_disk [15] http://en.wikipedia.org/wiki/Badblocks [16] http://en.wikipedia.org/wiki/Advanced_Technology_Attachment [17] http://en.wikipedia.org/wiki/Self-Monitoring,_Analysis,_and_Reporting_Technology [18] http://bit.ly/mu18nE [19] http://en.wikipedia.org/wiki/Hard_disk_failure [20] http://antbsd.twbbs.org/~ant/wordpress/?p=592 [21] http://research.google.com/pubs/pub32774.html [22] http://sourceforge.net/apps/trac/smartmontools/wiki [23] http://smartmontools.sourceforge.net/badblockhowto.html [24] http://blog.ssh.tw/?p=778 [25] http://en.wikipedia.org/wiki/Rsync [26] http://www.gnu.org/software/tar/ [27] http://en.wikipedia.org/wiki/Dd_%28Unix%29 [28] http://dcfldd.sourceforge.net/ [29] http://en.wikipedia.org/wiki/Hash_function [30] http://www.gnu.org/software/ddrescue/ddrescue.html [31] http://en.wikipedia.org/wiki/Partition_table_%28master_boot_record%29 [32] http://en.wikipedia.org/wiki/Partition_table_%28master_boot_record%29 [33] http://www.cgsecurity.org/wiki/TestDisk [34] http://www.cyberciti.biz/faq/recover-bad-superblock-from-corrupted-partition/ [35] http://blogold.chinaunix.net/u1/48373/showart_1331657.html [36] http://blogold.chinaunix.net/u1/48373/showart_1331657.html [37] http://foremost.sourceforge.net/ [38] http://www.sleuthkit.org/sleuthkit/ [39] http://en.wikipedia.org/wiki/Sector_slipping [40] http://www.pcguide.com/ref/hdd/geom/errorMapping-c.html [41] http://en.wikipedia.org/wiki/Disk_controller [42] http://bit.ly/lDNG2H [43] http://www.pcguide.com/ref/hdd/geom/formatLow-c.html [44] http://en.wikipedia.org/wiki/Cylinder-head-sector [45] http://en.wikipedia.org/wiki/Brick_%28electronics%29 [46] http://en.wikipedia.org/wiki/SCSI_Format_Unit_Command [47] http://www.dataclinic.co.uk/hard-drive-defects-table.htm [48] http://www.dataclinic.co.uk/hard-drive-defects-table.htm [49] http://www.mjm.co.uk/sectorremapping.html [50] http://smartmontools.sourceforge.net/badblockhowto.html [51] http://en.wikipedia.org/wiki/Differential_TTL [52] http://en.wikipedia.org/wiki/Serial_communication [53] http://sites.google.com/site/seagatefix/ [54] http://www.scribd.com/doc/39042992/Seagate-Diagnostic-Command [55] http://bit.ly/locdlW [56] http://en.wikipedia.org/wiki/Data_remanence [57] http://en.wikipedia.org/wiki/Peter_Gutmann_%28computer_scientist%29 [58] http://en.wikipedia.org/wiki/Data_remanence#Feasibility_of_recovering_overwritten_data [59] http://en.wikipedia.org/wiki/Magnetic_force_microscopy [60] http://en.wikipedia.org/wiki/National_Institute_of_Standards_and_Technology [61] http://csrc.nist.gov/publications/nistpubs/800-88/NISTSP800-88_rev1.pdf [62] http://www.springerlink.com/content/?Author=Craig+Wright [63] http://www.springerlink.com/content/408263ql11460147/ [64] http://en.wikipedia.org/wiki/Data_remanence#Standards [65] http://en.wikipedia.org/wiki/Data_erasure#Standards [66] http://lambda-diode.com/software/wipe/ [67] http://code.google.com/p/diskscrub/ [68] http://en.wikipedia.org/wiki/Darik%27s_Boot_and_Nuke [69] http://www.pcguide.com/ref/hdd/geom/format.htm [70] http://smartmontools.sourceforge.net/badblockhowto.html [71] http://www.pcguide.com/ref/hdd/geom/format.htm -- http://people.debian.org.tw/~chihchun/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 112.104.53.166

05/30 17:54, , 1F
大推,這篇文章非常實用
05/30 17:54, 1F

05/30 17:58, , 2F
這真的太有幫助了~ 感謝
05/30 17:58, 2F

05/30 18:09, , 3F
超專業!!
05/30 18:09, 3F

05/30 19:57, , 4F
專業推
05/30 19:57, 4F

05/30 20:38, , 5F
實用,推!
05/30 20:38, 5F

05/31 03:33, , 6F
05/31 03:33, 6F

06/05 14:40, , 7F
太棒了
06/05 14:40, 7F

06/25 10:40, , 8F
推~~
06/25 10:40, 8F
文章代碼(AID): #1DusLH5j (Linux)