Skip to main content

microvm_runtime/
provider.rs

1use crate::{error::VmRuntimeResult, model::VmView};
2
3/// State-changing operations on microVMs, executed by lifecycle jobs.
4pub trait VmProvider: Send + Sync + 'static {
5    /// Provision a new microVM. Fails if `vm_id` is already in use.
6    fn create_vm(&self, vm_id: &str) -> VmRuntimeResult<()>;
7
8    /// Start a created or stopped microVM. Fails if already running or destroyed.
9    fn start_vm(&self, vm_id: &str) -> VmRuntimeResult<()>;
10
11    /// Stop a running microVM. Fails if not currently running.
12    fn stop_vm(&self, vm_id: &str) -> VmRuntimeResult<()>;
13
14    /// Capture the state of a microVM as a named snapshot.
15    /// Fails if the VM is destroyed or the snapshot name already exists.
16    fn snapshot_vm(&self, vm_id: &str, snapshot_id: &str) -> VmRuntimeResult<()>;
17
18    /// Tear down a microVM. Terminal state — cannot be restarted.
19    fn destroy_vm(&self, vm_id: &str) -> VmRuntimeResult<()>;
20}
21
22/// Read-only queries against microVM state, used by query surfaces.
23pub trait VmQuery: Send + Sync + 'static {
24    /// Return all known VMs, sorted by identifier.
25    fn list_vms(&self) -> VmRuntimeResult<Vec<VmView>>;
26
27    /// Return a single VM by identifier, or `None` if it does not exist.
28    fn get_vm(&self, vm_id: &str) -> VmRuntimeResult<Option<VmView>>;
29
30    /// Return the snapshot names for a VM, or `None` if the VM does not exist.
31    fn list_snapshots(&self, vm_id: &str) -> VmRuntimeResult<Option<Vec<String>>>;
32}
33
34/// Unified trait object used by runners and query services.
35pub trait VmRuntime: VmProvider + VmQuery {}
36
37impl<T> VmRuntime for T where T: VmProvider + VmQuery {}