> [!IMPORTANT] > 当前(截止到 2024.11.24) systemd-boot[^1]: > 1. 只支持 UEFI(这可能是也是设计使然) > 2. ESP 发现只能在一个分区进行,如果你的其他系统安装在不同的 ESP 分区,则无法引导 > 3. 具有相比 Grub 更少的依赖,systemd 本身就支持 > 4. 对于 Gentoo 来说,并不需要手动添加内核启动项,`sys-kernel/installkernel`[^2] 会自动创建 > > 本文后续将在 gentoo wiki 维护:https://wiki.gentoo.org/wiki/User:Douglarek/Use_systemd-boot 本文假设你的 ESP 分区设置在 `/efi`,使用的 init 系统是 systemd,当前在用的是 Grub 引导,如果不是,则需要根据实际情况修改。 从 Grub 切换到 systemd-boot 大致需要以下几个步骤。 ### 添加 systemd-boot 支持 编辑 `/etc/portage/packages.use/installkernel`(一般在这个 use 文件,如果你放到了其他的地方编辑即可),移除所有 grub 相关,加入如下的 systemd-boot: ``` sys-apps/systemd boot # 添加 systemd boot 支持 sys-kernel/installkernel systemd-boot dracut # 内核编译的时候自动生成启动项 ``` ### 执行 USE 变更更新 ``` (root)$: emerge -avuDN @world ``` 这样会重新编译 systemd 以及 installkernel。 ### 确认当前系统在 UEFI 模式 ``` ls /sys/firmware/efi/efivars ```` 如果当前是 UEFI 模式,那么以上命令不报错,如果报错就别往下走了,使用 livecd 引导进入 EFI 模式,如下的操作在 livecd 操作即可。 ### 安装 systemd-boot 到 ESP 分区 ``` (root)$: bootctl install ```` 执行完一般输出如下: ``` Created "/efi/EFI/systemd". Created "/efi/EFI/BOOT". Created "/efi/loader". Created "/efi/loader/entries". Created "/efi/EFI/Linux". Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/efi/EFI/systemd/systemd-bootx64.efi". Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/efi/EFI/BOOT/BOOTX64.EFI". Random seed file /efi/loader/random-seed successfully written (32 bytes). Successfully initialized system token in EFI variable with 32 bytes. Created EFI boot entry "Linux Boot Manager". ``` ### 查看启动项 ``` (root)$: bootctl list ``` 如果发现没有任何启动项,那么进行下一步。 ### 配置内核添加启动项 ``` (root)$: emerge -a --config sys-kernel/gentoo-kernel ```` 注意自己使用的内核包,这时候再使用第 5 步的命令查看应该有启动项了: ``` type: Boot Loader Specification Type #1 (.conf) title: Gentoo Linux (default) (not reported/new) id: 58e13536913f47119e3a0f56d6982f70-6.11.7-gentoo-dist.conf source: /efi//loader/entries/58e13536913f47119e3a0f56d6982f70-6.11.7-gentoo-dist.conf sort-key: gentoo version: 6.11.7-gentoo-dist machine-id: 58e13536913f47119e3a0f56d6982f70 linux: /efi//58e13536913f47119e3a0f56d6982f70/6.11.7-gentoo-dist/linux initrd: /efi//58e13536913f47119e3a0f56d6982f70/6.11.7-gentoo-dist/initrd options: root=UUID=fb61f375-9c41-4f4f-b935-569ac81acb82 ro systemd.machine_id=58e13536913f47119e3a0f56d6982f70 ``` ### 配置启动内核参数(可选) 编辑 `/etc/kernel/cmdline` 写入: ``` quiet splash ```` **注意**这一步并不会变更实际的启动项内容,需要再次执行第 6 步进行启动项生成。 ### 配置启动选项(可选) 编辑 `/efi/loader/loader.conf`: ``` timeout 3 ``` 这一步会自动生效。 ### 自动更新 systemd-boot ``` (root)$: systemctl enable --now systemd-boot-update.service ```` 也可以使用 Portage 钩子。 ### 清理无用目录及依赖 ``` (root)$: rm -rf /boot/* (root)$: rm -rf /efi/EFI/gentoo (root)$: emerge -W grub (root)$: emerge -ac ```` 其实安装的时候如果你用的 Grub,很可能在 make.conf 还有个 `GRUB_PLATFORMS="efi-64"` 也可以删除,没啥用了。重启使用 systemd-boot 吧。 ### QA 1. 清理内核会自动清理 systemd-boot 启动项么 是的,使用 eclean-kernel 清理旧内核的时候可以自动清理 systemd-boot 的启动项,比如我当前有两个内核(新的是 6.11.8,旧的是 6.11.7),执行 eclean-kernel 后会自动清理: ``` (root)$: eclean-kernel -n 1 Legend: [-] file being removed [x] file does not exist (anymore) [+] file being kept (used by other kernels) * Removing kernel bls 6.11.7-gentoo-dist (not referenced by bootloader (symlinks)) [-] /lib/modules/6.11.7-gentoo-dist/../../../src/linux-6.11.7-gentoo-dist [-] /lib/modules/6.11.7-gentoo-dist [x] /efi/58e13536913f47119e3a0f56d6982f70/6.11.7-gentoo-dist/linux [x] /efi/58e13536913f47119e3a0f56d6982f70/6.11.7-gentoo-dist/initrd [x] /efi/58e13536913f47119e3a0f56d6982f70/6.11.7-gentoo-dist ``` 通过 bootctl 查看已经没了: (root)$: bootctl list type: Boot Loader Specification Type #1 (.conf) title: Gentoo Linux (default) (selected) id: 58e13536913f47119e3a0f56d6982f70-6.11.8-gentoo-dist.conf source: /efi//loader/entries/58e13536913f47119e3a0f56d6982f70-6.11.8-gentoo-dist.conf sort-key: gentoo version: 6.11.8-gentoo-dist machine-id: 58e13536913f47119e3a0f56d6982f70 linux: /efi//58e13536913f47119e3a0f56d6982f70/6.11.8-gentoo-dist/linux initrd: /efi//58e13536913f47119e3a0f56d6982f70/6.11.8-gentoo-dist/initrd options: root=UUID=fb61f375-9c41-4f4f-b935-569ac81acb82 ro systemd.machine_id=58e13536913f47119e3a0f56d6982f70 type: Automatic title: Reboot Into Firmware Interface id: auto-reboot-to-firmware-setup source: /sys/firmware/efi/efivars/LoaderEntries-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f 如果发现还是没有清理掉,可以手动执行 `bootctl cleanup`。 [^1]: https://systemd.io/BOOT/ 延伸阅读 [^2]: https://wiki.gentoo.org/wiki/Systemd/systemd-boot wiki systemd 条目