supermachine 0.7.108

Run any OCI/Docker image as a hardware-isolated microVM on macOS HVF (Linux KVM and Windows WHP in progress). Single library API, zero flags for the common case, sub-100 ms cold-restore from snapshot.
Documentation
//! Restore + first-exec bounded probe for KVM snapshots.
//!
//! Env:
//!   SUPERMACHINE_RESTORE_FIRST_EXEC_SNAPSHOT  full snapshot dir to restore
//!   SUPERMACHINE_RESTORE_FIRST_EXEC_CMD       shell command for the first exec (default: uname -r)
//!   SUPERMACHINE_RESTORE_FIRST_EXEC_TIMEOUT_SECS  exec timeout (default: 30)

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    use std::time::{Duration, Instant};
    use supermachine::{Image, VmConfig};

    let snap = std::env::var("SUPERMACHINE_RESTORE_FIRST_EXEC_SNAPSHOT")
        .expect("set SUPERMACHINE_RESTORE_FIRST_EXEC_SNAPSHOT to a full snapshot dir");
    let cmd = std::env::var("SUPERMACHINE_RESTORE_FIRST_EXEC_CMD")
        .unwrap_or_else(|_| "uname -r".to_owned());
    let timeout_secs = std::env::var("SUPERMACHINE_RESTORE_FIRST_EXEC_TIMEOUT_SECS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(30);

    eprintln!(
        "RESTORE_FIRST_EXEC_PROBE start snapshot={snap} cmd={cmd:?} timeout_secs={timeout_secs}"
    );
    let t0 = Instant::now();
    let image = Image::from_snapshot(&snap)?;
    let vm = image.start(&VmConfig::new())?;
    eprintln!(
        "RESTORE_FIRST_EXEC_PROBE restored_ms={}",
        t0.elapsed().as_millis()
    );

    let exec_t0 = Instant::now();
    let out = vm
        .exec_builder()
        .argv(["/bin/sh", "-c", &cmd])
        .timeout(Duration::from_secs(timeout_secs))
        .output()?;
    eprintln!(
        "RESTORE_FIRST_EXEC_PROBE first_exec_ms={} status={} stdout={} stderr={}",
        exec_t0.elapsed().as_millis(),
        out.status,
        String::from_utf8_lossy(&out.stdout).trim(),
        String::from_utf8_lossy(&out.stderr).trim()
    );
    if !out.success() {
        return Err(format!("first exec failed with status {}", out.status).into());
    }
    Ok(())
}

#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    eprintln!("this probe requires a linux-x86_64 KVM host");
    Ok(())
}