diff --git a/Cargo.lock b/Cargo.lock index c1bcbcd4..720ad29f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,48 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" +[[package]] +name = "async-broadcast" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435a87a52755b8f27fcf321ac4f04b2802e337c8c4872923137471ec39c37532" +dependencies = [ + "event-listener", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-io" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc" +dependencies = [ + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-io", + "futures-lite", + "parking", + "polling", + "rustix", + "slab", + "windows-sys 0.61.1", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -169,10 +211,12 @@ dependencies = [ "data-encoding", "dirs", "fn-error-context", + "futures-util", "indicatif", "indoc", "itertools", "libc", + "libsystemd", "nix", "notify", "oci-spec", @@ -199,6 +243,7 @@ dependencies = [ "which", "xshell", "yaml-rust2", + "zlink", ] [[package]] @@ -487,6 +532,15 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "console" version = "0.15.11" @@ -679,6 +733,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -771,6 +826,27 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5692dd7b5a1978a5aeb0ce83b7655c58ca8efdcb79d21036ea249da95afec2c6" +[[package]] +name = "event-listener" +version = "5.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + [[package]] name = "eyre" version = "0.6.12" @@ -888,6 +964,30 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" +[[package]] +name = "futures-lite" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "parking", + "pin-project-lite", +] + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.31" @@ -908,6 +1008,7 @@ checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -1022,6 +1123,15 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "http" version = "1.3.1" @@ -1340,23 +1450,28 @@ version = "0.1.0" dependencies = [ "bcvk", "camino", + "cap-std-ext", "cfg-if", "color-eyre", "dirs", + "libc", "libtest-mimic", "linkme", "paste", "rand", "regex", + "rustix", "scopeguard", "serde", "serde_json", "tempfile", + "tokio", "tracing", "tracing-error", "tracing-subscriber", "uuid", "xshell", + "zlink", ] [[package]] @@ -1482,6 +1597,24 @@ dependencies = [ "redox_syscall", ] +[[package]] +name = "libsystemd" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19c97a761fc86953c5b885422b22c891dbf5bcb9dcc99d0110d6ce4c052759f0" +dependencies = [ + "hmac", + "libc", + "log", + "nix", + "nom", + "once_cell", + "serde", + "sha2", + "thiserror 2.0.17", + "uuid", +] + [[package]] name = "libtest-mimic" version = "0.7.3" @@ -1646,6 +1779,15 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] + [[package]] name = "notify" version = "6.1.1" @@ -1793,6 +1935,12 @@ version = "4.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c6901729fa79e91a0913333229e9ca5dc725089d1c363b2f4b4760709dc4a52" +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + [[package]] name = "parking_lot" version = "0.12.4" @@ -1846,6 +1994,20 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" +[[package]] +name = "polling" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi", + "pin-project-lite", + "rustix", + "windows-sys 0.61.1", +] + [[package]] name = "portable-atomic" version = "1.11.1" @@ -2557,6 +2719,7 @@ dependencies = [ "slab", "socket2", "tokio-macros", + "tracing", "windows-sys 0.59.0", ] @@ -2591,6 +2754,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32da49809aab5c3bc678af03902d4ccddea2a87d028d86392a4b1560c6906c70" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", + "tokio-util", +] + [[package]] name = "tokio-util" version = "0.7.16" @@ -2841,6 +3016,7 @@ checksum = "2f87b8aa10b915a06587d0dec516c282ff295b475d94abf425d62b57710070a2" dependencies = [ "getrandom 0.3.3", "js-sys", + "serde", "wasm-bindgen", ] @@ -3559,3 +3735,70 @@ dependencies = [ "quote", "syn", ] + +[[package]] +name = "zlink" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd5ad065e2697e98764f2738793904ff0aaa672707d0ee1592dfb4c47800962a" +dependencies = [ + "zlink-smol", + "zlink-tokio", +] + +[[package]] +name = "zlink-core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cae6561573e9996c2c26bf1c723b137a99c2d571a3276bd8f4ac52bfa44b3f2c" +dependencies = [ + "futures-util", + "itoa", + "libc", + "pin-project-lite", + "rustix", + "ryu", + "serde", + "serde_json", + "tracing", + "zlink-macros", +] + +[[package]] +name = "zlink-macros" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4219a5aadc4844073d134e27fd52228933a462fbd07fe51ac5df8393f08168a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zlink-smol" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "befa02505904ee15f6721fdae1b6473d59649049c1f1810175fb7205a4dca82a" +dependencies = [ + "async-broadcast", + "async-channel", + "async-io", + "futures-lite", + "futures-util", + "pin-project-lite", + "zlink-core", +] + +[[package]] +name = "zlink-tokio" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "378110fc4fad5fa9359251eb830d87993efcca44245f538a552cf26001649378" +dependencies = [ + "futures-util", + "pin-project-lite", + "tokio", + "tokio-stream", + "zlink-core", +] diff --git a/Cargo.toml b/Cargo.toml index f4efd3f3..de28b15d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,9 @@ xshell = "0.2.7" # Require an extra opt-in for unsafe unsafe_code = "deny" # Absolutely must handle errors -unused_must_use = "forbid" +# "deny" rather than "forbid" so that proc-macro expansions (e.g. zlink) +# can locally #[allow(unused)] without conflicting. +unused_must_use = "deny" missing_docs = "deny" missing_debug_implementations = "deny" # Feel free to comment this one out locally during development of a patch. diff --git a/crates/integration-tests/Cargo.toml b/crates/integration-tests/Cargo.toml index daacd9c3..0aeb8f31 100644 --- a/crates/integration-tests/Cargo.toml +++ b/crates/integration-tests/Cargo.toml @@ -25,8 +25,10 @@ tracing-subscriber = { workspace = true } tracing-error = { workspace = true } xshell = { workspace = true } cfg-if = { workspace = true } -serde = "1.0.199" +serde = { version = "1.0.199", features = ["derive"] } serde_json = "1.0.116" +zlink = "0.4" +tokio = { version = "1", features = ["rt", "net", "macros"] } libtest-mimic = "0.7.3" tempfile = "3" uuid = { version = "1.18.1", features = ["v4"] } @@ -36,6 +38,9 @@ linkme = "0.3.30" paste = "1.0" rand = { workspace = true } scopeguard = "1" +cap-std-ext = { workspace = true } +libc = "0.2" +rustix = { version = "1", features = ["process"] } [lints] workspace = true diff --git a/crates/integration-tests/src/bin/cleanup.rs b/crates/integration-tests/src/bin/cleanup.rs index c4185741..0d241049 100644 --- a/crates/integration-tests/src/bin/cleanup.rs +++ b/crates/integration-tests/src/bin/cleanup.rs @@ -47,7 +47,7 @@ fn cleanup_integration_test_containers() -> Result<(), Box, +} + +#[derive(Debug, Deserialize)] +struct PsReply { + container_ids: Vec, +} + +#[derive(Debug, Clone, serde::Serialize, Deserialize, Default)] +#[allow(dead_code)] +struct EphemeralRunOpts { + ssh_keygen: Option, + name: Option, + label: Option>, + network: Option, + env: Option>, + rm: Option, + itype: Option, + memory: Option, + vcpus: Option, + console: Option, + bind: Option>, + ro_bind: Option>, + mount_disk_files: Option>, + kargs: Option>, + add_swap: Option, +} + +#[derive(Debug, Deserialize)] +#[allow(dead_code)] +struct RunReply { + container_id: String, +} + +#[derive(Debug, Deserialize)] +#[allow(dead_code)] +struct GetSshConnectionInfoReply { + container_id: String, + key_path: String, + user: String, + host: String, + port: u16, +} + +#[derive(Debug, Deserialize)] +#[allow(dead_code)] +struct ToDiskReply { + path: String, + cached: bool, +} + +// --------------------------------------------------------------------------- +// Error types (needed by proxy return types) +// --------------------------------------------------------------------------- + +#[derive(Debug, zlink::ReplyError, zlink::introspect::ReplyError)] +#[zlink(interface = "io.bootc.vk.images")] +enum ImagesError { + PodmanError { + #[allow(dead_code)] + message: String, + }, +} + +impl std::fmt::Display for ImagesError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::PodmanError { message } => write!(f, "podman error: {message}"), + } + } +} + +impl std::error::Error for ImagesError {} + +#[derive(Debug, zlink::ReplyError, zlink::introspect::ReplyError)] +#[zlink(interface = "io.bootc.vk.ephemeral")] +enum EphemeralError { + PodmanError { + #[allow(dead_code)] + message: String, + }, +} + +impl std::fmt::Display for EphemeralError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::PodmanError { message } => write!(f, "podman error: {message}"), + } + } +} + +impl std::error::Error for EphemeralError {} + +#[derive(Debug, zlink::ReplyError, zlink::introspect::ReplyError)] +#[zlink(interface = "io.bootc.vk.todisk")] +enum ToDiskError { + Failed { + #[allow(dead_code)] + message: String, + }, +} + +impl std::fmt::Display for ToDiskError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Failed { message } => write!(f, "to-disk failed: {message}"), + } + } +} + +impl std::error::Error for ToDiskError {} + +// --------------------------------------------------------------------------- +// Proxy traits +// --------------------------------------------------------------------------- + +#[zlink::proxy("io.bootc.vk.images")] +trait ImagesProxy { + async fn list(&mut self) -> zlink::Result>; +} + +#[zlink::proxy("io.bootc.vk.ephemeral")] +trait EphemeralProxy { + async fn ps(&mut self) -> zlink::Result>; + + async fn run( + &mut self, + image: String, + opts: Option, + ) -> zlink::Result>; + + async fn get_ssh_connection_info( + &mut self, + container_id: String, + ) -> zlink::Result>; +} + +#[zlink::proxy("io.bootc.vk.todisk")] +trait ToDiskProxy { + async fn to_disk( + &mut self, + source_image: String, + target_disk: String, + format: Option, + disk_size: Option, + filesystem: Option, + root_size: Option, + kargs: Option>, + ) -> zlink::Result>; +} + +// --------------------------------------------------------------------------- +// Helper: spawn bcvk with socket activation +// --------------------------------------------------------------------------- + +struct ActivatedBcvk { + conn: zlink::Connection, + rt: tokio::runtime::Runtime, +} + +/// Spawn bcvk with socket activation and return a zlink connection. +/// +/// Creates a Unix socketpair and spawns bcvk directly with socket-activation +/// env vars (`LISTEN_FDS`, `LISTEN_PID`, `LISTEN_FDNAMES`) set via +/// `libc::setenv` in a `pre_exec` hook. We avoid `Command::env()` because +/// Rust's std overwrites the global `environ` pointer *after* `pre_exec` +/// callbacks run, which would discard our `LISTEN_PID` value (which must +/// equal `getpid()` for libsystemd's `receive_descriptors` to accept it). +/// See `library/std/src/sys/process/unix/unix.rs` in rust-lang/rust. +/// +/// The child process is bound to the calling thread via +/// `lifecycle_bind_to_parent_thread`, so it is automatically killed when the +/// test thread exits. This must NOT be called inside `spawn_blocking`. +fn activated_connection() -> Result { + let bck = get_bck_command()?; + let (ours, theirs) = UnixStream::pair()?; + let theirs_fd: Arc = Arc::new(theirs.into()); + + let mut cmd = Command::new(&bck); + // Do NOT use cmd.env() here -- it causes Rust's Command to build an + // envp array that replaces environ after our pre_exec setenv calls. + cmd.take_fd_n(theirs_fd, 3) + .lifecycle_bind_to_parent_thread(); + #[allow(unsafe_code)] + unsafe { + cmd.pre_exec(|| { + let pid = rustix::process::getpid(); + let pid_dec = rustix::path::DecInt::new(pid.as_raw_nonzero().get()); + libc::setenv(c"LISTEN_PID".as_ptr(), pid_dec.as_c_str().as_ptr(), 1); + libc::setenv(c"LISTEN_FDS".as_ptr(), c"1".as_ptr(), 1); + libc::setenv(c"LISTEN_FDNAMES".as_ptr(), c"varlink".as_ptr(), 1); + Ok(()) + }); + } + let _child = cmd.spawn()?; + + ours.set_nonblocking(true)?; + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build()?; + let tokio_stream = rt.block_on(async { tokio::net::UnixStream::from_std(ours) })?; + let zlink_stream = zlink::unix::Stream::from(tokio_stream); + let conn = zlink::Connection::from(zlink_stream); + + Ok(ActivatedBcvk { conn, rt }) +} + +/// Remove a container by ID, ignoring errors (for test cleanup). +fn cleanup_container(id: &str) { + let _ = Command::new("podman").args(["rm", "-f", "--", id]).output(); +} + +// =========================================================================== +// Tests: io.bootc.vk.images +// =========================================================================== + +/// Verify that the images `List` method returns a vec of image name strings. +fn test_varlink_images_list() -> Result<()> { + let mut bcvk = activated_connection()?; + let reply = bcvk.rt.block_on(async { bcvk.conn.list().await })??; + // In CI there may be no bootc images; just verify deserialization succeeds. + for name in &reply.images { + assert!(!name.is_empty(), "image name must not be empty"); + } + Ok(()) +} +integration_test!(test_varlink_images_list); + +/// Verify that the test image appears in the images List after pulling it. +/// +/// This test pulls the primary test image (which has the `containers.bootc=1` +/// label) and then verifies it appears in the varlink List response. +fn test_varlink_images_list_contains_test_image() -> Result<()> { + let image = get_test_image(); + + // Ensure the image is pulled + let sh = shell()?; + xshell::cmd!(sh, "podman pull -q {image}").run()?; + + let mut bcvk = activated_connection()?; + let reply = bcvk.rt.block_on(async { bcvk.conn.list().await })??; + + assert!( + reply.images.iter().any(|name| name.contains(&image)), + "expected test image {image} in varlink images list, got: {:?}", + reply.images + ); + Ok(()) +} +integration_test!(test_varlink_images_list_contains_test_image); + +// =========================================================================== +// Tests: io.bootc.vk.ephemeral +// =========================================================================== + +/// Verify that the ephemeral `Ps` method returns container ID strings. +fn test_varlink_ephemeral_ps() -> Result<()> { + let mut bcvk = activated_connection()?; + let reply = bcvk.rt.block_on(async { bcvk.conn.ps().await })??; + for id in &reply.container_ids { + assert!(!id.is_empty(), "container ID must not be empty"); + } + Ok(()) +} +integration_test!(test_varlink_ephemeral_ps); + +/// Test that `Run` with a nonexistent image returns an error. +fn test_varlink_ephemeral_run_bad_image() -> Result<()> { + let mut bcvk = activated_connection()?; + let result = bcvk.rt.block_on(async { + bcvk.conn + .run( + "nonexistent-image-that-should-not-exist:latest".to_string(), + None, + ) + .await + })?; + match result { + Err(EphemeralError::PodmanError { .. }) => Ok(()), + Ok(reply) => Err(color_eyre::eyre::eyre!( + "expected error for nonexistent image, got container_id: {}", + reply.container_id + )), + } +} +integration_test!(test_varlink_ephemeral_run_bad_image); + +/// End-to-end test: Run a VM, verify it in Ps, get SSH connection info, +/// and actually SSH into it using the returned values. +fn test_varlink_ephemeral_run_ps_and_ssh() -> Result<()> { + let image = get_test_image(); + let mut bcvk = activated_connection()?; + + // Launch an ephemeral VM with SSH key injection + let run_reply = bcvk.rt.block_on(async { + bcvk.conn + .run( + image.clone(), + Some(EphemeralRunOpts { + ssh_keygen: Some(true), + ..Default::default() + }), + ) + .await + })??; + assert!( + !run_reply.container_id.is_empty(), + "expected non-empty container_id from Run" + ); + + // Verify it shows up in Ps + let ps_reply = bcvk.rt.block_on(async { bcvk.conn.ps().await })??; + assert!( + ps_reply.container_ids.contains(&run_reply.container_id), + "expected container {} to appear in Ps, got: {:?}", + run_reply.container_id, + ps_reply.container_ids + ); + + // Get SSH connection info + let ssh = bcvk.rt.block_on(async { + bcvk.conn + .get_ssh_connection_info(run_reply.container_id.clone()) + .await + })??; + + // Use the returned info to actually SSH into the VM. + // Retry with backoff since the VM needs time to boot. + let port = ssh.port.to_string(); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120); + loop { + let result = Command::new("podman") + .args([ + "exec", + "--", + &ssh.container_id, + "ssh", + "-i", + &ssh.key_path, + "-p", + &port, + "-o", + "StrictHostKeyChecking=no", + "-o", + "UserKnownHostsFile=/dev/null", + "-o", + "BatchMode=yes", + "-o", + "ConnectTimeout=2", + "-o", + "LogLevel=ERROR", + &format!("{}@{}", ssh.user, ssh.host), + "true", + ]) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status(); + + match result { + Ok(status) if status.success() => break, + _ if std::time::Instant::now() > deadline => { + cleanup_container(&run_reply.container_id); + return Err(color_eyre::eyre::eyre!( + "SSH did not become ready within 120s using info from GetSshConnectionInfo" + )); + } + _ => std::thread::sleep(std::time::Duration::from_secs(2)), + } + } + + // Clean up + cleanup_container(&run_reply.container_id); + Ok(()) +} +integration_test!(test_varlink_ephemeral_run_ps_and_ssh); + +// =========================================================================== +// Tests: io.bootc.vk.todisk +// =========================================================================== + +/// Test that `ToDisk` with a nonexistent image returns a `Failed` error. +fn test_varlink_todisk_bad_image() -> Result<()> { + let mut bcvk = activated_connection()?; + let target = tempfile::NamedTempFile::new()?; + let target_path = target.path().to_str().unwrap().to_string(); + // Remove the temp file so to_disk sees a fresh path + drop(target); + + let result = bcvk.rt.block_on(async { + bcvk.conn + .to_disk( + "nonexistent-image-that-should-not-exist:latest".to_string(), + target_path, + None, + None, + None, + None, + None, + ) + .await + })?; + match result { + Err(ToDiskError::Failed { .. }) => Ok(()), + Ok(reply) => Err(color_eyre::eyre::eyre!( + "expected Failed error for nonexistent image, got path: {}", + reply.path + )), + } +} +integration_test!(test_varlink_todisk_bad_image); + +/// Test that `ToDisk` rejects invalid format strings. +fn test_varlink_todisk_bad_format() -> Result<()> { + let mut bcvk = activated_connection()?; + let td = tempfile::TempDir::new()?; + let target_path = td.path().join("disk.img"); + let target = target_path.to_str().unwrap().to_string(); + + let result = bcvk.rt.block_on(async { + bcvk.conn + .to_disk( + // Image doesn't matter, format validation happens first + "anything:latest".to_string(), + target, + Some("vdi".to_string()), // invalid format + None, + None, + None, + None, + ) + .await + })?; + match result { + Err(ToDiskError::Failed { message }) => { + assert!( + message.contains("unsupported disk format"), + "expected 'unsupported disk format' in error, got: {message}" + ); + Ok(()) + } + Ok(reply) => Err(color_eyre::eyre::eyre!( + "expected Failed error for invalid format, got path: {}", + reply.path + )), + } +} +integration_test!(test_varlink_todisk_bad_format); + +/// Test the ToDisk success path: create a disk image from the test image. +/// +/// This is a heavyweight test that launches a VM internally. It verifies +/// the reply contains a valid path, that the file exists, and that it is +/// not marked as cached (first run). +fn test_varlink_todisk_creates_disk() -> Result<()> { + let image = get_test_image(); + let td = tempfile::TempDir::new()?; + let target_path = td.path().join("test-disk.raw"); + let target = target_path.to_str().unwrap().to_string(); + + let mut bcvk = activated_connection()?; + let reply = bcvk.rt.block_on(async { + bcvk.conn + .to_disk( + image, + target.clone(), + Some("raw".to_string()), + Some("10G".to_string()), + None, + None, + None, + ) + .await + })??; + + assert_eq!(reply.path, target, "reply path should match target_disk"); + assert!(!reply.cached, "first run should not be cached"); + assert!( + target_path.exists(), + "disk image should exist at {}", + target_path.display() + ); + + let metadata = std::fs::metadata(&target_path)?; + assert!(metadata.len() > 0, "disk image should have nonzero size"); + + Ok(()) +} +integration_test!(test_varlink_todisk_creates_disk); + +// =========================================================================== +// Tests: cross-interface / varlinkctl +// =========================================================================== + +/// Verify that `varlinkctl call` against the images List method works. +fn test_varlink_exec_varlinkctl() -> Result<()> { + let sh = shell()?; + let bck = get_bck_command()?; + let output = xshell::cmd!(sh, "varlinkctl call exec:{bck} io.bootc.vk.images.List").read()?; + let parsed: serde_json::Value = serde_json::from_str(&output)?; + assert!( + parsed.get("images").is_some(), + "response missing 'images' key" + ); + Ok(()) +} +integration_test!(test_varlink_exec_varlinkctl); + +/// Test that `varlinkctl introspect` shows all three interface names. +fn test_varlink_introspect_varlinkctl() -> Result<()> { + let sh = shell()?; + let bck = get_bck_command()?; + let output = xshell::cmd!(sh, "varlinkctl introspect exec:{bck} io.bootc.vk.images").read()?; + assert!( + output.contains("io.bootc.vk.images"), + "introspect output missing 'io.bootc.vk.images'" + ); + let output = + xshell::cmd!(sh, "varlinkctl introspect exec:{bck} io.bootc.vk.ephemeral").read()?; + assert!( + output.contains("io.bootc.vk.ephemeral"), + "introspect output missing 'io.bootc.vk.ephemeral'" + ); + let output = xshell::cmd!(sh, "varlinkctl introspect exec:{bck} io.bootc.vk.todisk").read()?; + assert!( + output.contains("io.bootc.vk.todisk"), + "introspect output missing 'io.bootc.vk.todisk'" + ); + Ok(()) +} +integration_test!(test_varlink_introspect_varlinkctl); diff --git a/crates/kit/Cargo.toml b/crates/kit/Cargo.toml index 2ffb32ed..c23bf9c5 100644 --- a/crates/kit/Cargo.toml +++ b/crates/kit/Cargo.toml @@ -54,6 +54,9 @@ rustix = { version = "1", features = ["thread", "net", "fs", "pipe", "system", " vsock = "0.5" nix = { version = "0.29", features = ["socket"] } libc = "0.2" +zlink = "0.4" +futures-util = "0.3" +libsystemd = "0.7" [dev-dependencies] similar-asserts = "1.5" diff --git a/crates/kit/src/ephemeral.rs b/crates/kit/src/ephemeral.rs index b9522428..652a3359 100644 --- a/crates/kit/src/ephemeral.rs +++ b/crates/kit/src/ephemeral.rs @@ -158,7 +158,7 @@ impl EphemeralCommands { } /// List ephemeral VM containers with bcvk.ephemeral=1 label -fn list_ephemeral_containers() -> Result> { +pub(crate) fn list_ephemeral_containers() -> Result> { use bootc_utils::CommandRunExt; let containers: Vec = Command::new("podman") @@ -174,10 +174,56 @@ fn list_ephemeral_containers() -> Result> { Ok(containers) } +/// Per-container result from a removal operation +#[derive(Debug)] +pub(crate) struct RemoveContainerResult { + /// Container ID that was targeted for removal + pub id: String, + /// Whether the container was successfully removed + pub removed: bool, + /// Error message if removal failed + pub error: Option, +} + +/// Remove a single container by ID, returning the result. +/// +/// Runs `podman rm -f` for the given container ID. This is the building +/// block used by both the CLI (`rm-all`) and the varlink `Rm` method. +pub(crate) fn remove_single_container(container_id: &str) -> RemoveContainerResult { + let result = Command::new("podman") + .args(["rm", "-f", "--", container_id]) + .output(); + match result { + Ok(output) if output.status.success() => RemoveContainerResult { + id: container_id.to_owned(), + removed: true, + error: None, + }, + Ok(output) => RemoveContainerResult { + id: container_id.to_owned(), + removed: false, + error: Some(String::from_utf8_lossy(&output.stderr).to_string()), + }, + Err(e) => RemoveContainerResult { + id: container_id.to_owned(), + removed: false, + error: Some(e.to_string()), + }, + } +} + +/// Remove the given ephemeral containers, returning per-container results +pub(crate) fn remove_ephemeral_containers( + containers: &[ContainerListEntry], +) -> Vec { + containers + .iter() + .map(|container| remove_single_container(&container.id)) + .collect() +} + /// Remove all ephemeral VM containers fn remove_all_ephemeral_containers(force: bool) -> Result<()> { - use bootc_utils::CommandRunExt; - let containers = list_ephemeral_containers()?; if containers.is_empty() { @@ -210,22 +256,17 @@ fn remove_all_ephemeral_containers(force: bool) -> Result<()> { } } - for container in &containers { - println!( - "Removing container {}", - &container.id[..12.min(container.id.len())] - ); - let result = Command::new("podman") - .args(["rm", "-f", &container.id]) - .run(); - - match result { - Ok(_) => println!("Removed {}", &container.id[..12.min(container.id.len())]), - Err(e) => eprintln!( + let results = remove_ephemeral_containers(&containers); + for result in &results { + let short_id = &result.id[..12.min(result.id.len())]; + if result.removed { + println!("Removed {short_id}"); + } else { + eprintln!( "Failed to remove {}: {}", - &container.id[..12.min(container.id.len())], - e - ), + short_id, + result.error.as_deref().unwrap_or("unknown error") + ); } } diff --git a/crates/kit/src/images.rs b/crates/kit/src/images.rs index ccede794..569d9379 100644 --- a/crates/kit/src/images.rs +++ b/crates/kit/src/images.rs @@ -202,7 +202,7 @@ pub fn list() -> Result> { /// Inspect a container image and return metadata. pub fn inspect(name: &str) -> Result { let mut r: Vec = Command::new("podman") - .args(["image", "inspect", name]) + .args(["image", "inspect", "--", name]) .run_and_parse_json() .map_err(|e| eyre!("{e}"))?; r.pop().ok_or_else(|| eyre!("No such image")) diff --git a/crates/kit/src/main.rs b/crates/kit/src/main.rs index ef566798..eb8c8777 100644 --- a/crates/kit/src/main.rs +++ b/crates/kit/src/main.rs @@ -57,6 +57,8 @@ pub(crate) mod systemd; mod to_disk; #[cfg(target_os = "linux")] mod utils; +#[cfg(target_os = "linux")] +mod varlink_ipc; /// Default state directory for bcvk container data #[cfg(target_os = "linux")] @@ -184,8 +186,9 @@ enum Commands { /// /// Sets up structured logging with environment-based filtering, /// error layer integration, and console output formatting. -/// Logs are filtered by RUST_LOG environment variable, defaulting to 'info'. -fn install_tracing() { +/// Logs are filtered by `RUST_LOG` environment variable, falling back +/// to `default_level` (typically `"info"` for CLI, `"warn"` for varlink). +fn install_tracing(default_level: &str) { use tracing_error::ErrorLayer; use tracing_subscriber::fmt; use tracing_subscriber::prelude::*; @@ -197,7 +200,7 @@ fn install_tracing() { .event_format(format) .with_writer(std::io::stderr); let filter_layer = EnvFilter::try_from_default_env() - .or_else(|_| EnvFilter::try_new("info")) + .or_else(|_| EnvFilter::try_new(default_level)) .unwrap(); tracing_subscriber::registry() @@ -215,9 +218,33 @@ fn install_tracing() { // On non-Linux, all commands return errors so post-match code is unreachable #[cfg_attr(not(target_os = "linux"), allow(unreachable_code))] fn main() -> Result<(), Report> { - install_tracing(); + // Detect varlink socket activation early to set a quieter default log + // level. The varlink protocol runs on a separate fd so logging doesn't + // interfere, but info-level chatter is unhelpful when running as a service. + // LISTEN_PID validation is handled by libsystemd::activation::receive_descriptors() + // in try_activated_listener(). We only check LISTEN_FDS here to select the log level. + #[cfg(target_os = "linux")] + let varlink_mode = std::env::var_os("LISTEN_FDS").is_some(); + #[cfg(not(target_os = "linux"))] + let varlink_mode = false; + + install_tracing(if varlink_mode { "warn" } else { "info" }); color_eyre::install()?; + // If invoked via varlink socket activation (e.g. `varlinkctl exec:bcvk`), + // serve the varlink interface and exit. This must happen before clap + // parsing since the activated process receives no CLI arguments. + #[cfg(target_os = "linux")] + if varlink_mode { + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .context("Init tokio runtime")?; + if rt.block_on(varlink_ipc::try_serve_varlink())? { + return Ok(()); + } + } + let cli = Cli::parse(); #[cfg(target_os = "linux")] @@ -245,7 +272,19 @@ fn main() -> Result<(), Report> { #[cfg(target_os = "linux")] Commands::ToDisk(opts) => { - to_disk::run(opts)?; + let target = opts.target_disk.clone(); + match to_disk::run(opts)? { + to_disk::RunOutcome::Cached => { + println!("Reusing existing cached disk image at: {target}"); + } + to_disk::RunOutcome::Created => {} + to_disk::RunOutcome::DryRunWouldReuse => { + println!("would-reuse"); + } + to_disk::RunOutcome::DryRunWouldRegenerate => { + println!("would-regenerate"); + } + } } #[cfg(target_os = "linux")] diff --git a/crates/kit/src/podman.rs b/crates/kit/src/podman.rs index 923adf44..26b4ad98 100644 --- a/crates/kit/src/podman.rs +++ b/crates/kit/src/podman.rs @@ -39,6 +39,7 @@ pub fn get_image_size(image: &str) -> Result { .arg("inspect") .arg("--format=json") .arg("--type=image") + .arg("--") .arg(image) .run_and_parse_json() .map_err(|e| eyre!("podman inspect failed for image {}: {}", image, e))?; diff --git a/crates/kit/src/run_ephemeral.rs b/crates/kit/src/run_ephemeral.rs index 3957261d..124276d1 100644 --- a/crates/kit/src/run_ephemeral.rs +++ b/crates/kit/src/run_ephemeral.rs @@ -178,7 +178,10 @@ pub struct CommonVmOpts { #[clap(long, help = "Number of vCPUs (overridden by --itype if specified)")] pub vcpus: Option, - #[clap(long, help = "Enable console output to terminal for debugging")] + #[clap( + long, + help = "Connect the QEMU console to the container's stdio (visible via podman logs/attach)" + )] pub console: bool, #[clap( @@ -628,7 +631,7 @@ fn prepare_run_command_with_temp( } let entrypoint = opts.debug_entrypoint.as_deref().unwrap_or(ENTRYPOINT); - cmd.args([&opts.image, entrypoint]); + cmd.args(["--", &opts.image, entrypoint]); Ok((cmd, td)) } diff --git a/crates/kit/src/run_ephemeral_ssh.rs b/crates/kit/src/run_ephemeral_ssh.rs index 21c8e34b..b0e54cd1 100644 --- a/crates/kit/src/run_ephemeral_ssh.rs +++ b/crates/kit/src/run_ephemeral_ssh.rs @@ -33,7 +33,7 @@ fn show_container_logs(container_name: &str) { // Get container state in a single inspect call let state = Command::new("podman") - .args(["inspect", container_name]) + .args(["inspect", "--", container_name]) .output() .ok() .and_then(|output| { @@ -70,7 +70,7 @@ fn show_container_logs(container_name: &str) { } let output = match Command::new("podman") - .args(["logs", container_name]) + .args(["logs", "--", container_name]) .stderr(Stdio::inherit()) .output() { @@ -110,7 +110,7 @@ impl Drop for ContainerCleanup { fn drop(&mut self) { debug!("Cleaning up ephemeral container {}", self.container_id); let result = Command::new("podman") - .args(["rm", "-f", &self.container_id]) + .args(["rm", "-f", "--", &self.container_id]) .stdout(Stdio::null()) .stderr(Stdio::null()) .status(); @@ -137,7 +137,13 @@ pub struct RunEphemeralSshOpts { /// Check if container is running fn is_container_running(container_name: &str) -> Result { let output = Command::new("podman") - .args(["inspect", container_name, "--format", "{{.State.Status}}"]) + .args([ + "inspect", + "--format", + "{{.State.Status}}", + "--", + container_name, + ]) .output() .context("Failed to inspect container state")?; @@ -173,6 +179,7 @@ pub fn wait_for_vm_ssh( let mut cmd = Command::new("podman"); cmd.args([ "exec", + "--", container_name, "/var/lib/bcvk/entrypoint", "monitor-status", diff --git a/crates/kit/src/ssh.rs b/crates/kit/src/ssh.rs index 49177e73..2cdbbda7 100644 --- a/crates/kit/src/ssh.rs +++ b/crates/kit/src/ssh.rs @@ -80,6 +80,10 @@ pub fn generate_ssh_keypair(output_dir: &Utf8Path, key_name: &str) -> Result Result { generate_ssh_keypair(Utf8Path::new(CONTAINER_STATEDIR), "ssh") } @@ -109,75 +113,108 @@ pub fn generate_default_keypair() -> Result { /// let args = vec!["systemctl".to_string(), "status".to_string()]; /// connect("bootc-vm-abc123", args, &SshConnectionOptions::default())?; /// ``` -pub fn connect( +/// Build the `podman exec ... ssh ...` command for a container. +/// +/// Shared between [`connect`] (interactive/passthrough) and +/// [`connect_captured`] (output capture for IPC). +fn build_podman_ssh_command( container_name: &str, - args: Vec, + args: &[String], options: &SshConnectionOptions, -) -> Result { - debug!("Connecting to VM via container: {}", container_name); - - // Verify container exists and is running - verify_container_running(container_name)?; - - // Build podman exec command +) -> Result { let mut cmd = Command::new("podman"); if options.allocate_tty { - cmd.args(["exec", "-it", container_name, "ssh"]); + cmd.args(["exec", "-it", "--", container_name, "ssh"]); } else { - cmd.args(["exec", container_name, "ssh"]); + cmd.args(["exec", "--", container_name, "ssh"]); } - // SSH key path (hardcoded for container environment) let keypath = Utf8Path::new("/run/tmproot") .join(CONTAINER_STATEDIR.trim_start_matches('/')) .join("ssh"); cmd.args(["-i", keypath.as_str()]); - // Apply common SSH options options.common.apply_to_command(&mut cmd); - - // No prompts from SSH cmd.args(["-o", "BatchMode=yes"]); - // Even if we're providing a remote command, always allocate a tty - // so progress bars work because we're running synchronously. if options.allocate_tty { cmd.arg("-t"); } - // Connect to VM via QEMU port forwarding on localhost cmd.arg("root@127.0.0.1"); cmd.args(["-p", "2222"]); - // Add any additional arguments - let ssh_args = build_ssh_command(&args)?; + let ssh_args = build_ssh_command(args)?; if !ssh_args.is_empty() { debug!("Adding SSH arguments: {:?}", ssh_args); cmd.args(&ssh_args); } - debug!("Executing: podman {:?}", cmd.get_args().collect::>()); - debug!( - "Full command line: podman {}", - cmd.get_args() - .map(|s| s.to_string_lossy().to_string()) - .collect::>() - .join(" ") - ); + Ok(cmd) +} + +pub fn connect( + container_name: &str, + args: Vec, + options: &SshConnectionOptions, +) -> Result { + debug!("Connecting to VM via container: {}", container_name); + + verify_container_running(container_name)?; + + let mut cmd = build_podman_ssh_command(container_name, &args, options)?; - // Suppress output if requested (useful for connectivity testing) if options.suppress_output { cmd.stdout(Stdio::null()).stderr(Stdio::null()); } else { - // Explicitly inherit stdout/stderr to prevent them from being closed cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit()); } - // Execute the command and return status cmd.status() .map_err(|e| eyre!("Failed to execute SSH command: {}", e)) } +/// Result of running an SSH command with captured output. +#[derive(Debug)] +#[allow(dead_code)] +pub struct CapturedSshOutput { + /// Process exit code (-1 if terminated by signal) + pub exit_code: i32, + /// Captured standard output + pub stdout: String, + /// Captured standard error + pub stderr: String, +} + +/// Execute an SSH command inside a container, capturing stdout and stderr. +/// +/// Like [`connect`] but returns the output instead of passing it through +/// to the terminal. Intended for programmatic/IPC use. +#[allow(dead_code)] +pub fn connect_captured(container_name: &str, args: Vec) -> Result { + debug!("Executing captured SSH in container: {}", container_name); + + verify_container_running(container_name)?; + + let options = SshConnectionOptions { + allocate_tty: false, + suppress_output: false, + common: CommonSshOptions::default(), + }; + let mut cmd = build_podman_ssh_command(container_name, &args, &options)?; + cmd.stdout(Stdio::piped()).stderr(Stdio::piped()); + + let output = cmd + .output() + .map_err(|e| eyre!("Failed to execute SSH command: {}", e))?; + + Ok(CapturedSshOutput { + exit_code: output.status.code().unwrap_or(-1), + stdout: String::from_utf8_lossy(&output.stdout).into_owned(), + stderr: String::from_utf8_lossy(&output.stderr).into_owned(), + }) +} + /// Convenience function for connecting with error handling (non-zero exit = error) pub fn connect_via_container(container_name: &str, args: Vec) -> Result<()> { let status = connect(container_name, args, &SshConnectionOptions::default())?; @@ -288,7 +325,13 @@ impl SshConnectionOptions { /// Verify that a container exists and is running fn verify_container_running(container_name: &str) -> Result<()> { let status = Command::new("podman") - .args(["inspect", container_name, "--format", "{{.State.Status}}"]) + .args([ + "inspect", + "--format", + "{{.State.Status}}", + "--", + container_name, + ]) .output() .map_err(|e| eyre!("Failed to check container status: {}", e))?; diff --git a/crates/kit/src/to_disk.rs b/crates/kit/src/to_disk.rs index 33949f73..c5a9cd76 100644 --- a/crates/kit/src/to_disk.rs +++ b/crates/kit/src/to_disk.rs @@ -364,11 +364,25 @@ EOF } } +/// Outcome of a `run()` call, indicating whether a disk was created fresh +/// or reused from cache. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RunOutcome { + /// A new disk image was created (or an existing one was regenerated). + Created, + /// An existing cached disk image was reused without reinstalling. + Cached, + /// Dry-run mode: the disk would have been reused. + DryRunWouldReuse, + /// Dry-run mode: the disk would have been regenerated. + DryRunWouldRegenerate, +} + /// Execute a bootc installation using an ephemeral VM with SSH /// /// Main entry point for the bootc installation process. See module-level documentation /// for details on the installation workflow and architecture. -pub fn run(opts: ToDiskOpts) -> Result<()> { +pub fn run(opts: ToDiskOpts) -> Result { // Phase 0: Check for existing cached disk image let would_reuse = if opts.target_disk.exists() { debug!( @@ -389,14 +403,9 @@ pub fn run(opts: ToDiskOpts) -> Result<()> { )? { Ok(()) => { if opts.additional.dry_run { - println!("would-reuse"); - return Ok(()); + return Ok(RunOutcome::DryRunWouldReuse); } - println!( - "Reusing existing cached disk image (digest {image_digest}) at: {}", - opts.target_disk - ); - return Ok(()); + return Ok(RunOutcome::Cached); } Err(e) => { debug!("Existing disk does not match requirements, recreating: {e}"); @@ -413,14 +422,13 @@ pub fn run(opts: ToDiskOpts) -> Result<()> { false }; - // In dry-run mode, report whether we would regenerate + // In dry-run mode, report what would happen without doing it if opts.additional.dry_run { - if would_reuse { - println!("would-reuse"); + return if would_reuse { + Ok(RunOutcome::DryRunWouldReuse) } else { - println!("would-regenerate"); - } - return Ok(()); + Ok(RunOutcome::DryRunWouldRegenerate) + }; } // Phase 1: Validation and preparation @@ -532,7 +540,7 @@ pub fn run(opts: ToDiskOpts) -> Result<()> { let progress_bar = crate::boot_progress::create_boot_progress_bar(); let (duration, progress_bar) = wait_for_ssh_ready(&container_id, None, progress_bar)?; progress_bar.finish_and_clear(); - println!( + tracing::info!( "Connected ({} elapsed), beginning installation...", HumanDuration(duration) ); @@ -560,7 +568,7 @@ pub fn run(opts: ToDiskOpts) -> Result<()> { // Cleanup: stop and remove the container debug!("Cleaning up ephemeral container..."); let _ = std::process::Command::new("podman") - .args(["rm", "-f", &container_id]) + .args(["rm", "-f", "--", &container_id]) .output(); // Handle the result - remove disk file on failure @@ -578,7 +586,7 @@ pub fn run(opts: ToDiskOpts) -> Result<()> { debug!("Failed to write metadata to disk image: {}", e); // Don't fail the operation just because metadata couldn't be written } - Ok(()) + Ok(RunOutcome::Created) } Err(e) => { let _ = std::fs::remove_file(&opts.target_disk); diff --git a/crates/kit/src/varlink_ipc.rs b/crates/kit/src/varlink_ipc.rs new file mode 100644 index 00000000..f28af8bb --- /dev/null +++ b/crates/kit/src/varlink_ipc.rs @@ -0,0 +1,546 @@ +//! Varlink IPC interface for bcvk using the zlink crate. +//! +//! Exposes image listing, ephemeral VM launching, and disk image creation +//! over a Unix domain socket using the Varlink protocol. Three interfaces +//! are provided: +//! +//! - `io.bootc.vk.images` -- list bootc container image names +//! - `io.bootc.vk.ephemeral` -- list and launch ephemeral VM containers +//! - `io.bootc.vk.todisk` -- create bootable disk images from container images +//! +//! The API is intentionally minimal: it exposes only the operations that +//! require bcvk-specific knowledge (filtering by label, constructing the +//! right `podman run` invocation, orchestrating `bootc install`). Everything +//! else -- inspecting images, removing containers, executing commands -- is +//! left to `podman` directly. +//! +//! The server supports two activation modes: +//! - Direct listen: binds a Unix socket at the given `unix:` address +//! - Socket activation: when `LISTEN_FDS` is set (e.g. via `varlinkctl exec:`), +//! serves on the inherited fd 3 + +use serde::{Deserialize, Serialize}; + +// --------------------------------------------------------------------------- +// Reply wrapper types (varlink methods return named parameters) +// --------------------------------------------------------------------------- + +/// Reply for the images `List` method. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub(crate) struct ListReply { + /// Image names/tags that have `containers.bootc=1`. + /// Each entry is a full image reference (e.g. `quay.io/centos-bootc/centos-bootc:stream9`). + /// Dangling images (no tags) are omitted. + images: Vec, +} + +/// Reply for the ephemeral `Ps` method. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub(crate) struct PsReply { + /// Container IDs of running ephemeral VMs (label `bcvk.ephemeral=1`). + /// Use `podman inspect` for further details. + container_ids: Vec, +} + +/// Optional configuration for launching an ephemeral VM. +/// +/// All fields are optional and default to sensible values. +/// The VM always runs detached (no TTY/interactive mode). +#[derive(Debug, Clone, Serialize, Deserialize, Default, zlink::introspect::Type)] +pub(crate) struct EphemeralRunOpts { + /// Generate SSH keypair and inject into the VM. + ssh_keygen: Option, + /// Assign a name to the container. + name: Option, + /// Metadata labels in `key=value` form. + label: Option>, + /// Podman network configuration. + network: Option, + /// Environment variables in `key=value` form. + env: Option>, + /// Automatically remove container when it exits. + rm: Option, + /// Instance type (e.g. `"u1.small"`, `"u1.medium"`). + itype: Option, + /// Memory size (e.g. `"4G"`, `"2048M"`); overridden by `itype`. + memory: Option, + /// Number of vCPUs; overridden by `itype`. + vcpus: Option, + /// Connect QEMU console to container stdio (visible via `podman logs`/`attach`). + console: Option, + /// Read-write host bind mounts (`HOST_PATH[:NAME]`). + bind: Option>, + /// Read-only host bind mounts (`HOST_PATH[:NAME]`). + ro_bind: Option>, + /// Disk files as virtio-blk devices (`FILE[:NAME]`). + mount_disk_files: Option>, + /// Additional kernel command line arguments. + kargs: Option>, + /// Allocate swap of the given size (e.g. `"1G"`). + add_swap: Option, +} + +/// Reply for the ephemeral `Run` method. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub(crate) struct RunReply { + /// Container ID of the launched ephemeral VM. + container_id: String, +} + +/// Reply for the ephemeral `GetSshConnectionInfo` method. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub(crate) struct GetSshConnectionInfoReply { + /// Container ID to use with `podman exec`. + container_id: String, + /// Path to the SSH private key *inside* the container. + key_path: String, + /// SSH user to connect as. + user: String, + /// SSH host (inside the container). + host: String, + /// SSH port. + port: u16, +} + +/// Reply for the `io.bootc.vk.todisk` `ToDisk` method. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub(crate) struct ToDiskReply { + /// Absolute path to the created (or reused) disk image. + path: String, + /// Whether an existing cached disk image was reused. + cached: bool, +} + +// --------------------------------------------------------------------------- +// Error types +// --------------------------------------------------------------------------- + +/// Errors returned by the images interface. +#[derive(Debug, zlink::ReplyError, zlink::introspect::ReplyError)] +#[zlink(interface = "io.bootc.vk.images")] +enum ImagesError { + /// An error occurred when calling podman. + PodmanError { + /// Human-readable error description. + message: String, + }, +} + +/// Errors returned by the ephemeral interface. +#[derive(Debug, zlink::ReplyError, zlink::introspect::ReplyError)] +#[zlink(interface = "io.bootc.vk.ephemeral")] +enum EphemeralError { + /// An error occurred when calling podman. + PodmanError { + /// Human-readable error description. + message: String, + }, +} + +/// Errors returned by the todisk interface. +#[derive(Debug, zlink::ReplyError, zlink::introspect::ReplyError)] +#[zlink(interface = "io.bootc.vk.todisk")] +enum ToDiskError { + /// The disk image creation failed. + Failed { + /// Human-readable error description. + message: String, + }, +} + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +/// Map a `JoinError` (from `spawn_blocking`) to an `EphemeralError`. +fn ephemeral_join_err(e: tokio::task::JoinError) -> EphemeralError { + EphemeralError::PodmanError { + message: e.to_string(), + } +} + +// --------------------------------------------------------------------------- +// Service implementation +// --------------------------------------------------------------------------- + +/// Combined varlink service exposing images, ephemeral, and todisk interfaces. +#[derive(Debug)] +struct BcvkService; + +/// Version of the varlink API itself (independent of the crate version). +/// Only referenced by the unit test that guards against attribute drift. +#[cfg(test)] +const VARLINK_API_VERSION: &str = "0.1.0"; + +#[zlink::service( + interface = "io.bootc.vk.images", + vendor = "io.bootc.vk", + product = "bcvk", + version = "0.1.0", + url = "https://github.com/bootc-dev/bcvk" +)] +impl BcvkService { + /// List bootc image names (those with `containers.bootc=1`). + async fn list(&self) -> Result { + let entries = tokio::task::spawn_blocking(crate::images::list) + .await + .map_err(|e| ImagesError::PodmanError { + message: e.to_string(), + })? + .map_err(|e| ImagesError::PodmanError { + message: e.to_string(), + })?; + + let images = entries + .into_iter() + .filter_map(|img| img.names) + .flatten() + .collect(); + + Ok(ListReply { images }) + } + + /// List ephemeral VM container IDs (those with `bcvk.ephemeral=1`). + /// + /// Returns only the container IDs; use `podman inspect ` for + /// further details like state, image, or creation time. + #[zlink(interface = "io.bootc.vk.ephemeral")] + async fn ps(&self) -> Result { + let containers = tokio::task::spawn_blocking(crate::ephemeral::list_ephemeral_containers) + .await + .map_err(ephemeral_join_err)? + .map_err(|e| EphemeralError::PodmanError { + message: e.to_string(), + })?; + + let container_ids = containers.into_iter().map(|c| c.id).collect(); + Ok(PsReply { container_ids }) + } + + /// Launch an ephemeral VM in detached mode. + /// + /// Always runs detached. The returned container ID can be used with + /// `podman inspect`, `podman rm -f`, or passed to `GetSshConnectionInfo`. + /// See [`EphemeralRunOpts`] for additional options. + #[zlink(interface = "io.bootc.vk.ephemeral")] + async fn run( + &self, + image: String, + opts: Option, + ) -> Result { + let opts = opts.unwrap_or_default(); + let container_id = tokio::task::spawn_blocking(move || { + use crate::run_ephemeral::{CommonPodmanOptions, CommonVmOpts, RunEphemeralOpts}; + use std::str::FromStr; + + let itype = opts + .itype + .map(|s| { + crate::instancetypes::InstanceType::from_str(&s).map_err(|_| { + color_eyre::eyre::eyre!( + "unknown instance type: {s:?}, try e.g. \"u1.small\"" + ) + }) + }) + .transpose()?; + + let run_opts = RunEphemeralOpts { + image, + common: CommonVmOpts { + itype, + memory: crate::common_opts::MemoryOpts { + memory: opts + .memory + .unwrap_or_else(|| crate::common_opts::DEFAULT_MEMORY_USER_STR.into()), + }, + vcpus: opts.vcpus, + console: opts.console.unwrap_or(false), + ssh_keygen: opts.ssh_keygen.unwrap_or(false), + ..Default::default() + }, + podman: CommonPodmanOptions { + detach: true, + name: opts.name, + label: opts.label.unwrap_or_default(), + network: opts.network, + env: opts.env.unwrap_or_default(), + rm: opts.rm.unwrap_or(false), + ..Default::default() + }, + debug_entrypoint: None, + bind_mounts: opts.bind.unwrap_or_default(), + ro_bind_mounts: opts.ro_bind.unwrap_or_default(), + systemd_units_dir: None, + bind_storage_ro: false, + add_swap: opts.add_swap, + mount_disk_files: opts.mount_disk_files.unwrap_or_default(), + kernel_args: opts.kargs.unwrap_or_default(), + host_dns_servers: None, + }; + + crate::run_ephemeral::run_detached(run_opts) + }) + .await + .map_err(ephemeral_join_err)? + .map_err(|e| EphemeralError::PodmanError { + message: e.to_string(), + })?; + + Ok(RunReply { container_id }) + } + + /// Return the SSH connection details for an ephemeral VM. + /// + /// Given a container ID (from `Run` or `Ps`), returns the information + /// needed to connect via SSH. The caller can then construct: + /// + /// ```text + /// podman exec ssh -i -p \ + /// -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ + /// @ [command...] + /// ``` + /// + /// This does not verify that SSH is actually ready -- the caller + /// should retry on connection failure. + #[zlink(interface = "io.bootc.vk.ephemeral")] + async fn get_ssh_connection_info( + &self, + container_id: String, + ) -> Result { + // These are fixed conventions in bcvk's SSH setup. + // The key path is inside the container (under the tmproot bind mount). + let key_path = format!("/run/tmproot{}/ssh", crate::CONTAINER_STATEDIR); + + Ok(GetSshConnectionInfoReply { + container_id, + key_path, + user: "root".to_string(), + host: "127.0.0.1".to_string(), + port: 2222, + }) + } + + /// Create a bootable disk image from a container image. + /// + /// This is a long-running operation that orchestrates disk creation, + /// ephemeral VM launch, and `bootc install`. Supports caching: if a + /// disk already exists at `target_disk` with matching metadata, it is + /// reused without reinstalling. + /// + /// Parameters: + /// - `source_image`: container image reference to install + /// - `target_disk`: absolute path for the output disk image + /// - `format`: disk format, either `"raw"` (default) or `"qcow2"` + /// - `disk_size`: optional size string (e.g. `"10G"`, `"5120M"`) + /// - `filesystem`: optional root filesystem type (e.g. `"ext4"`, `"xfs"`) + /// - `root_size`: optional root partition size (e.g. `"8G"`) + /// - `kargs`: optional kernel arguments + #[zlink(interface = "io.bootc.vk.todisk")] + async fn to_disk( + &self, + source_image: String, + target_disk: String, + format: Option, + disk_size: Option, + filesystem: Option, + root_size: Option, + kargs: Option>, + ) -> Result { + let result = tokio::task::spawn_blocking(move || { + use camino::Utf8PathBuf; + + let format = match format.as_deref() { + None | Some("raw") => crate::to_disk::Format::Raw, + Some("qcow2") => crate::to_disk::Format::Qcow2, + Some(other) => { + return Err(color_eyre::eyre::eyre!( + "unsupported disk format: {other:?}, expected \"raw\" or \"qcow2\"" + )); + } + }; + + let opts = crate::to_disk::ToDiskOpts { + source_image, + target_disk: Utf8PathBuf::from(&target_disk), + install: crate::install_options::InstallOptions { + filesystem, + root_size, + karg: kargs.unwrap_or_default(), + ..Default::default() + }, + additional: crate::to_disk::ToDiskAdditionalOpts { + disk_size, + format, + ..Default::default() + }, + }; + + let outcome = crate::to_disk::run(opts)?; + let cached = outcome == crate::to_disk::RunOutcome::Cached; + + Ok::<_, color_eyre::Report>((target_disk, cached)) + }) + .await + .map_err(|e| ToDiskError::Failed { + message: e.to_string(), + })? + .map_err(|e| ToDiskError::Failed { + message: format!("{e:#}"), + })?; + + let (path, cached) = result; + Ok(ToDiskReply { path, cached }) + } +} + +// --------------------------------------------------------------------------- +// Client-side proxy traits (for future programmatic use) +// --------------------------------------------------------------------------- + +/// Proxy for calling image management methods on a remote bcvk service. +#[allow(dead_code)] +#[zlink::proxy("io.bootc.vk.images")] +trait ImagesProxy { + /// List bootc image names. + async fn list(&mut self) -> zlink::Result>; +} + +/// Proxy for calling ephemeral container methods on a remote bcvk service. +#[allow(dead_code)] +#[zlink::proxy("io.bootc.vk.ephemeral")] +trait EphemeralProxy { + /// List ephemeral VM container IDs. + async fn ps(&mut self) -> zlink::Result>; + + /// Launch an ephemeral VM in detached mode. + async fn run( + &mut self, + image: String, + opts: Option, + ) -> zlink::Result>; + + /// Get SSH connection info for an ephemeral VM. + async fn get_ssh_connection_info( + &mut self, + container_id: String, + ) -> zlink::Result>; +} + +/// Proxy for calling todisk methods on a remote bcvk service. +#[allow(dead_code)] +#[zlink::proxy("io.bootc.vk.todisk")] +trait ToDiskProxy { + /// Create a bootable disk image from a container image. + async fn to_disk( + &mut self, + source_image: String, + target_disk: String, + format: Option, + disk_size: Option, + filesystem: Option, + root_size: Option, + kargs: Option>, + ) -> zlink::Result>; +} + +// --------------------------------------------------------------------------- +// Socket activation +// --------------------------------------------------------------------------- + +/// A `Listener` that yields a single pre-connected socket, then blocks forever. +/// +/// Used for `varlinkctl exec:` activation where a connected socket pair is +/// passed on fd 3. After the first `accept()` returns the connection, subsequent +/// calls pend indefinitely (the server will be killed by the parent process once +/// the connection closes). +#[derive(Debug)] +struct ActivatedListener { + /// The connection to yield on the first accept(), consumed after use. + conn: Option>, +} + +impl zlink::Listener for ActivatedListener { + type Socket = zlink::unix::Stream; + + async fn accept(&mut self) -> zlink::Result> { + match self.conn.take() { + Some(conn) => Ok(conn), + None => std::future::pending().await, + } + } +} + +/// Try to build an [`ActivatedListener`] from a socket-activated fd. +/// +/// Uses `libsystemd` to receive file descriptors passed by the service +/// manager (checks `LISTEN_FDS`/`LISTEN_PID` and clears the env vars). +/// Returns `None` when the process was not socket-activated. +#[allow(unsafe_code)] +fn try_activated_listener() -> color_eyre::Result> { + use std::os::fd::{FromRawFd as _, IntoRawFd as _}; + + let fds = libsystemd::activation::receive_descriptors(true) + .map_err(|e| color_eyre::eyre::eyre!("Failed to receive activation fds: {e}"))?; + + let fd = match fds.into_iter().next() { + Some(fd) => fd, + None => return Ok(None), + }; + + // TODO: propose From for OwnedFd upstream so this + // unsafe can be removed: https://github.com/lucab/libsystemd-rs + // + // SAFETY: `libsystemd::activation::receive_descriptors(true)` validated + // the fd and transferred ownership. `into_raw_fd()` consumes the + // `FileDescriptor` wrapper, giving us sole ownership of a valid fd. + let std_stream = unsafe { std::os::unix::net::UnixStream::from_raw_fd(fd.into_raw_fd()) }; + std_stream.set_nonblocking(true)?; + let tokio_stream = tokio::net::UnixStream::from_std(std_stream)?; + let zlink_stream = zlink::unix::Stream::from(tokio_stream); + let conn = zlink::Connection::from(zlink_stream); + Ok(Some(ActivatedListener { conn: Some(conn) })) +} + +// --------------------------------------------------------------------------- +// Varlink auto-activation +// --------------------------------------------------------------------------- + +/// If the process was socket-activated, serve varlink and return `true`. +/// +/// This follows the systemd pattern used by `systemd-creds` and similar +/// tools: if the process was invoked with an activated socket (e.g. via +/// `varlinkctl exec:`), it serves varlink on that socket and returns +/// `true` so the caller can exit. Otherwise returns `false` and the +/// process continues with normal CLI handling. +pub(crate) async fn try_serve_varlink() -> color_eyre::Result { + let listener = match try_activated_listener()? { + Some(l) => l, + None => return Ok(false), + }; + + tracing::debug!("Socket activation detected, serving varlink"); + let server = zlink::Server::new(listener, BcvkService); + tokio::select! { + result = server.run() => result?, + _ = tokio::signal::ctrl_c() => { + tracing::debug!("Shutting down (activated)"); + } + } + Ok(true) +} + +#[cfg(test)] +mod tests { + use super::VARLINK_API_VERSION; + + #[test] + fn varlink_version_is_consistent() { + // The version in the #[zlink::service] attribute must match + // VARLINK_API_VERSION. Unfortunately zlink doesn't let us use a + // const in attribute position, so this test catches drift. + assert_eq!( + VARLINK_API_VERSION, "0.1.0", + "VARLINK_API_VERSION must match the #[zlink::service] version attribute" + ); + } +} diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 8a81e775..7b5b317e 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -5,6 +5,7 @@ - [Installation](./installation.md) - [Quick Start](./quick-start.md) - [Workflow Comparison](./workflow-comparison.md) +- [Varlink IPC](./varlink.md) # Reference diff --git a/docs/src/man/bcvk-ephemeral-run-ssh.md b/docs/src/man/bcvk-ephemeral-run-ssh.md index c6f1eb8b..745e83c5 100644 --- a/docs/src/man/bcvk-ephemeral-run-ssh.md +++ b/docs/src/man/bcvk-ephemeral-run-ssh.md @@ -64,7 +64,7 @@ For longer-running VMs where you need to reconnect multiple times, use **--console** - Enable console output to terminal for debugging + Connect the QEMU console to the container's stdio (visible via podman logs/attach) **--debug** diff --git a/docs/src/man/bcvk-ephemeral-run.md b/docs/src/man/bcvk-ephemeral-run.md index b3d26e8e..71c02f94 100644 --- a/docs/src/man/bcvk-ephemeral-run.md +++ b/docs/src/man/bcvk-ephemeral-run.md @@ -66,7 +66,7 @@ This design allows bcvk to provide VM-like isolation and boot behavior while lev **--console** - Enable console output to terminal for debugging + Connect the QEMU console to the container's stdio (visible via podman logs/attach) **--debug** diff --git a/docs/src/man/bcvk-libvirt-run.md b/docs/src/man/bcvk-libvirt-run.md index 47787750..ca6096cc 100644 --- a/docs/src/man/bcvk-libvirt-run.md +++ b/docs/src/man/bcvk-libvirt-run.md @@ -73,6 +73,14 @@ Run a bootable container as a persistent VM Default to composefs-native storage +**--bootloader**=*BOOTLOADER* + + Which bootloader to use for composefs-native backend + +**--allow-missing-fsverity** + + Allow installation without fs-verity support for composefs-native backend + **-p**, **--port**=*PORT_MAPPINGS* Port mapping from host to VM (format: host_port:guest_port, e.g., 8080:80) diff --git a/docs/src/man/bcvk-libvirt-ssh.md b/docs/src/man/bcvk-libvirt-ssh.md index cdcb6aff..f5209d4d 100644 --- a/docs/src/man/bcvk-libvirt-ssh.md +++ b/docs/src/man/bcvk-libvirt-ssh.md @@ -37,7 +37,7 @@ SSH to libvirt domain with embedded SSH key SSH connection timeout in seconds - Default: 30 + Default: 5 **--log-level**=*LOG_LEVEL* diff --git a/docs/src/man/bcvk-libvirt-upload.md b/docs/src/man/bcvk-libvirt-upload.md index 328721a5..07743c6b 100644 --- a/docs/src/man/bcvk-libvirt-upload.md +++ b/docs/src/man/bcvk-libvirt-upload.md @@ -57,6 +57,14 @@ Upload bootc disk images to libvirt with metadata annotations Default to composefs-native storage +**--bootloader**=*BOOTLOADER* + + Which bootloader to use for composefs-native backend + +**--allow-missing-fsverity** + + Allow installation without fs-verity support for composefs-native backend + **--memory**=*MEMORY* Memory size (e.g. 4G, 2048M, or plain number for MB) diff --git a/docs/src/man/bcvk-to-disk.md b/docs/src/man/bcvk-to-disk.md index e73fd050..4e2d8ec7 100644 --- a/docs/src/man/bcvk-to-disk.md +++ b/docs/src/man/bcvk-to-disk.md @@ -59,6 +59,14 @@ The installation process: Default to composefs-native storage +**--bootloader**=*BOOTLOADER* + + Which bootloader to use for composefs-native backend + +**--allow-missing-fsverity** + + Allow installation without fs-verity support for composefs-native backend + **--disk-size**=*DISK_SIZE* Disk size to create (e.g. 10G, 5120M, or plain number for bytes) @@ -89,7 +97,7 @@ The installation process: **--console** - Enable console output to terminal for debugging + Connect the QEMU console to the container's stdio (visible via podman logs/attach) **--debug** diff --git a/docs/src/varlink.md b/docs/src/varlink.md new file mode 100644 index 00000000..b48988b5 --- /dev/null +++ b/docs/src/varlink.md @@ -0,0 +1,56 @@ +# Using bcvk via varlink + +bcvk exposes a [varlink](https://varlink.org/) interface for programmatic access. +This is useful for building tooling on top of bcvk without parsing CLI output. + +At the current time there are varlink APIs for: + +- `bcvk images` -- list bootc image names +- `bcvk ephemeral` -- launch and query ephemeral VMs, get SSH connection info +- `bcvk to-disk` -- create bootable disk images + +The API is intentionally minimal: it exposes only operations that require +bcvk-specific knowledge. For example, `bcvk ephemeral rm-all` is not exposed +via varlink because you can do that directly via podman APIs. + +## Via subprocess + +bcvk serves varlink via [socket activation](https://varlink.org/#activation). +The idea is your higher level tool runs it as a subprocess, passing +the socket FD to it. + +An example of this with `varlinkctl exec:`: + +```bash +varlinkctl call exec:bcvk io.bootc.vk.images.List +``` + +## Introspecting + +The varlink API is defined in the source code; to see the version of +the API exposed by the tool, use `varlinkctl introspect`: + +```bash +varlinkctl introspect exec:bcvk io.bootc.vk.images +varlinkctl introspect exec:bcvk io.bootc.vk.ephemeral +varlinkctl introspect exec:bcvk io.bootc.vk.todisk +``` + +## SSH access to ephemeral VMs + +After launching a VM with `Run(ssh_keygen: true)`, use `GetSshConnectionInfo` +to get the connection details: + +```bash +varlinkctl call exec:bcvk io.bootc.vk.ephemeral.GetSshConnectionInfo \ + '{"container_id": "a1b2c3d4..."}' +``` + +This returns the container ID, key path, user, host, and port needed +to construct a `podman exec ... ssh ...` command: + +```bash +podman exec ssh -i -p \ + -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ + @ [command...] +```