如何使用 Grub2 在 UEFI 主板上启动多个驱动器

如何使用 Grub2 在 UEFI 主板上启动多个驱动器

我在 UEFI 主板上有一个 SSD 和一个 HDD。我可以通过更改 BIOS 中的启动顺序来切换操作系统,但我更希望 Grub2 只将 Windows7 列为启动选项。

解决方案应该可以帮助任何在 UEFI 主板上有多个驱动器的人。我见过使用 UEFI 从多个磁盘启动多个操作系统并且 rEFInd 看起来是一个不错的选择;这个问题是关于如何正确配置 Grub2 以查看 Windows7(或任何操作系统)的引导加载程序(如果可能的话) - 不使用 Grub-legacy 或其他工具(如 rEFInd)。

我正在使用 Ubuntu 12.10,Grub2 位于 SSD(主启动设备)上,而 Windows7 位于 HDD 上,安装时它决定将其分成三个分区。

启动信息脚本给出了详细的分解:

                  Boot Info Script 0.61      [1 April 2012]


============================= Boot Info Summary: ===============================

 => Grub2 (v1.99) is installed in the MBR of /dev/sda and looks at sector 1 of 
    the same hard drive for core.img. core.img is at this location and looks 
    in partition 72 for .
 => No boot loader is installed in the MBR of /dev/sdb.

sda1: __________________________________________________________________________

    File system:       ext4
    Boot sector type:  -
    Boot sector info: 
    Operating System:  Ubuntu 12.10
    Boot files:        /boot/grub/grub.cfg /etc/fstab

sda2: __________________________________________________________________________

    File system:       swap
    Boot sector type:  -
    Boot sector info: 

sdb1: __________________________________________________________________________

    File system:       vfat
    Boot sector type:  Windows 7: FAT32
    Boot sector info:  No errors found in the Boot Parameter Block.
    Operating System:  
    Boot files:        /efi/Boot/bootx64.efi

sdb2: __________________________________________________________________________

    File system:       
    Boot sector type:  -
    Boot sector info: 
    Mounting failed:   mount: unknown filesystem type ''

sdb3: __________________________________________________________________________

    File system:       ntfs
    Boot sector type:  Windows Vista/7: NTFS
    Boot sector info:  No errors found in the Boot Parameter Block.
    Operating System:  Windows 7
    Boot files:        /Windows/System32/winload.exe

============================ Drive/Partition Info: =============================

Drive: sda _____________________________________________________________________

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

Partition  Boot  Start Sector    End Sector  # of Sectors  Id System

/dev/sda1    *          2,048   218,439,679   218,437,632  83 Linux
/dev/sda2         218,439,680   234,440,703    16,001,024  82 Linux swap / Solaris


Drive: sdb _____________________________________________________________________

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
256 heads, 63 sectors/track, 121126 cylinders, total 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes

Partition  Boot  Start Sector    End Sector  # of Sectors  Id System

/dev/sdb1                   1 4,294,967,295 4,294,967,295  ee GPT

/dev/sdb1 ends after the last sector of /dev/sdb

GUID Partition Table detected.

Partition    Start Sector    End Sector  # of Sectors System
/dev/sdb1           2,048       206,847       204,800 EFI System partition
/dev/sdb2         206,848       468,991       262,144 Microsoft Reserved Partition (Windows)
/dev/sdb3         468,992 1,953,523,711 1,953,054,720 Data partition (Windows/Linux)

"blkid" output: ________________________________________________________________

Device           UUID                                   TYPE       LABEL

/dev/sda1        e20b187a-c3b0-45e1-98df-c955b3cffdde   ext4       
/dev/sda2        7663b629-19c9-49fc-a744-2beb2dcdb317   swap       
/dev/sdb1        F428-FB77                              vfat       
/dev/sdb3        162C36632C363DD5                       ntfs       

================================ Mount points: =================================

Device           Mount_Point              Type       Options

/dev/sda1        /                        ext4       (rw,errors=remount-ro)


=========================== sda1/boot/grub/grub.cfg: ===========================

--------------------------------------------------------------------------------
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  set have_grubenv=true
  load_env
fi
set default="0"

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 recordfail {
  set recordfail=1
  if [ -n "${have_grubenv}" ]; then if [ -z "${boot_once}" ]; then save_env recordfail; fi; 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
}

terminal_input console
terminal_output console
if [ "${recordfail}" = 1 ]; then
  set timeout=-1
else
  set timeout=6
fi
### 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
if background_color 44,0,30; then
  clear
fi
### END /etc/grub.d/05_debian_theme ###

### BEGIN /etc/grub.d/10_linux ###
function gfxmode {
  set gfxpayload="${1}"
  if [ "${1}" = "keep" ]; then
    set vt_handoff=vt.handoff=7
  else
    set vt_handoff=
  fi
}
if [ "${recordfail}" != 1 ]; then
  if [ -e ${prefix}/gfxblacklist.txt ]; then
    if hwmatch ${prefix}/gfxblacklist.txt 3; then
      if [ ${match} = 0 ]; then
        set linux_gfx_mode=keep
      else
        set linux_gfx_mode=text
      fi
    else
      set linux_gfx_mode=text
    fi
  else
    set linux_gfx_mode=keep
  fi
