//! Structured installation plan. //! //! The plan is canonical data. The human-readable rendering is derived from it, //! so the review screen, the destructive confirmation, dry-run and the actual //! execution all describe the same thing. use serde::{Deserialize, Serialize}; use crate::config::{InstallConfig, KernelProfile}; use crate::hardware::HardwareReport; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum InstallStep { StoragePrepare, StageInstall, FstabGenerate, PortageConfigure, PackagesBase, KernelInstall, /// Install the NVIDIA accelerated driver (no-op without supported hardware) /// and regenerate the initramfs so its modules are embedded. NvidiaDrivers, BootInstall, UsersConfigure, ServicesConfigure, DesktopInstall, Finalize, } impl InstallStep { pub fn name(self) -> &'static str { match self { InstallStep::StoragePrepare => "storage_prepare", InstallStep::StageInstall => "stage_install", InstallStep::FstabGenerate => "fstab_generate", InstallStep::PortageConfigure => "portage_configure", InstallStep::PackagesBase => "packages_base", InstallStep::KernelInstall => "kernel_install", InstallStep::NvidiaDrivers => "nvidia_drivers", InstallStep::BootInstall => "boot_install", InstallStep::UsersConfigure => "users_configure", InstallStep::ServicesConfigure => "services_configure", InstallStep::DesktopInstall => "desktop_install", InstallStep::Finalize => "finalize", } } /// The proven order. Do not reorder without understanding the fstab and /// subvolume ordering constraints documented in VM-BOOT-VERIFICATION.md. pub fn sequence() -> Vec { vec![ InstallStep::StoragePrepare, InstallStep::StageInstall, InstallStep::FstabGenerate, InstallStep::PortageConfigure, InstallStep::PackagesBase, InstallStep::KernelInstall, InstallStep::NvidiaDrivers, InstallStep::BootInstall, InstallStep::UsersConfigure, InstallStep::ServicesConfigure, InstallStep::DesktopInstall, InstallStep::Finalize, ] } } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct InstallPlan { pub steps: Vec, } impl InstallPlan { pub fn for_config(_config: &InstallConfig) -> Self { Self { steps: InstallStep::sequence(), } } pub fn render_human( &self, config: &InstallConfig, hardware: Option<&HardwareReport>, ) -> String { let mut o = String::new(); let enc = config.encryption.mode.as_str(); let fs = config.storage.filesystem.as_str(); let firmware = hardware.map(|h| h.firmware_str()).unwrap_or("unknown"); let kernel_pkg = config.kernel.selected_package(); o.push_str("Installation plan\n"); o.push_str("=================\n\n"); o.push_str("System:\n"); o.push_str(&format!(" Distribution : {}\n", config.system.distro_name)); o.push_str(&format!(" Architecture : {}\n", config.system.arch)); o.push_str(&format!(" Init system : {}\n", config.system.init_system)); o.push_str(&format!(" Firmware : {firmware}\n\n")); o.push_str("Identity:\n"); o.push_str(&format!(" Hostname : {}\n", config.system.hostname)); o.push_str(&format!(" Keymap : {}\n", config.system.keymap)); o.push_str(&format!(" Locale : {}\n", config.system.locale)); o.push_str(&format!(" Timezone : {}\n", config.system.timezone)); let user = if config.user.username.is_empty() { "" } else { &config.user.username }; o.push_str(&format!(" User : {user}\n\n")); o.push_str("Storage:\n"); let disk = if config.storage.disk.is_empty() { "".to_string() } else { config.storage.disk.clone() }; o.push_str(&format!(" Target disk : {disk}\n")); o.push_str(&format!( " Partitioning : GPT ({})\n", config.storage.partition_scheme )); o.push_str(&format!(" Encryption : {enc}\n")); o.push_str(&format!(" Filesystem : {fs}\n\n")); o.push_str(" Layout:\n"); o.push_str(&format!( " EFI System Partition (FAT32) {} MiB -> {}\n", config.storage.efi_size_mib, config.storage.efi_mount )); o.push_str(&format!( " {:<36} remainder\n", format!("{enc} container") )); o.push_str(&format!(" {:<34}\n", fs)); for sv in &config.storage.subvols { o.push_str(&format!(" {sv}\n")); } o.push('\n'); o.push_str("Boot and kernel:\n"); o.push_str(&format!( " Bootloader : {} (BLS entries)\n", "systemd-boot" )); // Authenticated boot is not implemented. Report the actual BLS boot // path unconditionally: never render the requested flags as active. o.push_str(" Unified UKI : not implemented (BLS entries used)\n"); o.push_str(" Secure Boot : not implemented (boot chain unsigned)\n"); let profile = match config.kernel.profile { KernelProfile::Safe => "safe", KernelProfile::Optimized => "optimized", KernelProfile::Aggressive => "aggressive", }; o.push_str(&format!( " Kernel : {kernel_pkg} ({profile} profile)\n" )); o.push_str(&format!( " Keep kernels : {}\n\n", config.kernel.keep_kernels )); o.push_str("Software:\n"); o.push_str(&format!(" Package policy : {}\n", config.packages.policy)); o.push_str(&format!( " Binary pkgs : {}\n", if config.packages.binary_packages { "yes" } else { "no" } )); o.push_str(&format!( " Accept license : {}\n", config.packages.accept_license )); o.push_str(&format!(" Desktop : {}\n", config.desktop.profile)); if config.desktop.profile == crate::config::DesktopProfile::Mango { o.push_str(" Session : Mango + DankMaterialShell (GURU overlay)\n"); } o.push_str(&format!( " Flatpak : {}\n", if config.desktop.flatpak { "yes" } else { "no" } )); o.push_str(&format!( " Audio : {}\n", config.desktop.audio_stack )); o.push_str(&format!( " AI assistant : {}\n", if config.ai.enabled { "yes" } else { "no" } )); if let Some(hw) = hardware { if let Some(line) = crate::nvidia::NvidiaPlan::for_hardware(hw).render() { o.push_str(" GPU acceleration:\n"); o.push_str(&line); } } o } pub fn destructive_preview(&self, config: &InstallConfig, disk_layout: &str) -> String { let mut o = String::new(); o.push_str(&format!("TARGET: {}\n\n", config.storage.disk)); o.push_str("ALL DATA ON THIS DEVICE WILL BE DESTROYED.\n"); o.push_str("This cannot be undone.\n\n"); o.push_str(&format!( " EFI (FAT32): {} MiB\n", config.storage.efi_size_mib )); if config.is_encrypted() { o.push_str(&format!( " {} (passphrase): remainder\n", config.encryption.mode.as_str().to_uppercase() )); } o.push_str(&format!(" {}:\n", config.storage.filesystem.as_str())); for sv in &config.storage.subvols { o.push_str(&format!(" {sv}\n")); } o.push_str("\nExisting partitions on the target:\n"); for line in disk_layout.lines() { o.push_str(" "); o.push_str(line); o.push('\n'); } o } } #[cfg(test)] mod tests { use super::*; #[test] fn sequence_matches_proven_order() { let names: Vec<&str> = InstallStep::sequence().iter().map(|s| s.name()).collect(); assert_eq!(names[0], "storage_prepare"); assert_eq!(names[1], "stage_install"); assert_eq!(names[2], "fstab_generate"); assert_eq!(names[3], "portage_configure"); assert_eq!(names[names.len() - 1], "finalize"); } #[test] fn render_contains_key_facts() { let mut cfg = InstallConfig::default(); cfg.system.hostname = "phoebus-test".into(); cfg.user.username = "tester".into(); cfg.storage.disk = "/dev/vda".into(); let plan = InstallPlan::for_config(&cfg); let text = plan.render_human(&cfg, None); assert!(text.contains("Installation plan")); assert!(text.contains("phoebus-test")); assert!(text.contains("@snapshots")); assert!(text.contains("luks2")); } #[test] fn destructive_preview_warns_loudly() { let mut cfg = InstallConfig::default(); cfg.storage.disk = "/dev/vda".into(); let text = InstallPlan::for_config(&cfg).destructive_preview(&cfg, "NAME SIZE\nvda 30G"); assert!(text.contains("ALL DATA ON THIS DEVICE WILL BE DESTROYED.")); assert!(text.contains("TARGET: /dev/vda")); assert!(text.contains("vda 30G")); } #[test] fn plan_exposes_the_resolved_nvidia_driver() { use crate::hardware::{GpuInfo, GpuVendor, HardwareReport}; let mut hw = HardwareReport::default(); hw.gpus.push(GpuInfo { vendor: GpuVendor::Nvidia, description: "GA104 [GeForce RTX 3070]".into(), pci_id: Some("10de:2482".into()), pci_address: Some("0000:01:00.0".into()), }); let cfg = InstallConfig::default(); let text = InstallPlan::for_config(&cfg).render_human(&cfg, Some(&hw)); assert!(text.contains("GPU acceleration:")); assert!(text.contains("NVIDIA driver")); assert!(text.contains("open (kernel-open)")); assert!(text.contains("10de:2482")); } #[test] fn plan_omits_gpu_acceleration_without_an_nvidia_gpu() { use crate::hardware::HardwareReport; let cfg = InstallConfig::default(); let text = InstallPlan::for_config(&cfg).render_human(&cfg, Some(&HardwareReport::default())); assert!(!text.contains("GPU acceleration:")); } #[test] fn step_sequence_places_nvidia_after_the_kernel_and_before_boot() { let names: Vec<&str> = InstallStep::sequence().iter().map(|s| s.name()).collect(); let kernel = names.iter().position(|n| *n == "kernel_install").unwrap(); let nvidia = names.iter().position(|n| *n == "nvidia_drivers").unwrap(); let boot = names.iter().position(|n| *n == "boot_install").unwrap(); assert!(kernel < nvidia && nvidia < boot); } #[test] fn boot_preview_reports_plain_bls_and_never_an_active_uki() { let cfg = InstallConfig::default(); let text = InstallPlan::for_config(&cfg).render_human(&cfg, None); assert!(text.contains("systemd-boot (BLS entries)")); assert!(text.contains("Unified UKI : not implemented (BLS entries used)")); assert!(text.contains("Secure Boot : not implemented (boot chain unsigned)")); // Even a config that requests UKI/Secure Boot must not render them // as active (validation rejects such configs; the preview is the // backstop). let mut requested = InstallConfig::default(); requested.kernel.uki = true; requested.kernel.secureboot = true; let text = InstallPlan::for_config(&requested).render_human(&requested, None); assert!(!text.contains("Unified UKI : yes"), "{text}"); assert!(!text.contains("Secure Boot : enabled"), "{text}"); assert!(text.contains("not implemented"), "{text}"); } }