Skip to content

Commit 7517d68

Browse files
cgwaltersjeckersb
authored andcommitted
Split out a hostexec module
We'll use this even in cases where we don't have the `install` feature. Signed-off-by: Colin Walters <[email protected]>
1 parent 8cd0037 commit 7517d68

File tree

6 files changed

+43
-34
lines changed

6 files changed

+43
-34
lines changed

lib/src/blockdev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::install::run_in_host_mountns;
1+
use crate::hostexec::run_in_host_mountns;
22
use crate::task::Task;
33
use anyhow::{anyhow, Context, Result};
44
use camino::{Utf8Path, Utf8PathBuf};

lib/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
674674
},
675675
#[cfg(feature = "install")]
676676
Opt::ExecInHostMountNamespace { args } => {
677-
crate::install::exec_in_host_mountns(args.as_slice())
677+
crate::hostexec::exec_in_host_mountns(args.as_slice())
678678
}
679679
Opt::Status(opts) => super::status::status(opts).await,
680680
Opt::Internals(opts) => match opts {

lib/src/hostexec.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! Run a command in the host mount namespace
2+
3+
use std::os::fd::AsFd;
4+
use std::os::unix::process::CommandExt;
5+
use std::process::Command;
6+
7+
use anyhow::{Context, Result};
8+
use camino::Utf8Path;
9+
use fn_error_context::context;
10+
11+
/// Run a command in the host mount namespace
12+
pub(crate) fn run_in_host_mountns(cmd: &str) -> Command {
13+
let mut c = Command::new("/proc/self/exe");
14+
c.args(["exec-in-host-mount-namespace", cmd]);
15+
c
16+
}
17+
18+
#[context("Re-exec in host mountns")]
19+
pub(crate) fn exec_in_host_mountns(args: &[std::ffi::OsString]) -> Result<()> {
20+
let (cmd, args) = args
21+
.split_first()
22+
.ok_or_else(|| anyhow::anyhow!("Missing command"))?;
23+
tracing::trace!("{cmd:?} {args:?}");
24+
let pid1mountns = std::fs::File::open("/proc/1/ns/mnt").context("open pid1 mountns")?;
25+
nix::sched::setns(pid1mountns.as_fd(), nix::sched::CloneFlags::CLONE_NEWNS).context("setns")?;
26+
rustix::process::chdir("/").context("chdir")?;
27+
// Work around supermin doing chroot() and not pivot_root
28+
// https://github.com/libguestfs/supermin/blob/5230e2c3cd07e82bd6431e871e239f7056bf25ad/init/init.c#L288
29+
if !Utf8Path::new("/usr").try_exists().context("/usr")?
30+
&& Utf8Path::new("/root/usr")
31+
.try_exists()
32+
.context("/root/usr")?
33+
{
34+
tracing::debug!("Using supermin workaround");
35+
rustix::process::chroot("/root").context("chroot")?;
36+
}
37+
Err(Command::new(cmd).args(args).exec()).context("exec")?
38+
}

lib/src/install.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ pub(crate) mod osconfig;
1212

1313
use std::io::Write;
1414
use std::os::fd::AsFd;
15-
use std::os::unix::process::CommandExt;
1615
use std::path::Path;
17-
use std::process::Command;
1816
use std::str::FromStr;
1917
use std::sync::Arc;
2018
use std::time::Duration;
@@ -40,6 +38,7 @@ use serde::{Deserialize, Serialize};
4038

4139
use self::baseline::InstallBlockDeviceOpts;
4240
use crate::containerenv::ContainerExecutionInfo;
41+
use crate::hostexec::run_in_host_mountns;
4342
use crate::mount::Filesystem;
4443
use crate::task::Task;
4544
use crate::utils::sigpolicy_from_opts;
@@ -727,35 +726,6 @@ async fn initialize_ostree_root_from_self(
727726
Ok(aleph)
728727
}
729728

730-
/// Run a command in the host mount namespace
731-
pub(crate) fn run_in_host_mountns(cmd: &str) -> Command {
732-
let mut c = Command::new("/proc/self/exe");
733-
c.args(["exec-in-host-mount-namespace", cmd]);
734-
c
735-
}
736-
737-
#[context("Re-exec in host mountns")]
738-
pub(crate) fn exec_in_host_mountns(args: &[std::ffi::OsString]) -> Result<()> {
739-
let (cmd, args) = args
740-
.split_first()
741-
.ok_or_else(|| anyhow::anyhow!("Missing command"))?;
742-
tracing::trace!("{cmd:?} {args:?}");
743-
let pid1mountns = std::fs::File::open("/proc/1/ns/mnt").context("open pid1 mountns")?;
744-
nix::sched::setns(pid1mountns.as_fd(), nix::sched::CloneFlags::CLONE_NEWNS).context("setns")?;
745-
rustix::process::chdir("/").context("chdir")?;
746-
// Work around supermin doing chroot() and not pivot_root
747-
// https://github.com/libguestfs/supermin/blob/5230e2c3cd07e82bd6431e871e239f7056bf25ad/init/init.c#L288
748-
if !Utf8Path::new("/usr").try_exists().context("/usr")?
749-
&& Utf8Path::new("/root/usr")
750-
.try_exists()
751-
.context("/root/usr")?
752-
{
753-
tracing::debug!("Using supermin workaround");
754-
rustix::process::chroot("/root").context("chroot")?;
755-
}
756-
Err(Command::new(cmd).args(args).exec()).context("exec")?
757-
}
758-
759729
#[context("Querying skopeo version")]
760730
fn require_skopeo_with_containers_storage() -> Result<()> {
761731
let out = Task::new_cmd("skopeo --version", run_in_host_mountns("skopeo"))

lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
pub mod cli;
2121
pub(crate) mod deploy;
2222
pub(crate) mod generator;
23+
pub(crate) mod hostexec;
2324
pub(crate) mod journal;
2425
mod lints;
2526
mod lsm;

lib/src/podman.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{anyhow, Result};
22
use serde::Deserialize;
33

4-
use crate::install::run_in_host_mountns;
4+
use crate::hostexec::run_in_host_mountns;
55
use crate::task::Task;
66

77
/// Where we look inside our container to find our own image

0 commit comments

Comments
 (0)