Skip to content

Commit

Permalink
Add some more mounts that the OCI describes as required
Browse files Browse the repository at this point in the history
  • Loading branch information
Terr committed Jan 2, 2024
1 parent 9dec1bc commit a8c9d04
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
43 changes: 34 additions & 9 deletions crates/libcarton/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ pub struct Mount {
}

impl Mount {
pub(crate) fn rootfs(source: PathBuf) -> Self {
Mount {
source: Some(source),
relative_target: "".into(),
fstype: None,
flags: mount::MsFlags::MS_BIND | mount::MsFlags::MS_PRIVATE,
data: None,
}
}

/// Defines a "bind" mount which can be used to share a directory from outside the container
/// with the container.
pub(crate) fn bind(
Expand All @@ -169,22 +179,22 @@ impl Mount {

/// When the container runs in a separate PID namespace it also needs a separate /proc mount that
/// will contain only this PID namespace's processes.
pub(crate) fn procfs(relative_target: PathBuf) -> Self {
pub(crate) fn procfs() -> Self {
Mount {
source: None::<PathBuf>,
relative_target,
relative_target: "proc".into(),
fstype: Some("proc".into()),
flags: mount::MsFlags::empty(),
data: None,
}
}

pub(crate) fn rootfs(source: PathBuf) -> Self {
pub(crate) fn sysfs() -> Self {
Mount {
source: Some(source),
relative_target: "".into(),
fstype: None,
flags: mount::MsFlags::MS_BIND | mount::MsFlags::MS_PRIVATE,
source: None::<PathBuf>,
relative_target: "sys".into(),
fstype: Some("sysfs".into()),
flags: mount::MsFlags::empty(),
data: None,
}
}
Expand All @@ -199,18 +209,33 @@ impl Mount {
}
}

pub(crate) fn devpts() -> Self {
Mount {
source: None::<PathBuf>,
relative_target: "dev/pts".into(),
fstype: Some("devpts".into()),
flags: mount::MsFlags::empty(),
data: None,
}
}

/// Returns the absolute path where the mount has been mounted
pub(crate) fn mount(&self, rootfs_path: &Path) -> Result<PathBuf, CartonError> {
let mount_path = rootfs_path.join(&self.relative_target);

if !mount_path.exists() {
info!("creating {}", mount_path.display());
std::fs::create_dir_all(&mount_path)?;
}

info!(
"mount {} ({}) at {}",
"mounting {} ({}) at {}",
&self
.source
.as_ref()
.map_or("(no source)", |p| p.to_str().unwrap()),
self.fstype.as_ref().map_or("bind mount", |f| f.as_str()),
&mount_path.display()
mount_path.display()
);

mount::mount(
Expand Down
5 changes: 4 additions & 1 deletion crates/libcarton/src/container_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ impl ContainerBuilder {
/// Adds mounting configuration for some important mounts, in the correct order.
pub fn add_default_mounts(mut self) -> Self {
self.config.mounts.extend(vec![
Mount::procfs("proc".into()),
Mount::procfs(),
Mount::sysfs(),
Mount::tmpfs("tmp".into()),
Mount::tmpfs("dev".into()),
Mount::devpts(),
Mount::tmpfs("dev/shm".into()),
]);

self
Expand Down
8 changes: 8 additions & 0 deletions crates/libcarton/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ pub enum CartonError {
SysCallFailed(String),
#[error("namespace error: {0}")]
NamespaceError(String),
#[error("I/O error: {0}")]
IOError(String),
}

impl From<std::io::Error> for CartonError {
fn from(error: std::io::Error) -> Self {
CartonError::IOError(format!("{}", error))
}
}

impl From<nix::Error> for CartonError {
Expand Down

0 comments on commit a8c9d04

Please sign in to comment.