2022-11-23 08:26:26 -06:00
|
|
|
use std::fs;
|
2022-11-25 10:47:24 -06:00
|
|
|
use std::os::unix::prelude::PermissionsExt;
|
2022-11-25 06:07:04 -06:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-11-26 07:55:15 -06:00
|
|
|
use std::process::Command;
|
2022-11-23 08:26:26 -06:00
|
|
|
|
2022-11-24 06:33:01 -06:00
|
|
|
use anyhow::{Context, Result};
|
2022-11-26 07:55:15 -06:00
|
|
|
use nix::unistd::sync;
|
2022-11-23 08:26:26 -06:00
|
|
|
|
2023-01-18 16:52:13 -06:00
|
|
|
use crate::esp::{EspGenerationPaths, EspPaths};
|
2022-12-11 19:09:19 -06:00
|
|
|
use crate::gc::Roots;
|
|
|
|
use crate::generation::{Generation, GenerationLink};
|
2023-01-01 18:54:57 -06:00
|
|
|
use crate::os_release::OsRelease;
|
2022-11-23 13:40:01 -06:00
|
|
|
use crate::pe;
|
2022-11-26 16:19:08 -06:00
|
|
|
use crate::signature::KeyPair;
|
2023-01-25 16:39:46 -06:00
|
|
|
use crate::systemd::SystemdVersion;
|
2023-01-12 20:10:58 -06:00
|
|
|
use crate::utils::SecureTempDirExt;
|
2022-11-23 04:59:54 -06:00
|
|
|
|
2022-11-26 07:55:15 -06:00
|
|
|
pub struct Installer {
|
2022-12-11 19:09:19 -06:00
|
|
|
gc_roots: Roots,
|
2022-11-26 07:55:15 -06:00
|
|
|
lanzaboote_stub: PathBuf,
|
2023-01-17 18:58:45 -06:00
|
|
|
systemd: PathBuf,
|
2023-01-26 17:37:05 -06:00
|
|
|
systemd_boot_loader_config: PathBuf,
|
2022-11-26 16:19:08 -06:00
|
|
|
key_pair: KeyPair,
|
2022-12-11 19:09:19 -06:00
|
|
|
configuration_limit: usize,
|
2023-01-18 16:52:13 -06:00
|
|
|
esp_paths: EspPaths,
|
2022-12-11 19:09:19 -06:00
|
|
|
generation_links: Vec<PathBuf>,
|
2022-11-26 07:55:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Installer {
|
|
|
|
pub fn new(
|
|
|
|
lanzaboote_stub: PathBuf,
|
2023-01-17 18:58:45 -06:00
|
|
|
systemd: PathBuf,
|
2023-01-26 17:37:05 -06:00
|
|
|
systemd_boot_loader_config: PathBuf,
|
2022-11-26 16:19:08 -06:00
|
|
|
key_pair: KeyPair,
|
2022-12-11 19:09:19 -06:00
|
|
|
configuration_limit: usize,
|
2022-11-26 15:23:00 -06:00
|
|
|
esp: PathBuf,
|
2022-12-11 19:09:19 -06:00
|
|
|
generation_links: Vec<PathBuf>,
|
2022-11-26 07:55:15 -06:00
|
|
|
) -> Self {
|
2023-01-18 16:52:13 -06:00
|
|
|
let mut gc_roots = Roots::new();
|
|
|
|
let esp_paths = EspPaths::new(esp);
|
|
|
|
gc_roots.extend(esp_paths.to_iter());
|
|
|
|
|
2022-11-26 07:55:15 -06:00
|
|
|
Self {
|
2023-01-18 16:52:13 -06:00
|
|
|
gc_roots,
|
2022-11-26 07:55:15 -06:00
|
|
|
lanzaboote_stub,
|
2023-01-17 18:58:45 -06:00
|
|
|
systemd,
|
2023-01-26 17:37:05 -06:00
|
|
|
systemd_boot_loader_config,
|
2022-11-26 16:19:08 -06:00
|
|
|
key_pair,
|
2022-12-11 19:09:19 -06:00
|
|
|
configuration_limit,
|
2023-01-18 16:52:13 -06:00
|
|
|
esp_paths,
|
2022-12-11 19:09:19 -06:00
|
|
|
generation_links,
|
2022-11-26 07:55:15 -06:00
|
|
|
}
|
2022-11-25 18:50:51 -06:00
|
|
|
}
|
2022-11-25 18:24:33 -06:00
|
|
|
|
2022-12-11 19:09:19 -06:00
|
|
|
pub fn install(&mut self) -> Result<()> {
|
|
|
|
let mut links = self
|
|
|
|
.generation_links
|
|
|
|
.iter()
|
|
|
|
.map(GenerationLink::from_path)
|
|
|
|
.collect::<Result<Vec<GenerationLink>>>()?;
|
|
|
|
|
|
|
|
// A configuration limit of 0 means there is no limit.
|
|
|
|
if self.configuration_limit > 0 {
|
|
|
|
// Sort the links by version.
|
|
|
|
links.sort_by_key(|l| l.version);
|
|
|
|
|
|
|
|
// Only install the number of generations configured.
|
|
|
|
links = links
|
|
|
|
.into_iter()
|
|
|
|
.rev()
|
|
|
|
.take(self.configuration_limit)
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
self.install_links(links)?;
|
|
|
|
|
2023-01-17 18:58:45 -06:00
|
|
|
self.install_systemd_boot()?;
|
|
|
|
|
2023-01-03 18:20:50 -06:00
|
|
|
// Only collect garbage in these two directories. This way, no files that do not belong to
|
|
|
|
// the NixOS installation are deleted. Lanzatool takes full control over the esp/EFI/nixos
|
|
|
|
// directory and deletes ALL files that it doesn't know about. Dual- or multiboot setups
|
|
|
|
// that need files in this directory will NOT work.
|
2023-01-18 16:52:13 -06:00
|
|
|
self.gc_roots.collect_garbage(&self.esp_paths.nixos)?;
|
2023-01-03 18:20:50 -06:00
|
|
|
// The esp/EFI/Linux directory is assumed to be potentially shared with other distros.
|
|
|
|
// Thus, only files that start with "nixos-" are garbage collected (i.e. potentially
|
|
|
|
// deleted).
|
|
|
|
self.gc_roots
|
2023-01-18 16:52:13 -06:00
|
|
|
.collect_garbage_with_filter(&self.esp_paths.linux, |p| {
|
2023-01-03 18:20:50 -06:00
|
|
|
p.file_name()
|
|
|
|
.and_then(|n| n.to_str())
|
|
|
|
.map_or(false, |n| n.starts_with("nixos-"))
|
|
|
|
})?;
|
2022-11-25 06:07:04 -06:00
|
|
|
|
2022-12-11 19:09:19 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn install_links(&mut self, links: Vec<GenerationLink>) -> Result<()> {
|
|
|
|
for link in links {
|
|
|
|
let generation_result = Generation::from_link(&link)
|
|
|
|
.with_context(|| format!("Failed to build generation from link: {link:?}"));
|
|
|
|
|
|
|
|
// Ignore failing to read a generation so that old malformed generations do not stop
|
|
|
|
// lanzatool from working.
|
2022-12-25 19:36:24 -06:00
|
|
|
let generation = match generation_result {
|
|
|
|
Ok(generation) => generation,
|
|
|
|
Err(e) => {
|
|
|
|
println!("Malformed generation: {:?}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2022-12-28 16:51:51 -06:00
|
|
|
|
2022-11-26 07:55:15 -06:00
|
|
|
println!("Installing generation {generation}");
|
2022-11-25 06:07:04 -06:00
|
|
|
|
2022-11-27 04:19:02 -06:00
|
|
|
self.install_generation(&generation)
|
|
|
|
.context("Failed to install generation")?;
|
|
|
|
|
2022-12-17 17:31:09 -06:00
|
|
|
for (name, bootspec) in &generation.spec.bootspec.specialisation {
|
|
|
|
let specialised_generation = generation.specialise(name, bootspec)?;
|
2022-11-27 04:19:02 -06:00
|
|
|
|
|
|
|
println!("Installing specialisation: {name} of generation: {generation}");
|
|
|
|
|
|
|
|
self.install_generation(&specialised_generation)
|
|
|
|
.context("Failed to install specialisation")?;
|
|
|
|
}
|
2022-11-26 07:55:15 -06:00
|
|
|
}
|
|
|
|
Ok(())
|
2022-11-25 06:07:04 -06:00
|
|
|
}
|
2022-11-23 04:59:54 -06:00
|
|
|
|
2022-12-11 19:09:19 -06:00
|
|
|
fn install_generation(&mut self, generation: &Generation) -> Result<()> {
|
2022-12-17 17:31:09 -06:00
|
|
|
let bootspec = &generation.spec.bootspec;
|
2022-11-26 07:55:15 -06:00
|
|
|
|
2023-01-18 16:52:13 -06:00
|
|
|
let esp_gen_paths = EspGenerationPaths::new(&self.esp_paths, generation)?;
|
|
|
|
self.gc_roots.extend(esp_gen_paths.to_iter());
|
2022-11-26 07:55:15 -06:00
|
|
|
|
2022-11-26 15:23:00 -06:00
|
|
|
let kernel_cmdline =
|
|
|
|
assemble_kernel_cmdline(&bootspec.init, bootspec.kernel_params.clone());
|
2022-11-26 07:55:15 -06:00
|
|
|
|
2023-01-12 20:10:58 -06:00
|
|
|
// This tempdir must live for the entire lifetime of the current function.
|
|
|
|
let tempdir = tempfile::tempdir()?;
|
2022-11-26 07:55:15 -06:00
|
|
|
|
2023-01-01 18:54:57 -06:00
|
|
|
let os_release = OsRelease::from_generation(generation)
|
|
|
|
.context("Failed to build OsRelease from generation.")?;
|
2023-01-12 20:10:58 -06:00
|
|
|
let os_release_path = tempdir
|
|
|
|
.write_secure_file("os-release", os_release.to_string().as_bytes())
|
|
|
|
.context("Failed to write os-release file.")?;
|
2023-01-01 18:54:57 -06:00
|
|
|
|
2022-12-03 06:16:46 -06:00
|
|
|
println!("Appending secrets to initrd...");
|
2022-11-26 07:55:15 -06:00
|
|
|
|
2023-01-12 20:10:58 -06:00
|
|
|
let initrd_location = tempdir.path().join("initrd");
|
2023-01-27 20:17:35 -06:00
|
|
|
fs::copy(
|
2022-12-17 17:31:09 -06:00
|
|
|
bootspec
|
|
|
|
.initrd
|
|
|
|
.as_ref()
|
|
|
|
.context("Lanzaboote does not support missing initrd yet")?,
|
|
|
|
&initrd_location,
|
|
|
|
)?;
|
2022-11-26 15:23:00 -06:00
|
|
|
if let Some(initrd_secrets_script) = &bootspec.initrd_secrets {
|
2022-11-26 16:19:08 -06:00
|
|
|
append_initrd_secrets(initrd_secrets_script, &initrd_location)?;
|
2022-11-26 07:55:15 -06:00
|
|
|
}
|
|
|
|
|
2023-01-23 06:52:24 -06:00
|
|
|
// The initrd and kernel don't need to be signed.
|
|
|
|
// The stub has their hashes embedded and will refuse loading on hash mismatches.
|
2023-01-17 18:58:45 -06:00
|
|
|
//
|
|
|
|
// The initrd and kernel are not forcibly installed because they are not built
|
|
|
|
// reproducibly. Forcibly installing (i.e. overwriting) them is likely to break older
|
|
|
|
// generations that point to the same initrd/kernel because the hash embedded in the stub
|
|
|
|
// will not match anymore.
|
2023-01-18 16:52:13 -06:00
|
|
|
install(&initrd_location, &esp_gen_paths.initrd)
|
|
|
|
.context("Failed to install initrd to ESP")?;
|
2023-01-23 06:52:24 -06:00
|
|
|
// Do not sign the kernel.
|
|
|
|
// Boot loader specification could be used to make a signed kernel load an unprotected initrd.
|
|
|
|
install(&bootspec.kernel, &esp_gen_paths.kernel)
|
2023-01-17 18:58:45 -06:00
|
|
|
.context("Failed to install kernel to ESP.")?;
|
2022-11-27 19:39:36 -06:00
|
|
|
|
2022-11-27 19:23:43 -06:00
|
|
|
let lanzaboote_image = pe::lanzaboote_image(
|
2023-01-12 20:10:58 -06:00
|
|
|
&tempdir,
|
2022-11-27 19:23:43 -06:00
|
|
|
&self.lanzaboote_stub,
|
2023-01-01 18:54:57 -06:00
|
|
|
&os_release_path,
|
2022-11-27 19:23:43 -06:00
|
|
|
&kernel_cmdline,
|
2023-01-18 16:52:13 -06:00
|
|
|
&esp_gen_paths.kernel,
|
|
|
|
&esp_gen_paths.initrd,
|
|
|
|
&self.esp_paths.esp,
|
2022-11-27 19:23:43 -06:00
|
|
|
)
|
|
|
|
.context("Failed to assemble stub")?;
|
2022-11-26 07:55:15 -06:00
|
|
|
|
2022-11-27 19:23:43 -06:00
|
|
|
install_signed(
|
|
|
|
&self.key_pair,
|
|
|
|
&lanzaboote_image,
|
2023-01-18 16:52:13 -06:00
|
|
|
&esp_gen_paths.lanzaboote_image,
|
2022-11-27 19:23:43 -06:00
|
|
|
)
|
|
|
|
.context("Failed to install lanzaboote")?;
|
2022-11-26 07:55:15 -06:00
|
|
|
|
2022-11-27 19:23:43 -06:00
|
|
|
// Sync files to persistent storage. This may improve the
|
|
|
|
// chance of a consistent boot directory in case the system
|
|
|
|
// crashes.
|
|
|
|
sync();
|
2022-11-26 07:55:15 -06:00
|
|
|
|
|
|
|
println!(
|
|
|
|
"Successfully installed lanzaboote to '{}'",
|
2023-01-18 16:52:13 -06:00
|
|
|
self.esp_paths.esp.display()
|
2022-11-26 07:55:15 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-01-17 18:58:45 -06:00
|
|
|
|
|
|
|
/// Install systemd-boot to ESP.
|
|
|
|
///
|
|
|
|
/// systemd-boot is only updated when a newer version is available OR when the currently
|
|
|
|
/// installed version is not signed. This enables switching to Lanzaboote without having to
|
|
|
|
/// manually delete previous unsigned systemd-boot binaries and minimizes the number of writes
|
|
|
|
/// to the ESP.
|
|
|
|
///
|
|
|
|
/// Checking for the version also allows us to skip buggy systemd versions in the future.
|
|
|
|
fn install_systemd_boot(&self) -> Result<()> {
|
|
|
|
let systemd_boot = self
|
|
|
|
.systemd
|
|
|
|
.join("lib/systemd/boot/efi/systemd-bootx64.efi");
|
|
|
|
|
|
|
|
let paths = [
|
|
|
|
(&systemd_boot, &self.esp_paths.efi_fallback),
|
|
|
|
(&systemd_boot, &self.esp_paths.systemd_boot),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (from, to) in paths {
|
|
|
|
if newer_systemd_boot(from, to)? || !&self.key_pair.verify(to) {
|
|
|
|
force_install_signed(&self.key_pair, from, to)
|
|
|
|
.with_context(|| format!("Failed to install systemd-boot binary to: {to:?}"))?;
|
|
|
|
}
|
|
|
|
}
|
2023-01-26 17:37:05 -06:00
|
|
|
|
|
|
|
install(
|
|
|
|
&self.systemd_boot_loader_config,
|
|
|
|
&self.esp_paths.systemd_boot_loader_config,
|
|
|
|
)
|
|
|
|
.with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Failed to install systemd-boot loader.conf to {:?}",
|
|
|
|
&self.esp_paths.systemd_boot_loader_config
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
2023-01-17 18:58:45 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-24 06:33:01 -06:00
|
|
|
}
|
2022-11-23 08:26:26 -06:00
|
|
|
|
2022-11-27 19:23:43 -06:00
|
|
|
/// Install a PE file. The PE gets signed in the process.
|
2022-12-03 06:16:46 -06:00
|
|
|
///
|
|
|
|
/// The file is only signed and copied if it doesn't exist at the destination
|
2022-11-27 19:23:43 -06:00
|
|
|
fn install_signed(key_pair: &KeyPair, from: &Path, to: &Path) -> Result<()> {
|
2022-12-03 06:16:46 -06:00
|
|
|
if to.exists() {
|
|
|
|
println!("{} already exists, skipping...", to.display());
|
|
|
|
} else {
|
2023-01-17 18:58:45 -06:00
|
|
|
force_install_signed(key_pair, from, to)?;
|
2022-12-03 06:16:46 -06:00
|
|
|
}
|
2022-11-27 19:23:43 -06:00
|
|
|
|
2022-12-03 06:16:46 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-01-17 18:58:45 -06:00
|
|
|
/// Sign and forcibly install a PE file.
|
|
|
|
///
|
|
|
|
/// If the file already exists at the destination, it is overwritten.
|
2023-01-27 20:17:35 -06:00
|
|
|
///
|
|
|
|
/// This is implemented as an atomic write. The file is first written to the destination with a
|
|
|
|
/// `.tmp` suffix and then renamed to its final name. This is atomic, because a rename is an atomic
|
|
|
|
/// operation on POSIX platforms.
|
2023-01-17 18:58:45 -06:00
|
|
|
fn force_install_signed(key_pair: &KeyPair, from: &Path, to: &Path) -> Result<()> {
|
|
|
|
println!("Signing and installing {}...", to.display());
|
2023-01-27 20:17:35 -06:00
|
|
|
let to_tmp = to.with_extension(".tmp");
|
|
|
|
ensure_parent_dir(&to_tmp);
|
2023-01-17 18:58:45 -06:00
|
|
|
key_pair
|
2023-01-27 20:17:35 -06:00
|
|
|
.sign_and_copy(from, &to_tmp)
|
2023-01-17 18:58:45 -06:00
|
|
|
.with_context(|| format!("Failed to copy and sign file from {from:?} to {to:?}"))?;
|
2023-01-27 20:17:35 -06:00
|
|
|
fs::rename(&to_tmp, to).with_context(|| {
|
|
|
|
format!("Failed to move temporary file {to_tmp:?} to final location {to:?}")
|
|
|
|
})?;
|
2023-01-17 18:58:45 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:17:35 -06:00
|
|
|
/// Install an arbitrary file.
|
|
|
|
///
|
|
|
|
/// The file is only copied if it doesn't exist at the destination.
|
2022-12-03 06:16:46 -06:00
|
|
|
///
|
2023-01-27 20:17:35 -06:00
|
|
|
/// This function is only designed to copy files to the ESP. It sets the permission bits of the
|
|
|
|
/// file at the destination to 0o755, the expected permissions for a vfat ESP. This is useful for
|
|
|
|
/// producing file systems trees which can then be converted to a file system image.
|
2022-12-03 06:16:46 -06:00
|
|
|
fn install(from: &Path, to: &Path) -> Result<()> {
|
|
|
|
if to.exists() {
|
|
|
|
println!("{} already exists, skipping...", to.display());
|
|
|
|
} else {
|
|
|
|
println!("Installing {}...", to.display());
|
|
|
|
ensure_parent_dir(to);
|
2023-01-27 20:17:35 -06:00
|
|
|
atomic_copy(from, to)?;
|
|
|
|
set_permission_bits(to, 0o755)
|
|
|
|
.with_context(|| format!("Failed to set permission bits to 0o755 on file: {to:?}"))?;
|
2022-12-03 06:16:46 -06:00
|
|
|
}
|
2022-11-27 19:23:43 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-26 07:55:15 -06:00
|
|
|
pub fn append_initrd_secrets(
|
|
|
|
append_initrd_secrets_path: &Path,
|
|
|
|
initrd_path: &PathBuf,
|
|
|
|
) -> Result<()> {
|
2022-11-25 18:50:51 -06:00
|
|
|
let status = Command::new(append_initrd_secrets_path)
|
2022-11-26 07:55:15 -06:00
|
|
|
.args(vec![initrd_path])
|
2022-11-25 18:50:51 -06:00
|
|
|
.status()
|
|
|
|
.context("Failed to append initrd secrets")?;
|
|
|
|
if !status.success() {
|
2022-11-26 07:55:15 -06:00
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"Failed to append initrd secrets with args `{:?}`",
|
|
|
|
vec![append_initrd_secrets_path, initrd_path]
|
2022-11-26 16:19:08 -06:00
|
|
|
));
|
2022-11-25 18:50:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-26 15:23:00 -06:00
|
|
|
fn assemble_kernel_cmdline(init: &Path, kernel_params: Vec<String>) -> Vec<String> {
|
|
|
|
let init_string = String::from(
|
|
|
|
init.to_str()
|
|
|
|
.expect("Failed to convert init path to string"),
|
|
|
|
);
|
2022-11-25 06:07:04 -06:00
|
|
|
let mut kernel_cmdline: Vec<String> = vec![format!("init={}", init_string)];
|
|
|
|
kernel_cmdline.extend(kernel_params);
|
|
|
|
kernel_cmdline
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:17:35 -06:00
|
|
|
/// Atomically copy a file.
|
|
|
|
///
|
|
|
|
/// The file is first written to the destination with a `.tmp` suffix and then renamed to its final
|
|
|
|
/// name. This is atomic, because a rename is an atomic operation on POSIX platforms.
|
|
|
|
fn atomic_copy(from: &Path, to: &Path) -> Result<()> {
|
|
|
|
let to_tmp = to.with_extension(".tmp");
|
2022-11-25 10:47:24 -06:00
|
|
|
|
2023-01-27 20:17:35 -06:00
|
|
|
fs::copy(from, &to_tmp)
|
|
|
|
.with_context(|| format!("Failed to copy from {from:?} to {to_tmp:?}",))?;
|
2022-11-25 10:47:24 -06:00
|
|
|
|
2023-01-27 20:17:35 -06:00
|
|
|
fs::rename(&to_tmp, to).with_context(|| {
|
|
|
|
format!("Failed to move temporary file {to_tmp:?} to final location {to:?}")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the octal permission bits of the specified file.
|
|
|
|
fn set_permission_bits(path: &Path, permission_bits: u32) -> Result<()> {
|
|
|
|
let mut perms = fs::metadata(path)
|
|
|
|
.with_context(|| format!("File {path:?} doesn't have any metadata"))?
|
|
|
|
.permissions();
|
|
|
|
perms.set_mode(permission_bits);
|
|
|
|
fs::set_permissions(path, perms)
|
|
|
|
.with_context(|| format!("Failed to set permissions on {path:?}"))
|
2022-11-23 08:26:26 -06:00
|
|
|
}
|
2022-11-26 08:32:43 -06:00
|
|
|
|
|
|
|
// Ensures the parent directory of an arbitrary path exists
|
|
|
|
fn ensure_parent_dir(path: &Path) {
|
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
fs::create_dir_all(parent).ok();
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 18:58:45 -06:00
|
|
|
|
|
|
|
/// Determine if a newer systemd-boot version is available.
|
|
|
|
///
|
|
|
|
/// "Newer" can mean
|
|
|
|
/// (1) no file exists at the destination,
|
|
|
|
/// (2) the file at the destination is malformed,
|
|
|
|
/// (3) a binary with a higher version is available.
|
|
|
|
fn newer_systemd_boot(from: &Path, to: &Path) -> Result<bool> {
|
|
|
|
// If the file doesn't exists at the destination, it should be installed.
|
|
|
|
if !to.exists() {
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the version from the source binary cannot be read, something is irrecoverably wrong.
|
2023-01-25 16:39:46 -06:00
|
|
|
let from_version = SystemdVersion::from_systemd_boot_binary(from)
|
2023-01-17 18:58:45 -06:00
|
|
|
.with_context(|| format!("Failed to read systemd-boot version from {from:?}."))?;
|
|
|
|
|
|
|
|
// If the version cannot be read from the destination binary, it is malformed. It should be
|
|
|
|
// forcibly reinstalled.
|
2023-01-25 16:39:46 -06:00
|
|
|
let to_version = match SystemdVersion::from_systemd_boot_binary(to) {
|
2023-01-17 18:58:45 -06:00
|
|
|
Ok(version) => version,
|
|
|
|
_ => return Ok(true),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(from_version > to_version)
|
|
|
|
}
|