安装GRUB2的教程                                                                   WINDOWS下使用GRUB2 制作支持BIOS和UEFI的WINPE、LINUX、MAXDOS多引导U盘


功能:
BIOS:winpe、Ubuntu18.04_x64 live、MAXDOS启动。
UEFI:winpe、Ubuntu18.04_x64 live启动。
一、BIOS和UEFI的启动过程区别
1、BIOS把MBR读出来交给CPU执行,做MBR做想做的事。
2、UEFI是查找磁盘里的\efi\boot\bootx64.efi文件,启动这个可执行程序,让这程序做想做的事。
具体更多的区别网上有很多的资料,上述只是与操作有关的关键点。

二、
材料准备。
1、U盘。
2、grub-2.02-for-windows
3、syslinux 6.03
4、maxdos
5、winpe
6、ubuntu-18.04-desktop-amd64
7、分区工具(推荐DiskGenius)

三、
U盘分区(小心操作)
1、清空U盘,最重要的是清除MBR。U盘原来的MBR没有清除,很可能导致安装grub2失败。如果在安装过程中grub2提示“grub- install: warning: Attempting to install GRUB to a disk with multiple partition labels. This is not supported yet.. ”,一般就是因为U盘原来的MBR没有清除。清空方法:
(1)组合键win+R 运行diskpart。
(2)输入list disk,得到目前所有的磁盘。
(3)输入select disk 2 ,定位到U盘。
(4)输入命令clean,清除所有,(MBR,分区和资料)。

Uploaded Image


2、给U盘分区。我的是一个32g的U盘,分了3GB空间来做启动盘,格式为FAT32,因为要兼容UEFI,所以没有选择分区隐藏、删除等保护措施。

四、部署UEFI下的WINPE
这里说明下将部署winpe放到这么前面的原因。因为winpe启动程序寻找文件都是用的绝对路径,不是相对路径,所以下载下来的winpe镜像目录结构是怎样就必须保持绝对路径不变,这样才能使得程序顺利启动。下面是操作步骤:
1、将winpe镜像挂载或者解压。
2、全选、复制、粘贴到U盘新分的区。

五、安装GRUB2
1、GRUB-2.06-FOR-WINDOWS解压到C:\
2、确定磁盘号

Uploaded Image



3、管理员身份打开CMD(很重要)

4、输入CD /D C:\GRUB-2.02-FOR-WINDOWS

5、I386安装
grub-install.exe --boot-directory=G:\ --target=i386-pc //./PHYSICALDRIVE2
注意盘符,改成自己的。PHYSICALDRIVE2中的“2”就是第二步查到的磁盘号。

6、UEFI 64为安装(注意下面的盘符,改成自己的)
grub-install.exe --boot-directory=G:\ --efi-directory=G: --removable --target=x86_64-efi

7、UEFI32位安装(I386安装)
grub-install.exe --boot-directory=G:\ --efi-directory=G: --removable --target=i386-efi
同样是注意盘符,改成自己的。
Uploaded Image

8、安装成功显示结果
C:\grub-2.02-for-windows>grub-install.exe –boot-directory=G:\ –target=i386-pc //./PHYSICALDRIVE2
Installing for i386-pc platform.
Installation finished. No error reported.
C:\grub-2.02-for-windows>grub-install.exe –boot-directory=G:\ –efi-directory=G: –removable –target=x86_64-efi
Installing for x86_64-efi platform.
Installation finished. No error reported.
C:\grub-2.02-for-windows>grub-install.exe –boot-directory=G:\ –efi-directory=G: –removable –target=i386-efi
Installing for i386-efi platform.
Installation finished. No error reported.
G:\盘根目录会生成相应的文件。
##No error reported 提示正确安装没有错误

六、写GRUB2配置文件
1、/GRUB目录下新建文件GRUB.CFG。

2、GRUB.CFG内容:


#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="0"
fi
if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi
export menuentry_id_option
if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi
function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}
function load_video {
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}
if [ x$feature_default_font_path = xy ] ; then
   font=unicode
else
    font="${prefix}/fonts/unicode.pf2"
fi
if loadfont $font ; then
    #set gfxmode=1024x768x32,1024x768x24,1024x768x16,1024x768,auto
    #set gfxpayload=keep
  loadfont /grub/fonts/dejavu-bold-16.pf2
  loadfont /gurb/fonts/dejavu-bold-14.pf2
  set gfxmode=auto
    load_video
    insmod gfxterm
    insmod png
    terminal_output gfxterm
  #set color_normal=light-gray/black
  #set color_highlight=white/black
    #background_image -m stretch $prefix/themes/splash.png
fi
if [ x$feature_timeout_style= xy ] ; then
  set timeout_style=menu
  set timeout=15
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
  set timeout=15
fi
set color_normal=light-gray/black
set color_highlight=white/black
if [ -e /grub/themes/starfield/starfield.png ]; then
    # binary_syslinux modifies the theme file to point to the correct
    # background picture
    set theme=/grub/themes/starfield/theme.txt
else
    set menu_color_normal=cyan/blue
    set menu_color_highlight=white/blue
fi
### END /etc/grub.d/00_header ###
### BEGIN /etc/grub.d/10_linux ###
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###
### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###
### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
    source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
    source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###
menuentry "Restart(重启)" {
    reboot
}
menuentry "Power Off(关机)" {
    halt
}


#本人GRUB.CFG的例子:
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="0"
fi
if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi
export menuentry_id_option
if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi
function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}
function load_video {
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}
if [ x$feature_default_font_path = xy ] ; then
   font=unicode
