Skip to main content

VirtualMachine

Trait VirtualMachine 

Source
pub trait VirtualMachine: Send + Sync {
    type Vcpu: Vcpu;
    type Memory: GuestMemory;

Show 13 methods // Required methods fn memory(&self) -> &Self::Memory; fn create_vcpu(&mut self, id: u32) -> Result<Self::Vcpu, HypervisorError>; fn add_virtio_device( &mut self, device: VirtioDeviceConfig, ) -> Result<(), HypervisorError>; fn start(&mut self) -> Result<(), HypervisorError>; fn pause(&mut self) -> Result<(), HypervisorError>; fn resume(&mut self) -> Result<(), HypervisorError>; fn stop(&mut self) -> Result<(), HypervisorError>; fn as_any(&self) -> &dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any; fn vcpu_count(&self) -> u32; fn snapshot_devices(&self) -> Result<Vec<DeviceSnapshot>, HypervisorError>; fn restore_devices( &mut self, snapshots: &[DeviceSnapshot], ) -> Result<(), HypervisorError>; // Provided method fn is_managed_execution(&self) -> bool { ... }
}
Expand description

Virtual machine trait for managing VM lifecycle and devices.

Required Associated Types§

Source

type Vcpu: Vcpu

The vCPU type for this VM.

Source

type Memory: GuestMemory

The guest memory type for this VM.

Required Methods§

Source

fn memory(&self) -> &Self::Memory

Returns a reference to the guest memory.

Source

fn create_vcpu(&mut self, id: u32) -> Result<Self::Vcpu, HypervisorError>

Creates a new vCPU.

For managed execution VMs, this may return a placeholder vCPU.

§Errors

Returns an error if the vCPU cannot be created.

Source

fn add_virtio_device( &mut self, device: VirtioDeviceConfig, ) -> Result<(), HypervisorError>

Adds a VirtIO device to the VM.

§Errors

Returns an error if the device cannot be added.

Source

fn start(&mut self) -> Result<(), HypervisorError>

Starts the VM.

§Errors

Returns an error if the VM cannot be started.

Source

fn pause(&mut self) -> Result<(), HypervisorError>

Pauses the VM.

§Errors

Returns an error if the VM cannot be paused.

Source

fn resume(&mut self) -> Result<(), HypervisorError>

Resumes a paused VM.

§Errors

Returns an error if the VM cannot be resumed.

Source

fn stop(&mut self) -> Result<(), HypervisorError>

Stops the VM.

§Errors

Returns an error if the VM cannot be stopped.

Source

fn as_any(&self) -> &dyn Any

Returns the VM as a reference to Any for downcasting.

This allows the caller to downcast the VM to its concrete type for platform-specific operations like IRQ injection.

Source

fn as_any_mut(&mut self) -> &mut dyn Any

Returns the VM as a mutable reference to Any for downcasting.

Source

fn vcpu_count(&self) -> u32

Returns the number of vCPUs in the VM.

Source

fn snapshot_devices(&self) -> Result<Vec<DeviceSnapshot>, HypervisorError>

Gets snapshots of all device states.

§Errors

Returns an error if device states cannot be captured.

Source

fn restore_devices( &mut self, snapshots: &[DeviceSnapshot], ) -> Result<(), HypervisorError>

Restores device states from snapshots.

§Errors

Returns an error if device states cannot be restored.

Provided Methods§

Source

fn is_managed_execution(&self) -> bool

Returns whether this VM uses managed execution.

Managed execution (returns true):

  • The hypervisor manages vCPU execution internally
  • start() begins VM execution, stop() ends it
  • create_vcpu() is optional/placeholder
  • Examples: macOS Virtualization.framework

Manual execution (returns false):

  • The caller must create vCPU threads and call vcpu.run() in a loop
  • start() and stop() are optional state markers
  • Examples: Linux KVM

Implementors§