Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move unshare into chdir #277

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion crates/shim/src/mount.rs
Original file line number Diff line number Diff line change
@@ -562,8 +562,8 @@ pub fn mount_rootfs(
None
};

unshare(CloneFlags::CLONE_FS).unwrap();
if let Some(workdir) = chdir {
unshare(CloneFlags::CLONE_FS).unwrap();
zzzzzzzzzy9 marked this conversation as resolved.
Show resolved Hide resolved
env::set_current_dir(Path::new(&workdir)).unwrap_or_else(|_| {
unsafe { libc::_exit(i32::from(MountExitCode::ChdirErr)) };
});
@@ -723,4 +723,47 @@ 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/0/work";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I don't think it's good idea to create test folder like this. Please use /tmp/$RANDOM-LONG-STRING. Thanks.

std::fs::create_dir_all(workdir).unwrap();
let upperdir =
"/var/lib/containerd-test/io.containerd.snapshotter.v1.overlayfs/snapshots/0/fs";
std::fs::create_dir_all(upperdir).unwrap();
let mut dirs: Vec<String> = Vec::with_capacity(125);
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 = "/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();
std::thread::spawn(move || mount_rootfs(fs_type, source, &options, target).unwrap());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why must it spawn thread to mount it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this mount_rootfs is called in a new thread by tokio::spawn_blocking, so I think it's better to test mount_rootfs in a spawn thread.

std::thread::sleep(std::time::Duration::from_secs(2));
let current_dir_after_mount = env::current_dir().unwrap();
assert_eq!(current_dir, current_dir_after_mount);
nix::mount::umount(target).unwrap();
std::fs::remove_dir_all(target).unwrap();
std::fs::remove_dir_all(upperdir).unwrap();
std::fs::remove_dir_all(workdir).unwrap();
for dir in dirs {
std::fs::remove_dir_all(dir).unwrap();
}
}
}