systemd-boot
systemd-boot(7),曾用名 gummiboot (德语里“橡皮筏”的意思),是一款易于配置的 UEFI 引导加载程序。它提供了一个用于选择启动项的文本菜单,以及一个用于配置内核命令行的编辑器。
注意,systemd-boot 只能启动 EFI 可执行程序(例如 Linux 内核 EFI boot stub,UEFI shell,GRUB 或者 Windows Boot Manager)。
支持的文件系统[编辑 | 编辑源代码]
systemd-boot 从固件继承了文件系统兼容性(例如至少支持 FAT12,FAT16 和 FAT32),还可以加载 esp/EFI/systemd/drivers/
目录下的 UEFI 驱动。
安装[编辑 | 编辑源代码]
systemd-boot 随 systemd包 包一同安装,其为 base包 元软件包的依赖,因此无需手动安装额外软件包。
安装 UEFI 启动管理器[编辑 | 编辑源代码]
要安装 systemd-boot,首先确保启动方式是 UEFI 模式,可以访问 UEFI 变量。用 efivar --list
命令进行检查,如果没有安装 efivar包 ,使用 ls /sys/firmware/efi/efivars
(如果目录存在,则表明系统是以 UEFI 模式启动的)。
下面的例子中会用 esp
表示 ESP 挂载点的路径,例如 /efi
或 /boot
。这将假设你已经 chroot 到了系统的挂载点下。
使用 bootctl(1) 将 systemd-boot 安装到 ESP:
# bootctl install
这将把 systemd-boot UEFI 启动管理器复制到 ESP,同时为其创建一项 UEFI 启动入口,并将其设置为 UEFI 启动顺序的第一项。
- 在 x64 UEFI 环境中,
/usr/lib/systemd/boot/efi/systemd-bootx64.efi
将被复制到esp/EFI/systemd/systemd-bootx64.efi
和esp/EFI/BOOT/BOOTX64.EFI
。 - 在 IA32 UEFI 环境中,
/usr/lib/systemd/boot/efi/systemd-bootia32.efi
将被复制到esp/EFI/systemd/systemd-bootia32.efi
和esp/EFI/BOOT/BOOTIA32.EFI
。
UEFI 启动选项将被命名为“Linux Boot Manager”,根据 UEFI 位数不同,启动选项将指向到 ESP 的 \EFI\systemd\systemd-bootx64.efi
或 \EFI\systemd\systemd-bootia32.efi
位置下。
- 在运行
bootctl install
时,systemd-boot 会尝试在/efi
,/boot
和/boot/efi
目录下寻找 ESP。可以通过--esp-path=esp
参数指定esp
目录(详细信息请参考 bootctl(1) § OPTIONS)。 - 安装 systemd-boot 将覆盖现有的
esp/EFI/BOOT/BOOTX64.EFI
(或是 IA32 UEFI 下的esp/EFI/BOOT/BOOTIA32.EFI
),例如 Microsoft 版本的文件。
要完成安装,请配置 systemd-boot。
通过 XBOOTLDR 安装[编辑 | 编辑源代码]
可以单独创建一个“Linux extended boot”(XBOOTLDR)类型的 /boot 分区将内核和 initramfs 从 ESP 中分离出来,有助于在现有 ESP 过小的情况下配置 Arch + Windows 双系统。
跟随通常步骤配置 ESP,然后在同一物理硬盘上为 XBOOTLDR 创建另一分区。XBOOTLDR 分区的类型 GUID 必须是 bc13c2ff-59e6-4262-a352-b275fd6f7172
[1](gdisk 下是 ea00
,fdisk 下是 xbootldr
)。XBOOTLDR 的容量必须至少为要安装的所有内核的总大小。
- systemd-boot 不会像 ESP 那样检查 XBOOTLDR 的文件系统,因此可以使用你的 UEFI 实现可读取的任意文件系统类型。
- 在启用“快速启动”时,UEFI 可能会跳过加载除 ESP 外的所有分区,可能会导致 systemd-boot无法在 XBOOTLDR 分区上找到启动项。在这种情况下,请禁用“快速启动”。
- XBOOTLDR 分区必须与 ESP 位于同一物理硬盘,否则 systemd-boot 将无法识别到该分区。
在安装时,将 ESP 挂载到 /mnt/efi
,将 XBOOTLDR 分区挂载到 /mnt/boot
。
chroot 后,执行:
# bootctl --esp-path=/efi --boot-path=/boot install
最后配置 systemd-boot。
更新 EFI 启动管理器[编辑 | 编辑源代码]
每当 systemd-boot 有新版本时,用户可以选择重新安装启动管理器。该操作可以手动或自动进行,具体方式将在下文中描述。
手动更新[编辑 | 编辑源代码]
使用bootctl 更新 systemd-boot:
# bootctl update
bootctl install
类似,systemd-boot 会尝试在 /efi
,/boot
和 /boot/efi
三个位置下寻找 ESP。可以用 --esp-path=esp
参数指定 esp
位置。自动更新[编辑 | 编辑源代码]
如果你需要自动更新 systemd-boot,你可以尝试使用 systemd 服务或 Pacman 钩子,下方介绍了这两种方法。
systemd 服务[编辑 | 编辑源代码]
在版本 250 后, systemd包 添加了 systemd-boot-update.service
。启用这个服务后将会在下次启动系统之后更新 bootloader。
pacman 钩子[编辑 | 编辑源代码]
软件包 systemd-boot-pacman-hookAUR 提供了一个 Pacman 钩子,将在每次更新 systemd包 后自动执行。
或者,不安装 systemd-boot-pacman-hook 实现相同功能,在 /etc/pacman.d/hooks/
目录下手动添加以下文件:
/etc/pacman.d/hooks/95-systemd-boot.hook
[Trigger] Type = Package Operation = Upgrade Target = systemd [Action] Description = Gracefully upgrading systemd-boot... When = PostTransaction Exec = /usr/bin/systemctl restart systemd-boot-update.service
为安全启动进行签名[编辑 | 编辑源代码]
如果你启用了安全启动,你需要添加一个 Pacman 钩子以在更新后自动为其重新签名:
/etc/pacman.d/hooks/80-secureboot.hook
[Trigger] Operation = Install Operation = Upgrade Type = Path Target = usr/lib/systemd/boot/efi/systemd-boot*.efi [Action] Description = Signing systemd-boot EFI binary for Secure Boot When = PostTransaction Exec = /bin/sh -c 'while read -r i; do sbsign --key /path/to/keyfile.key --cert /path/to/certificate.crt "$i"; done;' Depends = sh Depends = sbsigntools NeedsTargets
将 /path/to/keyfile.key
和 /path/to/certificate.crt
替换为你的签名密钥和证书,具体信息可参考 sbsign(1) 。
The created /usr/lib/systemd/boot/efi/systemd-boot*.efi.signed
will automatically be picked up by bootctl install
or bootctl update
. See bootctl(1) § SIGNED .EFI FILES.
As an alternative, use sbctl.
配置[编辑 | 编辑源代码]
bootctl
来确保 systemd-boot 能够正常读取配置内容。启动选单配置[编辑 | 编辑源代码]
配置文件保存于 esp/loader/loader.conf
,具体信息可参考 loader.conf(5) § OPTIONS 。
以下是一个简单的示例:
esp/loader/loader.conf
default arch.conf timeout 4 console-mode max editor no
- systemd-boot 不支持使用制表符进行缩进,请使用空格进行替代。
default
和timeout
可在启动选单中修改,变更将覆盖保存到LoaderEntryDefault
和LoaderConfigTimeout
这两个 UEFI 变量中。bootctl set-default ""
和bootctl set-timeout ""
可分别用于清除覆盖了default
andtimeout
选项的 UEFI 变量。- 如果你设置了
timeout 0
,可以通过按下空格键
来访问启动菜单。 - 基本配置文件示例位于
/usr/share/systemd/bootctl/loader.conf
。 - 如果在选择启动项页面时菜单显示异常或分辨率不对,可以尝试将
console-mode
设置为auto
(启发式选择最佳分辨率),keep
(保持固件提供的分辨率)或2
(尝试使用第一个非 UEFI 标准的分辨率)。
记住上一次的启动项[编辑 | 编辑源代码]
从 systemd 251 版本开始,可以将默认启动项设为 @saved
来记住上次使用的启动项。该选项对 Windows 双系统或 Windows 更新自动启动到了 Linux 的情况非常有用。
esp/loader/loader.conf
default @saved ...
更多信息请参考 loader.conf(5)。
增加启动选项[编辑 | 编辑源代码]
systemd-boot 会在 esp/loader/entries/*.conf
中查找启动选项,如果使用了 XBOOTLDR,那同时也会查找 boot/loader/entries/*.conf
。注意,esp
下的启动项只能调用 esp
下的文件(例如内核,initramfs,映像等),boot
下的启动项也一样只能调用 boot
下的文件。
.cmdline
的统一内核映像(UKI)将忽略所有传入的命令行选项(无论是使用 options
传入启动选项还是交互式传入的)。当未启用安全启动时,通过命令行传入的选项会覆盖掉 .cmdline
内置的选项。以下为从卷启动 Arch 的启动选项文件示例,其中卷的 UUID 为 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
:
esp/loader/entries/arch.conf
title Arch Linux linux /vmlinuz-linux initrd /initramfs-linux.img options root=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx rw
esp/loader/entries/arch-fallback.conf
title Arch Linux (fallback initramfs) linux /vmlinuz-linux initrd /initramfs-linux-fallback.img options root=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx rw
所有配置选项可参考引导加载器规范。
systemd-boot 会在启动时自动搜索位于 /EFI/Microsoft/Boot/Bootmgfw.efi
的 Windows Boot Manager,固件中的 Apple macOS Boot Manager,/shellx64.efi
(UEFI shell)和 /EFI/BOOT/bootx64.efi
(EFI Default Loader),同时也会在 /EFI/Linux/
内查找内核文件。在检测到后,会自动生成名称分别为 auto-windows
,auto-osx
,auto-efi-shell
和 auto-efi-default
的启动选项,因此这些选项不需要手动配置引导器。但和 rEFInd 不同,不会为其它 EFI 应用程序创建启动选项,所以这些还需要进行进一步设置。
initrd
中指定 /boot/amd-ucode.img
或/boot/intel-ucode.img
,并将其放置到主 initramfs 之前的首位。UEFI Shells 或其他 EFI 应用程序[编辑 | 编辑源代码]
如果你通过 edk2-shell包 安装了 UEFI shell,那么在对应 EFI 文件放置到了 esp/shellx64.efi
的情况下 systemd-boot 会自动检测到并为其创建新启动选项。
要启用自动检测,可以在安装软件包后执行如下命令:
# cp /usr/share/edk2-shell/x64/Shell.efi /boot/shellx64.efi
另外如果你安装了其他 EFI 应用程序到 ESP,也可以像这样进行加载:
efi
参数的文件路径是相对于你的 EFI 系统分区的。如果你的 EFI 系统分区挂载到了 /boot
,且你的 EFI 二进制文件位于 /boot/EFI/xx.efi
和 /boot/yy.efi
,那么你需要指定对应参数分别为 efi /EFI/xx.efi
和 efi /yy.efi
。esp/loader/entries/fwupd.conf
title Firmware updater efi /EFI/tools/fwupdx64.efi
esp/loader/entries/gdisk.conf
title GPT fdisk (gdisk) efi /EFI/tools/gdisk_x64.efi
Memtest86+[编辑 | 编辑源代码]
首先需要安装 memtest86+-efi包。如果使用了安全启动,需要同时对 EFI 二进制文件进行签名。
esp/loader/entries/memtest.conf
title Memtest86+ efi /memtest86+/memtest.efi
网络引导[编辑 | 编辑源代码]
systemd-boot 可以串联加载网络引导。下载 ipxe-arch.efi
EFI 二进制文件和签名,验证并将其放置到如 esp/EFI/arch_netboot/arch_netboot.efi
的位置下:
esp/loader/entries/arch_netboot.conf
title Arch Linux Netboot efi /EFI/arch_netboot/arch_netboot.efi
GRUB[编辑 | 编辑源代码]
systemd-boot 可以串联加载 GRUB。grubx64.efi
二进制文件的位置与安装 GRUB 到 ESP 时使用的 --bootloader-id=
参数一致。
esp/loader/entries/grub.conf
title GRUB efi /EFI/GRUB/grubx64.efi
从其它硬盘启动[编辑 | 编辑源代码]
systemd-boot 不能从它启动的 ESP 或 XBOOTLDR 分区所在硬盘外的分区中启动 EFI 二进制文件,但可以引导其它 UEFI shell 进行这一操作。
首先,按照上面的步骤安装 edk2-shell包。接着,在 UEFI shell 环境下,使用 map 命令获取带有对应 PARTUUID 的分区的 FS alias(例如 HD0a66666a2、HD0b、FS1 或 BLK7)并记录下来。
下一步,使用 exit
命令启动回到 LInux 环境,然后创建一条新启动选项来通过 UEFI shell 启动目标 EFI 应用:
esp/loader/entries/windows.conf
title Windows efi /shellx64.efi options -nointerrupt -nomap -noversion HD0b:EFI\Microsoft\Boot\Bootmgfw.efi
确保 efi
路径和复制到 esp 路径下的 shellx64.efi
位置一致。顺带一提,可以将 shellx64.efi
EFI 文件移动到其它位置来防止 systemd-boot 自动创建启动选项。
将 HD0b
替换为之前记录的 FS alias。
-nointerrupt
选项可以避免通过Ctrl+c
选项终端目标 EFI 程序允许。-nomap -noversion
选项会隐藏掉默认 UEFI shell 欢迎信息。- 如需让 UEFI shell 在目标 EFI 程序退出后(如出现错误等原因)自动回到启动引导器,可以添加
-exit
选项。 - 如果 UEFI shell 还会出现无用输出,可以添加
-noconsoleout
选项。
启动到 UEFI 固件设置[编辑 | 编辑源代码]
如果你设备的固件支持从操作系统重启到固件设置,那 systemd-boot 会自动检测到并添加启动到 UEFI 固件设置的选项。
对休眠的支持[编辑 | 编辑源代码]
参阅挂起与休眠。
为内核参数编辑器加上密码保护[编辑 | 编辑源代码]
你也可以安装 systemd-boot-passwordAUR,它支持 password
基本配置选项。使用 sbpctl generate
可以为该选项生成值。
使用如下命令安装 systemd-boot-password:
# sbpctl install esp
启用编辑器后,系统会提示你输入密码,然后才能编辑内核参数。
小提示[编辑 | 编辑源代码]
启动选单中的按键操作[编辑 | 编辑源代码]
在启动选单中,你可以使用 t
和 T
调整超时时间,使用 e
编辑当前启动项的内核参数。按下 h
可以看到一个简略的快捷键列表,完整的启动选单内可用快捷键列表可参考 systemd-boot(7) § KEY BINDINGS 。
选择下一次启动选项[编辑 | 编辑源代码]
启动管理器与 systemctl 命令集成,允许你选择重启后的启动选项。举个例子,假设你构建了一个自定义内核,并创建了一个启动项文件 esp/loader/entries/arch-custom.conf
来启动它,只需执行:
$ systemctl reboot --boot-loader-entry=arch-custom.conf
然后系统就会重启到对应的启动项,同时系统后续启动将保留现有设定不变。所有可用启动项清单可通过 --boot-loader-entry=help
选项查看。
可以通过以下命令直接启动到主板固件:
$ systemctl reboot --firmware-setup
统一内核映像[编辑 | 编辑源代码]
Unified kernel images (UKIs) in esp/EFI/Linux/
are automatically sourced by systemd-boot, and do not need an entry in esp/loader/entries
. (Note that unified kernel images must have a .efi
extension to be identified by systemd-boot.)
esp/loader/entries/
will be booted first if no default
is set in esp/loader/loader.conf
. Remove those entries, or set the default with the full file name, i.e. default arch-linux.efi
Grml on ESP[编辑 | 编辑源代码]
PKGBUILD
is available: grml-systemd-bootAUR.Grml is a small live system with a collection of software for system administration and rescue.
In order to install Grml on the ESP, we only need to copy the kernel vmlinuz
, the initramfs initrd.img
, and the squashed image grml64-small.squashfs
from the iso file to the ESP. To do so, first download grml64-small.iso and mount the file (the mountpoint is henceforth denoted mnt); the kernel and initramfs are located in mnt/boot/grml64small/
, and the squashed image resides in mnt/live/grml64-small/
.
Next, create a directory for Grml in your ESP,
# mkdir -p esp/grml
and copy the above-mentioned files in there:
# cp mnt/boot/grml64small/vmlinuz esp/grml # cp mnt/boot/grml64small/initrd.img esp/grml # cp mnt/live/grml64-small/grml64-small.squashfs esp/grml
In the last step, create an entry for the systemd-boot loader: In esp/loader/entries
create a grml.conf
file with the following content:
esp/loader/entries/grml.conf
title Grml Live Linux linux /grml/vmlinuz initrd /grml/initrd.img options apm=power-off boot=live live-media-path=/grml/ nomce net.ifnames=0
For an overview of the available boot options, consult the cheatcode for Grml.
Archiso on ESP[编辑 | 编辑源代码]
PKGBUILD
is available: archiso-systemd-bootAUR.As with Grml it is possible to use the Arch Linux ISO. To do this we need to copy the kernel vmlinuz-linux
, the initramfs initramfs-linux.img
, and the squashfs image airootfs.sfs
from the ISO file to the EFI system partition.
First download archlinux-YYYY.MM.DD-x86_64.iso.
Next, create a directory for archiso in your ESP:
# mkdir -p esp/EFI/archiso
Extract the contents of the arch
directory in there:
# bsdtar -v -x --no-same-permissions --strip-components 1 -f archlinux-YYYY.MM.DD-x86_64.iso -C esp/EFI/archiso arch
In the last step, create a boot entry for the systemd-boot loader: In esp/loader/entries
create a arch-rescue.conf
file with the following content:
esp/loader/entries/arch-rescue.conf
title Arch Linux (rescue system) linux /EFI/archiso/boot/x86_64/vmlinuz-linux initrd /EFI/archiso/boot/x86_64/initramfs-linux.img options archisobasedir=/EFI/archiso archisosearchfilename=/EFI/archiso/boot/x86_64/vmlinuz-linux
For an overview of the available boot options, consult the README.bootparams for mkinitcpio-archiso.
在 BIOS 系统上使用 systemd-boot[编辑 | 编辑源代码]
If you need a bootloader for BIOS systems that follows The Boot Loader Specification, then systemd-boot can be pressed into service on BIOS systems. The Clover boot loader supports booting from BIOS systems and provides a emulated UEFI environment.
排除问题[编辑 | 编辑源代码]
systemd-boot 无法显示我的启动项[编辑 | 编辑源代码]
该问题可能是由配置文件问题(如内核路径错误)导致的。可以执行以下命令进行检查:
# bootctl
在传统启动(BIOS 模式)下安装[编辑 | 编辑源代码]
如果你以 BIOS 模式启动电脑,你还是可以正常安装 systemd-boot,但需要在安装后手动向你的固件提供如何启动 systemd-boot EFI 文件的相关信息,为此你需要下列工具之一:
- 一个 UEFI Shell
- 你的 UEFI 固件设置中提供了更改启动选项的选项.
- 如果 UEFI 没有其它启动项,某些固件会直接使用
esp/EFI/BOOT/BOOTX64.EFI
。
满足条件后,进入你的 UEFI Shell 或是 UEFI 固件设置,修改你的默认 EFI 启动加载器为 esp/EFI/systemd/systemd-bootx64.efi
。
通过 efibootmgr 手动添加启动选项[编辑 | 编辑源代码]
如果运行bootctl install
命令失败,你可以通过 efibootmgr包手动增加选项:
# efibootmgr --create --disk /dev/sdX --part Y --loader '\EFI\systemd\systemd-bootx64.efi' --label "Linux Boot Manager" --unicode
用 EFI 系统分区的设备名称替换 /dev/sdXY
。
在 Windows 上通过 bcdedit 添加启动选项[编辑 | 编辑源代码]
If for any reason you need to create an UEFI boot entry from Windows, you can use the following commands from an Administrator prompt:
> bcdedit /copy {bootmgr} /d "Linux Boot Manager" > bcdedit /set {guid} path \EFI\systemd\systemd-bootx64.efi
Replace guid
with the id returned by the first command. You can also set it as the default entry using
> bcdedit /default {guid}
在 Windows 升级后看不到启动菜单[编辑 | 编辑源代码]
添加 Windows BitLocker TPM 解锁支持[编辑 | 编辑源代码]
在 loader.conf 中添加以下内容以阻止 BitLocker 请求恢复密钥:
esp/loader/loader.conf
reboot-for-bitlocker yes
这一步会设定 BootNext UEFI 变量,从而无需使 BitLocker 请求恢复密钥就能加载 Windows Boot Manager。该操作只需进行一次,且 systemd-boot 仍是默认引导加载程序。如果已自动检测到 Windows,则无需将其指定为条目。
注意,这是实验性功能,使用前请查阅 loader.conf(5)。