From 84790df859f799f53dd8345d250e2a05db996e10 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: Thu, 8 Aug 2024 12:08:06 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=A0=87=E5=87=86=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 5 +++++ src/main.rs | 20 +++++++++----------- src/network.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/network.rs diff --git a/Cargo.toml b/Cargo.toml index b6a6eb2..d0f26b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,8 @@ toml = "0.8.8" clap = {version = "4.5.0", features = ["derive"]} nix = {version = "0.29", features = ["sched", "process", "hostname", "mount", "fs", "env", "user", "term", "signal"]} serde = { version = "1.0", features = ["derive"] } + + +[[bin]] +name = "network" +path = "src/network.rs" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 217dfac..1bf6620 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,14 +95,14 @@ fn extend_image(image_name: &String) -> Result { .arg("-C") .arg(volume_path_str) .output()?; - let std_out = String::from_utf8_lossy(&out.stdout); - let std_err = String::from_utf8_lossy(&out.stderr); - if std_err.len() == 0 { - println!("解压缩完毕: {std_out:?}"); + + if out.status.success() { + println!("解压缩完毕: {image_name:?}"); Ok(volume_path) } else { // 删除 volume_path std::fs::remove_dir_all(volume_path)?; + let std_err = String::from_utf8_lossy(&out.stderr); Err(RockerError::from(io::Error::new(io::ErrorKind::Other, format!("解压缩镜像失败: {std_err}")))) } } @@ -134,12 +134,10 @@ fn init_container_overlay>(volume_path: P, upper_path: P, merged_ .arg(merged_dir) .output()?; - // let std_out = String::from_utf8_lossy(&out.stdout); - let std_err = String::from_utf8_lossy(&out.stderr); - - if std_err.len() == 0 { + if out.status.success() { println!("容器文件系统创建完成"); } else { + let std_err = String::from_utf8_lossy(&out.stderr); return Err(RockerError::from(io::Error::new(io::ErrorKind::Other, format!("容器文件系统创建失败: {std_err:?}")))); } Ok(()) @@ -172,11 +170,11 @@ fn init_container_custom_volume>(container_merged_path: P, custom .arg(host_path) .arg(container_path) .output()?; - // let std_out = String::from_utf8_lossy(&out.stdout); - let std_err = String::from_utf8_lossy(&out.stderr); - if std_err.len() == 0 { + + if out.status.success() { println!("创建自定义 volume: {custom_volume:?}"); } else { + let std_err = String::from_utf8_lossy(&out.stderr); return Err(RockerError::OtherError(format!("创建volume失败: {std_err}"))) } } diff --git a/src/network.rs b/src/network.rs new file mode 100644 index 0000000..56a8f1d --- /dev/null +++ b/src/network.rs @@ -0,0 +1,33 @@ + + + +fn main(){ + + +} + + + +#[test] +fn test_network() { + let bridge_name = "test_rocker"; + // ip link add xxxx + // ip link add name br0 type bridge + let out = std::process::Command::new("ip") + .arg("link") + .arg("add") + .arg("name") + .arg(bridge_name) + .arg("type") + .arg("bridge") + .output() + .unwrap(); + out.status.success(); + println!("{:?}", out); + + println!("123"); + +} + + +