-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perform I/O in mnt namespace directly instead of calling into docker
The current approach requires the binaries to be available inside the container. Change to perform I/O in the container's mount namespace directly. This is more robust and less expensive.
- Loading branch information
Showing
4 changed files
with
74 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod escape; | ||
pub mod namespace; | ||
pub mod tty_mode_guard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::fs::File; | ||
use std::os::fd::AsFd; | ||
|
||
use anyhow::Result; | ||
use rustix::process::Pid; | ||
use rustix::thread::{LinkNameSpaceType, UnshareFlags}; | ||
|
||
pub struct MntNamespace { | ||
fd: File, | ||
} | ||
|
||
impl MntNamespace { | ||
/// Open the mount namespace of a process. | ||
pub fn of_pid(pid: Pid) -> Result<MntNamespace> { | ||
let path = format!("/proc/{}/ns/mnt", pid.as_raw_nonzero()); | ||
let fd = File::open(path)?; | ||
Ok(MntNamespace { fd }) | ||
} | ||
|
||
/// Enter the mount namespace. | ||
pub fn enter<T: Send, F: FnOnce() -> T + Send>(&self, f: F) -> Result<T> { | ||
// To avoid messing with rest of the process, we do everything in a new thread. | ||
// Use scoped thread to avoid 'static bound (we need to access fd). | ||
std::thread::scope(|scope| { | ||
scope | ||
.spawn(|| -> Result<T> { | ||
// Unshare FS for this specific thread so we can switch to another namespace. | ||
// Not doing this will cause EINVAL when switching to namespaces. | ||
rustix::thread::unshare(UnshareFlags::FS)?; | ||
|
||
// Switch this particular thread to the container's mount namespace. | ||
rustix::thread::move_into_link_name_space( | ||
self.fd.as_fd(), | ||
Some(LinkNameSpaceType::Mount), | ||
)?; | ||
Ok(f()) | ||
}) | ||
.join() | ||
.map_err(|_| anyhow::anyhow!("work thread panicked"))? | ||
}) | ||
} | ||
} |