無需 USB/光碟,直接從硬碟啟動 ISO 映像 (Live/Installer)。原理:GRUB2 loopback 模組掛載 ISO → 載入 kernel/initrd → 以 iso-scan/filename= 參數告知核心 ISO 位置。本文以 Ubuntu 22.04/24.04 為例,完整示範 GRUB2 自訂選單配置。
casper/vmlinuz、casper/initrd)。# 下載
wget https://releases.ubuntu.com/24.04/ubuntu-24.04-desktop-amd64.iso -P ~/Downloads/
# 檢視內部 (掛載或歸檔管理器)
mkdir /tmp/iso && mount -o loop ~/Downloads/ubuntu-24.04-desktop-amd64.iso /tmp/iso
ls /tmp/iso/casper/
# 應見: vmlinuz, initrd (或 initrd.lz, initrd.gz)
umount /tmp/iso
# 查看分區
sudo fdisk -l
# 輸出範例: /dev/sda2 Linux filesystem (掛載於 /home)
# GRUB 命名: (hd0,2) # 磁碟 0 (sda), 分區 2 (sda2)
# 注意: GRUB 分區編號從 1 起算,磁碟從 0 起算
sudo vi /etc/grub.d/40_custom
在檔案末尾新增 (根據實際路徑調整):
menuentry "Ubuntu 24.04 Live ISO" {
set isofile="/home/username/Downloads/ubuntu-24.04-desktop-amd64.iso"
# 若 /home 為獨立分區 (hd0,2),則:
# set isofile="/username/Downloads/ubuntu-24.04-desktop-amd64.iso"
loopback loop (hd0,2)$isofile
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile quiet splash ---
initrd (loop)/casper/initrd
}
關鍵參數:
iso-scan/filename=:告知 casper 腳本 ISO 位置,以便掛載 rootfs (filesystem.squashfs)。boot=casper:Ubuntu Live 核心參數。---:分隔核心參數與 initrd 參數 (UEFI 可能需要)。sudo update-grub
# 或 grub2-mkconfig -o /boot/grub/grub.cfg (CentOS/RHEL)
reboot
Shift (BIOS) 或連按 Esc (UEFI) 顯示 GRUB 選單。| 發行版 | vmlinuz 路徑 | initrd 路徑 | 核心參數 |
|---|---|---|---|
| CentOS/Fedora Live | isolinux/vmlinuz0 | isolinux/initrd0.img | inst.stage2=hd:LABEL=... iso-scan/filename=... |
| Arch Linux | arch/boot/x86_64/vmlinuz | arch/boot/x86_64/initramfs*.img | archisolabel=... img_loop=... |
| Linux Mint | casper/vmlinuz | casper/initrd.lz | 同 Ubuntu |
| Kali Live | live/vmlinuz | live/initrd.img | boot=live findiso=$isofile |
| SystemRescue | sysresccd/boot/x86_64/vmlinuz | sysresccd/boot/x86_64/initramfs.xz | img_loop=$isofile |
isofile 變數未正確展開、分區檔案系統不支援 (如 LVM 內)。iso-scan/filename 路徑必須是 GRUB 視角的絕對路徑 (含分區前綴)。persistent 參數並建立 casper-rw 檔案/分區。# 在 40_custom 中新增多個 menuentry
# 或建立獨立腳本 /etc/grub.d/41_isos 並 chmod +x
# 統一管理 ISO 目錄:
ISO_DIR="/isos"
for iso in "$ISO_DIR"/*.iso; do
# 動態生成 menuentry (需解析 ISO 內部結構)
done
配置一次 GRUB2,即可隨時從硬碟啟動多款 Live 系統,適合救援、安裝、測試多種場景。