#!/usr/bin/env bash # shellcheck shell=bash # # Kernel selection and configuration. # # Policy: compile an "optimized but conservative" kernel. We start from the # Gentoo distribution kernel (sys-kernel/gentoo-kernel), which is a stable, # well-tested configuration, and apply a small, safe set of profile fragments. # We never generate a minimal config that only boots the installer's hardware. kernel_profile_file() { printf '%s/etc/kernel/config.d/00-phoebus-profile.config' "$1" } # kernel_modules_for_hardware - extra kernel packages suggested by hardware kernel_extra_packages() { printf '%s\n' sys-kernel/linux-firmware if hw_cpu_has vmx; then printf '%s\n' sys-firmware/intel-microcode fi if hw_cpu_has svm; then printf '%s\n' sys-firmware/amd-ucode fi } # kernel_select_package - final kernel package respecting the profile kernel_select_package() { local pkg profile pkg="$(config_get KERNEL_PACKAGE)" profile="$(config_get KERNEL_PROFILE)" case "$profile" in safe) # Safe profile prefers the prebuilt distribution kernel. printf '%s' "sys-kernel/gentoo-kernel-bin" ;; optimized) printf '%s' "${pkg:-sys-kernel/gentoo-kernel}" ;; aggressive) # Aggressive still uses the distribution sources but may take a # locally tuned savedconfig fragment. printf '%s' "${pkg:-sys-kernel/gentoo-kernel}" ;; *) printf '%s' "${pkg:-sys-kernel/gentoo-kernel}" ;; esac } # kernel_write_profile ROOT - safe config fragments (never minimal) kernel_write_profile() { local root="$1" profile file profile="$(config_get KERNEL_PROFILE)" file="$(kernel_profile_file "$root")" mkdir -p "$(dirname "$file")" { printf '# %s kernel profile: %s\n' "$(config_get DISTRO_NAME)" "$profile" printf '# Applied on top of the Gentoo distribution kernel configuration.\n' printf '# The base configuration is intentionally kept: it supports common\n' printf '# peripherals the user may attach later.\n' printf '\n' printf '# Storage / modern filesystems and encryption\n' printf 'CONFIG_BTRFS_FS=y\n' printf 'CONFIG_BTRFS_FS_POSIX_ACL=y\n' printf 'CONFIG_DM_CRYPT=y\n' printf 'CONFIG_DM_INTEGRITY=y\n' printf 'CONFIG_NVME_CORE=y\n' printf 'CONFIG_BLK_DEV_NVME=y\n' printf '\n' printf '# Input / USB / common buses\n' printf 'CONFIG_HID_GENERIC=y\n' printf 'CONFIG_USB_HID=y\n' printf 'CONFIG_USB_XHCI_HCD=y\n' printf '\n' printf '# systemd / containers / virtualisation guests\n' printf 'CONFIG_CGROUPS=y\n' printf 'CONFIG_NAMESPACES=y\n' printf 'CONFIG_VIRTIO=y\n' printf 'CONFIG_VIRTIO_PCI=y\n' printf 'CONFIG_VIRTIO_BLK=y\n' printf 'CONFIG_VIRTIO_NET=y\n' printf '\n' printf '# Compression used by Btrfs\n' printf 'CONFIG_ZSTD_COMPRESS=y\n' printf 'CONFIG_ZSTD_DECOMPRESS=y\n' } >"$file" log_info "wrote kernel profile fragment $file" } # kernel_write_cmdline ROOT - kernel command line for UKI/systemd-boot kernel_write_cmdline() { local root="$1" cmdline="" enc name uuid enc="$(config_get ENCRYPTION)" name="$(config_get LUKS_NAME)" if [[ "$enc" != "none" ]]; then uuid="$(blkid -s UUID -o value "$FS_ROOT_PART" 2>/dev/null)" if [[ -z "$uuid" ]]; then if config_bool DRY_RUN; then uuid="" else log_error "cannot determine LUKS UUID for kernel command line" return 1 fi fi # rd.luks.name== makes dracut/systemd create the mapper under # the configured name; rd.luks.uuid alone would produce luks- and the # root= below would not exist. cmdline+="rd.luks.name=$uuid=$name " cmdline+="root=/dev/mapper/$name " else cmdline+="root=$(filesystem_source_ref) " fi cmdline+="rootfstype=btrfs rootflags=subvol=@ rw quiet" local console console="$(config_get KERNEL_CONSOLE)" [[ -n "$console" ]] && cmdline+=" $console" mkdir -p "$root/etc/kernel" printf '%s\n' "$cmdline" >"$root/etc/kernel/cmdline" log_info "wrote /etc/kernel/cmdline: $cmdline" } # kernel_install ROOT - emerge the kernel and firmware kernel_install() { local root="$1" pkg pkg="$(kernel_select_package)" local -a packages=("$pkg") mapfile -t -O "${#packages[@]}" packages < <(kernel_extra_packages) log_info "installing kernel packages: ${packages[*]}" portage_emerge "$root" "${packages[@]}" } # kernel_update ROOT - rebuild the distribution kernel via emerge @module-rebuild kernel_update() { local root="$1" portage_chroot "$root" emerge --ask=n --quiet-build=y @module-rebuild } # kernel_available_list ROOT - kernels present in the ESP kernel_available_list() { local root="$1" esp esp="$root$(config_get EFI_MOUNT)" find "$esp" -maxdepth 4 -name 'vmlinuz*' -o -name '*.efi' 2>/dev/null | sed "s#^$root##" }