GRUB2 具備 loopback 模組,能將 ISO 檔案掛載為迴環裝置,直接讀取其中的內核 (vmlinuz) 與初始化映像 (initrd) 進行啟動。這意味著:
ubuntu-24.04-desktop-amd64.iso)# 以 Ubuntu 24.04 為例
mkdir -p /mnt/iso
mount -o loop ubuntu-24.04-desktop-amd64.iso /mnt/iso
ls /mnt/iso/casper/
# 輸出:vmlinuz initrd (或 vmlinuz.efi / initrd.lz 視版本而定)
umount /mnt/iso
記錄關鍵路徑(ISO 內部相對路徑):
/casper/vmlinuz 或 /casper/vmlinuz.efi/casper/initrd 或 /casper/initrd.lz / /casper/initrd.gzsudo fdisk -l
# 或
lsblk -f
GRUB 裝置命名規則:
hd0 = 第一顆磁碟 (sda/nvme0n1)(hd0,1) = 第一顆磁碟第一分區 (sda1)/dev/sda1 → (hd0,1)/dev/nvme0n1p3 → (hd0,3) (nvme 視為 hd0,分區號同 Linux)/dev/sdb2 → (hd1,2)假設 ISO 放在 /home/user/Downloads/ubuntu-24.04.iso,且 /home 是獨立分區 /dev/sda3,則 GRUB 路徑為 (hd0,3)/user/Downloads/ubuntu-24.04.iso。
sudo vim /etc/grub.d/40_custom
在檔案末尾新增(範例為 Ubuntu 24.04 Live ISO):
menuentry "Ubuntu 24.04 Live ISO" {
set isofile="/home/user/Downloads/ubuntu-24.04-desktop-amd64.iso"
# 若 /home 是獨立分區 (hd0,3),去掉 /home:
# set isofile="/user/Downloads/ubuntu-24.04-desktop-amd64.iso"
loopback loop (hd0,3)$isofile
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile quiet splash ---
initrd (loop)/casper/initrd
}
關鍵參數說明:
loopback loop (hdX,Y)$isofile:將 ISO 掛載為 loop 裝置iso-scan/filename=$isofile:告訴內核去哪裡找 ISO 檔(casper 腳本需此參數)boot=casper:Ubuntu/Debian Live 系統啟動標識quiet splash:隱藏啟動訊息、顯示啟動畫面---:傳遞給 initrd 的參數結束標記| 發行版 | 內核路徑 | initrd 路徑 | 關鍵啟動參數 |
|---|---|---|---|
| Ubuntu / Linux Mint / Pop!_OS | /casper/vmlinuz* | /casper/initrd* | boot=casper iso-scan/filename=$isofile |
| Debian Live | /live/vmlinuz* | /live/initrd* | boot=live findiso=$isofile |
| Fedora Workstation Live | /isolinux/vmlinuz* | /isolinux/initrd* | inst.stage2=hd:LABEL=... iso-scan/filename=$isofile |
| Arch Linux (archiso) | /arch/boot/x86_64/vmlinuz | /arch/boot/x86_64/initramfs*.img | archisolabel=... img_dev=/dev/disk/by-label/... img_loop=$isofile |
| openSUSE Live | /boot/x86_64/loader/linux | /boot/x86_64/loader/initrd | isofrom=$isofile |
| SystemRescue / GParted Live | /boot/vmlinuz* | /boot/initrd* | isoloop=$isofile 或 findiso=$isofile |
⚠️ 具體參數請搜尋「<發行版名稱> boot from ISO GRUB2」或查閱 ISO 內的 boot/grub/grub.cfg / isolinux.cfg。
sudo update-grub
# 或
sudo grub2-mkconfig -o /boot/grub/grub.cfg # Fedora/RHEL
# 或
sudo grub-mkconfig -o /boot/grub/grub.cfg # Arch/通用
重啟電腦,GRUB 選單會出現新增的「Ubuntu 24.04 Live ISO」項目,選擇即可進入 Live 環境。
建立 /etc/grub.d/42_iso_boot 可執行腳本,動態掃描 ISO 目錄自動生成選單:
#!/bin/bash
# /etc/grub.d/42_iso_boot
ISO_DIR="/home/user/ISOs"
GRUB_DEV="(hd0,3)" # 根據實際情況修改
for iso in "$ISO_DIR"/*.iso; do
[ -f "$iso" ] || continue
name=$(basename "$iso" .iso)
cat <
chmod +x /etc/grub.d/42_iso_boot
sudo update-grub
| 現象 | 可能原因 | 解決方案 |
|---|---|---|
| GRUB 選單不顯示新項目 | 40_custom 語法錯誤 / update-grub 未執行 | 執行 grub-script-check /etc/grub.d/40_custom 檢查語法 |
| 選單出現但啟動報錯 "file not found" | ISO 路徑錯誤 / GRUB 裝置名錯誤 / ISO 內核路徑變更 | 掛載 ISO 驗證內部路徑、重新核對 fdisk 輸出與 GRUB 命名 |
| 進入 initramfs shell / 停在 busybox | iso-scan 找不到 ISO / 檔案系統驅動缺失 | 確認 iso-scan/filename 參數正確、ISO 放在支援的檔案系統 (ext4/NTFS/FAT32) |
| UEFI 系統無法啟動 | Secure Boot 啟用 / 內核未簽名 / GRUB 非 UEFI 安裝 | 關閉 Secure Boot、使用簽名內核、確認 grub-install 目標為 x86_64-efi |
| NTFS 分區權限問題 | NTFS 掛載選項預設權限不足 | 在 /etc/fstab 加入 uid=1000,gid=1000,umask=022 選項重新掛載 |
GRUB2 的 loopback 功能讓硬碟直啟 ISO 成為現實。關鍵在於:正確的 GRUB 裝置名 + 正確的 ISO 內部內核/initrd 路徑 + 發行版專屬啟動參數。一次設定,長期受益,是 Linux 使用者必備的進階技能。