使用grub2制作U盘引导iso

创建U盘多引导ISO

创建U盘分区

安装grub2到U盘

拷贝所需文件

配置grub.cfg

#加载模块
insmod cat
insmod gzio
insmod ext2
insmod ntfs
#insmod ntldr
insmod font
insmod fat
insmod part_msdos
insmod loopback
insmod iso9660

set default=0   #这里是默认选项,0就是第一个
set timeout=5   #这里是默认等待时间

#设置显示情况,包括中文字体

#这里是设置字体文件,也就是前面我们拷贝的unicode.pf2
loadfont /boot/grub/unicode.pf2

# 这些是图形化设置的,加上更漂亮
# ------------------------------------------------------------------------
#set gfxmode=800x600          #设置分辨率
#set gfxpayload=keep          #这一句不太清除,网上没有找到相关资料,加上保险
#insmod gfxterm               #启用gfxterm模块,分辨率什么的,都靠它
#insmod vbe                   #加载vbe模块
#terminal_output gfxterm      #将终端设置为gfxterm模式,也就是使上面的分辨率什么的生效

#以下是设置背景图片
#set root='hd0,msdos1'
#insmod jpeg                                #开启jpeg模块
#background_image /boot/images/nature.jpg   #指定背景图片的路径
#set menu_color_normal=white/black          #菜单中未选中项目的字体颜色
#set menu_color_highlight=cyan/black        #选中项目的字体颜色
# ------------------------------------------------------------------------

#搜索ISO目录下有iso的硬盘设定为root分区,也可以改为在U盘根目录下放置的文件,但注意这个文件其他硬盘的根目录不能有
#search --set -f /iso/CDlinux_mini-0.9.7.1.iso

