supermachine 0.7.106

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
#[cfg(any(
    all(target_os = "linux", target_arch = "x86_64"),
    all(target_os = "macos", target_arch = "aarch64")
))]
mod supported {
    //! Bake the warm rustc snapshot used by axis_bench.

    use std::time::Duration;
    use supermachine::{Image, VmConfig};

    pub(super) fn main() -> Result<(), Box<dyn std::error::Error>> {
        let snap = format!(
            "{}/.local/supermachine-snapshots/rust_1_slim",
            std::env::var("HOME")?
        );
        let dest = format!(
            "{}/.local/supermachine-snapshots/rust_1_slim__warm__rustc_v1",
            std::env::var("HOME")?
        );
        if std::path::Path::new(&dest).is_dir() {
            println!("warm snapshot already exists, skipping");
            return Ok(());
        }
        let base = Image::from_snapshot(&snap)?;
        let pooled = base.acquire_with(&VmConfig::new())?;
        println!("warming...");
        let _ = pooled
            .exec_builder()
            .stage_file(
                "/tmp/main.rs",
                b"fn main() { println!(\"hello\"); }".to_vec(),
            )
            .argv(
                ["rustc", "-O", "/tmp/main.rs", "-o", "/tmp/m"]
                    .iter()
                    .copied(),
            )
            .chain(["/tmp/m"].iter().copied())
            .timeout(Duration::from_secs(60))
            .output()?;
        println!("snapshotting...");
        let _ = pooled.snapshot(&dest)?;
        println!("baked at {dest}");
        Ok(())
    }
}

#[cfg(any(
    all(target_os = "linux", target_arch = "x86_64"),
    all(target_os = "macos", target_arch = "aarch64")
))]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    supported::main()
}

#[cfg(not(any(
    all(target_os = "linux", target_arch = "x86_64"),
    all(target_os = "macos", target_arch = "aarch64")
)))]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    eprintln!("this example requires a linux-x86_64 (KVM) or macos-aarch64 (HVF) host; unsupported on this platform");
    Ok(())
}