phoebus_os/installer/lib/boot.sh
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

150 lines
4.9 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Bootloader installation (UEFI + systemd-boot, Unified Kernel Images).
#
# The ESP is mounted in the target at EFI_MOUNT. systemd-boot is installed and
# configured to boot UKIs produced by kernel-install. Secure Boot is scaffolded
# but not enforced in v0.1.
boot_esp_path() {
printf '%s%s' "$1" "$(config_get EFI_MOUNT)"
}
# boot_install ROOT - install systemd-boot into the ESP
boot_install() {
local root="$1" esp
esp="$(config_get EFI_MOUNT)"
portage_chroot "$root" bootctl --esp-path="$esp" install
}
# boot_write_loader_conf ROOT
boot_write_loader_conf() {
local root="$1" esp dir
esp="$(boot_esp_path "$root")"
dir="$esp/loader"
mkdir -p "$dir"
{
printf '# systemd-boot configuration\n'
printf 'default *\n'
printf 'timeout %s\n' "3"
printf 'console-mode keep\n'
printf 'editor no\n'
} >"$dir/loader.conf"
log_info "wrote $dir/loader.conf"
}
# boot_write_install_conf ROOT
# Configure Gentoo installkernel. v0.1 uses layout=bls (Boot Loader
# Specification): the kernel and dracut initramfs are placed on the ESP and
# systemd-boot entry files carry the kernel command line. The dracut UKI
# generator does not embed /etc/kernel/cmdline, which produced an unbootable
# image (no root=), so UKI is deferred until that path is verified.
boot_write_install_conf() {
local root="$1" dir
dir="$root/etc/kernel"
mkdir -p "$dir"
{
printf '# /etc/kernel/install.conf - Gentoo installkernel\n'
printf '# When /etc/kernel/install.conf exists it is used INSTEAD of the\n'
printf '# package default in /usr/lib/kernel/install.conf, so every required\n'
printf '# generator must be listed here.\n'
printf '# installkernel always requires uki_generator when dracut generates\n'
printf '# the initramfs; "none" disables UKI assembly for the BLS layout.\n'
printf 'layout=bls\n'
printf 'initrd_generator=dracut\n'
printf 'uki_generator=none\n'
} >"$dir/install.conf"
log_info "wrote $dir/install.conf (layout=bls)"
}
# boot_write_dracut_conf ROOT
# A generic initramfs with the storage/crypto modules forced in. This keeps
# the encrypted Btrfs root unlockable and survives kernel updates.
boot_write_dracut_conf() {
local root="$1"
local dir="$root/etc/dracut.conf.d"
mkdir -p "$dir"
{
printf '# %s dracut configuration\n' "$(config_get DISTRO_NAME)"
printf '# Generic (non-hostonly) initramfs for predictable boot behaviour.\n'
printf 'hostonly="no"\n'
printf 'compress="zstd"\n'
printf 'add_dracutmodules+=" crypt btrfs "\n'
} >"$dir/10-phoebus.conf"
log_info "wrote $dir/10-phoebus.conf"
}
# boot_generate_uki ROOT - trigger kernel-install for every installed kernel
boot_generate_uki() {
local root="$1"
portage_chroot "$root" kernel-install add-all
}
# boot_install_fallback ROOT - ensure the default EFI fallback path works even
# when writing the UEFI boot variable failed.
boot_install_fallback() {
local root="$1" esp src
esp="$(boot_esp_path "$root")"
mkdir -p "$esp/EFI/BOOT" "$esp/EFI/systemd"
src="$esp/EFI/systemd/systemd-bootx64.efi"
if [[ ! -e "$src" ]]; then
# Boot loader provided by sys-apps/systemd[boot] inside the target.
local vendor="$root/usr/lib/systemd/boot/efi/systemd-bootx64.efi"
if [[ -e "$vendor" ]]; then
src="$vendor"
else
log_warn "systemd-boot binary not found; EFI fallback not installed"
return 1
fi
fi
run_cmd "install fallback bootloader" cp -f "$src" "$esp/EFI/BOOT/BOOTX64.EFI"
}
# boot_random_seed ROOT - enable systemd random seed persistence
boot_random_seed() {
local root="$1" esp
esp="$(boot_esp_path "$root")"
mkdir -p "$esp/loader"
if [[ ! -e "$esp/loader/random-seed" ]]; then
run_cmd "create random seed" \
dd if=/dev/urandom of="$esp/loader/random-seed" bs=512 count=1 status=none
fi
}
# boot_verify ROOT - confirm bootloader artifacts exist
boot_verify() {
local root="$1" esp
esp="$(boot_esp_path "$root")"
if [[ ! -e "$esp/EFI/systemd/systemd-bootx64.efi" && \
! -e "$esp/EFI/BOOT/BOOTX64.EFI" ]]; then
log_error "no systemd-boot loader found in $esp"
return 1
fi
[[ -e "$esp/loader/loader.conf" ]] || {
log_error "loader.conf not found"; return 1; }
return 0
}
# boot_secureboot_status - report, never change firmware state
boot_secureboot_status() {
if [[ -d /sys/firmware/efi/efivars ]]; then
if command -v mokutil >/dev/null 2>&1; then
mokutil --sb-state 2>/dev/null || printf 'unknown'
else
printf 'Secure Boot state unknown (mokutil not installed)'
fi
else
printf 'not booted in UEFI mode'
fi
}
# boot_secureboot_scaffold ROOT - placeholders for future enrollment
boot_secureboot_scaffold() {
local root="$1"
mkdir -p "$root/etc/secureboot"
{
printf '# Secure Boot is not enabled by this version of the installer.\n'
printf '# Future: sbctl / systemd-sbsign key enrollment and UKI signing.\n'
} >"$root/etc/secureboot/README"
}