From Gentoo Wiki
Jump to: navigation, search
This page is a translated version of the page GRUB2 and the translation is 51% complete.

Other languages:
English • ‎español • ‎français • ‎日本語 • ‎한국어 • ‎polski • ‎português do Brasil • ‎русский • ‎中文(中国大陆)‎

GRUB2 (the GRand Unified Bootloader version 2) is a replacement for the original GRUB boot loader, now referred to as "GRUB Legacy". GRUB2 has an entirely separate code base from GRUB Legacy, and features a new shell-like syntax for advanced scripting capabilities.

要获取大体上的了解, 请查看 GRUB2 Quick Start.

If migrating from GRUB Legacy to GRUB2, see GRUB2 Migration.


The sys-boot/grub package is slotted. Both grub-0.97 (GRUB Legacy) and grub-2.02 may be installed at the same time; however, only one version at a time may be installed in the Master Boot Record (MBR) of a hard drive.


默认情况下,GRUB2 将会在创建时探测默认的启动平台。也可以通过在 make.conf 中设置GRUB_PLATFORMS 变量来指定特定平台。

FILE /etc/portage/make.confExample of setting the GRUB_PLATFORMS variable for EMU, EFI, and PC platforms
GRUB_PLATFORMS="emu efi-32 efi-64 pc"

对于不同的 CPU,所支持的平台如下表所示:

Platform i386 ia64 mips mipsel powerpc spac64 x86_64
arc No No No Yes No No No
coreboot Yes No No No No No 32-bit
efi Yes Yes No No No No Yes
emu Yes Yes Yes Yes Yes Yes Yes
ieee1275 Yes No No No Yes Yes 32-bit
loongson No No No Yes No No No
multiboot Yes No No No No No 32-bit
qemu Yes No No No No No 32-bit
qemu-mips No No Yes No No No No
pc Yes No No No No No 32-bit
Whenever the values in the GRUB_PLATFORMS variable are adjusted GRUB2 will need to be re-emerged in order to build the changed binary. Be sure to use the --newuse --deep options as shown in the emerge section below.

The amd64 profiles enable support for (U)EFI functionality by default. When using a BIOS-based system, set GRUB_PLATFORMS variable to emu to avoid unneeded dependencies.

USE flags

USE flags for sys-boot/grub GNU GRUB boot loader

debug Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see global
device-mapper Enable support for device-mapper from sys-fs/lvm2 local
doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally global
efiemu Build and install the efiemu runtimes local
fonts Build and install fonts for the gfxterm module local
libzfs Enable support for sys-fs/zfs local
mount Build and install the grub-mount utility local
multislot Allow concurrent installation of sys-boot/grub SLOT 0 and sys-boot/grub SLOT 2 by renaming all programs. local
nls Add Native Language Support (using gettext - GNU locale utilities) global
sdl Add support for Simple Direct Layer (media library) global
static !!do not set this during bootstrap!! Causes binaries to be statically linked instead of dynamically global
test Workaround to pull in packages needed to run with FEATURES=test. Portage-2.1.2 handles this internally, so don't set it in make.conf/package.use anymore global
themes Build and install GRUB themes (starfield) local
truetype Build and install grub-mkfont conversion utility local


To install GRUB2 use the normal emerge syntax:

root #emerge --ask --newuse --deep sys-boot/grub:2

Additional software

Optionally, install the os-prober utility (provided through the sys-boot/os-prober package) to have GRUB2 probe for other operating systems when running the grub-mkconfig command. In most instances, this will enable GRUB2 to automatically detect other operating systems including Windows 7, 8.1, 10, other distributions of Linux, etc.

root #emerge --ask --newuse sys-boot/os-prober

GRUB2(以及可选包 sys-boot/os-prober)的安装程序不会自动启用引导程序,它仅仅完成操作系统相关软件的安装。要将引导程序安装到系统并负责引导操作系统启动,仍需要执行其他一些相关步骤,相关说明在 配置 章节中有详细描述。


