From 7b8f483d5ae56e7f4bd603e8196c3343fd3a2355 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 7 Mar 2022 12:27:35 -0500 Subject: [PATCH] bootabletree: New module with helpers for bootable ostree commits Right now this only offers an API to find the kernel directory, but I could imagine we do more stuff here in the future. I think this will be generally useful (e.g. ostree and rpm-ostree have duplicate code for this) as is, but it's specifically prep for using this in container layer splitting. --- lib/src/bootabletree.rs | 29 +++++++++++++++++++++++++++++ lib/src/lib.rs | 1 + lib/tests/it/main.rs | 12 ++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 lib/src/bootabletree.rs diff --git a/lib/src/bootabletree.rs b/lib/src/bootabletree.rs new file mode 100644 index 00000000..6be01cb9 --- /dev/null +++ b/lib/src/bootabletree.rs @@ -0,0 +1,29 @@ +//! Helper functions for bootable OSTrees. + +use anyhow::Result; +use ostree::gio; +use ostree::prelude::*; + +const MODULES: &str = "/usr/lib/modules"; + +/// Find the kernel modules directory in a bootable OSTree commit. +pub fn find_kernel_dir( + root: &gio::File, + cancellable: Option<&gio::Cancellable>, +) -> Result> { + let moddir = root.resolve_relative_path(MODULES); + let e = moddir.enumerate_children( + "standard::name", + gio::FileQueryInfoFlags::NOFOLLOW_SYMLINKS, + cancellable, + )?; + let mut r = None; + for child in e.clone() { + let child = &child?; + let childpath = e.child(child); + if child.file_type() == gio::FileType::Directory && r.replace(childpath).is_some() { + anyhow::bail!("Found multiple subdirectories in {}", MODULES); + } + } + Ok(r) +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 38d4e822..c0b9b8e8 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -26,6 +26,7 @@ type Result = anyhow::Result; // Import global functions. mod globals; +pub mod bootabletree; pub mod cli; pub mod container; pub mod container_utils; diff --git a/lib/tests/it/main.rs b/lib/tests/it/main.rs index e5a9fd7d..699d5034 100644 --- a/lib/tests/it/main.rs +++ b/lib/tests/it/main.rs @@ -5,6 +5,7 @@ use ostree_ext::container::store::PrepareResult; use ostree_ext::container::{ Config, ImageReference, OstreeImageReference, SignatureSource, Transport, }; +use ostree_ext::prelude::FileExt; use ostree_ext::tar::TarImportOptions; use ostree_ext::{gio, glib}; use sh_inline::bash_in; @@ -300,6 +301,17 @@ async fn test_tar_import_export() -> Result<()> { "#, imported_commit = imported_commit.as_str() )?; + + let (root, _) = fixture + .destrepo() + .read_commit(&imported_commit, gio::NONE_CANCELLABLE)?; + let kdir = ostree_ext::bootabletree::find_kernel_dir(&root, gio::NONE_CANCELLABLE)?; + let kdir = kdir.unwrap(); + assert_eq!( + kdir.basename().unwrap().to_str().unwrap(), + "5.10.18-200.x86_64" + ); + Ok(()) }