Skip to content

Commit ab1149d

Browse files
committed
install: Only open / once
We juggle multiple things we call in different places the "rootfs", in some the container's root, in others the target root. Add a handy member variable of `State` for the container root and use it consistently. This improves clarity now, and is prep for later patches. Signed-off-by: Colin Walters <[email protected]>
1 parent 0bd1956 commit ab1149d

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

lib/src/install.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ pub(crate) struct State {
310310
pub(crate) install_config: Option<config::InstallConfiguration>,
311311
/// The parsed contents of the authorized_keys (not the file path)
312312
pub(crate) root_ssh_authorized_keys: Option<String>,
313+
/// The root filesystem of the running container
314+
pub(crate) container_root: Dir,
313315
}
314316

315317
impl State {
@@ -320,8 +322,7 @@ impl State {
320322
return Ok(None);
321323
}
322324
// We always use the physical container root to bootstrap policy
323-
let rootfs = &Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
324-
let r = ostree::SePolicy::new_at(rootfs.as_raw_fd(), gio::Cancellable::NONE)?;
325+
let r = ostree::SePolicy::new_at(self.container_root.as_raw_fd(), gio::Cancellable::NONE)?;
325326
let csum = r
326327
.csum()
327328
.ok_or_else(|| anyhow::anyhow!("SELinux enabled, but no policy found in root"))?;
@@ -449,7 +450,10 @@ impl SourceInfo {
449450
// Inspect container information and convert it to an ostree image reference
450451
// that pulls from containers-storage.
451452
#[context("Gathering source info from container env")]
452-
pub(crate) fn from_container(container_info: &ContainerExecutionInfo) -> Result<Self> {
453+
pub(crate) fn from_container(
454+
root: &Dir,
455+
container_info: &ContainerExecutionInfo,
456+
) -> Result<Self> {
453457
if !container_info.engine.starts_with("podman") {
454458
anyhow::bail!("Currently this command only supports being executed via podman");
455459
}
@@ -463,7 +467,6 @@ impl SourceInfo {
463467
tracing::debug!("Finding digest for image ID {}", container_info.imageid);
464468
let digest = crate::podman::imageid_to_digest(&container_info.imageid)?;
465469

466-
let root = Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
467470
let have_host_container_storage = Utf8Path::new(crate::podman::CONTAINER_STORAGE)
468471
.try_exists()?
469472
&& ostree_ext::mountutil::is_mountpoint(
@@ -483,19 +486,26 @@ impl SourceInfo {
483486
require_skopeo_with_containers_storage()?;
484487
}
485488

486-
Self::new(imageref, Some(digest), true, have_host_container_storage)
489+
Self::new(
490+
imageref,
491+
Some(digest),
492+
root,
493+
true,
494+
have_host_container_storage,
495+
)
487496
}
488497

489498
#[context("Creating source info from a given imageref")]
490-
pub(crate) fn from_imageref(imageref: &str) -> Result<Self> {
499+
pub(crate) fn from_imageref(imageref: &str, root: &Dir) -> Result<Self> {
491500
let imageref = ostree_container::ImageReference::try_from(imageref)?;
492-
Self::new(imageref, None, false, false)
501+
Self::new(imageref, None, root, false, false)
493502
}
494503

495504
/// Construct a new source information structure
496505
fn new(
497506
imageref: ostree_container::ImageReference,
498507
digest: Option<String>,
508+
root: &Dir,
499509
in_host_mountns: bool,
500510
have_host_container_storage: bool,
501511
) -> Result<Self> {
@@ -504,7 +514,6 @@ impl SourceInfo {
504514
.args(["--repo=/ostree/repo", "rev-parse", "--single"])
505515
.quiet()
506516
.read()?;
507-
let root = cap_std::fs::Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
508517
let repo = ostree::Repo::open_at_dir(root.as_fd(), "ostree/repo")?;
509518
let root = repo
510519
.read_commit(commit.trim(), cancellable)
@@ -1097,9 +1106,9 @@ async fn prepare_install(
10971106
};
10981107
tracing::trace!("Read container engine info {:?}", container_info);
10991108

1100-
SourceInfo::from_container(&container_info)?
1109+
SourceInfo::from_container(&rootfs, &container_info)?
11011110
}
1102-
Some(source) => SourceInfo::from_imageref(&source)?,
1111+
Some(source) => SourceInfo::from_imageref(&source, &rootfs)?,
11031112
};
11041113

11051114
// Parse the target CLI image reference options and create the *target* image
@@ -1177,6 +1186,7 @@ async fn prepare_install(
11771186
target_imgref,
11781187
install_config,
11791188
root_ssh_authorized_keys,
1189+
container_root: rootfs,
11801190
});
11811191

11821192
Ok(state)

0 commit comments

Comments
 (0)