GRUB

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page GRUB and the translation is 68% complete.
Outdated translations are marked like this.

GRUB 2GRand Unified Bootloader version 2),有时记为 GRUB 2,通常称为 GRUB,是一个多引导辅助引导加载程序,能够从大多数系统架构上的多种文件系统中加载内核。GRUB 支持 PC BIOS,PC EFI,IEEE 1275(开放硬件),SPARC 和 MIPS Lemote Yeeloong。

GRUB 使用一个完全独立的代码库取代了原来的 GRUB Legacy 引导加载程序,该代码库具有用于高级脚本功能的新的类似 shell 的语法。

若要快速设置, 请查看 GRUB2 Quick Start

若将系统从 GRUB Legacy 迁移,请查看 GRUB2 Migration

安装

准备

使用grub-install 时,GRUB会根据make.conf 中设置的目标架构 来控制GRUB_PLATFORMSamd64 架构包含了适用于大多数系统的默认配置文件。

文件 /etc/portage/make.conf设置 GRUB_PLATFORMS 变量以支持 EMU, EFI, 和 PC 平台的示例
GRUB_PLATFORMS="emu efi-32 efi-64 pc"

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

平台 架构
i386 ia64 mips mipsel powerpc sparc64 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
IEEE 1275 (Open Firmware) 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 GRUB 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 pc to avoid unneeded dependencies.

USE 标记

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

device-mapper Enable support for device-mapper from sys-fs/lvm2
doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
efiemu Build and install the efiemu runtimes
fonts Build and install fonts for the gfxterm module
libzfs Enable support for sys-fs/zfs
mount Build and install the grub-mount utility
nls Add Native Language Support (using gettext - GNU locale utilities)
sdl Add support for Simple Direct Layer (media library)
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
themes Build and install GRUB themes (starfield)
truetype Build and install grub-mkfont conversion utility
verify-sig Verify upstream signatures on distfiles

Emerge

使用 emerge 安装 GRUB:

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

附加软件

你也可以选择通过安装 os-prober 工具(由 sys-boot/os-prober 包提供),使得 GRUB 可以在运行 grub-mkconfig 命令时检测到其他操作系统并生成启动项。在大多数情况下,这样可以让 GRUB 自动探测到各类常见的操作系统(包括 Windows 7, Windows 8.1, 10,其他 Linux 发行版等等)。

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

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

安装GRUB引导程序

安装 GRUB 作为系统的引导程序,重点考虑操作系统通过何种方式启动(电脑使用着哪种固件,举例:旧版的BIOS还是后继版本的UEFI),以及准备安装引导程序的磁盘的分区格式是什么样的(是使用 MBR 还是 GPT 分区布局)。

本文讨论以下情况:

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

使用 GPT 引导UEFI

警告
Even though 64-bit x86 processors ("x86-64" or "x64") support running legacy 32-bit software, it is not possible for an EFI implementation to do the same. A 64-bit EFI will not be able to run 32-bit .efi loaders! It should be noted that some early 64-bit-capable systems, i.e. systems with an x64 processor (an x86 processor featuring AMD64 or Intel 64), used a 32-bit implementation of EFI, like some early MacBooks (Intel Core 2) and some pre-2010 Windows computers. In such cases, even though the processor is 64-bit, (U)EFI is implemented as 32-bit software making efi-32 the proper EFI target because, likewise, a 32-bit EFI cannot run 64-bit .efi loaders.
附注
If the UEFI-CSM is used, refer to BIOS with MBR or BIOS with GPT instead. CSM stands for "Compatibility Support Module" and is a BIOS emulation, making the UEFI behave like a BIOS. In the firmware setup it is often called "Legacy Mode" or "Compatibility Mode". UEFI-CSM has always been optional and was phased out on mainstream systems (such as PCs) in 2020.

为 UEFI 引导程序设置 GPT 分区

重要
使用 UEFI GPT 启动时, 系统 必须 有一个包含 FAT 文件系统的独立 EFI系统分区

