想在單個 U 盤裡同時裝兩套 Linux 系統,可以讓 GRUB legacy 負責第一階段引導,再把控制權交給 GRUB2 的 core.img。以下為完整思路。
插入 U 盤後先用 fdisk -l 確認設備名(本文為 /dev/sdb),然後:
fdisk /dev/sdb
用 p 查看現有分區,若有一個 fat32 分區先用 d 刪掉;接著用 n 新建主分區(選 p,Partition number 填 1),再用 n 建第二個主分區;然後用 a 把兩個分區都設為可引導;最後 w 寫入分區表。
mkfs -t ext3 /dev/sdb1 mkfs -t ext3 /dev/sdb2 mkdir /mnt/sdb1 mount -t ext3 /dev/sdb1 /mnt/sdb1 mkdir /mnt/sdb2 mount -t ext3 /dev/sdb2 /mnt/sdb2
把 bt5 光盤解開,將 casper 與 preseed 兩個目錄拷到第一分區根目錄;再把 linux deepin 的 ISO 整包放到第二分區根目錄。
grub-install --root-directory=/mnt/sdb1 /dev/sdb
把 /boot/grub/grub.conf 拷到 U 盤對應目錄,編輯為:
timeout 20 default 0 title Windows 7 map (hd0) (hd1) map (hd1) (hd0) rootnoverify (hd1,0) makeactive chainloader +1 title backtrack 5 root (hd0,0) kernel /casper/vmlinuz root=/dev/sdb1 file=/preseed/custom.seed boot=casper locale=zh_CN text -- initrd /casper/initrd.gz title linux deepin root (hd0,1) kernel /boot/grub/core.img savedefault boot
含義很直白:第一分區由 GRUB 直接引導 bt5;第二分區先由 GRUB 加載 core.img,再把控制權交給這個 GRUB2 鏡像。此時 core.img 還不存在,稍後生成。
grub> root(hd1,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd1)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5(hd1)"... 15 sectors are embedded. succeeded
Running "install /boot/grub/stage1 (hd1)(hd1)1+15 p (hd1,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
Done.
grub> quit
帶著 U 盤重啟,應該就能進入 bt5(基於 Ubuntu)。
grub-install --root-directory=/mnt/sdb2 /dev/sdb
編輯其配置文件 grub.cfg:
# DO NOT EDIT THIS FILE
# It is automatically generated by /usr/sbin/grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
### BEGIN /etc/grub.d/00_header ###
set timeout=10
### END /etc/grub.d/00_header ###
### BEGIN /etc/grub.d/05_debian_theme ###
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
### END /etc/grub.d/05_debian_theme ###
### BEGIN /etc/grub.d/10_linux ###
menuentry "linux deepin" {
loopback loop (hd0,2)/deepin_12.06_zh-hans_i386.iso
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=/deepin_12.06_zh-hans_i386.iso locale=zh_CN.UTF-8 noprompt noeject
initrd (loop)/casper/initrd.lz
}
注意分區編號的差異:GRUB legacy 從 0 開始,所以第一分區是 (hd0,0);GRUB2 從 1 開始,第二分區寫作 (hd0,2)。
安裝 GRUB2 時它的 MBR 會覆蓋 GRUB 的 MBR,因此需要重啟進入一個正常的 Linux 系統,再執行一次前面的 root(hd1,0) 與 setup (hd1) 流程,把 GRUB 的 MBR 覆蓋回來。此後再重啟,你手上就是一個雙 Linux 系統的啟動 U 盤了。