GRUB/zh-cn

GRUB2 boot loader is a replacement for GRUB version 1, 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.

安装
The package is slotted. Both grub-0.97 (GRUB Legacy) and grub-2.00 may be installed at the same time; however, only one version of GRUB may be installed in a Master Boot Record (MBR) at a time.

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

对于不同的 CPU，所支持的平台如下表所示：

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

Emerge
To install GRUB2 use the normal syntax:

Additional software
Optionally, install the utility (provided through the  package) to have GRUB2 probe for other operating systems when running the  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.

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

配置
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 script is used to generate a grub configuration. It uses the scripts under together with the  configuration file to generate the final  - the only configuration file used by GRUB2 itself.

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 using the  command. This utility will use the scripts in and the settings in.

After modifying one or more settings, run the utility with the   option pointing to the output file located at  (this is GRUB2's default output location):

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

设置配置参数
以下列出了 中最常用的变量，通过设置这些变量，可以控制 GRUB2 的行为:

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

After modifying the parameters, regenerate the GRUB2 configuration file with.

启用或禁用配置脚本
The directory contains the scripts that  uses to generate a  file. By default the contents of this directory should be similar to the following:

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

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

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

将 脚本修改为:

在上面的脚本中， 或者  FreeBSD 是系统所驻留的分区. 如果正常安装了 UFS 文件系统用于 FreeBSD 系统分区，那么 就成为了一个容器 （有点像一个逻辑分区）. 这个容器由根分区和交换分区组成. 接着使用 命令确认 脚本是可执行文件. 如果该脚本没有可执行权限，请使用 命令设置.

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

安装引导程序
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
 * BIOS 引导，使用 GPT
 * UEFI 引导，使用 GPT

为操作系统选择适当的安装指令.

BIOS 引导，使用 MBR
确认  可用 - 如果该目录使用了单独的分区，请确保该分区已经挂载.

Run the command to copy the relevant files to. 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 an output such as the one below is to be expected:

accepts a  option to set the CPU architecture and system platform. If unspecified, will attempt to guess the proper values; on an / system it will use   by default. also accepts a  option to tell the GRUB2 installer which directory to look for GRUB2's boot files. This defaults to the current 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
如果需要使用 分区，首先请挂载该  分区:

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

accepts a  option to set the CPU architecture and system platform. If unspecified, will attempt to guess the correct values; on an / system it will use   by default. also accepts a  option to tell the GRUB2 installer which directory to look in for GRUB2's boot files. This defaults to the current 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  (which is different from the EFI System Partition (ESP) which has type  ) 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 command will overwrite any existing filesystem with one of its own.

要将某个分区设置为 BIOS 分区，可以使用命令行工具 ，执行以下指令即可（把其中的   修改为拟作为 BIOS 引导分区的相应编号）：

在使用 包的  工具时，完成这项工作，需要通过设置该分区类型为   并为其设置   标签.

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 格式磁盘使用 工具按下 后的输出信息，该磁盘同时包含 BIOS 引导分区 [0xEF02] 和 EFI 分区 [0xEF00] ：

对于相同的设置， 工具的输出会稍微有些不同之处：

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

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

UEFI 引导，使用 GPT
首先确保 目录可用 - 如果是使用一个单独的分区，请确保已经正确挂载到  ：

Run the command to copy the relevant files to. This should install GRUB2 in, copy the core image to , and call efibootmgr to add a boot entry.

The above command assumes the vfat EFI System Partition (ESP) is mounted at. If the ESP is mounted directly at, use.

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

为 UEFI 引导程序设置 GPT 分区
使用 GRUB2 引导 UEFI GPT 系统时， 系统 必须 有一个包含 FAT 文件系统的独立 EFI 分区.

The EFI partition can replace having a partition on  by having a  partition on. 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 folder will be located in the root  partition (at ) and the EFI partition will mount in the boot folder (at ). For further clarification, see the example file below.

创建一个100MB的 分区将会有足够的空间容纳下多个  文件（当然，大部分系统只会使用一个入口，多个入口并非必须的）.

Create the partition using the partitioning tool of choice. The  and   tools fit nicely for this purpose. When using the utility, be sure to use type.

紧接着，请使用  在 EFI 系统分区上创建 FAT 文件系统，并参照以下示例将该分区加入  配置文件.

备选方案: 使用默认的 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 then copy the file  located at  to. This example assumes a 64-bit UEFI system, adjust accordingly for 32-bit UEFI systems.

扩展特性
GRUB2有很多特性使得其成为非常强大的启动引导器. 它支持：


 * 从UEFI平台引导.
 * 从GPT分区的磁盘引导，不需要hybrid MBR (hybrid MBR可以出于兼容性和便携性的需求而被使能).
 * 从 btrfs 格式的 引导分区引导.
 * 直接从btrfs磁盘陈列引导而不需要initramfs服务于早期的挂载设置.
 * 直接从逻辑管理卷引导，(如LVM2).
 * 引导并支持DM-RAID (RAID 0, 1, 4, 5, 6, 9和10).
 * 从加密的设备引导(LUKS).

一些特殊的特性在下面将被详细的解释.

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

要想了解chainloading的更多信息，可以查看Chainloading子页面.

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

执行配置缺省的GRUB2位于的配置文件. 例如：

为了找到您的系统显卡所支持的显示模式，在GRUB2的shell里使用下面的命令行：

Troubleshooting
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 was correctly generated with, or generate one with a custom menu entry.

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

os-prober and UEFI in chroot
The 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 like in the Handbook):

外部资源
更多的信息，请参见：


 * GNU GRUB 2 manual page
 * Network (PXE) section of GRUB2
 * Legacy BIOS issues with GPT article
 * GPT and Hybrid MBR article
 * GPT fdisk utility page
 * Arch Linux GRUB2 wiki article
 * Fedora GRUB2 wiki article : Encountering the dreaded GRUB2 boot prompt
 * ubuntu UEFI booting help
 * http://unix.stackexchange.com/questions/109272/dualboot-freebsd-gentoo-with-grub2-mbr
 * A blog post entry on locking specific GRUB2 boot entries with a password