Windows 10大部分情况下会被os-prober自动添加到grub引导菜单中。
对于我的特定设置,我希望完全隐藏grub菜单,并在按下热键启动Ubuntu之前自动启动Windows。因此,在我的特定情况下,os-prober不是一个选项,因为隐藏grub菜单的最重要步骤(大多数我找到的答案都忽略了)是设置禁用os-prober标志,否则菜单将显示直到os-prober完成。
这个过程比我预期的要花费更长的时间,因为有很多部分答案,但大多数都依赖于版本,并可能让你误入歧途。我花了很多时间尝试编写自己的grub菜单条目,尝试使用不存在的grub命令,如ntdlr。另一个注意事项是,我的grub上的chainloader函数尝试引导bios而不是efi,并且无法工作。(我确定我做错了什么?)
最后,解决方案实际上很简单,因为脚本会在您的/boot/grub/grub.cfg中为您找到引导分区的UUIDS。因此,您可以跳过大多数人提到的fdisk和blkid步骤。
第一步是确保
/boot/grub/grub.cfg文件是最新的,可以使用
update-grub命令来生成.cfg文件。在终端中执行此操作。
sudo update-grub
第二步是在/etc/grub.d/40_custom中添加自定义菜单项。不要浪费时间尝试编写自己的菜单项,只需打开/boot/grub/grub.cfg,在其中搜索"menuentry"并复制自动生成的菜单项。
列表中的第一个将是Ubuntu,我的看起来是这样的。
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-9e66eed6-e672-49ff-a07c-afdc00809148' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root 9e66eed6-e672-49ff-a07c-afdc00809148
else
search --no-floppy --fs-uuid --set=root 9e66eed6-e672-49ff-a07c-afdc00809148
fi
linux /boot/vmlinuz-5.4.0-39-generic root=UUID=9e66eed6-e672-49ff-a07c-afdc00809148 ro quiet splash $vt_handoff
initrd /boot/initrd.img-5.4.0-39-generic
}
Windows 的操作方式会类似。将两者都复制到 /etc/grub.d/40_custom 中。我仅对 Ubuntu 做了一个改动,即添加了 --hotkey=key 参数,这样 grub 就会根据热键来启动与之关联的操作系统。
menuentry 'Ubuntu2' --class ubuntu --class gnu-linux --class gnu --class os --hotkey=u $menuentry_id_option 'gnulinux-simple-9e66eed6-e672-49ff-a07c-afdc00809148' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root 9e66eed6-e672-49ff-a07c-afdc00809148
else
search --no-floppy --fs-uuid --set=root 9e66eed6-e672-49ff-a07c-afdc00809148
fi
linux /boot/vmlinuz-5.4.0-39-generic root=UUID=9e66eed6-e672-49ff-a07c-afdc00809148 ro quiet splash $vt_handoff
initrd /boot/initrd.img-5.4.0-39-generic
}
我在这里使用
--hotkey=u来设置Ubuntu的启动热键为u。
然后我调整了Windows的条目,用grub的
--id flag替换了
$menuentry_id_option。
menuentry "Windows 10" --class windows --class os --id windows-custom {
insmod part_gpt
insmod fat
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root 0EAE-C882
else
search --no-floppy --fs-uuid --set=root 0EAE-C882
fi
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}
我使用了--id windows-custom。请将更改保存到/etc/grub.d/40_custom。
最后打开/etc/default/grub并添加:
GRUB_DISABLE_OS_PROBER=true
将
GRUB_DEFAULT=0
改为
GRUB_DEFAULT=windows-custom
,将
GRUB_TIMEOUT=10
改为您自己选择的时间,我使用0.5只是为了给自己一点额外的时间,在POST之后按下
u 启动Ubuntu。
希望这对某人能够节省一些麻烦,干杯!