|
|
@ -27,7 +27,15 @@ static mut STACK: [u8; 1024*1024*1] = [0; 1024*1024*1];
|
|
|
|
static CLONE_FLAG: i32 = 0b1101100000000100000000000000000; // CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWIPC | CLONE_NEWNET;
|
|
|
|
static CLONE_FLAG: i32 = 0b1101100000000100000000000000000; // CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWIPC | CLONE_NEWNET;
|
|
|
|
|
|
|
|
|
|
|
|
static START_T: OnceLock<time::Instant> = OnceLock::new();
|
|
|
|
static START_T: OnceLock<time::Instant> = OnceLock::new();
|
|
|
|
static CONTAINER_INFO: OnceLock<ContainerInfo> = OnceLock::new();
|
|
|
|
static LOGO: &str = r#"
|
|
|
|
|
|
|
|
____ __
|
|
|
|
|
|
|
|
/\ _`\ /\ \
|
|
|
|
|
|
|
|
\ \ \L\ \ ___ ___\ \ \/'\ __ _ __
|
|
|
|
|
|
|
|
\ \ , / / __`\ /'___\ \ , < /'__`\/\`'__\
|
|
|
|
|
|
|
|
\ \ \\ \ /\ \L\ \/\ \__/\ \ \\`\ /\ __/\ \ \/
|
|
|
|
|
|
|
|
\ \_\ \_\ \____/\ \____\\ \_\ \_\ \____\\ \_\
|
|
|
|
|
|
|
|
\/_/\/ /\/___/ \/____/ \/_/\/_/\/____/ \/_/
|
|
|
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(version, about, long_about = None)]
|
|
|
|
#[command(version, about, long_about = None)]
|
|
|
@ -237,7 +245,7 @@ fn init_container_pivot<P: AsRef<Path>>(merged_path: P) -> Result<()> {
|
|
|
|
std::env::set_current_dir("/")?;
|
|
|
|
std::env::set_current_dir("/")?;
|
|
|
|
|
|
|
|
|
|
|
|
// 卸载 old_root, 并删除临时文件
|
|
|
|
// 卸载 old_root, 并删除临时文件
|
|
|
|
umount2(".pivot_root", MntFlags::MNT_FORCE).unwrap();
|
|
|
|
umount2(".pivot_root", MntFlags::MNT_DETACH).unwrap();
|
|
|
|
std::fs::remove_dir(".pivot_root").unwrap();
|
|
|
|
std::fs::remove_dir(".pivot_root").unwrap();
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -421,7 +429,9 @@ fn run_container(container_info: &ContainerInfo, is_exec_cmd: Option<&String>) -
|
|
|
|
init_container_custom_volume(&container_merged_path, &container_info.volume).unwrap();
|
|
|
|
init_container_custom_volume(&container_merged_path, &container_info.volume).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sethostname(USER_NAME).unwrap();
|
|
|
|
sethostname(USER_NAME).unwrap();
|
|
|
|
init_container_env(&container_info.env).unwrap();
|
|
|
|
if container_info.env.len() > 0 {
|
|
|
|
|
|
|
|
init_container_env(&container_info.env).unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
init_container_pivot(&container_merged_path).unwrap();
|
|
|
|
init_container_pivot(&container_merged_path).unwrap();
|
|
|
|
init_container_mount().unwrap();
|
|
|
|
init_container_mount().unwrap();
|
|
|
|
init_container_log(container_info.log).unwrap();
|
|
|
|
init_container_log(container_info.log).unwrap();
|
|
|
@ -594,14 +604,14 @@ fn stop_container(containers_id: &str, is_remove: bool) -> Result<()> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.for_each(|s| {
|
|
|
|
.for_each(|s| {
|
|
|
|
match umount2(s.as_str(), MntFlags::MNT_FORCE) {
|
|
|
|
match umount2(s.as_str(), MntFlags::MNT_DETACH) {
|
|
|
|
Ok(_) => println!("卸载自定卷{s}"),
|
|
|
|
Ok(_) => println!("卸载自定卷{s}"),
|
|
|
|
Err(e) => println!("卸载卷{s}失败: {e:?}"),
|
|
|
|
Err(e) => println!("卸载卷{s}失败: {e:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 卸载overlayfs
|
|
|
|
// 卸载overlayfs
|
|
|
|
match umount2(container_merged_path.to_str().unwrap(), MntFlags::MNT_FORCE) {
|
|
|
|
match umount2(container_merged_path.to_str().unwrap(), MntFlags::MNT_DETACH) {
|
|
|
|
Ok(_) => println!("卸载overlayfs卷: {container_merged_path:?}"),
|
|
|
|
Ok(_) => println!("卸载overlayfs卷: {container_merged_path:?}"),
|
|
|
|
Err(e) => println!("卸载overlayfs失败: {e:?}"),
|
|
|
|
Err(e) => println!("卸载overlayfs失败: {e:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -615,11 +625,11 @@ fn stop_container(containers_id: &str, is_remove: bool) -> Result<()> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
println!("容器不存在: {container_id}, 尝试删除");
|
|
|
|
println!("容器不存在: {container_id}, 强制删除");
|
|
|
|
// todo 需要强制删除一下目录
|
|
|
|
// todo 需要强制删除一下目录
|
|
|
|
match fs::remove_dir_all(container_work_path) {
|
|
|
|
match fs::remove_dir_all(&container_work_path) {
|
|
|
|
Ok(_) => println!("删除容器 {container_id} 成功"),
|
|
|
|
Ok(_) => println!("强制删除 {container_work_path:?} 成功"),
|
|
|
|
Err(e) => println!("删除容器失败: {e:?}"),
|
|
|
|
Err(e) => println!("强制删除失败: {e:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -631,6 +641,7 @@ fn stop_container(containers_id: &str, is_remove: bool) -> Result<()> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<()>{
|
|
|
|
fn main() -> Result<()>{
|
|
|
|
|
|
|
|
println!("{LOGO}");
|
|
|
|
START_T.get_or_init(|| {
|
|
|
|
START_T.get_or_init(|| {
|
|
|
|
time::Instant::now()
|
|
|
|
time::Instant::now()
|
|
|
|
});
|
|
|
|
});
|
|
|
|