萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

GRUB2 直接啟動 Linux ISO 鏡像:免燒錄、免 USB、硬碟直啟 Live 系統完整教學

核心概念

GRUB2 具備 loopback 模組,能將 ISO 檔案掛載為迴環裝置,直接讀取其中的內核 (vmlinuz) 與初始化映像 (initrd) 進行啟動。這意味著:

  • ✅ 無需燒錄光碟
  • ✅ 無需製作 USB 啟動碟
  • ✅ 無需佔用 USB 隨身碟
  • ✅ 硬碟空間允許下可保存多個 ISO 多重啟動
  • ⚠️ 僅支援 GRUB2 的 Linux 系統(Ubuntu、Debian、Fedora、Arch、openSUSE 等)
  • ⚠️ **無法從 Windows Boot Manager 直接啟動 Linux ISO**

前置需求

  • 已安裝 Linux 系統且使用 GRUB2 作為啟動載入器
  • 目標 ISO 檔案(建議 Live/Desktop 版,如 ubuntu-24.04-desktop-amd64.iso)
  • ISO 放置於**非啟動分區**的一般資料分區(ext4、NTFS、FAT32 均可)
  • root/sudo 權限

步驟 1:取得 ISO 內核與 initrd 路徑

# 以 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
  • initrd:/casper/initrd 或 /casper/initrd.lz / /casper/initrd.gz

步驟 2:確認 ISO 所在分區的 GRUB 裝置名

sudo fdisk -l
# 或
lsblk -f

GRUB 裝置命名規則:

  • 磁碟編號從 0 起:hd0 = 第一顆磁碟 (sda/nvme0n1)
  • 分區編號從 1 起:(hd0,1) = 第一顆磁碟第一分區 (sda1)
  • Linux 裝置名 → GRUB 裝置名對照:
    • /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。

步驟 3:編輯 GRUB 自訂啟動項

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*.imgarchisolabel=... img_dev=/dev/disk/by-label/... img_loop=$isofile
openSUSE Live/boot/x86_64/loader/linux/boot/x86_64/loader/initrdisofrom=$isofile
SystemRescue / GParted Live/boot/vmlinuz*/boot/initrd*isoloop=$isofile 或 findiso=$isofile

⚠️ 具體參數請搜尋「<發行版名稱> boot from ISO GRUB2」或查閱 ISO 內的 boot/grub/grub.cfg / isolinux.cfg。

步驟 4:更新 GRUB 設定並重啟

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 環境。

進階:多 ISO 管理腳本

建立 /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 / 停在 busyboxiso-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 選項重新掛載

應用場景

  • 🛠️ 系統救援:常駐 SystemRescue、Clonezilla、GParted Live ISO
  • 🧪 試用新發行版:不動分區、不佔 USB、隨時啟動體驗
  • 💾 硬碟安裝:從 Live ISO 直接安裝到同顆硬碟的其他分區
  • 🖥️ 無光驅/無 USB 情境:老舊筆電、伺服器機房維護
  • 🧑‍💻 開發測試:快速啟動不同環境驗證軟體相容性

總結

GRUB2 的 loopback 功能讓硬碟直啟 ISO 成為現實。關鍵在於:正確的 GRUB 裝置名 + 正確的 ISO 內部內核/initrd 路徑 + 發行版專屬啟動參數。一次設定,長期受益,是 Linux 使用者必備的進階技能。

copyright © 萬盛學電腦網 all rights reserved