There are two important aspects to the configuration of GRUB2:

  1. Installation of GRUB2 software as the boot loader of the system.
  2. Configuration of the GRUB2 boot loader.

GRUB2 软件的安装过程与系统的类型有关,相关描述请参考 安装引导程序。我们这里先来看看 GRUB2 引导程序自己的配置。


The grub-mkconfig script is used to generate a grub configuration. It uses the scripts under /etc/grub.d/* together with the /etc/default/grub configuration file to generate the final /boot/grub/grub.cfg - the only configuration file used by GRUB2 itself.

File Format Edits recommended? Description
/usr/sbin/grub-mkconfig POSIX shell script No Installed as part of the sys-boot/grub:2 package. Run this script to generate /boot/grub/grub.cfg after configuring the files described below.
/boot/grub/grub.cfg GRUB2 shell script No The file generated by grub-mkconfig. This file is evaluated by GRUB2's built-in script interpreter and doesn't necessarily support all POSIX commands or syntax. See the scripting reference in the GRUB manual for supported features. Be aware that modifications to this file won't persist to the next time grub-mkconfig is run.
/etc/grub.d/* POSIX shell scripts Maybe Each script under /etc/grub.d/* that has its execute bit set is evaluated in sequence, and the stdout is concatenated to form the final /boot/grub/grub.cfg (or whatever file is given to the grub-mkconfig -o option). These scripts use the current system shell and therefore can use any supported syntax. Ideally they should be POSIX-compatible scripts, and the output script must be compatible with the GRUB2 interpreter. It may be necessary to disable or add scripts. For instance, to add menu items that couldn't be automatically generated.
/boot/grub/custom.cfg GRUB2 shell script Maybe The /etc/grub.d/41_custom script will reference this file to be read in at boot time if it exists. This file provides a place to add additional entries or commands and does not require regeneration of the main grub.cfg file.
/etc/default/grub POSIX shell script Yes In most cases this is the only file that should be modified directly. It is mainly used to assign variables used by the scripts in /etc/grub.d to generate a working configuration file. See GRUB2 configuration variables or the official reference for supported variables.

GRUB2 does not require the administrator to manually maintain a boot option configuration (as is the case with boot loaders such as GRUB Legacy and LILO). Instead it can generate its configuration file (/boot/grub/grub.cfg) using the grub-mkconfig command. This utility will use the scripts in /etc/grub.d/ and the settings in /etc/default/grub.

The grub-mkconfig utility does not work properly when using software RAID. Manual configuration of the scripts in /etc/grub.d/ is necessary, as otherwise after installation the system will be left in a non-bootable state.

After modifying one or more settings, run the grub-mkconfig utility with the -o option pointing to the output file located at /boot/grub/grub.cfg (this is GRUB2's default output location):

root #grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.3.0-gentoo

Each time the grub-mkconfig utility is called a new configuration will be generated.

If grub-mkconfig does not report any found entries then no entries were found. In this case GRUB2 will offer no boot selections when upon system restart which may be a tricky, time consuming situation to resolve. Make sure the output is satisfactory before restarting the system.


以下列出了 /etc/default/grub 中最常用的变量,通过设置这些变量,可以控制 GRUB2 的行为:

Variable Explanation Default value
GRUB_DEFAULT Defines the default menu entry selected on boot. May be a numeric index, a menu title, or "saved". Defaults to first detected entry.
GRUB_TIMEOUT Delay (in seconds) before booting default menu entry. Set to 0 to boot immediately or -1 to wait indefinitely. The default is 5 seconds.
GRUB_CMDLINE_LINUX Parameters to be passed on the kernel command line for all Linux menu entries. For instance, to support hibernation, users will need to add GRUB_CMDLINE_LINUX="resume=/dev/sdXY" with /dev/sdXY being the swap partition.
GRUB_CMDLINE_LINUX_DEFAULT Parameters to be passed on the kernel command line for non-recovery Linux menu entries.
GRUB_DEVICE The initial root device (i.e. the kernel's root= parameter). Set this to override the grub-mkconfig command's root device auto-detection. For example, GRUB_DEVICE=/dev/ram0 will force root=/dev/ram0 to be used in the kernel command line.

需获取更为完整的参数列表,请参考 GRUB2 configuration variables

After modifying the parameters, regenerate the GRUB2 configuration file with grub-mkconfig.


The directory /etc/grub.d/ contains the scripts that grub-mkconfig uses to generate a grub.cfg file. By default the contents of this directory should be similar to the following:

user $ls /etc/grub.d/
00_header  10_linux  20_linux_xen  30_os-prober  40_custom  41_custom README

GRUB2 将会执行该目录中所有标记为可执行的脚本文件(默认情况下,该目录中所有脚本都被标记为可执行文件)。要禁用其中某些脚本,可以直接使用 chmod 命令清除它们的可执行权限位。 下面给出的例子中,禁用了除 00_header10_linux 之外的其他脚本:

root #chmod -x /etc/grub.d/{20_linux_xen,30_os-prober,40_custom,41_custom}

After modifying the scripts (or removing the executable bit), regenerate the GRUB2 configuration file using grub-mkconfig.


一些 GRUB2 的功能或特性只能通过修改配置脚本来获得。例如,要支持 FreeBSD 的双启动,应该对脚本作出如下调整。

/etc/grub.d/40_custom 脚本修改为:

FILE /etc/grub.d/40_customAdding an entry for dual booting
menuentry "FreeBSD" --class freebsd --class bsd --class os {
 insmod ufs2
 insmod bsd
 set root=(hd0,1)
 kfreebsd /boot/kernel/kernel
 kfreebsd_loadenv /boot/device.hints
 set kFreeBSD.vfs.root.mountfrom=ufs:/dev/ada0s1a
 set kFreeBSD.vfs.root.mountfrom.options=rw
 set kFreeBSD.hw.psm.synaptics_support=1

在上面的脚本中,/dev/sda1 或者 (hd0,1) FreeBSD 是系统所驻留的分区。 如果正常安装了 UFS 文件系统用于 FreeBSD 系统分区,那么 /dev/sda1 就成为了一个容器 (有点像一个逻辑分区)。 这个容器由根分区和交换分区组成。接着使用 ls -la /etc/grub.d/40_custom 命令确认40_custom 脚本是可执行文件。如果该脚本没有可执行权限,请使用 chmod u+x 40_custom 命令设置。

Users familiar with how GRUB Legacy numbered partitions should note that GRUB2 numbers partitions starting from 1, not 0.

Next install GRUB2 using the grub-install command and update GRUB2's configuration file:

root #grub-install /dev/sda
root #grub-mkconfig -o /boot/grub/grub.cfg


Installing GRUB2 as the system's boot loader depends on how the system is meant to boot (through BIOS or UEFI) and how the disk on which the boot loader should be installed is partitioned (using MBR or GPT partition layout).



BIOS 引导,使用 MBR

When the system is meant to dual-boot with Microsoft Windows, make sure that the system itself does not have an UEFI firmware. Even when such systems are booted in 'legacy BIOS' mode, Microsoft Windows will refuse to boot.

确认 /boot 可用 - 如果该目录使用了单独的分区,请确保该分区已经挂载。

root #mount /boot

Run the grub-install command to copy the relevant files to /boot/grub. On the PC platform, this also installs a boot image to the Master Boot Record (MBR) or a partition's boot sector. If all goes well, after running grub-install an output such as the one below is to be expected:

root #grub-install /dev/sda
Installation finished. No error reported.

grub-install accepts a --target option to set the CPU architecture and system platform. If unspecified, grub-install will attempt to guess the proper values; on an amd64/x86 system it will use i386-pc by default. grub-install also accepts a --boot-directory option to tell the GRUB2 installer which directory to look for GRUB2's boot files. This defaults to the current /boot but is useful when trying to move a root partition.

为 BIOS 引导程序设置 MBR 分区

Be sure to leave enough free space before the first partition. Starting the first partition at sector 2048 leaves at least 1 MiB of disk space for the master boot record. It is recommended (but not mandatory) to create an additional partition for GRUB called the BIOS boot partition. This partition just needs to be defined, but not formatted. It is only needed if the system is later migrated to the GPT partition layout. When sticking with MBR, this is not needed.

如果是按照 Gentoo installation instructions 中的步骤进行操作,那么该 BIOS boot partition 应该已经创建好。

BIOS 引导,使用 GPT

When the system is meant to dual-boot with Microsoft Windows, make sure that the system itself does not have an UEFI firmware. Even when such systems are booted in 'legacy BIOS' mode, Microsoft Windows will refuse to boot. Also, older Microsoft Windows systems might not support GPT. It is possible to use a hybrid MBR-GPT approach; see Hybrid partition table.

如果需要使用 /boot 分区,首先请挂载该 /boot 分区:

root #mount /boot

If all goes well, after running the grub-install command an output such as the one below is to be expected:

root #grub-install /dev/sda
Installation finished. No error reported.

grub-install accepts a --target option to set the CPU architecture and system platform. If unspecified, grub-install will attempt to guess the correct values; on an amd64/x86 system it will use i386-pc by default. grub-install also accepts a --boot-directory option to tell the GRUB2 installer which directory to look in for GRUB2's boot files. This defaults to the current /boot but is useful when trying to move a root partition.

为 BIOS 引导程序设置 GPT 分区

When a GPT partition table is present on the system, a small BIOS boot partition with type EF02 (which is different from the EFI System Partition (ESP) which has type EF00) will need to be available. 1 MiB will be enough to work, but 2-4 MiB is a safer option. This BIOS boot partition will hold the stage 2 of the bootloader. BIOS boot partitions do not need to be formatted with a filesystem; the grub-install command will overwrite any existing filesystem with one of its own.

BIOS 引导分区与常规情况下挂载的/boot 不是 同一个分区。/boot 和 BIOS 引导区是不同的分区,应该分别处理。BIOS 引导分区一般 不应该 挂载到系统中(就是说它“不应该”在 /etc/fstab 中被定义)。而 /boot 分区毫无疑问 “可以” 常态地挂载到系统上,因此可以在 /etc/fstab 文件中添加其定义。

要将某个分区设置为 BIOS 分区,可以使用命令行工具 parted (sys-block/parted),执行以下指令即可(把其中的 1 修改为拟作为 BIOS 引导分区的相应编号):

(parted)set 1 bios_grub on

在使用 sys-apps/gptfdisk 包的 cgdisk 工具时,完成这项工作,需要通过设置该分区类型为 0xEF02 并为其设置 gptbios 标签。

An EFI System Partition is not required, but it would be sensible to make sure that the BIOS boot partition is large enough to be converted to one, should the system motherboard later be upgraded to an UEFI board.

下面是在对一个 GPT 格式磁盘使用 gdisk 工具按下p 后的输出信息,该磁盘同时包含 BIOS 引导分区 [0xEF02] 和 EFI 分区 [0xEF00] :

root #gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.1
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): p
Disk /dev/sdc: 976773168 sectors, 465.8 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): AA369F4D-37A4-4C0D-A357-DC24B99A6337
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 976773134
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048       828377087   395.0 GiB   8E00  Linux LVM
   2       828377088       891291647   30.0 GiB    0700  Microsoft basic data
   3       891291648       975177727   40.0 GiB    0700  Microsoft basic data
   4       975177728       976754687   770.0 MiB   8300  Linux filesystem
   5       976754688       976756735   1024.0 KiB  EF02  BIOS boot partition
   6       976756736       976773134   8.0 MiB     EF00  EFI System
Command (? for help): 
如果是使用 fdisk,那么无需为 GPT 类型码输入十六进制前缀 0x

对于相同的设置, parted 工具的输出会稍微有些不同之处:

root #parted /dev/sdc
GNU Parted 3.0
Using /dev/sdc
(parted) print
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start   End    Size    File system  Name                  Flags
 1      1049kB  424GB  424GB                Linux LVM             lvm
 2      424GB   456GB  32.2GB               Microsoft basic data
 3      456GB   499GB  42.9GB               Microsoft basic data
 4      499GB   500GB  807MB   ext2         Linux filesystem
 5      500GB   500GB  1049kB               BIOS boot partition   bios_grub
 6      500GB   500GB  8396kB               EFI System            boot

对于那些已经熟悉 fdisk 磁盘分区工具的用户而言,通过 gdisk 创建磁盘分区显得更为直接。启动 gdisk,在主菜单下输入 n (创建新分区),指定开始和结束的扇区(如果需要),然后设置磁盘类型为 EF00 ,即 EFI 系统分区。

按照 Gentoo 安装说明 操作的用户,应该已经按照要求正确地设置好了磁盘分区结构。

UEFI 引导,使用 GPT

首先确保 /boot 目录可用 - 如果是使用一个单独的分区,请确保已经正确挂载到 /boot

root #mount /boot

Run the grub-install command to copy the relevant files to /boot/grub. This should install GRUB2 in /boot/grub, copy the core image to /boot/efi/EFI/gentoo/grubx64.efi, and call efibootmgr to add a boot entry.

root #grub-install --efi-directory=/boot/efi
Installation finished. No error reported.

The above command assumes the vfat EFI System Partition (ESP) is mounted at /boot/efi. If the ESP is mounted directly at /boot, use --efi-directory=/boot.

grub-install also accepts a --target option to set the CPU architecture and system platform. If unspecified, grub-install will attempt to guess the proper values; on an AMD64 UEFI-booted system it will use x86_64-efi by default. grub-install also accepts a --boot-directory option to tell the GRUB2 installer which directory to look for GRUB2's boot files. This defaults to /boot but is useful when trying to move a root partition.

为 UEFI 引导程序设置 GPT 分区

使用 GRUB2 引导 UEFI GPT 系统时, 系统 必须 有一个包含 FAT 文件系统的独立 EFI 分区。

The EFI partition can replace having a /boot partition on /dev/sda1 by having a /boot/efi partition on /dev/sda1. This is to say a successful UEFI boot scenario using GRUB2 can operate with two partitions total (three total if a swap partition is needed): a root partition and an EFI partition. Using this configuration, the /boot folder will be located in the root / partition (at /boot) and the EFI partition will mount in the boot folder (at /boot/efi). For further clarification, see the example /etc/fstab file below.

FILE /etc/fstabExample of an UEFI capable /etc/fstab file with a swap partition:
/dev/sda1		/boot/efi	vfat		noauto,noatime	1 2
/dev/sda2		none		swap		sw		0 0
/dev/sda3		/		ext4		noatime		0 1

创建一个100MB的 /boot/efi 分区将会有足够的空间容纳下多个

  • .efi 文件(当然,大部分系统只会使用一个入口,多个入口并非必须的)。

Create the partition using the partitioning tool of choice. The gdisk (sys-apps/gptfdisk) and parted (sys-block/parted) tools fit nicely for this purpose. When using the gdisk utility, be sure to use type EF00.

紧接着,请使用 mkdosfs 在 EFI 系统分区上创建 FAT 文件系统,并参照以下示例将该分区加入 /etc/fstab 配置文件。

root #mkdosfs -F 32 -n efi-boot /dev/sda1
root #mkdir /boot/efi
FILE /etc/fstabAdding the /boot/efi mount entry
/dev/sda1		/boot/efi	vfat		noauto,noatime	1 2
root #mount /boot/efi
It is helpful to set the GRUB_PLATFORMS variable in /etc/portage/make.conf. This will assist GRUB2 in determining what options to use when detecting the proper EFI target. For 32-bit UEFI systems use efi-32. For 64-bit use efi-64.
In order for GRUB2 to install properly, the EFI directory must be mounted and the efivars kernel module must be loaded before the grub-install command will complete successfully.

备选方案: 使用默认的 UEFI 固件位置

If the system's UEFI firmware fails to find GRUB2's EFI bootloader file, using the default boot loader location should provide a working solution. This circumvents the boot menu managed by efibootmgr and thus offers reduced functionality, but is less error prone. To do this, verify the EFI partition is mounted at /boot/efi then copy the file grubx64.efi located at /boot/efi/EFI/gentoo/grubx64.efi to /boot/efi/EFI/BOOT/BOOTX64.EFI. This example assumes a 64-bit UEFI system, adjust accordingly for 32-bit UEFI systems.



  • Booting from UEFI platforms.
  • Booting from GPT partitioned drives without needing a hybrid MBR (hybrid MBR can enabled as needed for compatibility or portability).
  • Booting from a btrfs formatted /boot partition.
  • Booting from a ZFS pool.
  • Booting directly from a btrfs raid set without needing an initramfs for early mount setup.
  • Booting directly from logical volume management (such as LVM2).
  • Booting with support for DM-RAID (RAID 0, 1, 4, 5, 6, 9 and 10).
  • Booting from encrypted devices (LUKS).



与传统的GRUB相比,GRUB2被构建成为更好地支持了chainload模式。如要chain load另外一个启动引导器,您可以使用chainloader 选项。

FILE /etc/grub.d/40_customChainloading another bootloader
menuentry "Custom Super-bootloader example" {
     insmod part_msdos
     insmod chain
     chainloader (hd1,1)+1



为了让GRUB2使用framebuffer图形界面显示,重新emerge GRUB,使能USE标记truetype。这个将会安装缺省的True Type字体以及一个字体转换工具。

root #emerge --ask --newuse sys-boot/grub:2


FILE /etc/default/grubFramebuffer related settings
# Set resolution and color depth
# Keep resolution when loading the kernel
# Set a background image
# Use a custom font, converted using grub-mkfont utility


(grub)insmod all_video


Most of the issues can be resolved by ensuring that the partition layout is correct. Make sure enough space is available before the first partition of the disk, or optionally make sure that a BIOS boot partition is available. Also verify that /boot/grub/grub.cfg was correctly generated with grub-mkconfig, or generate one with a custom menu entry.

想要了解更多关于GRUB2排障的情况,请参考Troubleshooting 子文章。

os-prober and UEFI in chroot

The sys-boot/os-prober utility is used to discover alternate installs, such as Microsoft Windows. To function properly, it needs to have access to information from the live environment's udev to test for the EFI System Partition.

Run these commands in the host environment to provide the required files (example shows Gentoo mounted on /mnt/gentoo like in the Handbook):

root #mkdir -p /mnt/gentoo/run/udev
root #mount -o bind /run/udev /mnt/gentoo/run/udev
root #mount --make-rslave /mnt/gentoo/run/udev

Installing a new kernel

Whenever a new kernel is installed, GRUB2 must be reconfigured to recognize it. This can be done using grub-mkconfig, as shown below, or can be done manually.

Make sure the /boot partition is mounted for this step.
root #grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/kernel-3.3.8-gentoo
Found initrd image: /boot/initramfs-genkernel-x86_64-3.3.8-gentoo
Found linux image: /boot/kernel-3.2.12-gentoo
Found initrd image: /boot/initramfs-genkernel-x86_64-3.2.12-gentoo

Note that GRUB2 only requires to be reconfigured, not reinstalled to the boot drive's Master Boot Record (MBR). On the other hand, when GRUB2 itself has been upgraded it does need to be reinstalled on the boot drive, but usually does not need to be reconfigured.

See also


  • In Chainloading 描述了使用GRUB2引导其它的boot loaders,对于双引导系统或者当GRUB2需要被配置成从ISO文件引导时是非常值得阅读的。
  • In Advanced storage怎样在更高级的存储条件,例如软RAID,逻辑卷或者加密文件系统中安装和使用GRUB2的必要步骤被归档在这里。
  • In Configuration variables/etc/default/grub所使用的GRUB2配置变量的详细清单被归档在这里。
  • In Troubleshooting GRUB2的常错误(和它们的解决方案)被列在这里。
  • In Hybrid partition table 如何使用一个混合的MBR/GPT设置被归档在这里,连同怎样与GRUB2一道使用这样一个混合的分区布局一起。