phoebus_os/installer/lib/install.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

330 lines
11 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Installation orchestration.
#
# This is the only place that grants destructive authorization, and only after
# the plan has been displayed and explicitly approved. Each stage checks the
# return status of the one before it and stops on failure rather than
# continuing blindly.
declare -g INSTALL_TARGET_ROOT=""
declare -g INSTALL_FAILED_STAGE=""
# install_effective_root - the directory the installed system lives in.
# In dry-run mode this is a sandbox so file writes never touch the host.
install_effective_root() {
if config_bool DRY_RUN; then
printf '%s/dry-run-root' "${STATE_DIR:-/tmp/ai-installer}"
else
printf '%s' "$(config_get INSTALL_ROOT)"
fi
}
install_require_root() {
if config_bool DRY_RUN; then return 0; fi
if [[ "$(id -u)" -ne 0 ]]; then
log_error "the installer must run as root"
return 1
fi
return 0
}
# install_preflight - non-destructive checks before anything is touched
install_preflight() {
log_section "preflight"
install_require_root || return 1
if ! config_bool DRY_RUN && [[ ! -d /sys/firmware/efi ]]; then
log_error "system was not booted in UEFI mode; v0.1 supports UEFI only"
return 1
fi
local problems
problems="$(validate_config)"
if [[ -n "$problems" ]]; then
log_error "configuration is incomplete:"
while IFS= read -r problems; do log_error " $problems"; done <<<"$problems"
return 1
fi
partitions_require_tools || return 1
filesystem_require_tools || return 1
btrfs_require_tools || return 1
if [[ "$(config_get ENCRYPTION)" != "none" ]]; then
encryption_require_tools || return 1
fi
if [[ -z "$LUKS_PASSPHRASE" && "$(config_get ENCRYPTION)" != "none" ]]; then
log_error "encryption selected but no passphrase configured"
return 1
fi
return 0
}
# install_chroot_mount ROOT / install_chroot_umount ROOT
install_chroot_mount() {
local root="$1"
run_cmd "mount proc" mount -t proc proc "$root/proc" || return 1
run_cmd "mount sysfs" mount --rbind /sys "$root/sys" || return 1
run_cmd "mount devtmpfs" mount --rbind /dev "$root/dev" || return 1
run_cmd "mount devpts" mount --make-rslave "$root/dev" 2>/dev/null || true
[[ -d "$root/run" ]] && run_cmd "mount tmpfs on /run" mount -t tmpfs tmpfs "$root/run" || true
return 0
}
install_chroot_umount() {
local root="$1"
run_cmd "unmount $root/proc" umount -l "$root/proc" 2>/dev/null || true
run_cmd "unmount $root/sys" umount -l "$root/sys" 2>/dev/null || true
run_cmd "unmount $root/dev" umount -l "$root/dev" 2>/dev/null || true
}
# install_stage_storage - create partitions, filesystems and mount the target
install_stage_storage() {
local disk root
disk="$(config_get TARGET_DISK)"
root="$INSTALL_TARGET_ROOT"
log_section "storage: $disk"
if config_bool DRY_RUN; then
log_info "[dry-run] would partition and format $disk at $root"
mkdir -p "$root"
return 0
fi
partitions_wipe "$disk" || return 1
partitions_create_gpt "$disk" || return 1
partitions_verify "$disk" || return 1
FS_EFI_PART="$EFI_PART"
filesystem_mkfs_efi "$EFI_PART" || return 1
filesystem_prepare_root "$disk" "$ROOT_PART" || return 1
# Create the subvolume layout and mount the root; mounting the ESP afterwards
# avoids covering the ESP mount with the root filesystem.
btrfs_create_layout "$FS_ROOT_SOURCE" "$root" || return 1
filesystem_mount_efi "$EFI_PART" "$root" || return 1
btrfs_create_swapfile "$root" || return 1
# NOTE: /etc/fstab and /etc/crypttab are generated after the stage3 is
# extracted (install_stage_fstab); generating them here would be undone by
# the stage3 tarball, which ships its own /etc/fstab.
return 0
}
# install_stage_fstab - write fstab, crypttab and required runtime directories.
# Runs after the stage3 has been extracted so nothing overwrites these files.
install_stage_fstab() {
local root="$INSTALL_TARGET_ROOT"
log_section "fstab / crypttab"
if config_bool DRY_RUN; then
log_info "[dry-run] would write fstab and crypttab into $root"
return 0
fi
filesystem_generate_fstab "$root" || return 1
filesystem_generate_crypttab "$root" || return 1
# Some stage3 media omit /var/tmp; systemd's kernel-install needs it.
mkdir -p "$root/var/tmp" "$root/tmp" "$root/root" "$root/home"
chmod 1777 "$root/var/tmp" "$root/tmp" 2>/dev/null || true
chmod 0700 "$root/root" 2>/dev/null || true
return 0
}
# install_stage_base - extract the Gentoo systemd stage3
install_stage_base() {
local root="$INSTALL_TARGET_ROOT" tarball
log_section "base system (stage3)"
mkdir -p "$root" "${STATE_DIR:-/tmp/ai-installer}"
tarball="${STATE_DIR:-/tmp/ai-installer}/stage3-systemd.tar.xz"
if config_bool DRY_RUN; then
log_info "[dry-run] would fetch stage3 and extract into $root"
return 0
fi
portage_fetch_stage3 "$tarball" || return 1
portage_extract_stage3 "$root" "$tarball" || return 1
return 0
}
# install_stage_portage - configure Portage and sync
install_stage_portage() {
local root="$INSTALL_TARGET_ROOT"
log_section "portage configuration"
if config_bool DRY_RUN; then
log_info "[dry-run] would write Portage config and sync"
return 0
fi
portage_write_make_conf "$root" || return 1
portage_write_repos_conf "$root" || return 1
portage_write_binrepos "$root" || return 1
portage_write_package_files "$root" || return 1
cp -L /etc/resolv.conf "$root/etc/resolv.conf" 2>/dev/null || true
install_chroot_mount "$root" || return 1
portage_init_machine_id "$root" || log_warn "could not initialise machine-id"
portage_sync "$root" || { install_chroot_umount "$root"; return 1; }
portage_verification_setup "$root" || log_warn "getuto failed; binary package verification may fail"
install_chroot_umount "$root"
# Select the profile only after the tree exists, so the symlink is never
# dangling while Portage runs.
portage_select_profile "$root" || return 1
return 0
}
# install_stage_packages - base packages beyond the stage3
install_stage_packages() {
local root="$INSTALL_TARGET_ROOT"
local -a packages=()
log_section "base packages"
mapfile -t packages < <(portage_base_packages)
install_chroot_mount "$root" || return 1
portage_emerge "$root" "${packages[@]}" || { install_chroot_umount "$root"; return 1; }
install_chroot_umount "$root"
return 0
}
# install_stage_kernel
install_stage_kernel() {
local root="$INSTALL_TARGET_ROOT"
log_section "kernel"
kernel_write_profile "$root" || return 1
boot_write_install_conf "$root" || return 1
boot_write_dracut_conf "$root" || return 1
kernel_write_cmdline "$root" || return 1
install_chroot_mount "$root" || return 1
kernel_install "$root" || { install_chroot_umount "$root"; return 1; }
install_chroot_umount "$root"
return 0
}
# install_stage_boot
install_stage_boot() {
local root="$INSTALL_TARGET_ROOT"
log_section "bootloader"
install_chroot_mount "$root" || return 1
boot_install "$root" || log_warn "bootctl install failed; will rely on the EFI fallback path"
boot_generate_uki "$root" || log_warn "kernel-install add-all reported an issue"
install_chroot_umount "$root"
boot_write_loader_conf "$root" || return 1
boot_install_fallback "$root" || log_warn "EFI fallback bootloader not installed"
boot_random_seed "$root" || true
boot_verify "$root" || return 1
return 0
}
# install_stage_users
install_stage_users() {
local root="$INSTALL_TARGET_ROOT"
log_section "users"
install_chroot_mount "$root" || return 1
users_create "$root" || { install_chroot_umount "$root"; return 1; }
users_configure_sudo "$root" || { install_chroot_umount "$root"; return 1; }
install_chroot_umount "$root"
return 0
}
# install_stage_services - locale, timezone, keymap, systemd services
install_stage_services() {
local root="$INSTALL_TARGET_ROOT" tz locale keymap
log_section "system services"
tz="$(config_get TIMEZONE)"
locale="$(config_get LOCALE)"
keymap="$(config_get KEYMAP)"
network_configure_target "$root" || return 1
mkdir -p "$root/etc"
{
printf 'KEYMAP=%s\n' "$keymap"
} >"$root/etc/vconsole.conf"
ln -sf "/usr/share/zoneinfo/$tz" "$root/etc/localtime"
{
printf 'LANG=%s\n' "$locale"
} >"$root/etc/locale.conf"
portage_write_locale_gen "$root" "$locale"
install_chroot_mount "$root" || return 1
portage_chroot "$root" locale-gen || log_warn "locale-gen failed (continuing)"
local svc
local -a services=()
mapfile -t services < <(network_target_services)
mapfile -t -O "${#services[@]}" services < <(desktop_services)
printf '%s\n' "${services[@]}" | sort -u >"$root/etc/.phoebus-services"
while IFS= read -r svc; do
[[ -z "$svc" ]] && continue
portage_chroot "$root" systemctl enable "$svc" || log_warn "could not enable $svc"
done <"$root/etc/.phoebus-services"
rm -f "$root/etc/.phoebus-services"
install_chroot_umount "$root"
return 0
}
# install_stage_desktop
install_stage_desktop() {
local root="$INSTALL_TARGET_ROOT" profile
profile="$(config_get DESKTOP_PROFILE)"
log_section "desktop: $profile"
[[ "$profile" == "none" ]] && return 0
install_chroot_mount "$root" || return 1
desktop_install "$root" || { install_chroot_umount "$root"; return 1; }
desktop_configure_flatpak "$root" || true
install_chroot_umount "$root"
return 0
}
# install_export_logs ROOT - copy the installer log into the target so a
# failed installation can be diagnosed after reboot.
install_export_logs() {
local root="$1" dest
dest="$root/var/log/ai-installer"
[[ -r "${PHOEBUS_LOG_FILE:-}" ]] || return 0
mkdir -p "$dest" 2>/dev/null || return 0
cp -f "$PHOEBUS_LOG_FILE" "$dest/install.log" 2>/dev/null || true
}
# install_stage_finalize
install_stage_finalize() {
local root="$INSTALL_TARGET_ROOT"
log_section "finalize"
if ! config_bool DRY_RUN; then
install_export_logs "$root"
install_chroot_umount "$root"
filesystem_unmount_all "$root"
if [[ "$(config_get ENCRYPTION)" != "none" ]]; then
encryption_close "$(config_get LUKS_NAME)" || log_warn "could not close LUKS device"
fi
fi
log_info "installation finished"
return 0
}
# install_run - execute the staged installation, stopping on first failure
install_run() {
INSTALL_TARGET_ROOT="$(install_effective_root)"
INSTALL_FAILED_STAGE=""
mkdir -p "$INSTALL_TARGET_ROOT" 2>/dev/null || true
install_preflight || { INSTALL_FAILED_STAGE="preflight"; return 1; }
local stage rc
for stage in "${PLAN_STEPS[@]}"; do
rc=0
case "$stage" in
storage_prepare) install_stage_storage || rc=1 ;;
stage_install) install_stage_base || rc=1 ;;
fstab_generate) install_stage_fstab || rc=1 ;;
portage_configure) install_stage_portage || rc=1 ;;
packages_base) install_stage_packages || rc=1 ;;
kernel_install) install_stage_kernel || rc=1 ;;
boot_install) install_stage_boot || rc=1 ;;
users_configure) install_stage_users || rc=1 ;;
services_configure) install_stage_services || rc=1 ;;
desktop_install) install_stage_desktop || rc=1 ;;
finalize) install_stage_finalize || rc=1 ;;
*) log_warn "unknown stage: $stage" ;;
esac
if ((rc != 0)); then
INSTALL_FAILED_STAGE="$stage"
log_error "stage failed: $stage"
if ! config_bool DRY_RUN; then
install_export_logs "$INSTALL_TARGET_ROOT"
install_chroot_umount "$INSTALL_TARGET_ROOT"
fi
return 1
fi
log_info "stage complete: $stage"
done
return 0
}