Skip to content

Commit

Permalink
move unshare into chdir
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzzzzzzy9 committed Jun 25, 2024
1 parent e69120e commit 2503426
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion crates/shim/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ pub fn mount_rootfs(
None
};

unshare(CloneFlags::CLONE_FS).unwrap();
if let Some(workdir) = chdir {
unshare(CloneFlags::CLONE_FS).unwrap();
env::set_current_dir(Path::new(&workdir)).unwrap_or_else(|_| {
unsafe { libc::_exit(i32::from(MountExitCode::ChdirErr)) };
});
Expand Down Expand Up @@ -619,6 +619,7 @@ pub fn mount_rootfs(
#[cfg(target_os = "linux")]
mod tests {
use super::*;
use crate::util::mkdir;

#[test]
fn test_trim_flawed_dir() {
Expand Down Expand Up @@ -723,4 +724,41 @@ mod tests {
assert_eq!(options, expected_options);
}
}

#[test]
fn test_mount_rootfs() {
let fs_type = Some("overlay");
let source = Some("overlay");
let workdir =
"/var/lib/containerd-test/io.containerd.snapshotter.v1.overlayfs/snapshots/1/work";
std::fs::create_dir_all(workdir).unwrap();
let upperdir =
"/var/lib/containerd-test/io.containerd.snapshotter.v1.overlayfs/snapshots/1/fs";
std::fs::create_dir_all(upperdir).unwrap();
let mut dirs: Vec<String> = Vec::with_capacity(128);
for i in 1..=125 {
let mut dir =
"/var/lib/containerd-test/io.containerd.snapshotter.v1.overlayfs/snapshots/"
.to_string()
+ i.to_string().as_str()
+ "/fs";
std::fs::create_dir_all(&dir).unwrap();
dirs.push(dir);
}
let options = vec![
"index=off".to_string(),
"workdir=".to_string() + workdir,
"upperdir=".to_string() + upperdir,
"lowerdir=".to_string() + dirs.join(":").as_str(),
];

let target = std::path::PathBuf::from(
"/run/containerd/io.containerd.runtime.v2.task/k8s.io/mount-test/rootfs",
);
std::fs::create_dir_all(&target).unwrap();
let current_dir = env::current_dir().unwrap();
mount_rootfs(fs_type, source, &options, target).unwrap();
let current_dir_after_mount = env::current_dir().unwrap();
assert_eq!(current_dir, current_dir_after_mount);
}
}

0 comments on commit 2503426

Please sign in to comment.