GRUB/zh-cn

GRUB2 是一个支持现代操作系统的引导程序. 它是为了取代旧的GRUB 引导程序而设计的 (现在被称作 "GRUB Legacy"). GRUB2 拥有一套基于传统 GRUB Legacy 的独立代码，以及丰富的特性，比如全新的具备编制脚本的 shell 风格语法. 推荐当前的 GRUB Legacy 用户将系统迁移到 GRUB2 下.

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

安装
软件包已经纳入 Gentoo 系统中. 你可以同时安装 grub-0.97 (GRUB Legacy) 和 grub-2.00, 但是一次只能安装一个版本的 GRUB 到 MBR 中. 本文重点关注 GRUB2.

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

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

使用一般的 emerge 指令安装 GRUB2 的语法：

你也可以选择通过安装 工具（通过 包提供），使得 GRUB2 可以在运行  命令时探测其他操作系统. 在大多数情况下，这样可以让 GRUB2 自动探测到各类常见的操作系统（比如Windows 7, Windows 8.1, 等等）.

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

配置
配置 GRUB2 有两个主要的工作：
 * 1) 将 GRUB2 作为引导程序安装到系统上.
 * 2) 对 GRUB2 的引导程序进行配置.

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

主要配置文件
脚本用于生成 grub 的配置. 它综合使用 和  中的相关配置文件生成最终的  - GRUB2 所使用的唯一配置文件.

使用 GRUB2 无需管理员手动维护启动选项配置（像 GRUB Legacy 和 LILO 等引导程序所需要那样）. 相反，它可以通过使用 命令自动生成配置文件. 该工具会综合利用 中的脚本文件和  中的配置参数.

如果修改了一个或多个设置，在运行 工具时请使用  选项将输出指定到  （这是 GRUB2 默认的输出位置）:

每运行一次 工具都会生成一个新的配置文件.

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

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

配置完参数后，使用 命令重新生成 GRUB2 配置文件.

启用或禁用配置脚本
目录中包含了 用来生成  配置文件的一系列脚本. 默认情况下，该目录中看起来应该类似于下面的内容：

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

修改完这些脚本（或者通过清除可执行权限禁用这些脚本）后，请使用 重新生成配置文件.

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

将 脚本修改为:

or  is the partition in which FreeBSD resides. If the normal UFS install was used for the FreeBSD partition then is a container (something like a logical partition). It consists of the swap and root partition. Verify the script is executable by running. If the executable bit is not set then set it using the command.

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

Installing the boot loader
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).

This article covers the following situations:


 * BIOS with MBR
 * BIOS with GPT
 * UEFI with GPT

Select the installation instructions appropriate for the system.

BIOS with MBR
Make sure that the location is available - if this uses a separate partition, make sure that it is mounted:

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.

Partitioning for BIOS with 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.

If the Gentoo installation instructions were followed, this BIOS boot partition will already be available.

BIOS with GPT
If a partition is needed, start by mounting the  partition:

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 at the correct values; on an / system it will use   by default. also accepts a  option to tell the GRUB2 installer which directly to look for GRUB2's boot files. This defaults to the current but is useful when trying to move a root partition.

Partitioning for BIOS with 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 formatted with a filesystem; the command will overwrite any pre-existing filesystem with one of its own.

To set a partition as a BIOS partition use the command line tool  by typing (change   to the number of the partition to mark as a BIOS boot partition!):

With 's utility, this is accomplished by setting the partition type to   and give it a label of.

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 EFI board.

The following is the output of pressing the key using the  utility on a GPT partitioned disk with both a BIOS boot [0xEF02] partition and an EFI [0xEF00] partition:

Using the same setup, the utility gives an output with slightly different syntax:

Creating partitions in is straight forward for users already familiar with the  partitioning utility. After starting, type (for new) in the main menu, provide beginning and end sectors (if needed), then set the partition type   for an EFI system partition.

Users who have followed the Gentoo installation instructions will already have the proper partitioning layout set up.

UEFI with GPT
Make sure that the location is available - if this uses a separate partition, make sure that it is mounted:

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 EFI-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.

Partitioning for UEFI with GPT
For UEFI GPT boot using GRUB2, the system must have a dedicated EFI partition containing a FAT filesystem.

The EFI partition can replace having a partition on  by having a  partition on. This is to say a successful EFI 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.

Generating a 100MB partition for should provide plenty of space for holding multiple  files (multiple entries will most likely not be needed; most systems will only use one).

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.

Proceed to create a FAT filesystem on the EFI system partition using and add it to  by following the example below:

Alternative: using the default UEFI firmware location
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 EFI system, adjust accordingly for 32-bit EFI systems.

Extended features
GRUB2 has many features that make it a very powerful boot loader. It supports:


 * 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 partition.
 * 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).

Some specific features are explained in more detail next.

Chainloading
GRUB2 was built with a truly improved chainload mode when compared to GRUB Legacy. To chainload another boot loader, use the  option.

For more information on chainloading, please see the Chainloading sub-page.

Using framebuffer display
To have GRUB2 use a framebuffer graphical display, re-emerge GRUB with the  USE flag enabled. This will install a default True Type font as well as a font conversion utility.

Proceed to configure the default GRUB2 configuration file located at. For example:

In order to find out what display modes the system's graphics card supports, use the following commands on the 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.

For more GRUB2 troubleshooting, please refer to the Troubleshooting sub-article.

External resources
For more information, please see:


 * 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