Try   HackMD

SOPs: How to repair a broken Superblock

Issue description

執行環境: Zorin OS 17.1
Kernel: 6.5.0-44-generic

在OS底下進行實驗時,不慎將主要硬碟的Superblock(超級區塊)塗改,導致無法讀取,進而使得系統當機,無法進入OS

Root cause analysis

文件系統(filesystem)分區(partition)結構如下:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • Bootblock
    (啟動區塊,放置開機管理檔;一般來說就算該分區不裝有OS一樣會保留這個block)
  • Superblock
    (超級區塊,紀錄整個文件系統的相關資訊,包含inode和block的總數,已使用量,剩餘量等)
  • inode
    (index node,目錄節點,紀錄文件系統中,各項文件的修改時間、權限、實際存放的資料區塊位置等)
  • Datablocks
    (資料區塊,實際資料存放在此)
    根據上圖結構可知,一旦失去superblock,便會因為無法得知整個分區的相關資訊,進而無法使用分區內部資料

Solution

使用備份分割表修復

因為是在ubuntu下(文件系統:ext series),所以使用"e2fs"指令來查找備份分割表並還原至主要分割表
Command:sudo mke2fs -n /dev/"target device"/

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

由上圖可知,superblock分別備份在以下區塊:

  • 32768
  • 98304
  • 163840
  • 229376
  • 294912
  • 819200
  • 884736
  • 1605632
  • 2654208
  • 4096000

任選其中一塊,還原至superblock即可:
Command:e2fsck -f -b 98304 -y /dev/"target device"/

最後執行硬碟檢查:
Command: e2fsck -c -f -y /dev/"target divice"/
只要回報錯誤"error"是0,即是修復成功,如不成功,再使用其他block做還原即可

Ref.

can NOT boot-in OS because broken superblock
Repair a broken Superblock
repair superblock
Detatils about superblock & filesystem
How linux OS excute filesystem
disk physics manual
ext filesystem