簡述:U盤上以grub引導複數ISO9660鏡像
先前曾為此折騰過一段時間,成果如下:
首先建議對容量大於4GiB的U盤進行格式化:FAT32是不支持大於4GiB的文件的,這會導致無法在U碟上放置諸如openSUSE-13.1-DVD-x86_64.iso這樣的大型鏡像,推薦的一種方法是將U盤分區格式化為ext2(不建議使用ext3/ext4的原因與不建議使用NTFS的原因一樣)。不過此亦有限制,若需要在Windowz系列上使用此U盤就會比較麻煩。故而建議在U盤上分兩個區,一個FAT32區與一個ext2區;請依情況權衡兩個分區的大小。筆者的U盤分區方案如下所示:
NAME FSTYPE SIZE LABEL sdb 14.9G ├─sdb1 vfat 720.9M VFATPOOL └─sdb2 ext2 14.2G 2014-01-12
一般在GNU/Linux環境下有數個分區工具可用,如gparted與fdisk等,本文以後者為例;本例中先將U盤分為兩個區:
# lsblk -o NAME,FSTYPE,SIZE,LABEL # fdisk /dev/sdx
在插入U盤後請先不要掛載:依前者輸出來選擇塊設備,請將「x」替換為U盤所對應的塊設備的字母;關於如何使用fdisk,ArchWiki的Beginners' Guide是個不錯的教程。
之後,將第一個分區格式化為FAT32:
# mkfs.vfat -n 對應卷標 /dev/sdx1
再將第二個分區格式化為ext2:
# mke2fs -t ext2 -N 512 -m 0 -D -L 對應卷標 /dev/sdx2
(注:為加快格式化速度,可適當的減少「-N」後的值)
為安裝grub,需要掛載U盤分區。本例中掛載第二個ext2分區到/tmp/mntp(假定/tmp掛載的是tmpfs):
$ mkdir /tmp/mntp
# mount -t ext2 -o noatime /dev/sdx2 /tmp/mntp
# rm -r /tmp/mntp/*
# mkdir -p /tmp/mntp/data
安裝grub到U盤,另建一個目錄用於存放iso鏡像:
# grub-install --boot-directory=/tmp/mntp/boot --target=i386-pc --compress=xz --locales= --themes= /dev/sdx $ sudo chown -R $USER:storage /tmp/mntp $ mkdir /tmp/mntp/boot/iso
(注:包含chown那行用於修正文件所在組;乃亦可將文件權限更正為同組可讀寫,這樣方便在不同的機器上交換文件)
然後將鏡像拷貝進去即可。之後需要配置grub.cfg以引導鏡像文件,在本例中grub.cfg位於/tmp/mntp/boot/grub/grub.cfg。以筆者U盤上的grub.cfg為例(UTF-8,LF):
#set color_normal=light-gray/black #set color_highlight=black/light-gray #export color_normal #export color_highlight insmod acpi loadfont /boot/grub/fonts/unicode.pf2 insmod all_video insmod gfxterm set gfxmode=auto terminal_input console terminal_output gfxterm set volume_label=將此更改為置放鏡像文件的分區卷標 export volume_label #set default=0 set timeout=-1 ############## menuentry "[loopback]archlinux-2014.07.03-dual" { set isofile="/boot/iso/archlinux-2014.07.03-dual.iso" loopback loop $isofile linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCH_201407 img_label=$volume_label img_loop=$isofile earlymodules=loop initrd (loop)/arch/boot/x86_64/archiso.img } ## archlinux的官方鏡像比較特別,需要根據發佈年份月份來修改紅字部分; menuentry "[loopback]archlinux-2014.07.02-x86_64" { set isofile="/boot/iso/archlinux-2014.07.02-x86_64.iso" loopback loop $isofile linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ALIC_201407 img_label=$volume_label img_loop=$isofile earlymodules=loop initrd (loop)/arch/boot/x86_64/archiso.img } ## 同理; menuentry "[loopback]alde-light-201404" { set isofile="/boot/iso/alde-light-201404.iso" loopback loop $isofile linux (loop)/vmlinuz livemode root=LABEL=$volume_label iso=$isofile locale=zh_TW.UTF-8 initrd (loop)/initrd.img } ## alde為archlinux吧吧友製作的livecd:alde-201404-released; menuentry '[loopback]sage-0.2-dual'{ set isofile="/boot/iso/sage-0.2-dual.iso" loopback loop $isofile linux (loop)/boot/kernel-64 isoboot label=$volume_label root=iso=sage-0.2-dual.iso depth user=root add.pkg vga=788 quiet initrd (loop)/boot/initramfs-64 } ## sage是gentoo吧吧友製作的livecd,小巧而實用,0.2版在此:tieba.baidu.com/p/2271618062 #menuentry "[loopback]archlinux-2014.06-1-archboot" { # set isofile="/boot/iso/archlinux-2014.06-1-archboot.iso" # loopback loop $isofile # linux (loop)/boot/vmlinuz_x86_64 iso_loop_dev=LABEL=$volume_label iso_loop_path=$isofile # initrd (loop)/boot/initramfs_x86_64.img #} ## 見:https://wiki.archlinux.org/index.php/Archboot #menuentry "[loopback]archlinux-2013.11.01-dual" { # set isofile="/boot/iso/archlinux-2013.11.01-dual.iso" # loopback loop $isofile # linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCH_201311 img_dev=/dev/disk/by-label/$volume_label img_loop=$isofile earlymodules=loop # initrd (loop)/arch/boot/x86_64/archiso.img #} menuentry '[loopback]slackware64-14.1-install-dvd' { set isofile='/boot/iso/slackware64-14.1-install-dvd.iso' loopback loop (hd1,2)$isofile linux (loop)/kernels/huge.s/bzImage printk.time=0 initrd (loop)/isolinux/initrd.img } menuentry '[loopback]Fedora-20-x86_64-DVD' { set isofile='/boot/iso/Fedora-20-x86_64-DVD.iso' loopback loop $isofile linux (loop)/isolinux/vmlinuz noeject inst.stage2=hd:LABEL=$volume_label:/$isofile initrd (loop)/isolinux/initrd.img } menuentry '[loopback]Fedora-Live-Xfce-x86_64-20-1' { set isofile='/boot/iso/Fedora-Live-Xfce-x86_64-20-1.iso' loopback loop $isofile linux (loop)/isolinux/vmlinuz0 root=live:CDLABEL=Fedora-Live-Xfce-x86_64-20-1 iso-scan/filename=$isofile rd.live.image initrd (loop)/isolinux/initrd0.img } #menuentry "[loopback]openSUSE-13.1-DVD-x86_64" { # set isofile="/boot/iso/openSUSE-13.1-DVD-x86_64.iso" # loopback loop $isofile # linux (loop)/boot/x86_64/loader/linux install=hd:$isofile # initrd (loop)/boot/x86_64/loader/initrd #} #menuentry '[loopback]openSUSE-13.1-KDE-Live-x86_64' { # set isofile="/boot/iso/openSUSE-13.1-KDE-Live-x86_64.iso" # loopback loop $isofile # linux (loop)/boot/x86_64/loader/linux isofrom_device=/dev/disk/by-label/$volume_label isofrom_system=$isofile LANG=zh_CN.UTF-8 # initrd (loop)/boot/x86_64/loader/initrd #} menuentry '[loopback]debian-7.6.0-amd64-DVD-1' { set isofile="/boot/iso/debian-7.6.0-amd64-DVD-1.iso" set initrdfile="/boot/iso/debian-7.6.0-amd64-DVD-1.hdd.initrd.gz" loopback loop $isofile linux (loop)/install.amd/vmlinuz vga=791 iso-scan/ask_second_pass=true iso-scan/filename=$isofile initrd $initrdfile } ## 表示很不理解爲何debian不將關於硬盤安裝的某些細節寫入文檔中(或是文檔*太長*導致咱遺漏了),debian wiki中也沒發現什麼有關自硬盤安裝debian需要使用另一個initrd的線索:( #menuentry "[loopback]debian-live-7.5-amd64-xfce-desktop" { # set isofile="/boot/iso/debian-live-7.5-amd64-xfce-desktop.iso" # loopback loop $isofile # linux (loop)/live/vmlinuz boot=live fromiso=/dev/disk/by-label/$volume_label/$isofile config # initrd (loop)/live/initrd.img #} #menuentry '[loopback]CentOS-7.0-1406-x86_64-DVD' { # set isofile='/boot/iso/CentOS-7.0-1406-x86_64-DVD.iso' # loopback $isofile # linux (loop)/isolinux/vmlinuz noeject inst.stage2=hd:LABEL=$volume_label:/$isofile # initrd (loop)/isolinux/initrd.img #} #menuentry '[loopback]CentOS-7.0-1406-x86_64-GnomeLive' { # set isofile='/boot/iso/CentOS-7.0-1406-x86_64-GnomeLive.iso' # loopback loop $isofile # linux (loop)/isolinux/vmlinuz0 root=live:CDLABEL=CentOS-7-live-GNOME-x86_64 iso-scan/filename=$isofile rd.live.image # initrd (loop)/isolinux/initrd0.img #} #menuentry "[loopback]Sabayon_Linux_13.11_amd64_KDE" { # set isofile="/boot/iso/Sabayon_Linux_13.11_amd64_KDE.iso" # loopback loop $isofile # set root=(loop) # linux /boot/sabayon root=/dev/ram0 aufs cdroot locale=zh_TW loop=/livecd.squashfs looptype=squashfs isoboot=$isofile # initrd /boot/sabayon.igz #} #menuentry "[loopback]kali-linux-1.0.6-amd64" { # set isofile="/boot/iso/kali-linux-1.0.6-amd64.iso" # loopback loop $isofile # linux (loop)/live/vmlinuz findiso=$isofile boot=live noconfig=sudo username=root hostname=kali noswap # initrd (loop)/live/initrd.img #} #menuentry "[loopback]archbang-2013.09.01-x86_64" { # set isofile="/boot/iso/archbang-2013.09.01-x86_64.iso" # loopback loop $isofile # linux (loop)/arch/boot/x86_64/vmlinuz archisolabel=ARCHBANG img_dev=/dev/disk/by-label/$volume_label img_loop=$isofile earlymodules=loop # initrd (loop)/arch/boot/x86_64/archiso.img #} #menuentry "[loopback]gparted-live-0.17.0-1-amd64" { # set isofile="/boot/iso/gparted-live-0.17.0-1-amd64.iso" # loopback loop $isofile # linux (loop)/live/vmlinuz boot=live config union=aufs noswap noprompt vga=788 ip=frommedia toram=filesystem.squashfs findiso=$isofile # initrd (loop)/live/initrd.img #} #menuentry "[loopback]CDlinux-0.9.7.1" { # set isofile="/boot/iso/CDlinux-0.9.7.1.iso" # loopback loop $isofile # linux (loop)/CDlinux/bzImage CDL_LANG=zh_TW.UTF-8 CDL_AMOUNT=no CDL_DEV=LABEL=$volume_label CDL_LOOP=yes CDL_DIR=/boot/iso CDL_IMG=CDlinux-0.9.7.1.iso CDL_WAIT=0 # initrd (loop)/CDlinux/initrd #} #menuentry "[loopback]deepin_2013_zh-hant_amd64" { # set isofile="/boot/iso/deepin_2013_zh-hant_amd64.iso" # loopback loop $isofile # linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile locale=zh_TW.UTF-8 # initrd (loop)/casper/initrd.lz #} #menuentry "[loopback]ubuntu-14.04-desktop-amd64" { # set isofile="/boot/iso/ubuntu-14.04-desktop-amd64.iso" # loopback loop $isofile # linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=$isofile locale=zh_CN.UTF-8 # initrd (loop)/casper/initrd.lz #} #menuentry "[chainload]DEEPIN.PE" { # search --label --set=root VFATPOOL # ntldr /SETUPLDR.BIN #} ## Windowz PE的引導不在本文討論範圍內;關於VFATPOOL的意義,請參見本文開端部分; #menuentry "[memdisk]DEEPIN.DOSTOOL" { # linux16 "/boot/grub/memdisk.gz" floopy # initrd16 "/boot/img/DOSTOOL.IMG" #} submenu "grub2 other functions" { menuentry "List devices/partitions" { ls -l sleep 60 } menuentry "Enable serial terminal" { serial terminal_input --append serial terminal_output --append serial } } menuentry "Boot HDD" { chainloader +1 } menuentry "Reboot"{ reboot } menuentry "Halt now"{ halt }
以上就是筆者的grub.cfg,包含Arch Linux・Debian・Fedora・CentOS・openSUSE・Slackware等官方鏡像的引導菜單項;另外筆者的iso鏡像文件都放在U盤ext2分區中的「/boot/iso」中;請諸位按需修改,移除或添加注釋符號(#)以方便的屏蔽或激活某個菜單項。另:現在大多數Linux發行版的光碟鏡像都能以類似上面的方法引導(而另一小部分:如CentOS 6.5 LiveDVD,是由於某物(dracut)版本過舊所致,乃需要將其鏡像的文件提取到U盤後纔能引導)。
2014年4月05日 14:45
原来 grub2 也支持子菜单的呀 :-)
其实不是所有 ISO 文件都是 ISO9660 格式的,Arch 用过一段时间的 UDF。