From 0107754d62dd5b5738c2f648cd1a9815ae813677 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 22 Jul 2023 19:16:39 +0200 Subject: [PATCH] tool(architecture): make it generic Architecture is now a generic structure that can be specialized via an "external" trait for generating the paths you care about depending on your target bootloader. --- rust/tool/shared/src/architecture.rs | 35 ++++++++++++++++++++ rust/tool/shared/src/esp.rs | 46 ++------------------------- rust/tool/shared/src/lib.rs | 1 + rust/tool/systemd/src/architecture.rs | 19 +++++++++++ rust/tool/systemd/src/cli.rs | 8 ++--- rust/tool/systemd/src/esp.rs | 8 +++-- rust/tool/systemd/src/install.rs | 8 +++-- rust/tool/systemd/src/lib.rs | 1 + rust/tool/systemd/src/main.rs | 1 + 9 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 rust/tool/shared/src/architecture.rs create mode 100644 rust/tool/systemd/src/architecture.rs create mode 100644 rust/tool/systemd/src/lib.rs diff --git a/rust/tool/shared/src/architecture.rs b/rust/tool/shared/src/architecture.rs new file mode 100644 index 0000000..9a55f3b --- /dev/null +++ b/rust/tool/shared/src/architecture.rs @@ -0,0 +1,35 @@ +use std::path::PathBuf; + +use anyhow::{bail, Result}; + +/// Supported system +#[non_exhaustive] +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum Architecture { + X86, + AArch64, +} + +impl Architecture { + pub fn efi_representation(&self) -> &str { + match self { + Self::X86 => "x64", + Self::AArch64 => "aa64", + } + } + + pub fn efi_fallback_filename(&self) -> PathBuf { + format!("BOOT{}.EFI", self.efi_representation().to_ascii_uppercase()).into() + } +} + +impl Architecture { + /// Converts from a NixOS system double to a supported system + pub fn from_nixos_system(system_double: &str) -> Result { + Ok(match system_double { + "x86_64-linux" => Self::X86, + "aarch64-linux" => Self::AArch64, + _ => bail!("Unsupported NixOS system double: {}, please open an issue or a PR if you think this should be supported.", system_double) + }) + } +} diff --git a/rust/tool/shared/src/esp.rs b/rust/tool/shared/src/esp.rs index 348e64c..e09d927 100644 --- a/rust/tool/shared/src/esp.rs +++ b/rust/tool/shared/src/esp.rs @@ -3,53 +3,11 @@ use std::{ path::{Path, PathBuf}, }; -use anyhow::{Context, Result, bail}; +use anyhow::{Context, Result}; +use crate::architecture::Architecture; use crate::generation::Generation; -/// Supported system -#[allow(dead_code)] -#[non_exhaustive] -#[derive(Copy, Clone, PartialEq, Debug)] -pub enum Architecture { - X86, - AArch64, -} - -impl Architecture { - pub fn systemd_stub_filename(&self) -> &Path { - Path::new(match self { - Self::X86 => "linuxx64.efi.stub", - Self::AArch64 => "linuxaa64.efi.stub" - }) - } - - pub fn systemd_filename(&self) -> &Path { - Path::new(match self { - Self::X86 => "systemd-bootx64.efi", - Self::AArch64 => "systemd-bootaa64.efi" - }) - } - - pub fn efi_fallback_filename(&self) -> &Path { - Path::new(match self { - Self::X86 => "BOOTX64.EFI", - Self::AArch64 => "BOOTAA64.EFI", - }) - } -} - -impl Architecture { - /// Converts from a NixOS system double to a supported system - pub fn from_nixos_system(system_double: &str) -> Result { - Ok(match system_double { - "x86_64-linux" => Self::X86, - "aarch64-linux" => Self::AArch64, - _ => bail!("Unsupported NixOS system double: {}, please open an issue or a PR if you think this should be supported.", system_double) - }) - } -} - /// Generic ESP paths which can be specific to a bootloader pub trait EspPaths { /// Build an ESP path structure out of the ESP root directory diff --git a/rust/tool/shared/src/lib.rs b/rust/tool/shared/src/lib.rs index eb3c899..35c1465 100644 --- a/rust/tool/shared/src/lib.rs +++ b/rust/tool/shared/src/lib.rs @@ -1,3 +1,4 @@ +pub mod architecture; pub mod esp; pub mod gc; pub mod generation; diff --git a/rust/tool/systemd/src/architecture.rs b/rust/tool/systemd/src/architecture.rs new file mode 100644 index 0000000..173f148 --- /dev/null +++ b/rust/tool/systemd/src/architecture.rs @@ -0,0 +1,19 @@ +use std::path::PathBuf; + +use lanzaboote_tool::architecture::Architecture; + +/// Systemd-specific architecture helpers +pub trait SystemdArchitectureExt { + fn systemd_stub_filename(&self) -> PathBuf; + fn systemd_filename(&self) -> PathBuf; +} + +impl SystemdArchitectureExt for Architecture { + fn systemd_stub_filename(&self) -> PathBuf { + format!("linux{}.efi.stub", self.efi_representation()).into() + } + + fn systemd_filename(&self) -> PathBuf { + format!("systemd-boot{}.efi", self.efi_representation()).into() + } +} diff --git a/rust/tool/systemd/src/cli.rs b/rust/tool/systemd/src/cli.rs index c8d4b47..90d91c0 100644 --- a/rust/tool/systemd/src/cli.rs +++ b/rust/tool/systemd/src/cli.rs @@ -3,8 +3,8 @@ use std::path::PathBuf; use anyhow::{Context, Result}; use clap::{Parser, Subcommand}; -use crate::esp::Architecture; use crate::install; +use lanzaboote_tool::architecture::Architecture; use lanzaboote_tool::signature::KeyPair; /// The default log level. @@ -31,9 +31,9 @@ enum Commands { #[derive(Parser)] struct InstallCommand { - /// Target system + /// System for lanzaboote binaries, e.g. defines the EFI fallback path #[arg(long)] - target_system: String, + system: String, /// Systemd path #[arg(long)] @@ -95,7 +95,7 @@ fn install(args: InstallCommand) -> Result<()> { install::Installer::new( PathBuf::from(lanzaboote_stub), - Architecture::from_nixos_system(&args.target_system)?, + Architecture::from_nixos_system(&args.system)?, args.systemd, args.systemd_boot_loader_config, key_pair, diff --git a/rust/tool/systemd/src/esp.rs b/rust/tool/systemd/src/esp.rs index e3afd6e..7227367 100644 --- a/rust/tool/systemd/src/esp.rs +++ b/rust/tool/systemd/src/esp.rs @@ -1,5 +1,7 @@ use std::path::{Path, PathBuf}; +use crate::architecture::SystemdArchitectureExt; +use lanzaboote_tool::architecture::Architecture; use lanzaboote_tool::esp::EspPaths; /// Paths to the boot files that are not specific to a generation. @@ -18,7 +20,7 @@ pub struct SystemdEspPaths { } impl EspPaths<10> for SystemdEspPaths { - fn new(esp: impl AsRef) -> Self { + fn new(esp: impl AsRef, architecture: Architecture) -> Self { let esp = esp.as_ref(); let efi = esp.join("EFI"); let efi_nixos = efi.join("nixos"); @@ -34,9 +36,9 @@ impl EspPaths<10> for SystemdEspPaths { nixos: efi_nixos, linux: efi_linux, efi_fallback_dir: efi_efi_fallback_dir.clone(), - efi_fallback: efi_efi_fallback_dir.join("BOOTX64.EFI"), + efi_fallback: efi_efi_fallback_dir.join(architecture.efi_fallback_filename()), systemd: efi_systemd.clone(), - systemd_boot: efi_systemd.join("systemd-bootx64.efi"), + systemd_boot: efi_systemd.join(architecture.systemd_filename()), loader, systemd_boot_loader_config, } diff --git a/rust/tool/systemd/src/install.rs b/rust/tool/systemd/src/install.rs index d0de11d..55a0733 100644 --- a/rust/tool/systemd/src/install.rs +++ b/rust/tool/systemd/src/install.rs @@ -10,9 +10,11 @@ use anyhow::{anyhow, Context, Result}; use nix::unistd::syncfs; use tempfile::TempDir; +use crate::architecture::SystemdArchitectureExt; use crate::esp::SystemdEspPaths; use crate::version::SystemdVersion; -use lanzaboote_tool::esp::{EspGenerationPaths, EspPaths, Architecture}; +use lanzaboote_tool::architecture::Architecture; +use lanzaboote_tool::esp::{EspGenerationPaths, EspPaths}; use lanzaboote_tool::gc::Roots; use lanzaboote_tool::generation::{Generation, GenerationLink}; use lanzaboote_tool::os_release::OsRelease; @@ -45,8 +47,8 @@ impl Installer { generation_links: Vec, ) -> Self { let mut gc_roots = Roots::new(); - let esp_paths = EspPaths::new(esp, target_arch); - gc_roots.extend(esp_paths.to_iter()); + let esp_paths = SystemdEspPaths::new(esp, arch); + gc_roots.extend(esp_paths.iter()); Self { broken_gens: BTreeSet::new(), diff --git a/rust/tool/systemd/src/lib.rs b/rust/tool/systemd/src/lib.rs new file mode 100644 index 0000000..0f1102b --- /dev/null +++ b/rust/tool/systemd/src/lib.rs @@ -0,0 +1 @@ +pub mod architecture; diff --git a/rust/tool/systemd/src/main.rs b/rust/tool/systemd/src/main.rs index b453557..51633de 100644 --- a/rust/tool/systemd/src/main.rs +++ b/rust/tool/systemd/src/main.rs @@ -1,3 +1,4 @@ +mod architecture; mod cli; mod esp; mod install;