如何正确设置正确的 GRUB 2 默认菜单项?

30 grub2

我在 Gentoo Linux 系统上运行 GRUB 2.00。

我手动编译自己的内核,然后/boot使用make install. 我目前有以下内核/boot

# ls -1 /boot/vmlinuz*
/boot/vmlinuz-3.7.4-gentoo-5
/boot/vmlinuz-3.7.4-gentoo-first
/boot/vmlinuz-3.7.4-gentoo-fourth
/boot/vmlinuz-3.7.4-gentoo-third
Run Code Online (Sandbox Code Playgroud)

运行grub2-mkconfig结果如下:

# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.7.4-gentoo-third
Found linux image: /boot/vmlinuz-3.7.4-gentoo-fourth
Found linux image: /boot/vmlinuz-3.7.4-gentoo-first
Found linux image: /boot/vmlinuz-3.7.4-gentoo-5
done
Run Code Online (Sandbox Code Playgroud)

如果我现在阅读生成的/boot/grub2/grub.cfg文件,我会注意到已创建以下条目:

  • 开始的主要默认条目 vmlinuz-3.7.4-gentoo-third
  • 包含所有其他条目(包括恢复条目)的子菜单,顺序与grub2-mkconfig命令相同

问题是在启动时我想默认加载内核的第五个修订版 ( vmlinuz-3.7.4-gentoo-5),而不是第三个 ( vmlinuz-3.7.4-gentoo-third)。我也不想访问子菜单来选择要加载的正确内核。

我怎样才能改变这种行为?我如何告诉 GRUB 我想默认运行内核的第五个修订版而不是旧的第三个修订版?一般来说,如何更改默认输入行以匹配我想要的内核而不是 GRUB 选择的看似随机的内核?

我还尝试将以下几行放入/etc/default/grub

GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true
Run Code Online (Sandbox Code Playgroud)

这并没有按照我想要的方式解决问题。但至少 GRUB 似乎记住了我从中启动的最新内核并自动从子菜单中选择它。只是我不喜欢访问子菜单。

小智 14

要选择子菜单“高级选项”下的菜单项,请设置 GRUB_DEFAULT="1>3"。您现在可以运行 grub2-mkconfig 来生成 grub 配置文件。下次重启时 vmlinuz-3.7.4-gentoo-5 应该会启动。

注意:这不会更改 grub 菜单中的默认条目,即默认条目仍会指示您现在拥有的相同条目。但是默认启动的映像将是“高级选项”下的第 4 个映像。要使 vmlinuz-3.7.4-gentoo-5 作为 grub 菜单中的默认条目,图像的顺序应该是 vmlinuz-3.7.4-gentoo-5 应该是您运行 grub2-mkconfig 时的第一个图像.

  • 请注意,引号很重要。即 GRUB_DEFAULT=1>3 不起作用。 (4认同)

Ast*_*erg 10

如果您的 grub.cfg 中有一个子菜单,您应该使用这样的两位数字:

grub-reboot '1>3'
Run Code Online (Sandbox Code Playgroud)

一个是菜单索引,第二个是子菜单索引。

例如,从配置启动最后一个条目,如下所示:

grep menu /boot/grub/grub.cfg

menuentry 'Debian GNU/Linux' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
submenu 'Advanced options for Debian GNU/Linux' $menuentry_id_option 'gnulinux-advanced-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
    menuentry 'Debian GNU/Linux, with Linux 4.6.0-0.bpo.1-amd64' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.6.0-0.bpo.1-amd64-advanced-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
    menuentry 'Debian GNU/Linux, with Linux 4.6.0-0.bpo.1-amd64 (sysvinit)' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.6.0-0.bpo.1-amd64-init-sysvinit-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
    menuentry 'Debian GNU/Linux, with Linux 4.6.0-0.bpo.1-amd64 (recovery mode)' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.6.0-0.bpo.1-amd64-recovery-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
    menuentry 'Debian GNU/Linux, with Linux 3.16.0-4-amd64' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-4-amd64-advanced-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
    menuentry 'Debian GNU/Linux, with Linux 3.16.0-4-amd64 (sysvinit)' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-4-amd64-init-sysvinit-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
    menuentry 'Debian GNU/Linux, with Linux 3.16.0-4-amd64 (recovery mode)' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-4-amd64-recovery-1ffa16a9-fda2-43b5-91b9-c91a08ff190f' {
Run Code Online (Sandbox Code Playgroud)

你应该使用:

grub-reboot '1>5'
Run Code Online (Sandbox Code Playgroud)

并重新启动。

  • 奇怪的。子菜单索引从 0 开始,而主菜单从 1 (3认同)

小智 8

使用centos7(或RHEL7)和grub2:

grub 使用 grubenv 中的“saved_entry”条目作为默认引导,来自 /boot/grub2/grub.cfg 的片段:

if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="${saved_entry}"
fi
Run Code Online (Sandbox Code Playgroud)

检查“saved_entry”现在是什么我的构建:

grub2-editenv list
Run Code Online (Sandbox Code Playgroud)

saved_entry=CentOS Linux (3.10.0-693.2.2.el7.ari.x86_64) 7 (Core)

将其更改为我的调试版本:

grub2-editenv - set saved_entry='CentOS Linux 3.10.0-693.2.2.el7.ari.x86_64.debug) 7 (Core)'
Run Code Online (Sandbox Code Playgroud)


小智 5

你也可以使用GRUB_DEFAULT=saved. 在这种情况下,你会使用

$ grub2-set-default -h
Usage: grub2-set-default [OPTION] MENU_ENTRY
Set the default boot menu entry for GRUB.

  -h, --help              print this message and exit
  -v, --version           print the version information and exit
  --boot-directory=DIR    expect GRUB images under the directory DIR/grub2
                          instead of the /boot/grub2 directory

MENU_ENTRY is a number, a menu item title or a menu item identifier.
Run Code Online (Sandbox Code Playgroud)

或者如果它仅用于下次启动:

$ grub2-reboot -h
Usage: grub2-reboot [OPTION] MENU_ENTRY
Set the default boot menu entry for GRUB, for the next boot only.

  -h, --help              print this message and exit
  -v, --version           print the version information and exit
  --boot-directory=DIR    expect GRUB images under the directory DIR/grub2
                          instead of the /boot/grub2 directory

MENU_ENTRY is a number, a menu item title or a menu item identifier.
Run Code Online (Sandbox Code Playgroud)


bsd*_*bsd 0

您可以通过数值设置默认选择。如果你设置

`GRUB_DEFAULT=3` 
Run Code Online (Sandbox Code Playgroud)

/etc/default/grub
将把默认值设置为 gentoo-5 条目。
GRUB_DEFAULT是零偏移,因此 3 将根据需要选择第四个条目。

  • 我不知道为什么,但似乎不起作用。我按照你说的设置了`GRUB_DEFAULT=3`,然后重新运行`grub2-mkconfig`。但在启动时,默认条目仍然是“vmlinuz-3.7.4-gentoo-third”。 (2认同)
  • 我的“/etc/default/grub”文件中没有该选项。 (2认同)