//! GPT partitioning, filesystems, Btrfs layout, fstab/crypttab generation. //! //! The order is the proven one: partition, create ESP, LUKS format/open, //! mkfs.btrfs, mount top-level, create subvolumes, unmount top-level, mount the //! subvolume layout. fstab/crypttab are written *after* stage3 extraction. use phoebus_core::system as sysgen; use phoebus_core::{Encryption, InstallConfig, SecretString}; use crate::auth::DestructiveAuthorization; use crate::command::{CommandSpec, ProcessRunner}; use crate::encryption; use crate::error::{ExecError, Result}; use crate::exec::Exec; use crate::probe; #[derive(Debug, Clone, Default)] pub struct StorageState { pub efi_part: String, pub root_part: String, pub root_source: String, pub luks_uuid: Option, pub efi_uuid: Option, } pub fn require_tools(exec: &Exec) -> Result<()> { exec.require_tools(&[ "sgdisk", "partprobe", "blkid", "wipefs", "mkfs.fat", "mount", "umount", ]) } fn disk_transport(runner: &dyn ProcessRunner, disk: &str) -> String { let spec = CommandSpec::new("storage_prepare", "disk transport", "lsblk") .args(["-dn", "-o", "TRAN", disk]); match runner.execute(&spec) { Ok(out) => out.stdout_lossy().trim().to_string(), Err(_) => String::new(), } } /// The full storage preparation for a whole-disk install. pub fn prepare( exec: &Exec, config: &InstallConfig, auth: &DestructiveAuthorization, passphrase: Option<&SecretString>, state: &mut StorageState, ) -> Result<()> { let disk = &config.storage.disk; let root = exec.target(); wipe(exec, auth, disk)?; create_gpt(exec, auth, config, disk)?; identify_partitions(exec, disk, state)?; if state.efi_part.is_empty() || state.root_part.is_empty() { return Err(ExecError::operation( "storage_prepare", "identify partitions", "could not identify the created partitions", )); } mkfs_efi(exec, auth, &state.efi_part)?; prepare_root(exec, auth, config, passphrase, state)?; create_layout(exec, auth, config, &state.root_source, exec.root.as_path())?; mount_efi(exec, config, &state.efi_part, exec.root.as_path())?; create_swapfile(exec, config, &root, &state.root_source)?; Ok(()) } pub fn wipe(exec: &Exec, _auth: &DestructiveAuthorization, disk: &str) -> Result<()> { exec.run( CommandSpec::new("storage_prepare", "wipe partition signatures", "wipefs") .arg("-a") .arg(disk), )?; exec.run( CommandSpec::new("storage_prepare", "zap GPT/MBR", "sgdisk") .arg("--zap-all") .arg(disk), )?; Ok(()) } pub fn create_gpt( exec: &Exec, _auth: &DestructiveAuthorization, config: &InstallConfig, disk: &str, ) -> Result<()> { let root_type = if config.is_encrypted() { "8309" } else { "8300" }; let efi = config.storage.efi_size_mib; let mut spec = CommandSpec::new("storage_prepare", "create GPT", "sgdisk") .arg("--set-alignment=2048") .arg(format!("--new=1:0:+{efi}M")) .arg("--typecode=1:ef00") .arg("--change-name=1:EFI") .arg("--new=2:0:0") .arg(format!("--typecode=2:{root_type}")) .arg("--change-name=2:phoebus-root") .arg(disk); // The final positional disk must come last; CommandSpec preserves order. spec.stage = "storage_prepare".into(); exec.run(spec)?; exec.run( CommandSpec::new("storage_prepare", "re-read partition table", "partprobe").arg(disk), )?; if crate::exec::find_in_path("udevadm").is_some() { exec.run(CommandSpec::new("storage_prepare", "settle udev", "udevadm").arg("settle"))?; } Ok(()) } /// Identify the EFI and root partitions by GPT label, falling back to order. pub fn identify_partitions(exec: &Exec, disk: &str, state: &mut StorageState) -> Result<()> { let runner = exec.runner.as_ref(); let parts = probe::disk_partitions(runner, disk); state.efi_part.clear(); state.root_part.clear(); for part in &parts { match probe::blkid_partlabel(runner, part).as_deref() { Some("EFI") => state.efi_part = part.clone(), Some("phoebus-root") => state.root_part = part.clone(), _ => {} } } if state.efi_part.is_empty() { if let Some(p) = parts.first() { state.efi_part = p.clone(); } } if state.root_part.is_empty() { if let Some(p) = parts.get(1) { state.root_part = p.clone(); } } Ok(()) } pub fn mkfs_efi(exec: &Exec, _auth: &DestructiveAuthorization, part: &str) -> Result<()> { exec.run( CommandSpec::new("storage_prepare", "create FAT32 EFI filesystem", "mkfs.fat") .args(["-F32", "-n", "EFI"]) .arg(part), )?; Ok(()) } pub fn prepare_root( exec: &Exec, auth: &DestructiveAuthorization, config: &InstallConfig, passphrase: Option<&SecretString>, state: &mut StorageState, ) -> Result<()> { state.root_part = state.root_part.clone(); if config.encryption.mode == Encryption::None { mkfs_btrfs(exec, auth, config, &state.root_part)?; state.root_source = state.root_part.clone(); state.luks_uuid = None; } else { let passphrase = passphrase.ok_or_else(|| { ExecError::operation( "storage_prepare", "encrypt root", "no passphrase configured", ) })?; encryption::luks_format(exec, auth, config, &state.root_part, passphrase)?; encryption::luks_open(exec, config, &state.root_part, passphrase)?; let mapper = encryption::mapper_path(config); mkfs_btrfs(exec, auth, config, &mapper)?; state.root_source = mapper; state.luks_uuid = probe::blkid_uuid(exec.runner.as_ref(), &state.root_part); } exec.logger .info(format!("root filesystem source: {}", state.root_source)); Ok(()) } pub fn mkfs_btrfs( exec: &Exec, _auth: &DestructiveAuthorization, config: &InstallConfig, device: &str, ) -> Result<()> { exec.run( CommandSpec::new("storage_prepare", "create Btrfs filesystem", "mkfs.btrfs") .args(["-f", "-L", &config.storage.btrfs_label]) .arg(device), )?; Ok(()) } /// Create the subvolume layout and mount it. /// /// The top-level (subvolid=5) must be mounted to create @, @home, ...; only /// afterwards can the individual subvolumes be mounted. pub fn create_layout( exec: &Exec, _auth: &DestructiveAuthorization, config: &InstallConfig, device: &str, root: &std::path::Path, ) -> Result<()> { std::fs::create_dir_all(root).map_err(|e| ExecError::io(root.to_path_buf(), e))?; exec.run( CommandSpec::new("storage_prepare", "mount Btrfs top-level", "mount") .args(["-o", "subvolid=5"]) .arg(device) .arg(root.to_string_lossy().to_string()), )?; let mut created = true; for sv in &config.storage.subvols { let target = root.join(sv); let res = exec.run( CommandSpec::new("storage_prepare", format!("create subvolume {sv}"), "btrfs") .args(["subvolume", "create"]) .arg(target.to_string_lossy().to_string()), ); if res.is_err() { created = false; break; } } if !created { let _ = exec.run( CommandSpec::new("storage_prepare", "unmount top-level", "umount") .arg(root.to_string_lossy().to_string()), ); return Err(ExecError::operation( "storage_prepare", "create subvolumes", "a subvolume could not be created", )); } set_default_subvolume(exec, root).ok(); exec.run( CommandSpec::new("storage_prepare", "unmount top-level", "umount") .arg(root.to_string_lossy().to_string()), )?; mount_layout(exec, config, device, root) } fn set_default_subvolume(exec: &Exec, root: &std::path::Path) -> Result<()> { let spec = CommandSpec::new("storage_prepare", "list subvolumes", "btrfs") .args(["subvolume", "list", "-o"]) .arg(root.to_string_lossy().to_string()); let out = exec.runner.execute(&spec)?; let mut id: Option = None; for line in out.stdout_lossy().lines() { let fields: Vec<&str> = line.split_whitespace().collect(); if fields.last() == Some(&"@") && fields.len() >= 2 { id = Some(fields[1].to_string()); } } if let Some(id) = id { exec.run( CommandSpec::new("storage_prepare", "set @ as default", "btrfs") .args(["subvolume", "set-default", &id]) .arg(root.to_string_lossy().to_string()), )?; } Ok(()) } fn ssd_for(exec: &Exec, config: &InstallConfig) -> bool { let transport = disk_transport(exec.runner.as_ref(), &config.storage.disk); transport == "nvme" || transport == "sata" } pub fn btrfs_mount_opts(exec: &Exec, config: &InstallConfig) -> String { sysgen::btrfs_mount_opts(&config.storage.compress, ssd_for(exec, config)) } pub fn mount_layout( exec: &Exec, config: &InstallConfig, device: &str, root: &std::path::Path, ) -> Result<()> { let opts = btrfs_mount_opts(exec, config); std::fs::create_dir_all(root).map_err(|e| ExecError::io(root.to_path_buf(), e))?; let root_sv = config.storage.root_subvolume(); exec.run( CommandSpec::new("storage_prepare", "mount root subvolume", "mount") .arg("-o") .arg(format!("{opts},subvol={root_sv}")) .arg(device) .arg(root.to_string_lossy().to_string()), )?; for (sv, target) in [ ("@home", "home"), ("@var", "var"), ("@snapshots", ".snapshots"), ("@swap", "swap"), ] { if !config.storage.subvols.iter().any(|s| s == sv) { continue; } let mp = root.join(target); std::fs::create_dir_all(&mp).map_err(|e| ExecError::io(mp.clone(), e))?; exec.run( CommandSpec::new("storage_prepare", format!("mount {sv}"), "mount") .arg("-o") .arg(format!("{opts},subvol={sv}")) .arg(device) .arg(mp.to_string_lossy().to_string()), )?; } Ok(()) } pub fn mount_efi( exec: &Exec, config: &InstallConfig, part: &str, root: &std::path::Path, ) -> Result<()> { let mp = root.join(config.storage.efi_mount.trim_start_matches('/')); std::fs::create_dir_all(&mp).map_err(|e| ExecError::io(mp.clone(), e))?; exec.run( CommandSpec::new("storage_prepare", "mount EFI", "mount") .arg(part) .arg(mp.to_string_lossy().to_string()), )?; Ok(()) } pub fn create_swapfile( exec: &Exec, config: &InstallConfig, root: &phoebus_core::TargetRoot, _root_source: &str, ) -> Result<()> { let swapdir = root.ensure_dir("swap")?; let swapfile = swapdir.join("swapfile"); let size_mib = swap_size_mib(exec); let spec = CommandSpec::new("storage_prepare", "create Btrfs swapfile", "btrfs") .args([ "filesystem", "mkswapfile", "--size", &format!("{size_mib}m"), ]) .arg(swapfile.to_string_lossy().to_string()); if exec.run(spec).is_ok() { return Ok(()); } // Fallback for older btrfs-progs. exec.run( CommandSpec::new("storage_prepare", "create swapfile container", "truncate") .args(["-s", &format!("{size_mib}M")]) .arg(swapfile.to_string_lossy().to_string()), )?; exec.run( CommandSpec::new("storage_prepare", "disable CoW on swapfile", "chattr") .arg("+C") .arg(swapfile.to_string_lossy().to_string()), )?; exec.run( CommandSpec::new("storage_prepare", "make swap", "mkswap") .arg(swapfile.to_string_lossy().to_string()), )?; let _ = config; Ok(()) } fn swap_size_mib(_exec: &Exec) -> u64 { let mem = read_mem_mib(); let swap = if mem == 0 { 4096 } else { mem / 2 }; swap.clamp(1024, 8192) } fn read_mem_mib() -> u64 { std::fs::read_to_string("/proc/meminfo") .ok() .and_then(|text| { text.lines().find_map(|l| { l.strip_prefix("MemTotal:") .and_then(|r| r.split_whitespace().next()) .and_then(|kb| kb.parse::().ok()) .map(|kb| kb / 1024) }) }) .unwrap_or(0) } // --------------------------------------------------------------------------- // fstab / crypttab // --------------------------------------------------------------------------- /// The stable fstab source reference: the mapper path when encrypted, /// otherwise `UUID=`. pub fn root_source_ref(exec: &Exec, _config: &InstallConfig, state: &StorageState) -> String { if state.root_source.starts_with("/dev/mapper/") { state.root_source.clone() } else { probe::blkid_uuid(exec.runner.as_ref(), &state.root_source) .map(|u| format!("UUID={u}")) .unwrap_or_else(|| state.root_source.clone()) } } pub fn write_fstab(exec: &Exec, config: &InstallConfig, state: &mut StorageState) -> Result<()> { let root = exec.target(); let src = root_source_ref(exec, config, state); let opts = btrfs_mount_opts(exec, config); if state.efi_uuid.is_none() { state.efi_uuid = probe::blkid_uuid(exec.runner.as_ref(), &state.efi_part); } let text = sysgen::fstab(config, &src, state.efi_uuid.as_deref(), &opts); root.write("etc/fstab", text)?; exec.logger.info("wrote /etc/fstab"); Ok(()) } pub fn write_crypttab(exec: &Exec, config: &InstallConfig, state: &StorageState) -> Result<()> { if !config.is_encrypted() { return Ok(()); } let text = encryption::crypttab_line(exec, config, &state.root_part)?; exec.target().write("etc/crypttab", text)?; exec.logger.info("wrote /etc/crypttab"); Ok(()) } /// Unmount children first, the root last. pub fn unmount_all(exec: &Exec, config: &InstallConfig, root: &std::path::Path) -> Result<()> { let mut mounts = vec![ config.storage.efi_mount.clone(), "/.snapshots".to_string(), "/var".to_string(), "/home".to_string(), "/swap".to_string(), String::new(), ]; mounts.dedup(); for m in mounts { let target = root.join(m.trim_start_matches('/')); let spec = CommandSpec::new("finalize", format!("unmount {m}"), "umount") .args(["-R"]) .arg(target.to_string_lossy().to_string()); // Best effort. let _ = exec.run(spec); } Ok(()) } #[cfg(test)] mod tests { use super::*; use crate::command::RecordingRunner; use crate::log::Logger; use phoebus_core::Redactor; fn make_exec(runner: RecordingRunner) -> Exec { let path = tempfile::tempdir().unwrap().keep(); let logger = Logger::open(path.join("l.log"), Redactor::new(), false); Exec::new(Box::new(runner), logger, false, path.join("root"), path) } #[test] fn gpt_uses_luks_type_when_encrypted() { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); let cfg = InstallConfig::default(); let auth = crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap(); create_gpt(&exec, &auth, &cfg, "/dev/vda").unwrap(); let recorded = runner.recorded(); let gpt = recorded.iter().find(|s| s.program == "sgdisk").unwrap(); assert!(gpt.args.iter().any(|a| a == "--typecode=2:8309")); assert!(gpt.args.iter().any(|a| a == "--typecode=1:ef00")); assert_eq!(gpt.args.last().map(String::as_str), Some("/dev/vda")); } #[test] fn create_layout_creates_subvolumes_before_mounting_root() { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); let cfg = InstallConfig::default(); let auth = crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap(); let root = exec.root.clone(); create_layout(&exec, &auth, &cfg, "/dev/mapper/cryptroot", &root).unwrap(); let recorded = runner.recorded(); // Find the first subvolume create and the first subvol=@ mount. let create_idx = recorded .iter() .position(|s| { s.program == "btrfs" && s.args.first().map(String::as_str) == Some("subvolume") && s.args.get(1).map(String::as_str) == Some("create") }) .expect("subvolume create"); let mount_idx = recorded .iter() .position(|s| s.program == "mount" && s.args.iter().any(|a| a.contains("subvol=@"))) .expect("subvol mount"); assert!( create_idx < mount_idx, "subvolumes must be created before mounting @" ); } #[test] fn fstab_and_crypttab_written_into_target() { let runner = RecordingRunner::new(); runner.set_response( "blkid", crate::command::CommandOutput { status: 0, success: true, stdout: b"1111-2222\n".to_vec(), stderr: Vec::new(), duration: std::time::Duration::from_millis(1), }, ); let exec = make_exec(runner.clone()); let cfg = InstallConfig::default(); let mut state = StorageState { efi_part: "/dev/vda1".into(), root_part: "/dev/vda2".into(), root_source: "/dev/mapper/cryptroot".into(), ..Default::default() }; write_fstab(&exec, &cfg, &mut state).unwrap(); write_crypttab(&exec, &cfg, &state).unwrap(); let fstab = std::fs::read_to_string(exec.root.join("etc/fstab")).unwrap(); assert!(fstab.contains("subvol=@swap")); let crypttab = std::fs::read_to_string(exec.root.join("etc/crypttab")).unwrap(); assert!(crypttab.contains("cryptroot UUID=1111-2222 none luks,discard")); } }