From 40ff1d0b899289399c3f76a04f76714bf49db039 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 14 Aug 2024 09:37:14 -0400 Subject: [PATCH] wip Signed-off-by: Colin Walters --- Cargo.lock | 15 +++++++++++++++ Cargo.toml | 2 +- blockdev/Cargo.toml | 25 +++++++++++++++++++++++++ {lib => blockdev}/src/blockdev.rs | 16 +++------------- lib/src/install/baseline.rs | 15 +++++++++++---- 5 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 blockdev/Cargo.toml rename {lib => blockdev}/src/blockdev.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index b81e788b..13e0d46d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,6 +150,21 @@ dependencies = [ "generic-array", ] +[[package]] +name = "blockdev" +version = "0.0.0" +dependencies = [ + "anyhow", + "bootc-utils", + "camino", + "fn-error-context", + "indoc", + "regex", + "serde", + "serde_json", + "tracing", +] + [[package]] name = "bootc" version = "0.1.9" diff --git a/Cargo.toml b/Cargo.toml index f2c63fd9..a2aea4b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["cli", "lib", "xtask", "tests-integration"] +members = ["cli", "lib", "blockdev", "xtask", "tests-integration"] resolver = "2" [profile.dev] diff --git a/blockdev/Cargo.toml b/blockdev/Cargo.toml new file mode 100644 index 00000000..bcebccf0 --- /dev/null +++ b/blockdev/Cargo.toml @@ -0,0 +1,25 @@ +[package] +description = "Internal blockdev code" +# Should never be published to crates.io +publish = false +edition = "2021" +license = "MIT OR Apache-2.0" +name = "blockdev" +repository = "https://github.com/containers/bootc" +version = "0.0.0" + +[dependencies] +anyhow = { workspace = true } +bootc-utils = { path = "../utils" } +camino = { workspace = true, features = ["serde1"] } +fn-error-context = { workspace = true } +regex = "1.10.4" +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +tracing = { workspace = true } + +[dev-dependencies] +indoc = "2.0.5" + +[lib] +path = "src/blockdev.rs" diff --git a/lib/src/blockdev.rs b/blockdev/src/blockdev.rs similarity index 96% rename from lib/src/blockdev.rs rename to blockdev/src/blockdev.rs index 8631902f..0baccfb1 100644 --- a/lib/src/blockdev.rs +++ b/blockdev/src/blockdev.rs @@ -11,7 +11,6 @@ use regex::Regex; use serde::Deserialize; use crate::install::run_in_host_mountns; -use crate::task::Task; use bootc_utils::CommandRunExt; #[derive(Debug, Deserialize)] @@ -84,15 +83,6 @@ impl Device { } } -#[context("Failed to wipe {dev}")] -pub(crate) fn wipefs(dev: &Utf8Path) -> Result<()> { - Task::new_and_run( - format!("Wiping device {dev}"), - "wipefs", - ["-a", dev.as_str()], - ) -} - #[context("Listing device {dev}")] pub(crate) fn list_dev(dev: &Utf8Path) -> Result { let mut devs: DevicesOutput = Command::new("lsblk") @@ -176,10 +166,10 @@ impl Partition { #[context("Listing partitions of {dev}")] pub(crate) fn partitions_of(dev: &Utf8Path) -> Result { - let o = Task::new_quiet("sfdisk") + let o: SfDiskOutput = Command::new("sfdisk") .args(["-J", dev.as_str()]) - .read()?; - let o: SfDiskOutput = serde_json::from_str(&o).context("Parsing sfdisk output")?; + .run_and_parse_json() + .context("Parsing sfdisk output")?; Ok(o.partitiontable) } diff --git a/lib/src/install/baseline.rs b/lib/src/install/baseline.rs index 0aa86128..bc2c90db 100644 --- a/lib/src/install/baseline.rs +++ b/lib/src/install/baseline.rs @@ -158,6 +158,15 @@ fn mkfs<'a>( Ok(u) } +#[context("Failed to wipe {dev}")] +fn wipefs(dev: &Utf8Path) -> Result<()> { + Task::new_and_run( + format!("Wiping device {dev}"), + "wipefs", + ["-a", dev.as_str()], + ) +} + #[context("Creating rootfs")] pub(crate) fn install_create_rootfs( state: &State, @@ -184,11 +193,9 @@ pub(crate) fn install_create_rootfs( let dev = &opts.device; for child in device.children.iter().flatten() { let child = child.path(); - println!("Wiping {child}"); - crate::blockdev::wipefs(Utf8Path::new(&child))?; + wipefs(Utf8Path::new(&child))?; } - println!("Wiping {dev}"); - crate::blockdev::wipefs(dev)?; + wipefs(dev)?; } else if device.has_children() { anyhow::bail!( "Detected existing partitions on {}; use e.g. `wipefs` if you intend to overwrite",