UEFI systems boot from efi files on their EFI System Partition (ESP). The EFI System Partition can be just about any size, with implementation considerations in mind.

The EFI System Partition can contain just the bootloader, or the bootloader along with support files. Typically, /boot contains the kernel along with additional EFI support files, while /boot/efi contains the actual EFI files, which tend to be small.

附注
The EFI System Partition can be made at either /boot or /boot/efi but it tends to be simpler to just use /boot. Using /boot/efi allows files like the kernel and bootloader support files to be stored on a separate partition and filesystem or the root filesystem itself; the later requires that GRUB can access the root filesystem and read the required files, which may not always be possible (like with root filesystem encryption).

If generating a standalone EFI System Partition, under /boot/efi, a size of 128MB is reasonable, this allows multiple efi files to be stored.

附注
There is little harm in keeping backups of working efi files, which could still be booted if a new one is installed.

创建分区时,可以参考 tool of choice 介绍的可选工具。

用fdisk 分区
附注
As described in Preparing the disks Handbook section, /boot partition is used for ESP, which is formatted as FAT32. Depending on the preferred system configuration, a separate partition for /boot may be desirable, e.g. in the case of filesystem encryption.

If using fdisk to partition the boot partition, start by opening the disk for modification, in this example /dev/sda is used:

root #fdisk /dev/sda
Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

按“g”可以创建一个新的分区桌面:

Command (m for help):g
我们此时创建了一个新的GPT disklabel (GUID: 4AB36B49-BAAD-D544-AC45-E3154565018D).

按“n”可以创建一个新的分区:

Command (m for help):n
 
Partition number (1-128, default 1): 1
First sector (2048-121012190, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-121012190, default 121010175): +128M
 
Created a new partition 1 of type 'Linux filesystem' and of size 128 MiB.

Once the partition is created, the ESP and Boot flags must be set, this can be done by setting the partition type to EFI System Partition. This can be accomplished with the t option, to change the partition type, and then selecting 1, which corresponds to the EFI System Partition type:

Command (m for help):t
 
Selected partition 1
Partition type or alias (type L to list all): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.

最后,我们可以按 w来保存更改:

Command (m for help):w
 
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

With the partition created, it must be formatted to use the FAT32 filesystem, this can be done with:

root #mkfs.fat -F 32 -n efi-boot /dev/sda1
root #mkdir /boot/efi

To determine the partition uuid, or PARTUUID of the new partition, the following command can be use:

user $lsblk -o name,partuuid
lsblk -o name,partuuid
NAME         PARTUUID
sdd          
└─sd1       20f3d6cc-9781-3640-9232-0f5a8c662a60

为这个分区创建fstab

文件 /etc/fstab添加 /boot/efi 的挂载内容
PARTUUID=20f3d6cc-9781-3640-9232-0f5a8c662a60       /boot      vfat    noauto,noatime  1 2
root #mount /boot
附注
/etc/portage/make.conf 文件中设置 GRUB_PLATFORMS 变量将会非常有用。 这个变量将会协助 GRUB 在探测合适的 EFI 目标时确定使用何种选项。对于 32 位 UEFI 系统请使用 efi-32。对于 64 位系统请使用 efi-64
重要
为正确安装 GRUB, EFI 目录 必须 正确挂载 并且 必须在 grub-install 命令运行之前完成 efivars 内核模块的加载。

给EFI 平台安装GRUB

重要
EFI GRUB 必须安装在一个能用/boot分区或者是 ESP 分区里。 可以使用fstab指令来查看挂载状况,分区能被下面这条指令挂载:
root #mount /boot

运行 grub-install 命令复制相关文件到 /boot/grub 中。这个命令将把 Grub 安装到 /boot/grub 目录中,同时复制核心镜像文件到位置 /boot/efi/EFI/gentoo/grubx64.efi,然后调用 efibootmgr 来增加一个引导入口。