else
  set linux_gfx_mode=text
fi
export linux_gfx_mode
if [ "${linux_gfx_mode}" != "text" ]; then load_video; fi
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
recordfail
  gfxmode $linux_gfx_mode
  insmod gzio
  insmod part_msdos
  insmod ext2
  set root='hd0,msdos1'
  if [ x$feature_platform_search_hint = xy ]; then
    search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e20b187a-c3b0-45e1-98df-c955b3cffdde
  else
    search --no-floppy --fs-uuid --set=root e20b187a-c3b0-45e1-98df-c955b3cffdde
  fi
  linux /boot/vmlinuz-3.5.0-27-generic root=UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde ro   quiet splash $vt_handoff
  initrd  /boot/initrd.img-3.5.0-27-generic
}
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
  menuentry 'Ubuntu, with Linux 3.5.0-27-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.5.0-27-generic-advanced-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
  recordfail
    gfxmode $linux_gfx_mode
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e20b187a-c3b0-45e1-98df-c955b3cffdde
    else
      search --no-floppy --fs-uuid --set=root e20b187a-c3b0-45e1-98df-c955b3cffdde
    fi
    echo  'Loading Linux 3.5.0-27-generic ...'
    linux /boot/vmlinuz-3.5.0-27-generic root=UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde ro   quiet splash $vt_handoff
    echo  'Loading initial ramdisk ...'
    initrd  /boot/initrd.img-3.5.0-27-generic
  }
  menuentry 'Ubuntu, with Linux 3.5.0-27-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.5.0-27-generic-recovery-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
  recordfail
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e20b187a-c3b0-45e1-98df-c955b3cffdde
    else
      search --no-floppy --fs-uuid --set=root e20b187a-c3b0-45e1-98df-c955b3cffdde
    fi
    echo  'Loading Linux 3.5.0-27-generic ...'
    linux /boot/vmlinuz-3.5.0-27-generic root=UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde ro recovery nomodeset 
    echo  'Loading initial ramdisk ...'
    initrd  /boot/initrd.img-3.5.0-27-generic
  }
  menuentry 'Ubuntu, with Linux 3.5.0-26-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.5.0-26-generic-advanced-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
  recordfail
    gfxmode $linux_gfx_mode
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e20b187a-c3b0-45e1-98df-c955b3cffdde
    else
      search --no-floppy --fs-uuid --set=root e20b187a-c3b0-45e1-98df-c955b3cffdde
    fi
    echo  'Loading Linux 3.5.0-26-generic ...'
    linux /boot/vmlinuz-3.5.0-26-generic root=UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde ro   quiet splash $vt_handoff
    echo  'Loading initial ramdisk ...'
    initrd  /boot/initrd.img-3.5.0-26-generic
  }
  menuentry 'Ubuntu, with Linux 3.5.0-26-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.5.0-26-generic-recovery-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
  recordfail
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e20b187a-c3b0-45e1-98df-c955b3cffdde
    else
      search --no-floppy --fs-uuid --set=root e20b187a-c3b0-45e1-98df-c955b3cffdde
    fi
    echo  'Loading Linux 3.5.0-26-generic ...'
    linux /boot/vmlinuz-3.5.0-26-generic root=UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde ro recovery nomodeset 
    echo  'Loading initial ramdisk ...'
    initrd  /boot/initrd.img-3.5.0-26-generic
  }
  menuentry 'Ubuntu, with Linux 3.5.0-17-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.5.0-17-generic-advanced-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
  recordfail
    gfxmode $linux_gfx_mode
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e20b187a-c3b0-45e1-98df-c955b3cffdde
    else
      search --no-floppy --fs-uuid --set=root e20b187a-c3b0-45e1-98df-c955b3cffdde
    fi
    echo  'Loading Linux 3.5.0-17-generic ...'
    linux /boot/vmlinuz-3.5.0-17-generic root=UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde ro   quiet splash $vt_handoff
    echo  'Loading initial ramdisk ...'
    initrd  /boot/initrd.img-3.5.0-17-generic
  }
  menuentry 'Ubuntu, with Linux 3.5.0-17-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.5.0-17-generic-recovery-e20b187a-c3b0-45e1-98df-c955b3cffdde' {
  recordfail
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1  e20b187a-c3b0-45e1-98df-c955b3cffdde
    else
      search --no-floppy --fs-uuid --set=root e20b187a-c3b0-45e1-98df-c955b3cffdde
    fi
    echo  'Loading Linux 3.5.0-17-generic ...'
    linux /boot/vmlinuz-3.5.0-17-generic root=UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde ro recovery nomodeset 
    echo  'Loading initial ramdisk ...'
    initrd  /boot/initrd.img-3.5.0-17-generic
  }
}

### 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/30_uefi-firmware ###
### END /etc/grub.d/30_uefi-firmware ###

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

=============================== sda1/etc/fstab: ================================

--------------------------------------------------------------------------------
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=e20b187a-c3b0-45e1-98df-c955b3cffdde /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda2 during installation
UUID=7663b629-19c9-49fc-a744-2beb2dcdb317 none            swap    sw              0       0
--------------------------------------------------------------------------------

