From 5a80802d3998bb1a08b4524af90d62e6b19446b1 Mon Sep 17 00:00:00 2001 From: Willie Abrams Date: Tue, 10 Nov 2026 13:59:50 -0600 Subject: [PATCH] cmdline: add --reset-exit-code A guest reset and a guest power-off both end krunkit with exit code 0, so the caller cannot tell a reboot from a shutdown and start the VM again. libkrun 1.19.8 has krun_set_reset_exit_code for this (libkrun/libkrun#986). --reset-exit-code passes the code on; without the option krunkit doesn't make the call and nothing changes. Signed-off-by: Willie Abrams Assisted-by: LLM --- docs/usage.md | 5 +++++ src/cmdline.rs | 9 +++++++++ src/context.rs | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/docs/usage.md b/docs/usage.md index a2835c3..7260b9f 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -25,6 +25,11 @@ The URI (address) of the RESTful service. If not specified, defaults to `none:// Specify a path in which krunkit will write the PID to. The option does not provide any form of locking. +- `--reset-exit-code` + +Exit with this code when the guest resets, so the caller can tell a reboot from a power-off and start the virtual +machine again. Without the option, krunkit exits with 0 in both cases. Requires libkrun 1.19.8 or newer. + - `--timesync` Specify a Vsock port on which the host will send the current time to diff --git a/src/cmdline.rs b/src/cmdline.rs index a08f3f1..8a90825 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -53,6 +53,11 @@ pub struct Args { #[arg(long, short)] pub nested: bool, + /// Exit with this code when the guest resets, so the caller can tell a reboot from a + /// power-off and start the VM again. Without it both end krunkit with exit code 0. + #[arg(long = "reset-exit-code")] + pub reset_exit_code: Option, + /// Specify a pidfile path. #[arg(long)] pub pidfile: Option, @@ -533,6 +538,8 @@ mod tests { "5", "--pidfile", "/tmp/krunkit.pid", + "--reset-exit-code", + "32", ]; let mut args = Args::try_parse_from(cmdline).unwrap(); @@ -540,6 +547,8 @@ mod tests { let pidfile = args.pidfile.expect("pidfile argument not found"); assert_eq!(pidfile.to_str().unwrap(), "/tmp/krunkit.pid"); + assert_eq!(args.reset_exit_code, Some(32)); + let net = args .devices .pop() diff --git a/src/context.rs b/src/context.rs index eed1a6e..bde10e8 100644 --- a/src/context.rs +++ b/src/context.rs @@ -31,6 +31,7 @@ extern "C" { fn krun_set_smbios_oem_strings(ctx_id: u32, oem_strings: *const *const c_char) -> i32; fn krun_set_nested_virt(ctx_id: u32, enabled: bool) -> i32; fn krun_check_nested_virt() -> i32; + fn krun_set_reset_exit_code(ctx_id: u32, code: u8) -> i32; fn krun_start_enter(ctx_id: u32) -> i32; } @@ -187,6 +188,12 @@ impl TryFrom for KrunContext { set_smbios_oem_strings(id, &args.oem_strings)?; + if let Some(code) = args.reset_exit_code { + if unsafe { krun_set_reset_exit_code(id, code) } < 0 { + return Err(anyhow!("unable to set the reset exit code")); + } + } + if args.nested { match unsafe { krun_check_nested_virt() } { 1 => {