|
|
|
|
@ -154,7 +154,6 @@ fn init_container_overlay<P: AsRef<Path>>(volume_path: P, upper_path: P, merged_
|
|
|
|
|
.arg(dirs)
|
|
|
|
|
.arg(merged_dir)
|
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
|
|
if out.status.success() {
|
|
|
|
|
println!("容器文件系统创建完成");
|
|
|
|
|
} else {
|
|
|
|
|
@ -221,7 +220,7 @@ fn init_container_custom_volume<P: AsRef<Path>>(container_merged_path: P, custom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn init_container_env(env: &String) -> Result<()>{
|
|
|
|
|
fn get_env_vec(env: &String) -> Result<Vec<String>> {
|
|
|
|
|
for (k, _) in std::env::vars(){
|
|
|
|
|
std::env::remove_var(k);
|
|
|
|
|
}
|
|
|
|
|
@ -232,7 +231,10 @@ fn init_container_env(env: &String) -> Result<()>{
|
|
|
|
|
} else {
|
|
|
|
|
env.split(",").map(String::from).collect::<Vec<String>>()
|
|
|
|
|
};
|
|
|
|
|
Ok(env_vec)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init_container_env(env_vec: Vec<String>) -> Result<()>{
|
|
|
|
|
for item_env in env_vec.iter() {
|
|
|
|
|
let item_env_v = item_env.split("=").collect::<Vec<&str>>();
|
|
|
|
|
if item_env_v.len() == 2 {
|
|
|
|
|
@ -343,8 +345,10 @@ fn init_exec_ns(pid: i32) -> Result<()>{
|
|
|
|
|
// 把当前进程加入到指定pid的namespace
|
|
|
|
|
for ns_name in vec!["ipc", "uts", "net", "pid", "mnt", "time"] {
|
|
|
|
|
let ns_path = format!("/proc/{pid}/ns/{ns_name}");
|
|
|
|
|
let ns_fild = fs::File::open(ns_path)?;
|
|
|
|
|
setns(ns_fild.as_fd(), CloneFlags::from_bits_retain(0))? }
|
|
|
|
|
if let Ok(ns_fild) = fs::File::open(ns_path) {
|
|
|
|
|
setns(ns_fild.as_fd(), CloneFlags::from_bits_retain(0))?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -433,9 +437,10 @@ fn run_container(container_info: &ContainerInfo, is_exec_cmd: Option<&String>) -
|
|
|
|
|
let _cb = if let Some(exec_cmd) = is_exec_cmd {
|
|
|
|
|
let _cb = || {
|
|
|
|
|
init_exec_ns(container_info.pid).unwrap();
|
|
|
|
|
init_container_env(&Default::default()).unwrap();
|
|
|
|
|
let env_vec = get_env_vec(&Default::default()).unwrap();
|
|
|
|
|
init_container_user(rocker_uid, rocker_gid).unwrap();
|
|
|
|
|
|
|
|
|
|
init_container_env(env_vec).unwrap();
|
|
|
|
|
create_pause(container_root_pause_path).unwrap();
|
|
|
|
|
while container_merged_pause_path.exists() {
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
|
@ -453,7 +458,7 @@ fn run_container(container_info: &ContainerInfo, is_exec_cmd: Option<&String>) -
|
|
|
|
|
} else {
|
|
|
|
|
let _cb = || {
|
|
|
|
|
sethostname(USER_NAME).unwrap();
|
|
|
|
|
init_container_env(&container_info.env).unwrap();
|
|
|
|
|
let env_vec = get_env_vec(&container_info.env).unwrap();
|
|
|
|
|
|
|
|
|
|
let volume_path = extend_image(&container_info.image).unwrap();
|
|
|
|
|
init_container_overlay(&volume_path, &container_upper_path, &container_merged_path).unwrap();
|
|
|
|
|
@ -474,6 +479,9 @@ fn run_container(container_info: &ContainerInfo, is_exec_cmd: Option<&String>) -
|
|
|
|
|
// 挂载proc
|
|
|
|
|
init_container_proc().unwrap();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
init_container_env(env_vec).unwrap();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
init_container_pts().unwrap();
|
|
|
|
|
|
|
|
|
|
|