systemd-boot
systemd-boot(7)(有時被稱為 sd-boot),曾用名 gummiboot (德語裏「橡皮筏」的意思),是一款易於配置的 UEFI 啟動載入程式。它提供了一個用於選擇啟動項的文字選單,以及一個用於配置內核命令列的編輯器。
注意,systemd-boot 只能從安裝到的 EFI 系統分區或同一硬碟上的擴充啟動載入程式分區(XBOOTLDR 分區)啟動 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包 元軟件包的依賴,因此無需手動安裝額外軟件包。
要安裝 systemd-boot,首先確保啟動方式是 UEFI 模式,可以訪問 UEFI 變數。用 efivar --list 命令進行檢查,如果沒有安裝 efivar包 ,使用 ls /sys/firmware/efi/efivars (如果目錄存在,則表明系統是以 UEFI 模式啟動的)。
使用 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。
可以單獨建立一個「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。
每當 systemd-boot 有新版本時,用户可以選擇重新安裝啟動管理器。該操作可以手動或自動進行,具體方式將在下文中描述。
使用bootctl 更新 systemd-boot:
# bootctl update
bootctl install 類似,systemd-boot 會嘗試在 /efi,/boot 和 /boot/efi 三個位置下尋找 ESP。可以用 --esp-path=esp 參數指定 esp 位置。如果你需要自動更新 systemd-boot,你可以嘗試使用 systemd 服務或 Pacman 鈎子,下方介紹了這兩種方法。
在版本 250 後, systemd包 添加了 systemd-boot-update.service。啟用這個服務後將在每次啟動系統時執行以下命令:
# bootctl --no-variables --graceful update
與手動更新類似,它會在 /efi、/boot 和 /boot/efi 目錄下尋找 ESP。該命令將在 /usr/lib/systemd/boot/efi/ 目錄下存在新版本時更新 ESP 中安裝的所有 systemd-boot。它會先尋找以 .efi.signed 結尾的 systemd-boot 檔案,以允許用户為安全啟動對映像進行簽名。
軟件包 systemd-boot-pacman-hookAUR 提供了一個 Pacman 鈎子,將在每次更新 systemd包 後自動執行。該鈎子與 systemd 服務方式不同,它會在 systemd包 被更新時立即嘗試更新開機管理器。
如果不想安裝 AUR 包,可以在 /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 f; do /usr/lib/systemd/systemd-sbsign sign --private-key /path/to/keyfile.key --certificate /path/to/certificate.crt --output "${f}.signed" "$f"; done;'
Depends = sh
Depends = sbsigntools
NeedsTargets
將 /path/to/keyfile.key 和 /path/to/certificate.crt 替換為你的簽名金鑰和證書,具體資訊可參考 systemd-sbsign(1)。
已經建立的 /usr/lib/systemd/boot/efi/systemd-boot*.efi.signed 會自動地被 bootctl install 和 bootctl update 識別並使用。參見 bootctl(1) § SIGNED .EFI FILES.
另一個選擇是,使用 UEFI/安全啟動#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 ""可分別用於清除覆蓋了defaultandtimeout選項的 UEFI 變數。- 如果你設定了
timeout 0,可以通過按下空格鍵來訪問啟動選單。 - 基本設定檔範例位於
/usr/share/systemd/bootctl/loader.conf。 - 如果在選擇啟動項頁面時選單顯示異常或解像度不對,可以嘗試將
console-mode設定為auto(啟發式選擇最佳解像度),keep(保持韌體提供的解像度)或2(嘗試使用第一個非 UEFI 標準的解像度)。
可以將預設啟動項設為 @saved 來記住上次使用的啟動項。該選項對 Windows 雙系統或 Windows 更新自動啟動到了 Linux 的情況非常有用。
esp/loader/loader.conf
default @saved ...
更多資訊請參考 loader.conf(5)。
systemd-boot 會在它啟動的 EFI 系統分區下的 /loader/entries/ 目錄和同硬碟下的 XBOOTLDR 分區中尋找 .conf 檔案。
esp/loader/entries/*.conf下的啟動項只能調用esp下的檔案(例如內核,initramfs,映像等),boot/loader/entries/*.conf下的啟動項也一樣只能調用boot下的檔案。- 檔案路徑參數是相對於 EFI 系統分區或 XBOOTLDR 分區的根的。例如,如果你的 EFI 系統分區或 XBOOTLDR 掛載到了
/boot目錄,那麼就必須在linux參數中將/boot/vmlinuz-linux指定為/vmlinuz-linux。 - 啟用安全啟動後,內嵌
.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 之前的首位。如果你通過 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+-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
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選項。
如果你裝置的韌體支援從作業系統重新啟動到韌體設定,那 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
位於 esp/EFI/Linux/ 的統一內核映像會自動地被 systemd-boot 識別,無需在 esp/loader/entries 中添加條目。需要注意的是,擁有.efi 副檔名的統一內核鏡像才會被 systemd-boot 識別。
esp/loader/loader.conf 中未設定 default,則會優先啟動位於 esp/loader/entries/ 的檔案。請移除這些條目,或使用完整檔名來設定預設項目,例如 default arch-linux.efiGrml 是精簡的 Live 系統,包含一系列用於系統管理和救援的軟件。
如需將 Grml 安裝到 ESP,只需將 iso 鏡像中的內核檔案 vmlinuz、initramfs 鏡像 initrd.img 和壓縮鏡像 grml64-small.squashfs 複製到 ESP.
首先,下載 grml64-small.iso 並掛載(下文中稱掛載點為 mnt); 內核和 initramfs 位於 mnt/boot/grml64small/,壓縮鏡像位於 mnt/live/grml64-small/。
然後,在你的 ESP(EFI 系統分區)中新建一個 Grml 目錄。
# mkdir -p esp/grml
將上文提到的檔案複製到目錄中:
# 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
最後在 esp/loader/entries 中建立一個 grml.conf 檔案,以在 systemd-boot 中建立一個啟動項:
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
要檢視所有可用開機選項,請參考 cheatcode for Grml。
與 Grml 相同,也可以使用 Arch Linux 的 ISO 建立啟動環境。為此,需要將 ISO 檔案中的以下內容複製到 EFI 系統分區(ESP):
內核檔案:vmlinuz-linux
初始記憶體盤:initramfs-linux.img
壓縮的根檔案系統映像:airootfs.sfs
這些檔案是系統啟動所需的基本組件。後續步驟將説明如何掛載 ISO 並執行檔案複製操作。
第一步,下載 archlinux-YYYY.MM.DD-x86_64.iso
然後,在 EFI 系統分區(ESP)中建立一個名為 archiso 的目錄,用於存放這些檔案:
# mkdir -p esp/EFI/archiso
將 arch 目錄中所有內容解壓或複製到此資料夾中:
# bsdtar -v -x --no-same-permissions --strip-components 1 -f archlinux-YYYY.MM.DD-x86_64.iso -C esp/EFI/archiso arch
最後在 esp/loader/entries 中建立一個 arch-rescue.conf 檔案,以便在 systemd-boot 中添加一個啟動項:
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
如需了解可用的開機選項,參見 README.bootparams for mkinitcpio-archiso。
官方的 Arch ISO 目前不支援安全啟動。所以,進入 ISO 進行恢復或者維護前必須禁用安全啟動。這也會破壞系統的安全性,並不是個好辦法。
一個可能的選擇是使用 mkosi 建立簽名的統一內核映像,前提是系統上的安全啟動已經正確配置並可以執行。這可以讓你開機進一個簽名的 Arch 恢復環境而無需禁用安全啟動或隨身攜帶一個 ISO USB 驅動器。
https://swsnr.de/archlinux-rescue-image-with-mkosi/ 描述了配置相容安全啟動的 Arch 恢復鏡像的方法。開箱即用的 mkosi 配置可在 https://codeberg.org/swsnr/rescue-image 取得,可以自行添加軟件包。
如果需要一個符合 The Boot Loader Specification 的 BIOS 系統啟動載入器,也可以使用 systemd-boot. Clover 支援在 BIOS 系統中啟動並模擬一個 UEFI 環境(以便使用 systemd-boot).
該問題可能是由設定檔問題(如內核路徑錯誤)導致的。可以執行以下命令進行檢查:
# bootctl
如果你以 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 。
如果執行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 添加 UEFI 啟動入口,你可以用管理員權限執行這些指令:
> bcdedit /copy {bootmgr} /d "Linux Boot Manager"
> bcdedit /set {guid} path \EFI\systemd\systemd-bootx64.efi
用第一條指令返回的 id 替換 guid. 你也可以使用以下指令將它設定為預設入口:
> bcdedit /default {guid}
在 loader.conf 中添加以下內容以阻止 BitLocker 請求恢復金鑰:
esp/loader/loader.conf
reboot-for-bitlocker yes
這一步會設定 BootNext UEFI 變數,從而無需使 BitLocker 請求恢復金鑰就能載入 Windows Boot Manager。該操作只需進行一次,且 systemd-boot 仍是預設啟動載入程式。如果已自動檢測到 Windows,則無需將其指定為條目。
注意,這是實驗性功能,使用前請查閱 loader.conf(5)。
- https://systemd.io/BOOT/
- https://bbs.archlinux.org/viewtopic.php?id=254374
- https://uapi-group.org/specifications/specs/boot_loader_specification/