https://wiki.centos.org/zh/HowTos/Grub2

此文章主要以 Things to Know About Grub2 为基础,而对象是那些打开 /etc/grub2.cfg(或 /boot/grub2/grub.cfg)编辑开机选单的用户。

另外请参阅 上游关于 grub2 的文档grub 的使用手册

1. 开机选单是自动创建出来的

请勿尝试手动编辑开机选单,因为它是按照 /boot/ 目录内的文件自动创建出来的。然而你可以调整 /etc/default/grub 档内定义的通用设置,及在 /etc/grub.d/40_custom 档内加入个别自定项目。

/etc/default/grub 档的内容如下:

GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

通用于所有项目的内核选项都通过 GRUB_CMDLINE_LINUX 行来定义。举个例说,要是你想看见详细的开机消息,删除 rhgb quiet。要是你想看见标准的开机消息,只删除 rhgb。执行以下指令便能套用更改了的设置:

[root@host ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-229.14.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.14.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-229.4.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.4.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-229.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-605f01abef434fb98dd1309e774b72ba
Found initrd image: /boot/initramfs-0-rescue-605f01abef434fb98dd1309e774b72ba.img
done

UEFI 系统上的指令是 grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

2. 如何定义缺省项目

若要列出系统开机时显示的所有选项,请执行以下指令:

[root@host ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux 7 (Core), with Linux 3.10.0-229.14.1.el7.x86_64
1 : CentOS Linux 7 (Core), with Linux 3.10.0-229.4.2.el7.x86_64
2 : CentOS Linux 7 (Core), with Linux 3.10.0-229.el7.x86_64
3 : CentOS Linux 7 (Core), with Linux 0-rescue-605f01abef434fb98dd1309e774b72ba

又或者:

[root@host ~]# grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2

缺省的项目是通过 /etc/default/grub 档内的 GRUB_DEFAULT 行来定义。不过,要是 GRUB_DEFAULT 行被设置为 saved,这个选项便存储在 /boot/grub2/grubenv 档内。你可以这样查看它:

[root@host ~]# grub2-editenv list
saved_entry=CentOS Linux (3.10.0-229.14.1.el7.x86_64) 7 (Core)

/etc/default/grub 档内另一个有用的选项是:

GRUB_SAVEDEFAULT=true

连同 GRUB_DEFAULT=saved,它确保现时选择的开机项目会被设置下次开机采用 —— 适用于双重开机的系统,因为更新 Windows 须要数次重新开机。

/boot/grub2/grubenv 档是不能手动编辑的。请采用以下指令:

[root@host ~]# grub2-set-default 2
[root@host ~]# grub2-editenv list
saved_entry=2

留意上述 awk 指令输出的第一个项目的编号是 0。现在你可重新引导系统。

3. 修复模式及紧急模式

Linux 0-rescue-… 这个选项会令系统进入修复模式。这等同于单独用户模式。

此外,CentOS 并提供了一个紧急模式。在这模式下,systemd 引导后便会立刻出现一个指令壳。其它程序都不会被引导,而主文件系统将会以只读模式挂载。其它文件系统都不会被挂载。

要进入紧急模式,请在 grub2 的选单按 e 键来编辑设置。然后在内核选项的末端加入 systemd.unit=emergency.target

[如果你与 systemd 关系良好,你可以在 /usr/lib/systemd/system/emergency.service 档内查看紧急模式时发生什么事情。]

4. Stage 1.5(core.img)的收录位置

grub/grub2 的文件都位于 /boot 文件系统内。在一个传统(非 UEFI)的 BIOS 环境下,首先会装入一个开机映像(grub 是 stage1,grub2 是 boot.img),接著是装入 stage 1.5(grub2 是 core.img),它会引导 /boot 文件系统内的程序。stage 1.5 的收录位置可以是紧接 MBR,或在 /boot 文件系统的分区内。在 CentOS 7,缺省的位置是紧接 MBR。见 此图像

Translation of revision 9

以下内容为为额外追加

5. CentOS 7 下的update-grub命令的用法

Ubuntu下面经常使用update-grub来更新调整后的grub设置以生效,结果到了CentOS7下面居然找不到这个命令,原来不是这样用,请看下文:

在centos7下的update-grub命令是:grub2-mkconfig -o /boot/grub2/grub.cfg 这个命令。

还可以更改其他配置文件来更新启动项。

CentOS7启动项丢失则使用grub2-install /dev/sdx来安装grub2到磁盘启动区。

6. How to Configure the GRUB2 Boot Loader’s Settings

Ubuntu and most other Linux distributions now use the GRUB2 boot loader. You can change its settings to select a default operating system, set a background image, and choose how long GRUB counts down before automatically booting the default OS.

We configured GRUB2 on Ubuntu 14.04 here, but the process should be similar for other Linux distributions. You may have customized the original GRUB’s settings by editing its menu.lst file in the past, but the process is now different.

GRUB2 Configuration Basics

RELATED: GRUB2 101: How to Access and Use Your Linux Distribution’s Boot Loader

GRUB2 doesn’t use a menu.lst file. Instead, its main configuration file is the /boot/grub/grub.cfg file. However, you shouldn’t edit this file by hand! This file is just for GRUB2’s own usage. It’s automatically created by running the update-grub command as root — in other words, by running sudo update-grub on Ubuntu.

Your own GRUB settings are stored in the /etc/default/grub file. Edit this file to change GRUB2’s settings. Scripts are also located in the /etc/grub.d/ directory. For example, on Ubuntu, there are scripts here that configure the default theme. There’s also an os-prober script that checks the system’s internal hard drives for other installed operating systems — Windows, other Linux distributions, Mac OS X, and so on — and automatically adds them to GRUB2’s menu.

When you run the update-grub command, GRUB automatically combines the settings from the /etc/default/grub file, the scripts from the /etc/grub.d/ directory, and everything else, creating a /boot/grub/grub.cfg file that’s read at boot.

In other words, to customize your GRUB2 settings, you’ll have to edit the /etc/default/grub file and then run the sudo update-grub command.

Edit the GRUB Configuration File

RELATED: A Beginner’s Guide to Editing Text Files With Vi

Open the /etc/default/grub file for editing in a standard text editor. If you want to use a graphical text editor, open a terminal — or press Alt+F2 — and run the following command:

gksu gedit /etc/default/grub

For an easy-to-use terminal-based editor — Nano — use the following command. You can use any text editor you like, of course — including the standard vi text editor.

sudo nano /etc/default/grub

The /etc/default/grub file is short and should be easy to edit. As with any other configuration file, you need to edit the options to your desired state and then change the file. If any of the options below doesn’t already appear in the file, add it on a new line. If it does, edit the existing line instead of adding a duplicate one.

Choose the Default OS: Change the GRUB_DEFAULT= line. By default, GRUB_DEFAULT=0 uses the first entry as the default — change the number to 1 to use the second entry, 2 to use the third entry, or so on. You could also use GRUB_DEFAULT=saved and GRUB would automatically boot the last operating system you chose each time you boot. You can also specify a label in quotes. For example, if you had an operating system named Windows 7 (loader) in your OS list, you could use GRUB_DEFAULT=”Windows 7 (loader)”

Save a Default Operating System: If you choose GRUB_DEFAULT=saved, you also need to add a GRUB_SAVEDEFAULT=true line — otherwise it won’t work.

Choose Whether GRUB is Hidden: With only one operating system installed, Ubuntu defaults GRUB to automatically boot to the default OS with the GRUB_HIDDEN_TIMEOUT=0 option. This option specifies GRUB will be hidden and it will automatically boot to the default OS after 0 seconds –immediately, in other words. You can still access the menu by holding Shift as your computer boots. To set a higher timeout, use something like GRUB_HIDDEN_TIMEOUT=5 — GRUB will display an empty screen or splash screen for five seconds, during which you can press any key to view the menu. To prevent GRUB from being automatically hidden, comment the line out — just add a # before it so that it reads #GRUB_HIDDEN_TIMEOUT=0 .

Control GRUB’s Menu Timeout: If GRUB isn’t automatically hidden, you’ll see the menu each time your computer boots. GRUB will automatically bot the default operating system after a period of time, usually ten seconds. During that time, you can choose another OS or leave it be to automatically boot. To change the timeout period, edit the GRUB_TIMEOUT=10 line and enter any number of seconds you like. (Remember, this is only used if GRUB isn’t hidden.) To prevent GRUB from booting automatically and always wait for you to choose an OS, change the line to GRUB_TIMEOUT=-1

Choose a Background Image: The GRUB_BACKGROUND line controls whether a background image is used — by default, GRUB uses a white-on-black monochrome look. You can add a line like GRUB_BACKGROUND=”/home/user/Pictures/background.png” to specify an image file GRUB will use.

The image file must meet various specifications. GRUB supports JPG/JPEG images, but these are limited to 256 colors — so you probably won’t want to use a JPG image. Instead, you’ll probably want to use a PNG image that can have any number of colors. You could also use a TGA image file.

Make Your Changes Take Effect

To have your changes take effect, simply save the text file — File > Save in Gedit or Ctrl + O and then Enter to save the file in Nano — and then run the sudo update-grub command. Your changes will become part of the grub.cfg file and will be used each time you boot your computer.

These aren’t all of GRUB’s settings, but they are some of the most commonly changed. Other settings can be customized in the /etc/default/grub file, or by editing the scripts in the /etc/grub.d directory.

If you don’t want to edit the files by hand, you may be able to find graphical tools for customizing GRUB2 in your Linux distribution’s software repositories. The above method should work even on Linux distros where such tools aren’t easily available, or if you just have command-line access and want to do it by hand.