From Fedora Project Wiki
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 9: Line 9:
从 Fedora 16 开始,GRUB 2 就是 x86 BIOS 系统下默认的引导程序了。对于 BIOS 系统的升级也是默认安装 GRUB 2,但是您完全可以选择跳过配置引导程序。
从 Fedora 16 开始,GRUB 2 就是 x86 BIOS 系统下默认的引导程序了。对于 BIOS 系统的升级也是默认安装 GRUB 2,但是您完全可以选择跳过配置引导程序。


=任务/常见问题=
= 使用 Grubby 管理内核引导参数 =


==添加其他操作系统到 GRUB 2 菜单==
Grubby 可用于更新引导相关的配置文件,我们推荐你使用它更新内核引导参数和设定默认内核。


如果您还安装有其他操作系统而且希望通过 GRUB 2 引导它们,那么请按照以下操作来做:
下面我们简单介绍 <code>grubby</code> 的用法:


<pre>
* 为单个引导项添加一个内核参数:
# yum install os-prober
<pre># grubby --args=<NEW_PARAMETER> --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64</pre>
 
* 为单个引导项添加多个内核参数:
<pre># grubby --args="<NEW_PARAMETER1> <NEW_PARAMETER2 <NEW_PARAMETER_n>" --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64</pre>
 
* 为所有引导项添加内核参数:
<pre># grubby --args=<NEW_PARAMETER> --update-kernel=ALL</pre>
 
* 从当前和以后的所有引导项中删除内核参数:
<pre># grubby --remove-args=<PARAMETER_TO_REMOVE> --update-kernel=ALL</pre>
 
* 设置默认内核:
<pre># grubby --set-default=/boot/vmlinuz-5.11.12-300.fc34.x86_64</pre>
 
=更新 GRUB 2 主配置文件=
 
<code>/boot/grub2/grub.cfg</code> 是默认的 GRUB 配置文件。通常来说你不需要去改动这个静态文件,除非你对磁盘进行了更换或并行安装了其他系统。
 
== 探测使用中的固件 ==
 
执行下面的命令:
 
* 在 UEFI 系统:
<pre># ls -ld /sys/firmware/efi</pre>
 
* 在 BIOS 系统:
<pre># ls -lrt /etc/grub2.cfg</pre>
 
输出的结果即为你使用的固件。
 
<code>grub2-mkconfig -o /boot/grub2/grub.cfg</code> 将探测其他的操作系统并更新引导选单,这是通过 <code>os-prober</code> 实现的。
 
{{admon/note|使用 Fedora 32 或更早版本的用户可能需要使用 <code>grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg</code>. |}}
 
== 修复 GRUB 2 ==
 
