tool: improve sd-boot generation display name

Leverage the bootspec `label` field in its intended way. The VERSION_ID
of the os-release in the stub now only contains the generation number
and the build time. This makes a correct PRETTY_NAME entirely dependent
on correct information in the bootspec `label` field.
This commit is contained in:
nikstur 2023-02-09 00:19:33 +01:00
parent 01f8fd418a
commit a8d9ea128d
3 changed files with 4 additions and 38 deletions

View File

@ -80,22 +80,12 @@ impl Generation {
/// ///
/// This is currently implemented by poking around the filesystem to find the necessary data. /// This is currently implemented by poking around the filesystem to find the necessary data.
/// Ideally, the needed data should be included in the bootspec. /// Ideally, the needed data should be included in the bootspec.
pub fn describe(&self) -> Result<String> { pub fn describe(&self) -> 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.")?;
let build_time = self let build_time = self
.build_time .build_time
.map(|x| x.to_string()) .map(|x| x.to_string())
.unwrap_or_else(|| String::from("Unknown")); .unwrap_or_else(|| String::from("Unknown"));
format!("Generation {}, Built on {}", self.version, build_time)
Ok(format!(
"Generation {} NixOS {}, Linux Kernel {}, Built on {}",
self.version, nixos_version, kernel_version, build_time
))
} }
} }
@ -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> { fn read_build_time(path: &Path) -> Result<Date> {
let build_time = time::OffsetDateTime::from_unix_timestamp(fs::metadata(path)?.mtime())?.date(); let build_time = time::OffsetDateTime::from_unix_timestamp(fs::metadata(path)?.mtime())?.date();
Ok(build_time) Ok(build_time)

View File

@ -27,12 +27,7 @@ impl OsRelease {
// it is fine to use a dummy value. // it is fine to use a dummy value.
map.insert("ID".into(), String::from("lanza")); map.insert("ID".into(), String::from("lanza"));
map.insert("PRETTY_NAME".into(), generation.spec.bootspec.label.clone()); map.insert("PRETTY_NAME".into(), generation.spec.bootspec.label.clone());
map.insert( map.insert("VERSION_ID".into(), generation.describe());
"VERSION_ID".into(),
generation
.describe()
.context("Failed to describe generation.")?,
);
Ok(Self(map)) Ok(Self(map))
} }

View File

@ -30,7 +30,7 @@ fn generate_expected_os_release() -> Result<()> {
let expected = expect![[r#" let expected = expect![[r#"
ID=lanza ID=lanza
PRETTY_NAME=LanzaOS 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)?); expected.assert_eq(&String::from_utf8(os_release_section)?);