root #grub-install --efi-directory=/boot
Installation finished. No error reported.
重要
以上命令假定 FAT 格式的 EFI 系统分区(ESP)挂载到 /boot/efi 目录下,如果该 ESP 直接挂载到了 /boot 下,如果使用不同的ESP分区,可以特别声明它的路径。
附注
By default, GRUB installs targeting the type of system which the command is executed on. If another system type is used, the --target option can be used to specify it.
附注
The --removable option can be used to install GRUB to /boot/EFI/BOOT/BOOTX64.EFI, potentially enhancing compatibility, as this location is often checked by default.

BIOS 引导,使用 MBR

附注
如果系统需要安装 Microsfot Windows 的双启动引导程序,请确定该系统本身"不是"EFI 固件。因为即使该系统使用传统的 'legacy BIOS' 模式启动, Microsoft Windows 也会拒绝启动。

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

root #mount /boot

通过运行 grub-install 命令将相关文件复制到 /boot/grub 目录中。在 PC 平台上,这将同时安装一个引导镜像文件到主引导记录 (MBR) 或某个分区的启动扇区中。如果一切顺利,在运行了 grub-install 之后,应该会有如下输出信息:

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

可以为 grub-install 指定 --target 选项来设置 CPU 架构和系统平台。如果未明确指定, grub-install 会尝试猜测适当的值;对于 amd64/x86 系统 它将默认采用 i386-pc 。还可以为 grub-install 指定 --boot-directory 选项来告诉 GRUB 安装器在哪里寻找启动文件。该值默认为 /boot ,但是如果准备移动根分区,这个参数就变得非常有用了。

为 BIOS 引导程序设置 MBR 分区

应确保在首个分区前预留足够的磁盘空间。可以将第一个分区设置为从2048扇区开始,以便为主引导记录预留至少 1 MiB 空间。一般建议(但并非强制性要求)为 GRUB 创建一个称作“BIOS boot partitoin”的额外分区。只需要定义这个分区,不需要格式化。只有系统之后要迁移到 GPT 分区时才需要定义该分区,如果继续使用 MBR,则不需要这样做。

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

BIOS 引导,使用 GPT

警告
GPT was not designed for the legacy BIOS, yet with the protective MBR it includes a provision for it. Also, dual-boot with legacy operating systems designed to be booted from MBR, which is the de facto standard on computers with a BIOS, will need to access their partitions through the MBR, which can be accomplished by creating GPT/MBR hybrid partitions. This technique, however, has specific constraints.

On a BIOS system with GPT partitioning, GRUB relies on a partition called "BIOS boot partition". This partition is not formatted with a file system, instead grub-install will copy parts of the boot loader to it. The "BIOS boot partition" is not the same partition as a /boot partition.

附注
另请参阅 什么是BIOS引导分区?x86架构的手册。

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

root #mount /boot

如果一切顺利,在运行完 grub-install 命令之后,应该会出现类似于以下的信息提示:

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

可以为 grub2-install 指定 --target 选项来设置 CPU 架构和系统平台。如果未明确指定, grub2-install 会尝试猜测适当的值;对于 amd64/x86 系统 它将默认采用 i386-pc 。还可以为 grub2-install 指定 --boot-directory 选项来告诉 GRUB2 安装器在哪里寻找 GRUB2 的启动文件。该值默认为 /boot ,但是如果准备移动根分区,这个参数就变得非常有用了。

grub-install also accepts a --boot-directory option to tell the GRUB installer which directory to look in for the boot files. This defaults to the current /boot but is useful when trying to move a root partition.

Windows 双系统启动

附注
如果系统需要安装 Microsfot Windows 的双启动引导程序,请确定该系统本身"不是"EFI 固件。因为即使该系统使用传统的 'legacy BIOS' 模式启动, Microsoft Windows 也会拒绝启动。同时,老版本的 Microsoft Windows 系统可能不支持 GPT。此种情况下可以考虑使用 MBR-GPT 混合模式,请参考 Hybrid partition table.
附注
Traditionally x86-PCs used a BIOS as firmware. After the switch to (U)EFI on PCs (around 2005) there used to be a BIOS emulation called 'Compatibility Support Module' (CSM), PCs were therefore still compatible with existing operating systems. On mainstream PCs the EFI-CSM has been phased out since 2020. Even before 2020 some (U)EFI implementations, such as servers, lacked the CSM completely. 'Legacy BIOS mode' is therefore no longer available on modern UEFI systems. UEFI in its native boot mode demands the GUID Partition Table (GPT), a pre-installed operating system will therefore already be using a GPT partitioning scheme.

