Skip to content

Commit 591e998

Browse files
authored
Merge pull request #649 from cgwalters/install-pass-container-root
install: Only open / once
2 parents 767ec9c + ab1149d commit 591e998

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)
@@ -1110,9 +1119,9 @@ async fn prepare_install(
11101119
};
11111120
tracing::trace!("Read container engine info {:?}", container_info);
11121121

1113-
SourceInfo::from_container(&container_info)?
1122+
SourceInfo::from_container(&rootfs, &container_info)?
11141123
}
1115-
Some(source) => SourceInfo::from_imageref(&source)?,
1124+
Some(source) => SourceInfo::from_imageref(&source, &rootfs)?,
11161125
};
11171126

11181127
// Parse the target CLI image reference options and create the *target* image
@@ -1190,6 +1199,7 @@ async fn prepare_install(
11901199
target_imgref,
11911200
install_config,
11921201
root_ssh_authorized_keys,
1202+
container_root: rootfs,
11931203
});
11941204

11951205
Ok(state)

0 commit comments

Comments
 (0)