Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cgwalters committed Aug 30, 2024
1 parent a2c47e5 commit 9dbeb42
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions lib/src/mount.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
//! Helpers for interacting with mountpoints

use std::process::Command;
use std::{
fs::File,
os::fd::{AsFd, OwnedFd},
path::Path,
process::Command,
};

use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use bootc_utils::CommandRunExt;
use camino::Utf8Path;
use fn_error_context::context;
use rustix::mount::{MoveMountFlags, OpenTreeFlags};
use serde::Deserialize;

use crate::task::Task;
Expand Down Expand Up @@ -88,3 +94,30 @@ pub(crate) fn is_same_as_host(path: &Utf8Path) -> Result<bool> {
);
Ok(devstat.f_fsid == hostdevstat.f_fsid)
}

/// Open the target mount point in the mount namespace of pid 1
pub(crate) fn open_tree_pid1_mountns(p: impl AsRef<Path>) -> Result<OwnedFd> {
let p = p.as_ref();
let proc1_ns = "/proc/1/ns/mnt";
let pid1_mountns_fd: OwnedFd = File::open(proc1_ns)
.with_context(|| format!("Opening {proc1_ns}"))?
.into();
std::thread::scope(|s| {
let fd = s.spawn(move || -> Result<_> {
let allowed_types = Some(rustix::thread::LinkNameSpaceType::Mount);
rustix::thread::move_into_link_name_space(pid1_mountns_fd.as_fd(), allowed_types)
.context("setns")?;
let oflags = OpenTreeFlags::OPEN_TREE_CLOEXEC | OpenTreeFlags::OPEN_TREE_CLONE;
rustix::mount::open_tree(rustix::fs::CWD, p, oflags).map_err(Into::into)
});
fd.join().unwrap()
})
}

pub(crate) fn mount_from_pid1(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> Result<()> {
let dest = dest.as_ref();
let src = open_tree_pid1_mountns(src)?;
let flags = MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH;
rustix::mount::move_mount(src, "", , dest, flags)?;
Ok(())
}

0 comments on commit 9dbeb42

Please sign in to comment.