An already installed Windows will refuse to boot when the boot mode or the partitioning scheme is changed. Also, older Windows systems don't support GPT (or EFI) at all, demanding that a BIOS or the EFI-CSM along with an MBR must be used. If Windows supports EFI it can be re-installed in the native UEFI mode and the GPT partitioning scheme, as well as Linux; see section UEFI with GPT.

Hybrid partitioning between GPT and MBR creates both a valid GPT partition table and a valid MBR partition table at the same time, but limits the total number of hybrid partitions to four because of the four primary partition limit of the MBR. Since the ESP (the EFI System Partition holding the EFI bootloaders) takes up one partition this leaves only three shared partitions between MBR and GPT. When one partition is used for Windows and one for Linux, there is only one additional hybrid partition possible, like a separate Linux /boot partition or a shared data partition between the two operating systems.

警告
Normally, a GPT partition table will always also create an MBR partition table which holds only one partition spanning over the whole disk. This will ensure that older software doesn't mistake the disk as 'empty'. The MBR with its protective partition is therefore called a 'protecive MBR' and is part of the GPT specification. By defining hybrid partitions, this protective feature of the GPT is sacrificed! Legacy software will no longer see used space when analyzing the hybrid MBR. Software that is unaware of the GPT and only sees the MBR may mistake undefined disk space as unused and empty space. Data written outside the defined MBR partitions may cause data loss to the underlying GPT partitions!

If there are two physical disks available to the system, a great solution is to have one disk use the GPT and the other the MBR partitioning scheme. Normally, the Windows installation uses only one partition as 'system partition' and 'boot partition', called 'drive C:'. When in BIOS mode the initial partition for booting, the 'system partition', must be an MBR partition. This applies to every Windows version since Windows XP and includes Windows 10. Since Windows Vista (actually Windows XP x64 Edition) the Microsoft operating system supports accessing GPT partitions. The solution is to relocate the 'system partition' part of an installation to the MBR partitioned disk, and convert the 'boot partition' (the one containing \WINDOWS) into a GPT partitioned disk. Windows can thereafter access all the GPT partitions on the one disk, and will continue to use the MBR partitions (or hybrid partitions) on the disk containing the 'system partition'. The Windows installation (containing \WINDOWS) would be a GPT partition, even when booted in BIOS mode. Windows 11 no longer supports BIOS/CSM/MBR mode.

为 BIOS 引导程序设置 GPT 分区

当系统使用的分区表为 GPT 时,需要设置一个类型为 EF02 小体量BIOS 引导分区(不同于 EFI 系统分区 (ESP)EF00类型)。该分区有 1 MiB 空间足矣,但使用 2-4 MiB 空间会更加可靠。 该 BIOS 引导分区主要用于存储引导程序的 stage 2 代码。它无需格式化为某类文件系统, grub-install 命令会用自己的文件系统覆盖掉该分区上面的任何文件系统。

重要
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 标签。

并非必须要使用 EFI 系统分区,但比较明智的做法是确保该 BIOS 引导分区空间足够大,确保能够随时转换成 EFI 分区,以支持系统将来升级到 UEFI 主板。

下面是在对一个 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
  
(parted)

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

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

PowerPC 的开放式固件(IEEE 1275)

请查看这里

Install on encrypted partition

If the whole disk is encrypted, including /boot, extra steps need to be taken, to allow GRUB to decrypt and mount the device.

The device-mapper USE flag needs to be set when emerging thee GRUB package. Then the sys-fs/cryptsetup package needs to be installed. The partition needs to be encrypted as luks1 partition type.

