From 75a2805718f7323c3329b271c49859b28ca91664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=B3=E5=85=89=E5=B0=91=E5=B9=B4?= <849317537@qq.com> Date: Tue, 13 Aug 2024 13:50:56 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pypy.env | 4 ++++ src/main.rs | 20 ++++++++++++++------ test.env | 7 ------- 3 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 pypy.env delete mode 100644 test.env diff --git a/pypy.env b/pypy.env new file mode 100644 index 0000000..c128c8f --- /dev/null +++ b/pypy.env @@ -0,0 +1,4 @@ +TERM=xterm +VIRTUAL_ENV=/python_env +VIRTUAL_ENV_PROMPT=(python_env) +PATH=/python_env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c26be25..af3f772 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,7 +154,6 @@ fn init_container_overlay>(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>(container_merged_path: P, custom } -fn init_container_env(env: &String) -> Result<()>{ +fn get_env_vec(env: &String) -> Result> { 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::>() }; + Ok(env_vec) +} +fn init_container_env(env_vec: Vec) -> Result<()>{ for item_env in env_vec.iter() { let item_env_v = item_env.split("=").collect::>(); 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(); diff --git a/test.env b/test.env deleted file mode 100644 index 74ad18f..0000000 --- a/test.env +++ /dev/null @@ -1,7 +0,0 @@ -LANG=C.UTF-8 -TERM=xterm -PYPY_VERSION=7.3.16 -PYTHON_GET_PIP_SHA256=95c5ee602b2f3cc50ae053d716c3c89bea62c58568f64d7d25924d399b2d5218 -PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/3843bff3a0a61da5b63ea0b7d34794c5c51a2f11/get-pip.py -PATH=/opt/pypy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -_=/usr/bin/env \ No newline at end of file