Skip to content

Commit 60b73be

Browse files
authored
Merge pull request #1167 from cgwalters/reallyclean
store: Really remove empty /etc/resolv.conf and /etc/hostname
2 parents f1ab775 + 98995f6 commit 60b73be

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

ostree-ext/src/container/store.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ fn timestamp_of_manifest_or_config(
466466
/// Automatically clean up files that may have been injected by container
467467
/// builds. xref https://github.com/containers/buildah/issues/4242
468468
fn cleanup_root(root: &Dir) -> Result<()> {
469-
const RUNTIME_INJECTED: &[&str] = &["etc/hostname", "etc/resolv.conf"];
469+
const RUNTIME_INJECTED: &[&str] = &["usr/etc/hostname", "usr/etc/resolv.conf"];
470470
for ent in RUNTIME_INJECTED {
471471
if let Some(meta) = root.symlink_metadata_optional(ent)? {
472472
if meta.is_file() && meta.size() == 0 {
@@ -1055,15 +1055,16 @@ impl ImageImporter {
10551055
.with_context(|| format!("Checking out layer {commit}"))?;
10561056
}
10571057

1058+
let root_dir = td.open_dir(rootpath)?;
1059+
10581060
let modifier =
10591061
ostree::RepoCommitModifier::new(ostree::RepoCommitModifierFlags::CONSUME, None);
10601062
modifier.set_devino_cache(&devino);
10611063
// If we have derived layers, then we need to handle the case where
10621064
// the derived layers include custom policy. Just relabel everything
10631065
// in this case.
10641066
if have_derived_layers {
1065-
let rootpath = td.open_dir(rootpath)?;
1066-
let sepolicy = ostree::SePolicy::new_at(rootpath.as_raw_fd(), cancellable)?;
1067+
let sepolicy = ostree::SePolicy::new_at(root_dir.as_raw_fd(), cancellable)?;
10671068
tracing::debug!("labeling from merged tree");
10681069
modifier.set_sepolicy(Some(&sepolicy));
10691070
} else if let Some(base) = base_commit.as_ref() {
@@ -1074,7 +1075,7 @@ impl ImageImporter {
10741075
unreachable!()
10751076
}
10761077

1077-
cleanup_root(&td)?;
1078+
cleanup_root(&root_dir)?;
10781079

10791080
let mt = ostree::MutableTree::new();
10801081
repo.write_dfd_to_mtree(
@@ -1965,23 +1966,24 @@ mod tests {
19651966
#[test]
19661967
fn test_cleanup_root() -> Result<()> {
19671968
let td = cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
1968-
1969+
let usretc = "usr/etc";
19691970
cleanup_root(&td).unwrap();
1970-
td.create_dir("etc")?;
1971-
td.write("etc/hostname", b"hostname")?;
1971+
td.create_dir_all(usretc)?;
1972+
let usretc = &td.open_dir(usretc)?;
1973+
usretc.write("hostname", b"hostname")?;
19721974
cleanup_root(&td).unwrap();
1973-
assert!(td.try_exists("etc/hostname")?);
1974-
td.write("etc/hostname", b"")?;
1975+
assert!(usretc.try_exists("hostname")?);
1976+
usretc.write("hostname", b"")?;
19751977
cleanup_root(&td).unwrap();
1976-
assert!(!td.try_exists("etc/hostname")?);
1978+
assert!(!td.try_exists("hostname")?);
19771979

1978-
td.symlink_contents("../run/systemd/stub-resolv.conf", "etc/resolv.conf")?;
1980+
usretc.symlink_contents("../run/systemd/stub-resolv.conf", "resolv.conf")?;
19791981
cleanup_root(&td).unwrap();
1980-
assert!(td.symlink_metadata("etc/resolv.conf")?.is_symlink());
1981-
td.remove_file("etc/resolv.conf")?;
1982-
td.write("etc/resolv.conf", b"")?;
1982+
assert!(usretc.symlink_metadata("resolv.conf")?.is_symlink());
1983+
usretc.remove_file("resolv.conf")?;
1984+
usretc.write("resolv.conf", b"")?;
19831985
cleanup_root(&td).unwrap();
1984-
assert!(!td.try_exists("etc/resolv.conf")?);
1986+
assert!(!usretc.try_exists("resolv.conf")?);
19851987

19861988
Ok(())
19871989
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std assert
2+
use tap.nu
3+
4+
tap begin "verify there's not an empty /etc/resolv.conf in the image"
5+
6+
let st = bootc status --json | from json
7+
8+
let booted_ostree = $st.status.booted.ostree.checksum;
9+
10+
# ostree ls should probably have --json and a clean way to not error on ENOENT
11+
let resolvconf = ostree ls $booted_ostree /usr/etc | split row (char newline) | find resolv.conf
12+
if ($resolvconf | length) > 0 {
13+
let parts = $resolvconf | first | split row -r '\s+'
14+
let ty = $parts | first | split chars | first
15+
# If resolv.conf exists in the image, currently require it in our
16+
# test suite to be a symlink (which is hopefully to the systemd/stub-resolv.conf)
17+
assert equal $ty 'l'
18+
print "resolv.conf is a symlink"
19+
} else {
20+
print "No resolv.conf found in commit"
21+
}
22+
23+
tap ok

0 commit comments

Comments
 (0)