Skip to main content

hm_vm/
backend.rs

1//! Backend trait for pluggable VM implementations.
2
3use std::fmt;
4use std::path::Path;
5
6use anyhow::Result;
7use async_trait::async_trait;
8
9use crate::types::{OutputSink, SnapshotId, SnapshotLabel, VmConfig};
10
11/// Factory that creates and manages virtual machines.
12#[async_trait]
13pub trait VmBackend: Send + Sync + fmt::Debug {
14    /// Boot a new VM from the given OCI image reference.
15    async fn create(&self, image: &str, config: &VmConfig) -> Result<Box<dyn Vm>>;
16
17    /// Restore a VM from a previously taken snapshot.
18    async fn restore(&self, snapshot: &SnapshotId, config: &VmConfig) -> Result<Box<dyn Vm>>;
19
20    /// Check whether a snapshot exists in the backend store.
21    async fn snapshot_exists(&self, snapshot: &SnapshotId) -> Result<bool>;
22
23    /// Delete a snapshot from the backend store.
24    async fn remove_snapshot(&self, snapshot: &SnapshotId) -> Result<()>;
25}
26
27/// Handle to a running virtual machine.
28#[async_trait]
29pub trait Vm: Send {
30    /// Copy a host path into the guest filesystem.
31    async fn inject(&self, host_path: &Path, guest_path: &str) -> Result<()>;
32
33    /// Run a command inside the VM and stream output to `sink`.
34    async fn exec(
35        &self,
36        cmd: &str,
37        env: &[(String, String)],
38        working_dir: &str,
39        sink: &dyn OutputSink,
40    ) -> Result<i32>;
41
42    /// Capture the current VM state as a named snapshot.
43    async fn snapshot(&mut self, label: &SnapshotLabel) -> Result<SnapshotId>;
44
45    /// Tear down the VM and release all resources.
46    async fn destroy(&mut self) -> Result<()>;
47}