bootc_lib/bootc_composefs/
image.rs1use anyhow::Result;
4use cap_std_ext::cap_std::fs::Dir;
5use cap_std_ext::dirext::CapStdExtDirExt as _;
6
7pub(crate) fn setup_root_conf_path() -> &'static str {
9 bootc_initramfs_setup::SETUP_ROOT_CONF_PATH.trim_start_matches('/')
10}
11
12pub(crate) fn is_composefs_native(root: &Dir) -> Result<bool> {
16 Ok(root
17 .symlink_metadata_optional(setup_root_conf_path())?
18 .is_some())
19}
20
21pub(crate) fn defaults_to_composefs_backend(
27 root: &Dir,
28 has_ostree_prepareroot: bool,
29) -> Result<bool> {
30 Ok(!has_ostree_prepareroot && is_composefs_native(root)?)
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36 use camino::Utf8Path;
37 use ostree_ext::ostree_prepareroot;
38
39 const OSTREE_PREPAREROOT: &str = "usr/lib/ostree/prepare-root.conf";
40 const OSTREE_PREPAREROOT_ETC: &str = "etc/ostree/prepare-root.conf";
41
42 #[test]
43 fn test_defaults_to_composefs_backend() -> Result<()> {
44 let setup_root = setup_root_conf_path();
45 let cases: &[(&[&str], bool, bool)] = &[
47 (&[], false, false),
48 (&[setup_root], true, true),
49 (&[OSTREE_PREPAREROOT], false, false),
50 (&[OSTREE_PREPAREROOT_ETC], false, false),
51 (&[setup_root, OSTREE_PREPAREROOT], true, false),
52 (&[setup_root, OSTREE_PREPAREROOT_ETC], true, false),
53 ];
54 for &(files, native, composefs) in cases {
55 let td = cap_std_ext::cap_tempfile::tempdir(cap_std_ext::cap_std::ambient_authority())?;
56 for f in files {
57 let f = Utf8Path::new(f);
58 td.create_dir_all(f.parent().unwrap())?;
59 td.write(f, "")?;
61 }
62 assert_eq!(is_composefs_native(&td)?, native, "{files:?}");
63 let has_ostree = ostree_prepareroot::load_config_from_root(&td)?.is_some();
64 assert_eq!(
65 defaults_to_composefs_backend(&td, has_ostree)?,
66 composefs,
67 "{files:?}"
68 );
69 }
70 Ok(())
71 }
72}