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

Linux 硬碟直啟 ISO 映像:GRUB2 循環掛載啟動 Ubuntu/CentOS Live 系統完整教學

編輯:關於 Linux

無需 USB/光碟,直接從硬碟啟動 ISO 映像 (Live/Installer)。原理:GRUB2 loopback 模組掛載 ISO → 載入 kernel/initrd → 以 iso-scan/filename= 參數告知核心 ISO 位置。本文以 Ubuntu 22.04/24.04 為例,完整示範 GRUB2 自訂選單配置。

前置條件

  • 已安裝 Linux 系統且使用 GRUB2 (傳統 BIOS 或 UEFI)。
  • ISO 為 Live/Desktop 版 (含 casper/vmlinuz、casper/initrd)。
  • ISO 放置於 Linux 可讀取的分區 (ext4/xfs/btrfs/ntfs/fat32),不可在 LVM/LUKS 內 (除非核心支援)。

步驟 1:取得 ISO 並確認內部結構

# 下載
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

步驟 2:確認 GRUB 裝置名稱

# 查看分區
sudo fdisk -l
# 輸出範例: /dev/sda2  Linux filesystem (掛載於 /home)
# GRUB 命名: (hd0,2)  # 磁碟 0 (sda), 分區 2 (sda2)
# 注意: GRUB 分區編號從 1 起算,磁碟從 0 起算

步驟 3:編輯 GRUB 自訂選單

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 可能需要)。

步驟 4:更新 GRUB 並重啟

sudo update-grub
# 或 grub2-mkconfig -o /boot/grub/grub.cfg (CentOS/RHEL)
reboot

步驟 5:開機選單選擇 ISO 啟動

  • 重啟時按住 Shift (BIOS) 或連按 Esc (UEFI) 顯示 GRUB 選單。
  • 選擇 "Ubuntu 24.04 Live ISO" 進入 Live 環境。

其他發行版參數對照

發行版vmlinuz 路徑initrd 路徑核心參數
CentOS/Fedora Liveisolinux/vmlinuz0isolinux/initrd0.imginst.stage2=hd:LABEL=... iso-scan/filename=...
Arch Linuxarch/boot/x86_64/vmlinuzarch/boot/x86_64/initramfs*.imgarchisolabel=... img_loop=...
Linux Mintcasper/vmlinuzcasper/initrd.lz同 Ubuntu
Kali Livelive/vmlinuzlive/initrd.imgboot=live findiso=$isofile
SystemRescuesysresccd/boot/x86_64/vmlinuzsysresccd/boot/x86_64/initramfs.xzimg_loop=$isofile

常見問題

  • 黑畫面/核心恐慌:ISO 路徑錯誤、isofile 變數未正確展開、分區檔案系統不支援 (如 LVM 內)。
  • 找不到 ISO:iso-scan/filename 路徑必須是 GRUB 視角的絕對路徑 (含分區前綴)。
  • UEFI Secure Boot:需關閉 Secure Boot 或簽署 GRUB/核心。
  • 持久化儲存:加 persistent 參數並建立 casper-rw 檔案/分區。
  • 多 ISO 管理技巧

    # 在 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 系統,適合救援、安裝、測試多種場景。

    Copyright © Windows教程網 All Rights Reserved