Merge pull request #42 from nix-community/lanzatool-small-fixes

Lanzatool: small fixes
This commit is contained in:
nikstur 2022-12-31 00:00:36 +01:00 committed by GitHub
commit e439cf452b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 38 deletions

View File

@ -53,21 +53,13 @@ fn nixos_path(path: impl AsRef<Path>, name: &str) -> Result<PathBuf> {
.read_link() .read_link()
.unwrap_or_else(|_| path.as_ref().into()); .unwrap_or_else(|_| path.as_ref().into());
let parent = resolved.parent().ok_or_else(|| { let parent_final_component = resolved
anyhow::anyhow!(format!( .parent()
"Path: {} does not have a parent", .and_then(|x| x.file_name())
resolved.display() .and_then(|x| x.to_str())
)) .with_context(|| format!("Failed to extract final component from: {:?}", resolved))?;
})?;
let without_store = parent.strip_prefix("/nix/store").with_context(|| { let nixos_filename = format!("{}-{}.efi", parent_final_component, name);
format!(
"Failed to strip /nix/store from path {}",
path.as_ref().display()
)
})?;
let nixos_filename = format!("{}-{}.efi", without_store.display(), name);
Ok(PathBuf::from(nixos_filename)) Ok(PathBuf::from(nixos_filename))
} }
@ -82,3 +74,22 @@ fn generation_path(generation: &Generation) -> PathBuf {
PathBuf::from(format!("nixos-generation-{}.efi", generation)) PathBuf::from(format!("nixos-generation-{}.efi", generation))
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nixos_path_creates_correct_filename_from_nix_store_path() -> Result<()> {
let path =
Path::new("/nix/store/xqplddjjjy1lhzyzbcv4dza11ccpcfds-initrd-linux-6.1.1/initrd");
let generated_filename = nixos_path(path, "initrd")?;
let expected_filename =
PathBuf::from("xqplddjjjy1lhzyzbcv4dza11ccpcfds-initrd-linux-6.1.1-initrd.efi");
assert_eq!(generated_filename, expected_filename);
Ok(())
}
}

View File

@ -87,24 +87,29 @@ impl fmt::Display for Generation {
} }
} }
fn parse_version(toplevel: impl AsRef<Path>) -> Result<u64> { /// Parse version number from a path.
let file_name = toplevel ///
/// Expects a path in the format of "system-{version}-link".
fn parse_version(path: impl AsRef<Path>) -> Result<u64> {
let generation_version = path
.as_ref() .as_ref()
.file_name() .file_name()
.ok_or_else(|| anyhow::anyhow!("Failed to extract file name from generation"))?; .and_then(|x| x.to_str())
.and_then(|x| x.split('-').nth(1))
.and_then(|x| x.parse::<u64>().ok())
.with_context(|| format!("Failed to extract version from: {:?}", path.as_ref()))?;
let file_name_str = file_name Ok(generation_version)
.to_str() }
.with_context(|| "Failed to convert file name of generation to string")?;
#[cfg(test)]
let generation_version = file_name_str mod tests {
.split('-') use super::*;
.nth(1)
.ok_or_else(|| anyhow::anyhow!("Failed to extract version from generation"))?; #[test]
fn parse_version_correctly() {
let parsed_generation_version = generation_version let path = Path::new("system-2-link");
.parse() let parsed_version = parse_version(path).unwrap();
.with_context(|| format!("Failed to parse generation version: {}", generation_version))?; assert_eq!(parsed_version, 2,);
}
Ok(parsed_generation_version)
} }

View File

@ -32,7 +32,7 @@ pub fn lanzaboote_image(
let kernel_path_file = write_to_tmp( let kernel_path_file = write_to_tmp(
target_dir, target_dir,
"kernel-esp-path", "kernel-esp-path",
esp_relative_path_string(esp, kernel_path), esp_relative_path_string(esp, kernel_path)?,
)?; )?;
let kernel_hash_file = write_to_tmp( let kernel_hash_file = write_to_tmp(
target_dir, target_dir,
@ -43,7 +43,7 @@ pub fn lanzaboote_image(
let initrd_path_file = write_to_tmp( let initrd_path_file = write_to_tmp(
target_dir, target_dir,
"initrd-esp-path", "initrd-esp-path",
esp_relative_path_string(esp, initrd_path), esp_relative_path_string(esp, initrd_path)?,
)?; )?;
let initrd_hash_file = write_to_tmp( let initrd_hash_file = write_to_tmp(
target_dir, target_dir,
@ -167,17 +167,17 @@ fn write_to_tmp(
Ok(path) Ok(path)
} }
fn esp_relative_path_string(esp: &Path, path: &Path) -> String { fn esp_relative_path_string(esp: &Path, path: &Path) -> Result<String> {
let relative_path = path let relative_path = path
.strip_prefix(esp) .strip_prefix(esp)
.expect("Failed to make path relative to esp") .with_context(|| format!("Failed to strip prefix: {:?} from path: {:?}", esp, path))?
.to_owned(); .to_owned();
let relative_path_string = relative_path let relative_path_string = relative_path
.into_os_string() .into_os_string()
.into_string() .into_string()
.expect("Failed to convert path '{}' to a relative string path") .expect("Failed to convert path '{}' to a relative string path")
.replace('/', "\\"); .replace('/', "\\");
format!("\\{}", &relative_path_string) Ok(format!("\\{}", &relative_path_string))
} }
fn stub_offset(binary: &Path) -> Result<u64> { fn stub_offset(binary: &Path) -> Result<u64> {
@ -205,5 +205,13 @@ fn image_base(pe: &PE) -> u64 {
} }
fn file_size(path: impl AsRef<Path>) -> Result<u64> { fn file_size(path: impl AsRef<Path>) -> Result<u64> {
Ok(fs::File::open(path)?.metadata()?.size()) Ok(fs::File::open(&path)
.with_context(|| {
format!(
"Failed to read file to calculate its size: {:?}",
path.as_ref()
)
})?
.metadata()?
.size())
} }

View File

@ -1,4 +1,5 @@
use std::ffi::OsString; use std::ffi::OsString;
use std::io::Write;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
@ -31,7 +32,7 @@ impl KeyPair {
let output = Command::new("sbsign").args(&args).output()?; let output = Command::new("sbsign").args(&args).output()?;
if !output.status.success() { if !output.status.success() {
print!("{:?}", output.stderr); std::io::stderr().write_all(&output.stderr).unwrap();
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"Failed to sign file using sbsign with args `{:?}`", "Failed to sign file using sbsign with args `{:?}`",
&args &args