Try   HackMD

Note: after restoring image-file via dd command, remember to repair the disk (main partition table)

Issue description

使用dd command將製作好的OS image file還原至USB時,發現無法使用也無法掛載的情況,如下:

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 →

Root cause analysis

使用gdisk分析

安裝
Command: sudo apt install gdisk

執行
Command: sudo gdisk /dev/"target disk"

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 →

根據圖中訊息可以得到以下訊息:

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 →

重要:備份分割表標頭檔無效用,然而其主要分割標頭檔有效;從主要分割表中重新生成主要分割標頭檔
警告:主要分割表與備份分割表並不一致

判斷是使用dd command進行檔案還原時,會出現主要分割表並未寫入的狀況,導致無法識別分割磁區,進而無法掛載並操作;而備份分割表因為分別寫入多個區塊(block),故能夠成功寫入
主要分割表/備份分割表/主要磁區關係如下:

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 →

生成主要磁區標頭檔流程如下:

Created with Raphaël 2.2.0執行gdisk,發現主要分割標頭檔有效,而備份分割標頭檔無效讀取主要分割標頭檔(舊)->主要分割表讀取備份分割標頭檔(新)->備份分割表比對主要(舊)/備份(新)分割表(CRC)是否一致?自動再生成主要分割標頭檔自動生成/修補後生成主要分割並讀取磁區提示訊息:需要修補主要分割標頭檔/主要分割表(舊->新)yesno

根據以上流程圖可以得知,USB中的主要分割標頭檔/主要分割表,還是維持在執行dd command之前的狀態,所以產生錯誤

Issue recurrence

製作一OS USB,模擬GPT分割表缺損的狀況(sdb)

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 →

使用dd command還原image file

command: dd bs=512 if=/path to image.img/ of=/path to target USB/

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 →

製作完成,因為GPT分割表缺損,無法掛載

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 →

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 →

Solution

使用gdisk修復

執行
command: sudo gdisk /dev/"target disk"

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 →

選擇"r"進入修復選項

輸入"?"提示符可詳列出所有選項

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 →

輸入"b"提示符以選擇"載入備份GPT分割表標頭檔(重建主要分割表)",再輸入"w"寫入指令

選擇"Y"提示符確認

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 →

修復完成,重新啟動後使用

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 →

Result

可辨識和掛載磁區

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 →

Ref.

gdisk command
gdisk command in chinese
Details about GPT & MBR
GUID Partition Table (GPT) Disk Layout
Retrieve data from disk after dd