如果你的系统无法引导,尝试进入恢复模式以对引导进行修复,详见 [https://docs.fedoraproject.org/en-US/fedora/rawhide/install-guide/advanced/Boot_Options/#sect-boot-options-rescue Fedora Docs 的相关文章]。


# grub2-mkconfig -o /boot/grub2/grub.cfg
然后,执行下面的命令挂载根分区:
</pre>


{{admon/note|笔记|从 F16 Beta 开始,系统探测器应该在安装过程中便自动为其他已安装好的系统生成菜单项并添加到配置中,因此您通常无需再次执行本操作。
<pre># chroot /mnt/sysimage</pre>
}}


==设置默认项==
== 重新安装 GRUB 2 ==


由于 <code>grub2-mkconfig</code> (和系统探测器),我们不能预知 <code>/boot/grub2/grub.cfg</code> 中的各项的顺序,因此我们通过名称/标题来设置默认项。要设置默认项,首先,我们在 <code>/etc/default/grub</code> 中设置 <pre>grub_default=saved</pre> 并运行 <pre>grub2-mkconfig -o /boot/grub2/grub.cfg</pre> 来更新 grub.cfg 。然后调用 <pre>grub2-set-default <标题或名称></pre>  和最近安装的 Fedora 项标题。从此之后,您便可以通过调用 <pre>grub2-set-default <标题或名称></pre> 改变默认项或通过运行 <pre>grub2-editenv list</pre> 查看它。
GRUB 2 软件包包含了安装引导器和生成 <code>grub.cfg</code> 配置文件的相关脚本。


要设置默认项,您还需要可能的菜单项列表,您可以通过 <pre>grep menuentry /boot/grub2/grub.cfg</pre> 找到它。
<code>grub2-install</code> 会在 MBR 安装引导器,引导器的相关文件会储存在 <code>/boot/</code> 目录。


(难道就没有更好地办法吗?)
要重装 GRUB 2:


{{admon/note|注意|有其他更简单的方法来设置默认项,但是如果再次运行 grub2-mkconfig 或是在它重复运行时容易出错。这些方法包括在 <code>/boot/grub2/grub.cfg</code> 中直接设置默认项和设置 GRUB_DEFAULT 为 <code>/etc/default/grub</code> 中的一个序号或某一项的标题。这些方法都不建议使用。
* 了解系统正在使用的固件有那些。 [https://fedoraproject.org/wiki/GRUB_2#Discovering_what_firmware_the_system_is_running 请阅读本章节]  了解详情。
}}


如果您了解相关的风险,但却仍然想直接修改 <code>/boot/grub2/grub.cfg</code> ,下面是您可以做的:
* 在 UEFI 系统:
编辑 <code>/boot/grub2/grub.cfg</code> ,改变行
<pre># dnf reinstall shim-* grub-efi-*</pre>
{{admon/caution|这是方法不建议使用|因为这在运行 <code>grub2-mkconfig</code> 后就会失效,甚至更新内核后就失效了。
}}


* 在 BIOS 系统:
** 查看 <code>/boot/</code> 目录位于哪个分区上:
<pre>
<pre>
set default="0"  
# mount | grep "/boot "
/dev/sda4 on /boot type ext4 (rw,relatime,seclabel)
</pre>
</pre>


就如上面的例子,目录位于 <code>/dev/sda4</code>.
 
** 重新安装引导器:
<pre>
<pre>
set default="5"
# grub2-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.
</pre>
</pre>


==更新 GRUB 2 设置==
{{admon/warning| Do not use the <code>grub2-install</code> command on UEFI systems. On those systems, bootloaders are in the <code>shim</code> and <code>grub-efi</code> packages. By reinstalling those packages, the bootloaders are reinstalled to their proper location in <code>/boot/efi/</code> (the EFI system partition). }}


在 Feodra 下直接编辑 <code>/boot/grub2/grub.cfg</code> 是安全的。Fedora 里的 grubby 在更新内核后就会以一种安全的行为修改配置信息。在 Fedora 中不包含也不需要包含其他的发行版(尤其是 Debian 及其衍生版)提供的添加一个 {{command|update-grub}} 命令的软件补丁。
= Appendix =


==邂逅可怕的 GRUB 2 启动提示符==
== Enabling serial console in GRUB 2 ==


如果设置不当,GRUB 2 可能会加载失败,随后进入启动提示符中。要解决此问题,按如下步骤执行:
To enable serial console for usage on virtual environments you need to run the following command:


1. 列出 GRUB 2 识别的驱动器:
<pre>
<pre>
grub2> ls
# grubby --args="systemd.journald.forward_to_console=1 console=ttyS0,38400 console=tty1" --update-kernel=/boot/vmlinuz-5.11.16-300.fc34.x86_64
</pre>
# grubby --set-default=/boot/vmlinuz-5.11.16-300.fc34.x86_64
</pre>
 
The first command specifies the baud rate, console forwarding for <code>systemd</code>, what console to use (<code>tty1</code>) and on what kernel such changes should be applied. The second command ensures the specified kernel is going to be loaded by default on next reboot.
 
For instructions on how to enable serial consol in GRUB 2 for baremetal machines, see [https://www.gnu.org/software/grub/manual/grub/html_node/Serial-terminal.html Using GRUB via a serial line].
 
In UEFI boot environment, use <code>efi0</code> instead of <code>--unit=0</code>. If it does not work, check that your serial port is visible in your UEFI environment, e.g. by running <code>devtree</code> or <code>dh -p SerialIO</code> in EFI Shell. See [https://lists.gnu.org/archive/html/help-grub/2017-01/msg00007.html Grub2 UEFI boot and serial console output] for more information.
 
== Fixing a damaged GRUB 2 configuration file using a plaintext stub file ==
 
On Fedora 34 and later, you can repair a malfunctioning <code>grub.cfg</code> configuration file by creating a stub file with the following content.
 
# Discover the  Universally Unique Identifier (UUID) for the <code>/boot/</code> mount point:


2. 对于一个含有三个分区的 dos 分区表 <code>/dev/sda</code> ,输出看起来类似于:
<pre>
<pre>
(hd0) (hd0,msdos3) (hd0,msdos2) (hd0,msdos1)
# lsblk --fs
NAME  FSTYPE  FSVER            LABEL                UUID                                FSAVAIL FSUSE% MOUNTPOINT
sr0    iso9660 Joliet Extension Fedora-WS-Live-34-1-2 2021-04-23-11-17-40-00                    0  100% /run/media/jdoe/Fedora-WS-Live-34-1-2
zram0                                                                                                    [SWAP]
vda                                                                                                     
├─vda1 ext4    1.0                                    dc29837b-22dc-4469-be85-fc9acf3009fd  699.8M    21% /boot
└─vda2 btrfs                    fedora_localhost-live c58f3698-5587-40f2-b920-64d46c43161d  23.7G    14% /home
</pre>
</pre>


3. 对于一个含有四个分区的 gpt 分区表 <code>/dev/sda</code> ,输出看起来类似于:
The UUID of <code>/boot/</code> is <code>dc29837b-22dc-4469-be85-fc9acf3009fd</code>.
 
# Create a custom grub.cfg file with the following content:
<pre>
<pre>
(hd0) (hd0,gpt4) (hd0,gpt3)  (hd0,gpt2) (hd0,gpt1)
search --no-floppy --fs-uuid --set=dev dc29837b-22dc-4469-be85-fc9acf3009fd
set prefix=($dev)/grub2
 
export $prefix
configfile $prefix/grub.cfg
</pre>
</pre>


4. 通过这些信息您就可以在驱动器的每个分区探测并定位您的 vmlinuz 和 initramfs 文件了:
At the end of first line (<code>--set=dev</code>), there is the UUID value of the <code>/boot/</code> mount point.
 
The above example assumes default partitioning, where a separate ext4 file system is mounted at <code>/boot/</code>. In case of other configurations, you need to insert <code>/boot/</code> into line 2. For example:
 
<pre>
<pre>
ls (hd0,1)/  
...
</pre>
set prefix=($dev)/boot/grub2
...
</pre>
 
# Move or copy the custom grub.cfg file you created in the previous step to /boot/efi/EFI/fedora/grub.cfg
 
== Setting default entry ==
 
{{admon/warning|Look to (default) kernel sysconfig options. |if file <code>/etc/sysconfig/kernel</code> have <pre>UPDATEDEFAULT=yes</pre> in every kernel update the grub entry is update to last entry, if you don't want that set:  <pre>UPDATEDEFAULT=no</pre> (write "no" in lower case) |}}
 
{{admon/warning|Some parts of this section is wrong or outdated for F17 and later releases. | <s>Be also aware of [[https://bugzilla.redhat.com/show_bug.cgi?id=768106 Bug 768106 - grubby does not support grub2 set default="${saved_entry}" and replaces with "0"]].</s>  version grubby-8.28-1.fc19 has fixed issues with "Default Menuentry" as noted in the linked bug-report|}}
 
Due to <code>grub2-mkconfig</code> (and os-prober) we cannot predict the order of the entries in <code>/boot/grub2/grub.cfg</code>, so we set the default by name/title instead.
 
Open <code>/etc/default/grub</code> and ensure this line exists:
 
<pre>GRUB_DEFAULT=saved</pre>
 
and ensure this line not exists:
 
<pre>GRUB_SAVEDEFAULT=true</pre>
 
or ensure this line exists:
 
<pre>GRUB_SAVEDEFAULT=false</pre>
 
{{admon/note|Note|If GRUB_SAVEDEFAULT is set to true, then, when an entry is selected, save it as a new default entry for use by future runs of GRUB. So, maybe, you need be sure that GRUB_SAVEDEFAULT is not set to true. GRUB_SAVEDEFAULT is only useful if GRUB_DEFAULT is saved.}}
 
Apply the change to <code>grub.cfg</code> by running: <pre>grub2-mkconfig -o /boot/grub2/grub.cfg</pre>


将列出 <code>/dev/sda1</code> 下的文件。如果这个分区包含 <code>/boot</code> ,输出就会显示 vmlinuz 和 initramfs 的全名。
Now list all possible menu entries
<pre>grep -P "submenu|^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2</pre>  


5. 有了地址和 vmlinuz 和 initramfs 的全名您就可以启动您的系统了。
Now set the desired default menu entry <pre>grub2-set-default "<submenu title><menu entry title>"</pre>


5a. 声明您的根分区:
Verify the default menu entry <pre>grub2-editenv list</pre>
<pre>
grub> set root=(hd0,3)
</pre>


5b. 声明您要想使用的内核:
{{admon/note|Note|The above method fails to work on some F20 (and newer) systems due to a missing or improperly linked /boot/grub2/grubenv file. The /boot/grub2/grubenv is symbolic linked to /boot/efi/EFI/fedora/grubenv but /boot is not mounted at the time of booting. So grub2 does not have access to the environment variables. To fix this, change /boot/grub2/grubenv to point to ../efi/EFI/fedora/grubenv instead and your chosen default OS will boot without any problems.}}
<pre>
grub> linux (hd0,1)/vmlinuz-3.0.0-1.fc16.i686 root=/dev/sda3 rhgb quiet selinux=0
# 注意:需要的话添加其他内核参数
# 注意:对照自己的系统改变内容
</pre>


5c. 声明要使用的 initrd :
{{admon/note|Note|There are other, simpler, ways of setting the default entry, but they are prone to error if/when grub2-mkconfig is re-run. These include directly setting the default in /boot/grub2/grub.cfg or setting GRUB_DEFAULT to either a number or an entry title in /etc/default/grub. Neither of these methods is recommended.}}
<pre> 
grub> initrd (hd0,1)/initramfs-3.0.0-1.fc16.i686.img
# 注意:对照自己的系统改变内容
</pre>


5d. 通知 GRUB 2 启动所选文件:
If you understand the risks involved and still want to directly modify /boot/grub2/grub.cfg, here's how you can do it:
<pre>
grub> boot
</pre>


6. 启动后,打开一个终端。
Edit /boot/grub2/grub.cfg, and change the line


7. 使用 <code>grub2-mkconfig</code> 命令来重新创建 grub 2 启动系统需要的 grub.cfg 文件:
{{admon/caution|This is not the recommended method|This will not survive grub2-mkconfig. It might not even survive a kernel update.}}
<pre>
grub2-mkconfig -o /boot/grub2/grub.cfg
</pre>


8. 使用 <code>grub2-install</code> 命令并根据不得配置来安装 grub 2 到您的硬盘驱动器中:
<pre>
<pre>
grub2-install --boot-directory=/boot /dev/sda
set default="0"
# 注意:您的驱动器可能会有其他的设备名称。使用 mount 命令输出检查。
</pre>
</pre>


==其他 GRUB 2 问题==
to
 
'''缺少软盘''':一些用户报告称安装 GRUB 2 到分区启动扇区时可能会失败,如果计算机软盘控制器在 BIOS 中处于激活状态但却没有实际的软盘。一个可能的替代办法是从救援模式(系统安装后)运行:
<pre>
<pre>
grub2-install <target device> --no-floppy
set default="5"
</pre>
</pre>



Latest revision as of 01:08, 8 May 2021

介绍

GRUB 2 是 GNU GRUB (GRand Unified Bootloader) 的最新版本。bootloader(引导程序) 是计算机开机时第一个运行的软件程序。它负责加载操作系统内核并把控制权交给操作系统内核(Fedora 下就是 Linux 了)。反过来,内核初始化操作系统的其余部分。

GRUB 2 已经取代了之前的 GRUB (即,0.9x 版本),使 GRUB 成为了 GRUB Legacy 。

从 Fedora 16 开始,GRUB 2 就是 x86 BIOS 系统下默认的引导程序了。对于 BIOS 系统的升级也是默认安装 GRUB 2,但是您完全可以选择跳过配置引导程序。

使用 Grubby 管理内核引导参数

Grubby 可用于更新引导相关的配置文件,我们推荐你使用它更新内核引导参数和设定默认内核。

下面我们简单介绍 grubby 的用法:

  • 为单个引导项添加一个内核参数:
# grubby --args=<NEW_PARAMETER> --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64
  • 为单个引导项添加多个内核参数:
# grubby --args="<NEW_PARAMETER1> <NEW_PARAMETER2 <NEW_PARAMETER_n>" --update-kernel=/boot/vmlinuz-5.11.14-300.fc34.x86_64
  • 为所有引导项添加内核参数:
# grubby --args=<NEW_PARAMETER> --update-kernel=ALL
  • 从当前和以后的所有引导项中删除内核参数:
# grubby --remove-args=<PARAMETER_TO_REMOVE> --update-kernel=ALL
  • 设置默认内核:
# grubby --set-default=/boot/vmlinuz-5.11.12-300.fc34.x86_64

更新 GRUB 2 主配置文件

/boot/grub2/grub.cfg 是默认的 GRUB 配置文件。通常来说你不需要去改动这个静态文件,除非你对磁盘进行了更换或并行安装了其他系统。

探测使用中的固件

执行下面的命令:

  • 在 UEFI 系统:
# ls -ld /sys/firmware/efi
  • 在 BIOS 系统:
# ls -lrt /etc/grub2.cfg

输出的结果即为你使用的固件。

grub2-mkconfig -o /boot/grub2/grub.cfg 将探测其他的操作系统并更新引导选单,这是通过 os-prober 实现的。

使用 Fedora 32 或更早版本的用户可能需要使用 grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg.

修复 GRUB 2

如果你的系统无法引导,尝试进入恢复模式以对引导进行修复,详见 Fedora Docs 的相关文章

然后,执行下面的命令挂载根分区:

# chroot /mnt/sysimage

重新安装 GRUB 2

GRUB 2 软件包包含了安装引导器和生成 grub.cfg 配置文件的相关脚本。

grub2-install 会在 MBR 安装引导器,引导器的相关文件会储存在 /boot/ 目录。

要重装 GRUB 2:

  • 在 UEFI 系统:
# dnf reinstall shim-* grub-efi-*
  • 在 BIOS 系统:
    • 查看 /boot/ 目录位于哪个分区上:
# mount | grep "/boot "
/dev/sda4 on /boot type ext4 (rw,relatime,seclabel)

就如上面的例子,目录位于 /dev/sda4.

    • 重新安装引导器:
# grub2-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.
Do not use the grub2-install command on UEFI systems. On those systems, bootloaders are in the shim and grub-efi packages. By reinstalling those packages, the bootloaders are reinstalled to their proper location in /boot/efi/ (the EFI system partition).

Appendix

Enabling serial console in GRUB 2

To enable serial console for usage on virtual environments you need to run the following command:

# grubby --args="systemd.journald.forward_to_console=1 console=ttyS0,38400 console=tty1" --update-kernel=/boot/vmlinuz-5.11.16-300.fc34.x86_64
# grubby --set-default=/boot/vmlinuz-5.11.16-300.fc34.x86_64

The first command specifies the baud rate, console forwarding for systemd, what console to use (tty1) and on what kernel such changes should be applied. The second command ensures the specified kernel is going to be loaded by default on next reboot.

For instructions on how to enable serial consol in GRUB 2 for baremetal machines, see Using GRUB via a serial line.

In UEFI boot environment, use efi0 instead of --unit=0. If it does not work, check that your serial port is visible in your UEFI environment, e.g. by running devtree or dh -p SerialIO in EFI Shell. See Grub2 UEFI boot and serial console output for more information.

Fixing a damaged GRUB 2 configuration file using a plaintext stub file

On Fedora 34 and later, you can repair a malfunctioning grub.cfg configuration file by creating a stub file with the following content.

  1. Discover the Universally Unique Identifier (UUID) for the /boot/ mount point:
# lsblk --fs
NAME   FSTYPE  FSVER            LABEL                 UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sr0    iso9660 Joliet Extension Fedora-WS-Live-34-1-2 2021-04-23-11-17-40-00                     0   100% /run/media/jdoe/Fedora-WS-Live-34-1-2
zram0                                                                                                     [SWAP]
vda                                                                                                       
├─vda1 ext4    1.0                                    dc29837b-22dc-4469-be85-fc9acf3009fd  699.8M    21% /boot
└─vda2 btrfs                    fedora_localhost-live c58f3698-5587-40f2-b920-64d46c43161d   23.7G    14% /home

The UUID of /boot/ is dc29837b-22dc-4469-be85-fc9acf3009fd.

  1. Create a custom grub.cfg file with the following content:
search --no-floppy --fs-uuid --set=dev dc29837b-22dc-4469-be85-fc9acf3009fd
set prefix=($dev)/grub2

export $prefix
configfile $prefix/grub.cfg

At the end of first line (--set=dev), there is the UUID value of the /boot/ mount point.

The above example assumes default partitioning, where a separate ext4 file system is mounted at /boot/. In case of other configurations, you need to insert /boot/ into line 2. For example:

...
set prefix=($dev)/boot/grub2
...
  1. Move or copy the custom grub.cfg file you created in the previous step to /boot/efi/EFI/fedora/grub.cfg

Setting default entry

Look to (default) kernel sysconfig options.
if file /etc/sysconfig/kernel have
UPDATEDEFAULT=yes
in every kernel update the grub entry is update to last entry, if you don't want that set:
UPDATEDEFAULT=no
(write "no" in lower case)
Some parts of this section is wrong or outdated for F17 and later releases.
Be also aware of [Bug 768106 - grubby does not support grub2 set default="${saved_entry}" and replaces with "0"]. version grubby-8.28-1.fc19 has fixed issues with "Default Menuentry" as noted in the linked bug-report

Due to grub2-mkconfig (and os-prober) we cannot predict the order of the entries in /boot/grub2/grub.cfg, so we set the default by name/title instead.

Open /etc/default/grub and ensure this line exists:

GRUB_DEFAULT=saved

and ensure this line not exists:

GRUB_SAVEDEFAULT=true

or ensure this line exists:

GRUB_SAVEDEFAULT=false
Note
If GRUB_SAVEDEFAULT is set to true, then, when an entry is selected, save it as a new default entry for use by future runs of GRUB. So, maybe, you need be sure that GRUB_SAVEDEFAULT is not set to true. GRUB_SAVEDEFAULT is only useful if GRUB_DEFAULT is saved.

Apply the change to grub.cfg by running:

grub2-mkconfig -o /boot/grub2/grub.cfg

Now list all possible menu entries

grep -P "submenu|^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2

Now set the desired default menu entry

grub2-set-default "<submenu title><menu entry title>"

Verify the default menu entry

grub2-editenv list
Note
The above method fails to work on some F20 (and newer) systems due to a missing or improperly linked /boot/grub2/grubenv file. The /boot/grub2/grubenv is symbolic linked to /boot/efi/EFI/fedora/grubenv but /boot is not mounted at the time of booting. So grub2 does not have access to the environment variables. To fix this, change /boot/grub2/grubenv to point to ../efi/EFI/fedora/grubenv instead and your chosen default OS will boot without any problems.
Note
There are other, simpler, ways of setting the default entry, but they are prone to error if/when grub2-mkconfig is re-run. These include directly setting the default in /boot/grub2/grub.cfg or setting GRUB_DEFAULT to either a number or an entry title in /etc/default/grub. Neither of these methods is recommended.

If you understand the risks involved and still want to directly modify /boot/grub2/grub.cfg, here's how you can do it:

Edit /boot/grub2/grub.cfg, and change the line

This is not the recommended method
This will not survive grub2-mkconfig. It might not even survive a kernel update.
set default="0" 

to

set default="5"

深入阅读

http://www.gnu.org/software/grub/manual/grub.html

http://fedoraproject.org/wiki/Features/Grub2

http://fedoraproject.org/wiki/Anaconda/Features/Grub2Migration