menuentry "WinPE--LMT(grub4dos)" {
    set iso_path=/iso/lmt7.iso
    search --set -f ${iso_path}
    set g4d_dir="/boot/grub4dos"
    set cfgfile="find --set-root ${iso_path};map --mem ${iso_path} (0xff);map --hook;chainloader (0xff);boot"
    echo 'Loading ISO via grub4dos...'
    linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "WinPE--LMT(memdisk)" {
    set iso_path=/iso/lmt7.iso
    search --set -f ${iso_path}
    echo 'Loading Memdisk...'
    linux16 /boot/grub/memdisk iso raw
    echo 'Loading ISO...'
    initrd16 ${iso_path}
}

menuentry "WinPE--Deepin WinPE 4.2(grub4dos)" {
    set iso_path=/iso/Deepin_Windows_PE_4.2_Build_100910.iso
    search --set -f ${iso_path}
    set g4d_dir="/boot/grub4dos"
    set cfgfile="find --set-root ${iso_path};map --mem ${iso_path} (0xff);map --hook;chainloader (0xff);boot"
    echo 'Loading ISO via grub4dos...'
    linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "WinPE--Deepin WinPE 4.2(grub4dos+firadisk)" {
    set iso_path=/iso/Deepin_Windows_PE_4.2_Build_100910.iso
    set firadisk=/iso/firadisk.ima
    search --set -f ${iso_path}
    set g4d_dir="/boot/grub4dos"
    set cfgfile="find --set-root ${iso_path};map --mem ${firadisk} (fd0);map --mem ${iso_path} (0xff);map --hook;chainloader (0xff);boot"
    echo 'Loading ISO via grub4dos...'
    linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "WinPE--Deepin WinPE 4.2(memdisk)" {
    set iso_path=/iso/Deepin_Windows_PE_4.2_Build_100910.iso
    search --set -f ${iso_path}
    echo 'Loading Memdisk...'
    linux16 /boot/grub/memdisk iso raw
    echo 'Loading ISO...'
    initrd16 ${iso_path}
}

menuentry "CDlinux (console only)" {
    set gfxpayload=1280x1024x32,1024x768x32,800x600x32,800x600x16,800x600,640x480
    set iso_path=/iso/CDlinux_mini-0.9.7.1.iso
    loopback loop ${iso_path}
    linux (loop)/CDlinux/bzImage iso-scan/filename=${iso_path} quiet CDL_LANG=en_US.UTF-8
    initrd (loop)/CDlinux/initrd
}

menuentry "Linux Mint 17.2 ISO" {
    set iso_path=/iso/linuxmint-17.2-cinnamon-32bit.iso
    loopback loop ${iso_path}
    linux (loop)/casper/vmlinuz file=/cdrom/preseed/linuxmint.seed boot=casper initrd=/casper/initrd.lz iso-scan/filename=${iso_path} noeject noprompt splash --
    initrd (loop)/casper/initrd.lz
}

menuentry 'BOOT HDisk'{
    insmod ntfs
    search --set -f /bootmgr
    chainloader +1
}

menuentry "Reboot" --class windows --class os {
    reboot
}

#menuentry 'CentOS Linux 7 1503 Live' {
#    set iso_name=CentOS-7-livecd-x86_64
#    set iso_path=/iso/CentOS-7-x86_64-LiveCD-1503.iso
#    loopback loop ${iso_path}
#    linux (loop)/isolinux/vmlinuz0 iso-scan/filename=${iso_path} root=live:CDLABEL=${iso_name} rootfstype=auto ro rd.live.image quiet rhgb rd.luks=0 rd.md=0 rd.dm=0
#    initrd (loop)/isolinux/initrd0.img
#}

#menuentry 'Fedora LiveCD' {
#    set iso_name=Fedora-Live-Desktop-i686-20-1
#    set iso_path=/iso/Fedora-Live-Desktop-i686-20-1.iso
#    loopback loop ${iso_path}
#    linux (loop)/isolinux/vmlinuz0 iso-scan/filename=${iso_path} root=live:CDLABEL=${iso_name} rootfstype=auto ro rd.live.image quiet rhgb rd.luks=0 rd.md=0 rd.dm=0
#    initrd (loop)/isolinux/initrd0.img
#}

#menuentry 'Fedora LiveCD X86_64' {
#    set iso_name=Fedora-Live-Desktop-x86_64-20-1
#    set iso_path=/iso/Fedora-Live-Desktop-x86_64-20-1.iso
#    loopback loop ${iso_path}
#    linux (loop)/isolinux/vmlinuz0 iso-scan/filename=${iso_path} root=live:CDLABEL=${iso_name} rootfstype=auto ro rd.live.image quiet rhgb rd.luks=0 rd.md=0 rd.dm=0
#    initrd (loop)/isolinux/initrd0.img
#}

#menuentry "Install Ubuntu Server 12.04" {
#    set isofile="/iso/ubuntu-12.04-server-amd64.iso"
#    loopback loop $isofile
#    set gfxpayload=keep
#    linux   (loop)/install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed iso-scan/filename=$isofile quiet --
#    initrd  (loop)/install/initrd.gz
#}

#menuentry "Install Ubuntu Server 14.04" {
#    set isofile="/iso/ubuntu-14.04.1-server-amd64.iso"
#    loopback loop $isofile
#    set gfxpayload=keep
#    linux   (loop)/install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed iso-scan/filename=$isofile quiet --
#    initrd  (loop)/install/initrd.gz
#}

历史资料

U盘安装grub2

grub.cfg

pendrivelinux范例

# This grub.cfg file was created by Lance http://www.pendrivelinux.com
# Suggested Entries and the suggestor, if available, will also be noted.
# http://pendrivelinux.com/downloads/multibootlinux/grub.cfg

set timeout=10
set default=0

menuentry "Ubuntu Desktop ISO" {
 loopback loop /ubuntu.iso
 linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=/ubuntu.iso noeject noprompt splash --
 initrd (loop)/casper/initrd.lz
}

# http://www.linuxmint.com
# http://mirrors.ustc.edu.cn/linuxmint-cd/stable/
# ftp://ftp.heanet.ie/pub/linuxmint.com/stable/15/linuxmint-15-cinnamon-dvd-32bit.iso
menuentry "Linux Mint ISO" {
 loopback loop /linuxmint.iso
 linux (loop)/casper/vmlinuz file=/cdrom/preseed/mint.seed boot=casper initrd=/casper/initrd.lz iso-scan/filename=/linuxmint.iso noeject noprompt splash --
 initrd (loop)/casper/initrd.lz
}

menuentry "DBAN ISO" {
 loopback loop /dban.iso
 linux (loop)/DBAN.BZI nuke="dwipe" iso-scan/filename=/dban.iso silent --
}

menuentry "Tinycore ISO" {
 loopback loop /tinycore.iso
 linux (loop)/boot/bzImage --
 initrd (loop)/boot/tinycore.gz
}

menuentry "Memtest 86+" {
 linux16 /memtest86+.bin
}

menuentry "SystemRescueCd" {
 loopback loop /systemrescuecd.iso
 linux (loop)/isolinux/rescuecd isoloop=/systemrescuecd.iso setkmap=us docache dostartx
 initrd (loop)/isolinux/initram.igz
}
Adding an Unlisted ISO: To try ISO Files that are not yet listed, use the existing menuentry examples in /boot/grub/grub.cfg and append any options normally found in the distributions syslinux.cfg file on the "append" line to the "linux" line of the menu entry.

多引导配置范例

# http://www.cnblogs.com/eshizhan/archive/2012/04/24/2467632.html
set timeout=10
set default=0

menuentry "SystemRescueCd-x86-3.7.1.iso" {
 set isofile="/boot/iso/systemrescuecd-x86-3.7.1.iso"
 if cpuid; then set cputype="64"; else set cputype="32"; fi
 loopback loop $isofile
 linux (loop)/isolinux/rescue$cputype isoloop=$isofile setkmap=us
 initrd (loop)/isolinux/initram.igz
}

menuentry "WinPE-2009.iso" {
 set g4d_dir="/boot/grub4dos"
 set cfgfile="find --set-root /boot/iso/winpe-2009.iso;map --mem /boot/iso/winpe-2009.iso (0xff);map --hook;chainloader (0xff);boot"
 linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "wowpc.iso" {
 set g4d_dir="/boot/grub4dos"
 set cfgfile="find --set-root /boot/iso/wowpc.iso;map --mem /boot/iso/wowpc.iso (0xff);map --hook;chainloader (0xff);boot"
 linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "Archlinux-2012.09.07-dual.iso" {
 set isofile="/boot/iso/archlinux-2012.09.07-dual.iso"
 set dev_label="MULTIBOOT"
 if cpuid; then set cputype="x86_64"; else set cputype="i686"; fi
 loopback loop $isofile
 linux (loop)/arch/boot/$cputype/vmlinuz archisolabel=ARCH_201209 img_dev=/dev/disk/by-label/$dev_label img_loop=$isofile
 initrd (loop)/arch/boot/$cputype/archiso.img
}

menuentry "SliTaz-4.0-grub.iso" {
 set isofile="/boot/iso/slitaz-4.0-grub.iso"
 loopback loop $isofile
 linux (loop)/boot/bzImage rw root=/dev/null gfxpayload=vga=normal autologin lang=en kmap=us
 initrd (loop)/boot/rootfs.gz
}

menuentry "TinyCore-current.iso" {
 set isofile="/boot/iso/tinycore-current.iso"
 loopback loop $isofile
 linux (loop)/boot/vmlinuz quiet cde iso=/mnt/sdb1$isofile
 initrd (loop)/boot/core.gz
}

menuentry "XiaoPan-0.4.6.1.iso" {
 set isofile="/boot/iso/xiaopan-0.4.6.1.iso"
 loopback loop $isofile
 linux (loop)/boot/vmlinuz loglevel=3 cde waitusb=3 iso=/mnt/sdb1$isofile mydata=xiaopan restore=sdb1/boot/iso tz=CST-8 showapps lang=en_US.utf8
 initrd (loop)/boot/core.gz
}

menuentry "GRUB4DOS" {
 set g4d_dir="/boot/grub4dos"
 linux $g4d_dir/grub.exe --config-file=$g4d_dir/menu.lst
}

我的grub.cfg

#加载模块
insmod cat
insmod gzio
insmod ext2
insmod ntfs
#insmod ntldr
insmod font
insmod fat
insmod part_msdos
insmod loopback
insmod iso9660

set default=0   #这里是默认选项,0就是第一个
set timeout=5   #这里是默认等待时间

#设置显示情况,包括中文字体

#这里是设置字体文件,也就是前面我们拷贝的unicode.pf2
loadfont /boot/grub/unicode.pf2

# 这些是图形化设置的,加上更漂亮
# ------------------------------------------------------------------------
#set gfxmode=800x600          #设置分辨率
#set gfxpayload=keep          #这一句不太清除,网上没有找到相关资料,加上保险
#insmod gfxterm               #启用gfxterm模块,分辨率什么的,都靠它
#insmod vbe                   #加载vbe模块
#terminal_output gfxterm      #将终端设置为gfxterm模式,也就是使上面的分辨率什么的生效

#以下是设置背景图片
#set root='hd0,msdos1'
#insmod jpeg                                #开启jpeg模块
#background_image /boot/images/nature.jpg   #指定背景图片的路径
#set menu_color_normal=white/black          #菜单中未选中项目的字体颜色
#set menu_color_highlight=cyan/black        #选中项目的字体颜色
# ------------------------------------------------------------------------

#搜索ISO目录下有iso的硬盘设定为root分区,也可以改为在U盘根目录下放置的文件,但注意这个文件其他硬盘的根目录不能有
#search --set -f /iso/CDlinux_mini-0.9.7.1.iso

menuentry "WinPE--LMT(grub4dos)" {
    set iso_path=/iso/lmt7.iso
    search --set -f ${iso_path}
    set g4d_dir="/boot/grub4dos"
    set cfgfile="find --set-root ${iso_path};map --mem ${iso_path} (0xff);map --hook;chainloader (0xff);boot"
    echo 'Loading ISO via grub4dos...'
    linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "WinPE--LMT(memdisk)" {
    set iso_path=/iso/lmt7.iso
    search --set -f ${iso_path}
    echo 'Loading Memdisk...'
    linux16 /boot/grub/memdisk iso raw
    echo 'Loading ISO...'
    initrd16 ${iso_path}
}

menuentry "WinPE--Deepin WinPE 4.2(grub4dos)" {
    set iso_path=/iso/Deepin_Windows_PE_4.2_Build_100910.iso
    search --set -f ${iso_path}
    set g4d_dir="/boot/grub4dos"
    set cfgfile="find --set-root ${iso_path};map --mem ${iso_path} (0xff);map --hook;chainloader (0xff);boot"
    echo 'Loading ISO via grub4dos...'
    linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "WinPE--Deepin WinPE 4.2(grub4dos+firadisk)" {
    set iso_path=/iso/Deepin_Windows_PE_4.2_Build_100910.iso
    set firadisk=/iso/firadisk.ima
    search --set -f ${iso_path}
    set g4d_dir="/boot/grub4dos"
    set cfgfile="find --set-root ${iso_path};map --mem ${firadisk} (fd0);map --mem ${iso_path} (0xff);map --hook;chainloader (0xff);boot"
    echo 'Loading ISO via grub4dos...'
    linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "WinPE--Deepin WinPE 4.2(memdisk)" {
    set iso_path=/iso/Deepin_Windows_PE_4.2_Build_100910.iso
    search --set -f ${iso_path}
    echo 'Loading Memdisk...'
    linux16 /boot/grub/memdisk iso raw
    echo 'Loading ISO...'
    initrd16 ${iso_path}
}

menuentry "CDlinux (console only)" {
    set gfxpayload=1280x1024x32,1024x768x32,800x600x32,800x600x16,800x600,640x480
    set iso_path=/iso/CDlinux_mini-0.9.7.1.iso
    loopback loop ${iso_path}
    linux (loop)/CDlinux/bzImage iso-scan/filename=${iso_path} quiet CDL_LANG=en_US.UTF-8
    initrd (loop)/CDlinux/initrd
}

menuentry "Linux Mint 17.2 ISO" {
    set iso_path=/iso/linuxmint-17.2-cinnamon-32bit.iso
    loopback loop ${iso_path}
    linux (loop)/casper/vmlinuz file=/cdrom/preseed/linuxmint.seed boot=casper initrd=/casper/initrd.lz iso-scan/filename=${iso_path} noeject noprompt splash --
    initrd (loop)/casper/initrd.lz
}

menuentry 'CentOS Linux 7 1503 Live' {
    set iso_name=CentOS-7-livecd-x86_64
    set iso_path=/iso/CentOS-7-x86_64-LiveCD-1503.iso
    loopback loop ${iso_path}
    linux (loop)/isolinux/vmlinuz0 iso-scan/filename=${iso_path} root=live:CDLABEL=${iso_name} rootfstype=auto ro rd.live.image quiet rhgb rd.luks=0 rd.md=0 rd.dm=0
    initrd (loop)/isolinux/initrd0.img
}

menuentry 'BOOT HDisk'{
    insmod ntfs
    search --set -f /bootmgr
    chainloader +1
}

menuentry "Reboot" --class windows --class os {
    reboot
}

#menuentry 'Fedora LiveCD' {
#    set iso_name=Fedora-Live-Desktop-i686-20-1
#    set iso_path=/iso/Fedora-Live-Desktop-i686-20-1.iso
#    loopback loop ${iso_path}
#    linux (loop)/isolinux/vmlinuz0 iso-scan/filename=${iso_path} root=live:CDLABEL=${iso_name} rootfstype=auto ro rd.live.image quiet rhgb rd.luks=0 rd.md=0 rd.dm=0
#    initrd (loop)/isolinux/initrd0.img
#}

#menuentry 'Fedora LiveCD X86_64' {
#    set iso_name=Fedora-Live-Desktop-x86_64-20-1
#    set iso_path=/iso/Fedora-Live-Desktop-x86_64-20-1.iso
#    loopback loop ${iso_path}
#    linux (loop)/isolinux/vmlinuz0 iso-scan/filename=${iso_path} root=live:CDLABEL=${iso_name} rootfstype=auto ro rd.live.image quiet rhgb rd.luks=0 rd.md=0 rd.dm=0
#    initrd (loop)/isolinux/initrd0.img
#}

#menuentry "Install Ubuntu Server 12.04" {
#    set isofile="/iso/ubuntu-12.04-server-amd64.iso"
#    loopback loop $isofile
#    set gfxpayload=keep
#    linux   (loop)/install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed iso-scan/filename=$isofile quiet --
#    initrd  (loop)/install/initrd.gz
#}

#menuentry "Install Ubuntu Server 14.04" {
#    set isofile="/iso/ubuntu-14.04.1-server-amd64.iso"
#    loopback loop $isofile
#    set gfxpayload=keep
#    linux   (loop)/install/vmlinuz  file=/cdrom/preseed/ubuntu-server.seed iso-scan/filename=$isofile quiet --
#    initrd  (loop)/install/initrd.gz
#}


U盘启动WinPE

GRUB2->grub4dos方式

通过grub4dos提供的map功能引导WinPE.iso。

menuentry "[iso] -- WinPE(g4d)" {
    set g4d_dir="/boot/grub4dos"
    set cfgfile="find --set-root /boot/iso/winpe.iso;map --mem /boot/iso/winpe.iso (0xff);map --hook;chainloader (0xff);boot"
    linux $g4d_dir/grub.exe --config-file=$cfgfile
}

menuentry "[iso] -- WinPE(g4d)" --class windows --class os {
    set cfgfile="map /image/winpe.iso (hd32) || map --mem /image/winpe.iso (hd32); map --hook; chainloader (hd32); rootnoverify (hd32)"
    linux16 /grub4dos/grub.exe --config-file=$cfgfile
    initrd16 /image/winpe.iso

grub2+syslinux/memdisk

首先下载最新版的Syslinux,解开压缩包,memdisk文件夹下面有一个memdisk文件(没有任何扩展名),把memdisk文件复制到U盘boot文件夹下。

GRUB2的memdisk是用来读取core.img的。和syslinux的MEMDISK完全不同……详见: http://wiki.xtronics.com/index.php/Grub2_howto

借助MEMDISK(几十KB的文件),GRUB2就能加载几乎任意的镜像文件(ISO、IMG等)了,相当于是「虚拟光驱」的作用?不过引导速度比GRUB4DOS稍微慢一点。

关键就是memdisk后面的raw参数

menuentry 'Boot Windows 8 PE ISO'{
    set root='(hd0,msdos1)'
    echo 'Loading Memdisk...'
    # 不需要insmod memdisk,因为用的是Syslinux的MEMDISK而不是GRUB2的memdisk模块。
    #insmod memdisk
    linux16 /boot/memdisk iso raw
    echo 'Loading ISO...'
    initrd16 /boot/Win8PE.iso
}

menuentry '[iso] -- /WinPE(memdisk) ' {
    #insmod memdisk
    insmod fat
    set root='hd0,msdos1'
    linux16 /boot/memdisk iso raw
    initrd16 /winpe.iso

ntldr方式

目前网络上的WinPE到处都是,各种版本其实都差不多,主要需要如下5个文件:

LDRXPE  NTCOM  PE  SETUPLDR.BIN  WINPE.IS_

大致过程是:LDRXPE调用NTCOM和SETUPLDR.BIN读取PE找到WINPE.IS_镜像,加载WinPE。

编译syslinux

测试编译报错,以后再来查找原因。

工具下载

在boot.ini中添加grub4dos

WinXP

Win7

Windows7上使用Grub4DOS有两种方法实现(除了直接将Grub安装至MBR):

  1. 通过创建boot.ini添加启动启动项。
  2. 通过windows 7的BOOTMGR启动管理器添加启动项。

使用bcdeditbcdedit /enum all来查看系统中硬盘或分区的GUID 标识,例如{dc0a0dea-4a4e-11df-900b-d0c518f1f5f1}。其中{legacy}表示上一代操作系统,{current} 表示当前启动硬盘上的操作系统(Windows 7),下面均以{GUID}表示。

BCDEdit修改启动选项的常用命令格式:

命令 说明
bcdedit 或 bcdedit /enum all 查看系统中硬盘或分区的GUID 标识
bcdedit /set {GUID} Description "Windows XP Professional SP3" 修改{GUID}在启动菜单中的显示内容
bcdedit /default {GUID} 设置指定GUID上的操作系统为默认启动的系统
bcdedit /timeout 3 将默认的启动菜单显示时间更改为3秒
bcdedit /displayorder {current} {legacy} 将首先显示默认启动的Windows 7,然后再显示Windows XP,
bcdedit /delete {GUID} 删除GUID上的启动菜单