else
    font="${prefix}/fonts/unicode.pf2"
fi
if loadfont $font ; then
    #set gfxmode=1024x768x32,1024x768x24,1024x768x16,1024x768,auto
    #set gfxpayload=keep
  loadfont /grub/fonts/dejavu-bold-16.pf2
  loadfont /gurb/fonts/dejavu-bold-14.pf2
  set gfxmode=auto
    load_video
    insmod gfxterm
    insmod png
    terminal_output gfxterm
  #set color_normal=light-gray/black
  #set color_highlight=white/black
    #background_image -m stretch $prefix/themes/taylor swift1.jpg
fi
if [ x$feature_timeout_style= xy ] ; then
  set timeout_style=menu
  set timeout=3
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
  set timeout=3
fi
set color_normal=light-gray/black
set color_highlight=white/black
if [ -e /grub/themes/starfield/usetheme.TXT ]; then
    # binary_syslinux modifies the theme file to point to the correct
    # background picture
    set theme=/grub/themes/starfield/theme.txt
    insmod jpeg
else
    set menu_color_normal=cyan/blue
    set menu_color_highlight=white/blue
fi
### END /etc/grub.d/00_header ###
### BEGIN /etc/grub.d/10_linux ###
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###
### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###
### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
    source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
    source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###


menuentry "正常启动win10" {
set root=(hd0,msdos1)
chainloader +1
}
#set root=(hd0,msdos1)  这一句,表示将指针交给HD0,即0号物理盘;msdos1表示微软分区格式(FAT,NTFS)中的第1个分区。[用DiskGenius可以很清楚看到这些参数。]
#set root=(hd0,msdos1)  也可以写成set root=(hd0,2)[这里假定第1个分区是EXT格式,第2个分区是FAT格式] [set root=(hd0,2)表示直接指定第2个物理分区]

menuentry "Restart(重启)" {
    reboot
}


menuentry "Power Off(关机)" {
    halt
}


menuentry "引导 KUBUNTU Linux系统" {
set root=(hd1,4)
chainloader +1
}


menuentry "WinPE" {
set root=(hd0,3)
#直接指出第3块分区;(分区号是从1号开始算的)。
linux16 $prefix/i386-pc/MEMDISK iso raw
#这里调用 MEMDISK来虚拟磁盘。
#$prefix是指本启动盘的根目录
echo "正在启动WinPE,请耐心等待...."
initrd16 /nst/winPE.iso
}


menuentry "DiskGenius_PE" {
set root=(hd0,msdos1)
linux16 $prefix/i386-pc/MEMDISK iso raw
echo "正在启动DiskGenius_PE,请耐心等待...."
initrd16 /nst/DiskGenius_PE.iso
}

注意事项:
1.安装GRUB的分区必须事先格式化为FAT文件系统,不能为NTFS文件系统。
2.GRUB.CFG配置文件必须放入\grub文件夹中。
3.grub\i386-pc文件夹中,默认有memdisk.mod;如果需要用虚拟盘,则必须还要有memdisk(没有扩展名的,默认没有,需另外从网上下载:
   https://www.syslinux.org/old/download.php,在压缩包/bios/memdisk/即可以找到memdisk)。
   并将memdisk拷贝到grub\i386-pc文件夹中。
   需要说明的是:memdisk.mod(有扩展名) 和 memdisk(没有扩展名)不是同一个文件!
  
   memdisk的用法例子:menuentry "DiskGenius_PE" {
                                       set root=(hd0,msdos3)
                                       #从哪个物理盘启动,哪个物理盘就变成0号盘。所以hd0表示指针指向自身这个物理盘。msdos1这项,表示微软系统盘(物理盘中其它系统格式的
                                       #分区不列入计算范围)的第3个分区。 也可以直接指出第几个分区(去掉msdos,只保留数字;这种表达就是全部系统格式的分区都包含在内)。
                                       #注意,分区号是从1号开始的,而物理盘号是从0号开始的!
                                       linux16 $prefix/i386-pc/MEMDISK iso raw
                                       echo "正在启动DiskGenius_PE,请耐心等待...."
                                       initrd16 /nst/DiskGenius_PE.iso
                                       #memdisk限制,DiskGenius_PE.iso文件的大小不能超过2GB.

因为memdisk有容量限制,并且启动比较慢。所以最好的方法是在磁盘中另外分一个区,再将WINPE释放到此分区中,然后再用grub2指向并启动此WINPE分区。

启动gpt分区中efi的WINPE:

由于GRUB2不能在UEFI模式下对ISO文件进行仿真,那么我们应该如何在UEFI模式下引导WindowsPE呢?答案是必须使用WIM格式的WindowsPE。

具体说来就是首先用GRUB2链式加载微软的"bootmgfw.efi"引导管理器,然后再由"bootmgfw.efi"根据BCD文件的指引去启动WindowsPE。
第一步,从PE中提取"WEPE64.WIM"与"WEPE.SDI"(BOOT.SDI)文件。
第二步,从Win10原版安装光盘中提取"bootmgfw.efi"文件([sources/install.wim]/Windows/Boot/EFI/bootmgfw.efi)。
第三步,将提取的三个文件放置到一个FAT32或NTFS磁盘分区上,这里假定放到'(hd0,gpt3)/winpe/'目录中。
第五步,将BCD文件同样放置到'(hd0,gpt3)/winpe/'目录中。

最后,在'grub.cfg'中加入如下菜单项:
menuentry 'UEFI Windows PE Boot Manager'  {
    chainloader (hd0,gpt3)/winpe/bootmgfw.efi
}