Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

VMware 虛擬機內核編譯實戰:VFS 掛載失敗、initrd 缺失與 SCSI 驅動內建排查全記錄

編輯:Linux 教學

環境與目標

  • 平台:VMware Workstation / Fusion
  • 客體 OS:Debian (採用 make-kpkg 打包流程)
  • 內核版本:2.6.18 (kernel.org 官方源碼)
  • 目標:編譯自定內核並成功啟動,作為驅動開發測試環境

第一次嘗試:直接編譯安裝


make menuconfig
make-kpkg --revision=1.0 kernel-image
dpkg -i ../linux-image-2.6.18_1.0_*.deb

啟動報錯


VFS: Cannot open root device "sda" or unknown-block(0,0)
Please append a correct "root=" boot option
Kernel panic: VFS: Unable to mount root fs on unknown-block(0,0)

原因分析與修正

1. 缺少 SCSI 磁碟驅動 (VMware 虛擬磁碟呈現為 SCSI)

VMware 預設虛擬控制器為 BusLogic / LSI Logic SCSI。內核必須內建對應低層驅動:


Device Drivers → SCSI device support → SCSI low-level drivers
  [*] BusLogic SCSI support
  [*] LSI Logic (Symbios Logic) SCSI support

2. 根檔案系統驅動未內建

使用 ext3,需設為 Y:


File systems → Ext3 journalling file system support [*]

3. 第二次嘗試:內建上述驅動後重新打包


make-kpkg --revision=1.0 kernel-image
dpkg -i ...

結果:同樣 Kernel Panic

驅動已內建,但仍無法掛載根裝置。推測 initramfs 缺失或裝置命名問題。

關鍵突破:啟用 initrd


make-kpkg --revision=1.0 kernel-image --initrd
dpkg -i ...

成功啟動!

加上 --initrd 參數後,make-kpkg 自動產生包含必要模組的 initrd.img,GRUB 佈建時同步更新,內核啟動階段由 initrd 載入 SCSI/檔案系統模組並掛載真實根分區。

深度複盤:為什麼驅動內建仍需 initrd?

  1. Debian 內核打包機制:make-kpkg 預設不生成 initrd,假設所有啟動必需驅動皆內建。但實際上,SCSI 中層、SD 模組、磁碟分區探測 等輔助代碼若為模組,仍需 initrd 載入。
  2. 模組依賴鏈:即使 CONFIG_SCSI_BUSLOGIC=y,CONFIG_SCSI_MOD、CONFIG_SD_MOD、CONFIG_BLK_DEV_SD 若為 M,內核核心無法自動載入,必須由 initrd 的 init 腳本 modprobe 載入。
  3. 裝置節點建立:initrd 中的 udev 負責建立 /dev/sda* 節點,內核直接掛載需正確節點。

最佳實踐總結

項目建議設定
虛擬磁碟控制器LSI Logic SAS (較新相容性佳)
SCSI 低層驅動CONFIG_SCSI_BUSLOGIC=y / CONFIG_SCSI_LSILOGIC=y
SCSI 中層/磁碟CONFIG_SCSI_MOD=y CONFIG_SD_MOD=y CONFIG_BLK_DEV_SD=y
根檔案系統CONFIG_EXT3_FS=y (或 ext4/xfs)
打包命令make-kpkg --initrd --revision=custom kernel-image
GRUB 更新dpkg 安裝自動執行 update-grub

延伸學習:initramfs 結構與自定

  • Debian 使用 initramfs-tools,配置 /etc/initramfs-tools/modules 強制包含模組。
  • 查看 initrd 內容:lsinitramfs /boot/initrd.img-2.6.18-custom。
  • 手動重建:update-initramfs -c -k 2.6.18-custom。

結語

VMware 環境編譯內核,「驅動內建 + 生成 initrd」 才是穩妥組合。單純內建驅動在模組化架構下往往不足,initrd 承擔了模組載入、裝置節點建立、根分區切換等關鍵任務。理解 initrd 機制,是內核定製與驅動開發的必修課。

Copyright © Windows教程網 All Rights Reserved