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.
This commit is contained in:
Raito Bezarius 2023-07-22 19:16:39 +02:00 committed by nikstur
parent 609c11f26d
commit 0107754d62
9 changed files with 73 additions and 54 deletions

View File

@ -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<Self> {
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)
})
}
}

View File

@ -3,53 +3,11 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result};
use crate::architecture::Architecture;
use crate::generation::Generation; 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<Self> {
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 /// Generic ESP paths which can be specific to a bootloader
pub trait EspPaths<const N: usize> { pub trait EspPaths<const N: usize> {
/// Build an ESP path structure out of the ESP root directory /// Build an ESP path structure out of the ESP root directory

View File

@ -1,3 +1,4 @@
pub mod architecture;
pub mod esp; pub mod esp;
pub mod gc; pub mod gc;
pub mod generation; pub mod generation;

View File

@ -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()
}
}

View File

@ -3,8 +3,8 @@ use std::path::PathBuf;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use crate::esp::Architecture;
use crate::install; use crate::install;
use lanzaboote_tool::architecture::Architecture;
use lanzaboote_tool::signature::KeyPair; use lanzaboote_tool::signature::KeyPair;
/// The default log level. /// The default log level.
@ -31,9 +31,9 @@ enum Commands {
#[derive(Parser)] #[derive(Parser)]
struct InstallCommand { struct InstallCommand {
/// Target system /// System for lanzaboote binaries, e.g. defines the EFI fallback path
#[arg(long)] #[arg(long)]
target_system: String, system: String,
/// Systemd path /// Systemd path
#[arg(long)] #[arg(long)]
@ -95,7 +95,7 @@ fn install(args: InstallCommand) -> Result<()> {
install::Installer::new( install::Installer::new(
PathBuf::from(lanzaboote_stub), PathBuf::from(lanzaboote_stub),
Architecture::from_nixos_system(&args.target_system)?, Architecture::from_nixos_system(&args.system)?,
args.systemd, args.systemd,
args.systemd_boot_loader_config, args.systemd_boot_loader_config,
key_pair, key_pair,

View File

@ -1,5 +1,7 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use crate::architecture::SystemdArchitectureExt;
use lanzaboote_tool::architecture::Architecture;
use lanzaboote_tool::esp::EspPaths; use lanzaboote_tool::esp::EspPaths;
/// Paths to the boot files that are not specific to a generation. /// Paths to the boot files that are not specific to a generation.
@ -18,7 +20,7 @@ pub struct SystemdEspPaths {
} }
impl EspPaths<10> for SystemdEspPaths { impl EspPaths<10> for SystemdEspPaths {
fn new(esp: impl AsRef<Path>) -> Self { fn new(esp: impl AsRef<Path>, architecture: Architecture) -> Self {
let esp = esp.as_ref(); let esp = esp.as_ref();
let efi = esp.join("EFI"); let efi = esp.join("EFI");
let efi_nixos = efi.join("nixos"); let efi_nixos = efi.join("nixos");
@ -34,9 +36,9 @@ impl EspPaths<10> for SystemdEspPaths {
nixos: efi_nixos, nixos: efi_nixos,
linux: efi_linux, linux: efi_linux,
efi_fallback_dir: efi_efi_fallback_dir.clone(), 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: efi_systemd.clone(),
systemd_boot: efi_systemd.join("systemd-bootx64.efi"), systemd_boot: efi_systemd.join(architecture.systemd_filename()),
loader, loader,
systemd_boot_loader_config, systemd_boot_loader_config,
} }

View File

@ -10,9 +10,11 @@ use anyhow::{anyhow, Context, Result};
use nix::unistd::syncfs; use nix::unistd::syncfs;
use tempfile::TempDir; use tempfile::TempDir;
use crate::architecture::SystemdArchitectureExt;
use crate::esp::SystemdEspPaths; use crate::esp::SystemdEspPaths;
use crate::version::SystemdVersion; 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::gc::Roots;
use lanzaboote_tool::generation::{Generation, GenerationLink}; use lanzaboote_tool::generation::{Generation, GenerationLink};
use lanzaboote_tool::os_release::OsRelease; use lanzaboote_tool::os_release::OsRelease;
@ -45,8 +47,8 @@ impl Installer {
generation_links: Vec<PathBuf>, generation_links: Vec<PathBuf>,
) -> Self { ) -> Self {
let mut gc_roots = Roots::new(); let mut gc_roots = Roots::new();
let esp_paths = EspPaths::new(esp, target_arch); let esp_paths = SystemdEspPaths::new(esp, arch);
gc_roots.extend(esp_paths.to_iter()); gc_roots.extend(esp_paths.iter());
Self { Self {
broken_gens: BTreeSet::new(), broken_gens: BTreeSet::new(),

View File

@ -0,0 +1 @@
pub mod architecture;

View File

@ -1,3 +1,4 @@
mod architecture;
mod cli; mod cli;
mod esp; mod esp;
mod install; mod install;