當 Xen 虛擬機發生異常、無法正常啟動時,系統管理員可透過掛載虛擬磁碟映像檔來進行離線檢查與修復。本文將詳細說明從確認映像檔格式、分析分割區佈局,到掛載特定分割區進行修復的完整操作流程。
首先使用 file 指令查看映像檔的實際格式,確認是否為原始磁碟映像、qcow2、vmdk 等格式,以決定後續處理方式。
root@virt ~]# file rheltest.img
rheltest.img: DOS/MBR boot sector; partition 1 : ID=0x83, active, start-CHS (0x0,32,33), end-CHS (0x3ff,254,63), startsector 63, 2088387 sectors
透過 fdisk -lu 列出映像檔內的分割區表,重點記錄目標分割區的 起始扇區 與 扇區大小(通常為 512 bytes),以計算正確的掛載偏移量(Offset = 起始扇區 × 扇區大小)。
fdisk -lu rheltest.img
Disk rheltest.img: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
rheltest.img1 * 63 2088449 1044193+ 83 Linux
此例中,分割區 1 起始扇區為 63,偏移量 = 63 × 512 = 32256 bytes。
使用 mount 搭配 loop、offset 參數,將映像檔內的特定分割區掛載至目標目錄(如 /mnt/repair),即可對檔案系統進行檢查、修改設定檔、備份資料等操作。
mkdir -p /mnt/repair
mount -o loop,offset=32256 rheltest.img /mnt/repair
掛載成功後,進入 /mnt/repair 即可存取該分割區內的所有檔案。修復完成後,記得先 umount /mnt/repair 卸載,再重新啟動虛擬機驗證。
紅帽官方知識庫文件提供更詳細的說明與進階案例:Red Hat Knowledgebase: DOC-15367