Skip to content

Commit 2b175ad

Browse files
committed
Improve parsing of ostree prepare-root config
Prep for further work. Signed-off-by: Colin Walters <[email protected]>
1 parent 292d9e1 commit 2b175ad

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

lib/src/lints.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use std::collections::BTreeSet;
66
use std::env::consts::ARCH;
77
use std::os::unix::ffi::OsStrExt;
88

9-
use anyhow::{Context, Result};
9+
use anyhow::Result;
1010
use camino::{Utf8Path, Utf8PathBuf};
1111
use cap_std::fs::Dir;
1212
use cap_std_ext::cap_std;
1313
use cap_std_ext::cap_std::fs::MetadataExt;
1414
use cap_std_ext::dirext::CapStdExtDirExt as _;
1515
use fn_error_context::context;
1616
use indoc::indoc;
17+
use ostree_ext::ostree_prepareroot;
1718
use serde::Serialize;
1819

1920
/// Reference to embedded default baseimage content that should exist.
@@ -286,15 +287,8 @@ fn check_baseimage_root_norecurse(dir: &Dir) -> LintResult {
286287
return lint_err("Expected /ostree -> {expected}, not {link:?}");
287288
}
288289

289-
// Check the prepare-root config
290-
let prepareroot_path = "usr/lib/ostree/prepare-root.conf";
291-
let config_data = dir
292-
.read_to_string(prepareroot_path)
293-
.context(prepareroot_path)?;
294-
let config = ostree_ext::glib::KeyFile::new();
295-
config.load_from_data(&config_data, ostree_ext::glib::KeyFileFlags::empty())?;
296-
297-
if !ostree_ext::ostree_prepareroot::overlayfs_enabled_in_config(&config)? {
290+
let config = ostree_prepareroot::require_config_from_root(dir)?;
291+
if !ostree_prepareroot::overlayfs_enabled_in_config(&config)? {
298292
return lint_err("{prepareroot_path} does not have composefs enabled");
299293
}
300294

ostree-ext/src/ostree_prepareroot.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
44
// SPDX-License-Identifier: Apache-2.0 OR MIT
55

6+
use std::io::Read;
67
use std::str::FromStr;
78

89
use anyhow::{Context, Result};
910
use camino::Utf8Path;
11+
use cap_std_ext::dirext::CapStdExtDirExt;
1012
use glib::Cast;
13+
use ocidir::cap_std::fs::Dir;
1114
use ostree::prelude::FileExt;
1215
use ostree::{gio, glib};
1316

@@ -37,6 +40,29 @@ pub(crate) fn load_config(root: &ostree::RepoFile) -> Result<Option<glib::KeyFil
3740
Ok(None)
3841
}
3942

43+
/// Load the configuration from the target root.
44+
pub fn load_config_from_root(root: &Dir) -> Result<Option<glib::KeyFile>> {
45+
for path in ["etc", "usr/lib"].into_iter().map(Utf8Path::new) {
46+
let path = path.join(CONF_PATH);
47+
let Some(mut f) = root.open_optional(&path)? else {
48+
continue;
49+
};
50+
let mut contents = String::new();
51+
f.read_to_string(&mut contents)?;
52+
let kf = glib::KeyFile::new();
53+
kf.load_from_data(&contents, glib::KeyFileFlags::NONE)
54+
.with_context(|| format!("Parsing {path}"))?;
55+
return Ok(Some(kf));
56+
}
57+
Ok(None)
58+
}
59+
60+
/// Require the configuration in the target root.
61+
pub fn require_config_from_root(root: &Dir) -> Result<glib::KeyFile> {
62+
load_config_from_root(root)?
63+
.ok_or_else(|| anyhow::anyhow!("Failed to find {CONF_PATH} in /usr/lib or /etc"))
64+
}
65+
4066
/// Query whether the target root has the `root.transient` key
4167
/// which sets up a transient overlayfs.
4268
pub(crate) fn overlayfs_root_enabled(root: &ostree::RepoFile) -> Result<bool> {

0 commit comments

Comments
 (0)