=================== sda1: Location of files loaded by Grub: ====================

           GiB - GB             File                                 Fragment(s)

               =                boot/grub/grub.cfg                             1
               =                boot/initrd.img-3.5.0-17-generic               1
               =                boot/initrd.img-3.5.0-26-generic               1
               =                boot/initrd.img-3.5.0-27-generic               1
               =                boot/vmlinuz-3.5.0-17-generic                  1
               =                boot/vmlinuz-3.5.0-26-generic                  1
               =                boot/vmlinuz-3.5.0-27-generic                  2
               =                initrd.img                                     1
               =                initrd.img.old                                 1
               =                vmlinuz                                        2
               =                vmlinuz.old                                    2

=============================== StdErr Messages: ===============================

awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in
awk: cmd. line:36: Math support is not compiled in

总之,Ubuntu 除了驻留的根分区 (sda1) 外,还有一个交换分区 (sda2)。那里一切都很好。Windows7 制作了 3 个分区,第一个 (sdb1) 用于 EFI,第二个 (sdb2) 为不相关的 Windows 保留,第三个 (sdb3) 是实际的 Windows 安装。

sdb1 的文件层次结构为

> ls -aR
.:
.  ..  EFI

./EFI:
.  ..  Boot  Microsoft

./EFI/Boot:
.  ..  bootx64.efi

./EFI/Microsoft:
.  ..  Boot

./EFI/Microsoft/Boot:
.   BCD      BCD.LOG1  bootmgfw.efi  BOOTSTAT.DAT  da-DK  el-GR  es-ES  Fonts  hu-HU  ja-JP  memtest.efi  nl-NL  pt-BR  ru-RU  tr-TR  zh-HK
..  BCD.LOG  BCD.LOG2  bootmgr.efi   cs-CZ         de-DE  en-US  fi-FI  fr-FR  it-IT  ko-KR  nb-NO        pl-PL  pt-PT  sv-SE  zh-CN  zh-TW
etc

通过使用

grub-mkdevicemap

在“/boot/grub”中生成了文件“device.map”,它标识 SSD 是(hd0),HDD 是(hd1)。hd0 是主启动设备,Grub2 位于此处。

在 grub-customizer 中,我尝试创建一个条目,将 grub2 定向到 Windows7 引导加载程序:

set root='(hd1,1)'
search-no-floppy-fs-uuid-set F428-FB77
drivemap -s (hd1) ${root}
chainloader +1

但是,在重新启动时从 grub 中选择它会出现此错误:

error: no such device: F428-FB77.
error: invalid signature.

Press any key to continue..._

我接近了吗?

答案1

您的问题是,您已在 BIOS 模式下安装了 Linux 和 GRUB,而您的 Windows 安装处于 EFI 模式。GRUB 无法切换启动模式,因此您无法对当前的GRUB 安装。您必须添加 EFI 模式 GRUB(或其他 EFI 模式启动程序,如 rEFInd)才能完成此工作。不幸的是,从您的工作安装中执行此操作很棘手,因为它需要使用名为 的 EFI 模式程序efibootmgr,而该程序无法在您当前的 BIOS 模式 Linux 启动中工作,从而产生了先有鸡还是先有蛋的难题。您可以从启动的紧急 Ubuntu 光盘中执行此操作在 EFI 模式下,但这会使 GRUB 设置过程变得复杂。请参阅我关于该主题的网页如果您想尝试此操作,请参阅有关 EFI 引导加载程序的信息以及如何手动安装它们。Ubuntu 的启动修复工具,从 EFI 模式紧急磁盘运行,将能够相对轻松地安装 EFI 模式 GRUB,但我对此并不十分确定。我也不喜欢 Boot Repair,因为它的更改相当笨拙。

另一个选择是放弃 GRUB,转而使用重新索引。如果您在 BIOS 模式下启动 Linux,/dev/sdb1在 处挂载(您的 ESP)/boot/efi,然后安装 Debian 软件包版本的 rEFInd,它应该会在您下次以 EFI 模式启动计算机时自行启动(您可能需要选择“Windows”选项才能启动它)。这通过使用一种变通方法来实现,虽然也有点笨拙,但在我看来,并不像 Boot Repair 那样糟糕。一旦您在 EFI 模式下启动 Linux,它也是可逆的:键入sudo mount /dev/sdb1 /boot/efi再次挂载 ESP(/etc/fstab为此创建一个条目是个好主意,无论您选择哪种解决方案),然后键入sudo mvrefind.sh /boot/efi/EFI/Microsoft/Boot /boot/efi/EFI/refind

第三个选项是在 BIOS 模式下重新安装 Windows。为此,您需要清除 上的分区表/dev/sdb,将其从 GPT 转换为 MBR 格式,然后重新安装 Windows,确保在 BIOS 模式下执行此操作。这是最不优雅的解决方案,但如果您对 Windows 安装感到满意并且 上没有需要保留的数据/dev/sdb,那么它可能是最有可能成功且不会造成复杂情况的解决方案。

相关内容