codeironman f5b2038871
ospp project (feature) add namespace overlayfs cgroup (#949)
## 开发进展:
## namespace
- pid_namespace 基本实现,基于pid_struct等数据结构实现隔离
- mnt_namespace 基本实现,挂载点的隔离通过不同的挂载树来实现
- usernamespace 作为支持性的namespace,目前受限实现全局静态
## overlayfs
- 实现若干个文件系统的叠加,在mount中传入多个路径作为多个fs的mount路径以及最后merge层的fs路径
- copy-up机制的,除最上层外其他层为只读层,满足写时拷贝,需要修改的时候copy到上层修改
- whiteout特殊文件,用于标记在下层需要被删除的文件用来掩盖需要删除的文件
## cgroups
- 目前cgroups还处于框架阶段,之后具体实现具体的内存、CPU等子系统
2024-10-31 00:50:34 +08:00

39 lines
1.4 KiB
Rust

extern crate nix;
use nix::sched::{self, CloneFlags};
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::{self, fork, ForkResult};
use std::process;
fn main() {
let clone_flags = CloneFlags::CLONE_NEWPID | CloneFlags::CLONE_NEWNS;
println!("Parent process. PID: {}", unistd::getpid());
unsafe {
match fork() {
Ok(ForkResult::Parent { child }) => {
println!("Parent process. Child PID: {}", child);
match waitpid(child, None) {
Ok(WaitStatus::Exited(pid, status)) => {
println!("Child {} exited with status: {}", pid, status);
}
Ok(_) => println!("Child process did not exit normally."),
Err(e) => println!("Error waiting for child process: {:?}", e),
}
}
Ok(ForkResult::Child) => {
// 使用 unshare 创建新的命名空间
println!("Child process. PID: {}", unistd::getpid());
if let Err(e) = sched::unshare(clone_flags) {
println!("Failed to unshare: {:?}", e);
process::exit(1);
}
println!("Child process. PID: {}", unistd::getpid());
}
Err(err) => {
println!("Fork failed: {:?}", err);
process::exit(1);
}
}
}
}