#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
mod supported {
use std::io::{Read, Write};
use std::time::Instant;
use supermachine::{Image, Vm, VmConfig};
pub(super) fn main() -> Result<(), Box<dyn std::error::Error>> {
let bundle_dir = std::env::args()
.nth(1)
.ok_or("usage: bundle_load <BUNDLE_DIR>")?;
let t0 = Instant::now();
let image = Image::from_snapshot(&bundle_dir)?;
eprintln!("loaded {bundle_dir} in {:?}", t0.elapsed());
eprintln!(
" memory={} MiB vcpus={} layers={}",
image.memory_mib(),
image.vcpus(),
image.snapshot_path().display(),
);
let t1 = Instant::now();
let vm = Vm::start(&image, &VmConfig::new())?;
eprintln!("vm started in {:?}", t1.elapsed());
let mut sock = vm.connect()?;
sock.write_all(b"GET / HTTP/1.1\r\nHost: workload\r\nConnection: close\r\n\r\n")?;
let mut response = Vec::new();
sock.read_to_end(&mut response)?;
let preview = String::from_utf8_lossy(&response);
for line in preview.lines().take(4) {
eprintln!(" {line}");
}
vm.stop()?;
eprintln!("done in {:?}", t0.elapsed());
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(())
}