After installing GRUB on the device, depending on the way the kernel is setup, initramfs might need to be modified in order for the system to boot completely. If a distribution kernel is installed, Dracut will be installed on the system as well and can be used to finish the configuration.

配置

配置 GRUB 有两个主要的工作:

主要配置文件

grub-mkconfig 脚本用于生成 grub 的配置。它综合使用 /etc/grub.d/*/etc/default/grub 中的相关配置文件生成最终的 /boot/grub/grub.cfg - GRUB 所使用的唯一配置文件。

文件 格式 是否建议编辑? -
/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 GRUB shell script No The file generated by grub-mkconfig. This file is evaluated by GRUB'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 GRUB 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 GRUB 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 GRUB configuration variables or the official reference for supported variables.

使用 GRUB 无需管理员手动维护启动选项配置(像 GRUB LegacyLILO 等引导程序所需要那样)。相反,它可以通过使用 grub-mkconfig 命令自动生成配置文件 (/boot/grub/grub.cfg)。该工具会综合利用 /etc/grub.d/ 中的脚本文件和 /etc/default/grub 中的配置参数。

警告
在使用软件 RAID 的情况下,grub-mkconfig 不能正常工作。这种情况下需要手动配置 /etc/grub.d/ 中的脚本文件,否则完成安装后系统将停留在没有引导程序的状态下。

如果修改了一个或多个设置,那么在运行 grub-mkconfig 工具时请使用 -o 选项将输出指定到 /boot/grub/grub.cfg(这是 GRUB 默认的输出位置):

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

每运行一次 grub-mkconfig 都会生成一个新的配置文件。

警告
如果 grub-mkconfig 没有显示任何找到的操作系统入口,那么说明该工具没有找到任何可用操作系统。 这种情况下, 启动计算机时 GRUB 将不会提供任何启动选项,这种情况相当棘手,将会耗费大量时间用于排除问题。所以,请在重启机器前确保该工具的输出中包含了合适的引导入口信息。

设置配置参数

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

变量 说明 默认值
GRUB_DEFAULT 定义启动菜单中的默认操作系统选项。可以是数字索引、菜单标题或者 "saved"。 默认设置为首个检测到的操作系统入口。
GRUB_TIMEOUT 在启动默认操作系统之前的延迟时间(按秒计)。设置为 0 时表示立即启动,设置成 -1 时表示无限期等待用户输入。 默认值为 5 秒钟。
GRUB_CMDLINE_LINUX 需要传递给内核的参数,对所有 Linux 系统入口起作用。例如,如需支持休眠功能,用户应该设置 GRUB_CMDLINE_LINUX="resume=/dev/sdXY" ,其中的 /dev/sdXY 是交换分区。
GRUB_CMDLINE_LINUX_DEFAULT 需要传递给内核命令行的参数,仅对非恢复 Linux 菜单项起作用。
GRUB_DEVICE 初始根设备 (类似于内核 root= 参数)。设置该参数,可以覆盖 grub-mkconfig命令自动探测到的根设备。例如,GRUB_DEVICE=/dev/ram0 将强制在内核命令行中使用 root=/dev/ram0 作为根设备。

| GRUB_DEFAULT |定义启动菜单中的默认操作系统选项。可以是数字索引、菜单标题或者 "saved"。 | 默认设置为首个检测到的操作系统入口。 |-

| GRUB_TIMEOUT | 在启动默认操作系统之前的延迟时间(按秒计)。设置为 0 时表示立即启动,设置成 -1 时表示无限期等待用户输入。 | 默认值为 5 秒钟。 |-

| GRUB_CMDLINE_LINUX | 需要传递给内核的参数,对所有 Linux 系统入口起作用。例如,如需支持休眠功能,用户应该设置 GRUB_CMDLINE_LINUX="resume=/dev/sdXY" ,其中的 /dev/sdXY 是交换分区。 | |-

| GRUB_CMDLINE_LINUX_DEFAULT | 需要传递给内核命令行的参数,仅对非恢复 Linux 菜单项起作用。 | |-

| GRUB_DEVICE | 初始根设备 (类似于内核 root= 参数)。设置该参数,可以覆盖 grub-mkconfig命令自动探测到的根设备。例如,GRUB_DEVICE=/dev/ram0 将强制在内核命令行中使用 root=/dev/ram0 作为根设备。 | |-

|}

需获取更为完整的参数列表,请参考配置变量 子页面和 grub-mkconfig 内的 info 页面。

配置完参数后,使用 grub-mkconfig 命令重新生成 GRUB 配置文件。

启用或禁用配置脚本

/etc/grub.d/ 目录中包含了 grub-mkconfig 用来生成 grub.cfg 配置文件的一系列脚本。默认情况下,该目录中看起来应该类似于下面的内容:

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

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

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

修改完这些脚本(或者通过清除可执行权限禁用这些脚本)后,请使用 grub-mkconfig 重新生成配置文件。

调整配置脚本

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

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

文件 /etc/grub.d/40_custom增加双系统启动项目
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 partitions starting from 1, not 0, with GRUB.

下一步,安装 GRUB 并更新配置文件:

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

扩展特性

GRUB有很多特性使得其成为非常强大的启动引导器。它支持:

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

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

Chainloading

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

文件 /etc/grub.d/40_custom链式加载另一个启动引导
menuentry "Custom Super-bootloader example" {
     insmod part_msdos
     insmod chain
     chainloader (hd1,1)+1
}

要想了解链式加载的更多信息,可以查看链式加载子页面。

GRUB 菜单的密码保护

To secure GRUB so it is not possible for anyone to change boot parameters or use the command line, add a user/password combination to GRUB's configuration files. The program grub-mkpasswd-pbkdf2 generates password hashes for GRUBː

user $grub-mkpasswd-pbkdf2
Password:
Reenter password:
</div>

PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.9CA4611006FE96BC77A...

然后,将以下内容添加到:

文件 /etc/grub.d/35_auth:
# Grub 用户
echo 'set superusers="<username>"'
# Grub 密码
echo 'password_pbkdf2 <username> <password-hash>'

The above may lock all Grub menu entries, even the default entry. In that case, to allow users to boot some menu entries without a password, see Securing the grub boot loader.

The permissions for the file need to be properly set before doing grub-mkconfig:

root #chmod 755 /etc/grub.d/35_auth

使用framebuffer显示

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

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

继续配置位于 /etc/default/grub 的默认配置文件。例如:

文件 /etc/default/grub帧缓冲相关设置
# 设置分辨率和颜色深度
GRUB_GFXMODE=1366x768x32
 
# 加载内核时保持分辨率
GRUB_GFXPAYLOAD_LINUX=keep
  
# 设置背景图片
GRUB_BACKGROUND="/boot/grub/bg.png"
 
# 使用自定义字体,使用 grub-mkfont 工具转换
GRUB_FONT="/boot/grub/fonts/roboto.pf2"
 
# 设置菜单颜色
GRUB_COLOR_NORMAL="light-blue/black"
GRUB_COLOR_HIGHLIGHT="light-cyan/blue"

HiDPI 显示

On modern displays with high DPI ("HiDPI"), e.g. UHD (3840x2160), the standard font will look very small. If you like to have the same font as the kernel, Terminus can be used, which resembles a BIOS built-in textmode font.

To select this font in-kernel, CONFIG_FONT_TER16x32 has to be enabled.

内核 Kernel compiled-in fonts
Library routines  --->
      [*] Select compiled-in fonts
      [*] Terminus 16x32 font (not supported by all drivers)

The same font is available as media-fonts/terminus-font, which can then be used for GRUB as well.

root #emerge --ask media-fonts/terminus-font
root #grub-mkfont -s 32 -o /boot/grub/fonts/terminus32b.pf2 /usr/share/fonts/terminus/ter-u32b.otb

In the above example the filename chosen for grub-mkfont output is terminus32b.pf2. The font's path has to be accessible to GRUB during boot, so it should reside in the same mount point as GRUB does; this example uses /boot/grub/fonts. The font then has to be set as GRUB_FONT in /etc/default/grub in order to be used.

文件 /etc/default/grubFramebuffer related settings
# Use a custom font, converted using grub-mkfont utility
GRUB_FONT="/boot/grub/fonts/terminus32b.pf2"

Updating the GRUB configuration file grub.cfg will then activate the configuration with the new font.

root #grub-mkconfig -o /boot/grub/grub.cfg

Troubleshooting

附注
有关更多疑难解答,请参阅故障排除子文章。

大部分问题可以靠确认分区布局是正确的得以解决。请确保磁盘上第一个分区前面有足够多的空间或者确保一个BIOS boot partition是可用的。也请确认 /boot/grub/grub.cfggrub-mkconfig正确地生成,或者用一个自己定制的菜单项来生成。

操作系统探测器未运行 =

When running the grub-mkconfig command, os-prober is not running as expected, even though it is installed:

root #grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.11.14-gentoo-x86_64
Found initrd image: /boot/amd-uc.img /boot/initramfs-5.11.14-gentoo-x86_64.img
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
Adding boot menu entry for UEFI Firmware Settings ...
done

This can be corrected by setting the GRUB_DISABLE_OS_PROBER variable to false in /etc/default/grub file.

文件 /etc/default/grub
GRUB_DISABLE_OS_PROBER=false

Upon the next run, grub-mkconfig should find additional bootable partitions:

root #grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.11.14-gentoo-x86_64
Found initrd image: /boot/amd-uc.img /boot/initramfs-5.11.14-gentoo-x86_64.img
Warning: os-prober will be executed to detect other bootable partitions.
It's output will be used to detect bootable binaries on them and create new boot entries.
Found Windows Boot Manager on /dev/nvme0n1p2@/efi/Microsoft/Boot/bootmgfw.efi
Adding boot menu entry for UEFI Firmware Settings ...
done

主板固件未找到 .EFI 文件

Some especially older (pre-2020) motherboards from certain manufacturers seem to only support one location for the .EFI file in the EFI System Partition (ESP): the fallback or removable media path. If this seems to be the case, simply move GRUB's default file to the /efi/boot/ location. First, make sure the ESP is mounted. Presuming the ESP is mounted at /boot/efi (as suggested in the Handbook), execute:

root #mkdir -p /boot/efi/efi/boot
root #cp /boot/efi/efi/gentoo/grubx64.efi /boot/efi/efi/boot/bootx64.efi

On a 32-bit EFI implementation use bootia32.efi instead:

root #cp /boot/efi/efi/gentoo/grubia32.efi /boot/efi/efi/boot/bootia32.efi

The removable parameter can be used with grub-install command to generate this file automatically:

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

This should aid the motherboard firmware in loading the GRUB executable. Reboot the system to see if the firmware now correctly loads GRUB.

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

安装新内核

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

附注
确保此步骤已挂载 /boot 分区。
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
done

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

Automatic GRUB reconfiguration

If the package sys-kernel/installkernel-gentoo is installed and the USE flag grub is set, then on every installation of a kernel, grub-mkconfig will run automatically.

See also

  • 链式加载 描述了使用 GRUB 引导其它的启动引导,对于双引导系统或者当 GRUB 需要被配置成从 ISO 文件引导时,这篇文章是非常值得阅读的。
  • 高级储存记录了怎样在更高级的存储条件,例如软 RAID,逻辑卷或者加密文件系统中安装和使用 GRUB 的必要步骤。
  • 配置变量 记录了 /etc/default/grub 所使用的 GRUB 配置变量的详细清单。
  • 故障排除 列出了 GRUB 的常见错误(和它们的解决方案)。
  • 混合分区表 记录了如何使用混合 MBR/GPT 设置,以及这样的混合分区怎样与 GRUB 一起使用。

外部资源

更多的信息,请参见:

参考