Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

tool: improve sd-boot generation display name #101

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions rust/tool/src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,12 @@ impl Generation {
///
/// This is currently implemented by poking around the filesystem to find the necessary data.
/// Ideally, the needed data should be included in the bootspec.
pub fn describe(&self) -> Result<String> {
let toplevel = &self.spec.bootspec.toplevel.0;

let nixos_version = fs::read_to_string(toplevel.join("nixos-version"))
.unwrap_or_else(|_| String::from("Unknown"));
let kernel_version =
read_kernel_version(toplevel).context("Failed to read kernel version.")?;
pub fn describe(&self) -> String {
let build_time = self
.build_time
.map(|x| x.to_string())
.unwrap_or_else(|| String::from("Unknown"));

Ok(format!(
"Generation {} NixOS {}, Linux Kernel {}, Built on {}",
self.version, nixos_version, kernel_version, build_time
))
format!("Generation {}, Built on {}", self.version, build_time)
}
}

Expand All @@ -105,25 +95,6 @@ impl fmt::Display for Generation {
}
}

/// Read the kernel version from the name of a directory inside the toplevel directory.
///
/// The path looks something like this: $toplevel/kernel-modules/lib/modules/6.1.1
fn read_kernel_version(toplevel: &Path) -> Result<String> {
let path = fs::read_dir(toplevel.join("kernel-modules/lib/modules"))?
.into_iter()
.next()
.transpose()?
.map(|x| x.path())
.with_context(|| format!("Failed to read directory {:?}.", toplevel))?;

let file_name = path
.file_name()
.and_then(|x| x.to_str())
.context("Failed to convert path to filename string.")?;

Ok(String::from(file_name))
}

fn read_build_time(path: &Path) -> Result<Date> {
let build_time = time::OffsetDateTime::from_unix_timestamp(fs::metadata(path)?.mtime())?.date();
Ok(build_time)
Expand Down
7 changes: 1 addition & 6 deletions rust/tool/src/os_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ impl OsRelease {
// it is fine to use a dummy value.
map.insert("ID".into(), String::from("lanza"));
map.insert("PRETTY_NAME".into(), generation.spec.bootspec.label.clone());
map.insert(
"VERSION_ID".into(),
generation
.describe()
.context("Failed to describe generation.")?,
);
map.insert("VERSION_ID".into(), generation.describe());

Ok(Self(map))
}
Expand Down
2 changes: 1 addition & 1 deletion rust/tool/tests/os_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn generate_expected_os_release() -> Result<()> {
let expected = expect![[r#"
ID=lanza
PRETTY_NAME=LanzaOS
VERSION_ID=Generation 1 NixOS 23.05, Linux Kernel 6.1.1, Built on 1970-01-01
VERSION_ID=Generation 1, Built on 1970-01-01
"#]];

expected.assert_eq(&String::from_utf8(os_release_section)?);
Expand Down