use std::path::PathBuf;
use arcbox_hypervisor::VmConfig;
#[derive(Debug, Clone)]
pub struct SharedDirConfig {
pub host_path: PathBuf,
pub tag: String,
pub read_only: bool,
}
#[derive(Debug, Clone)]
pub struct BlockDeviceConfig {
pub path: PathBuf,
pub read_only: bool,
}
#[derive(Debug, Clone)]
pub struct VmmConfig {
pub vcpu_count: u32,
pub memory_size: u64,
pub kernel_path: PathBuf,
pub kernel_cmdline: String,
pub initrd_path: Option<PathBuf>,
pub enable_rosetta: bool,
pub serial_console: bool,
pub virtio_console: bool,
pub shared_dirs: Vec<SharedDirConfig>,
pub networking: bool,
pub vsock: bool,
pub guest_cid: Option<u32>,
pub balloon: bool,
pub block_devices: Vec<BlockDeviceConfig>,
pub bridge_nic_mac: Option<String>,
pub backend: VmBackend,
pub debug_console_socket: Option<PathBuf>,
}
impl Default for VmmConfig {
fn default() -> Self {
Self {
vcpu_count: arcbox_hypervisor::default_vm_cpu_count(),
memory_size: arcbox_hypervisor::default_vm_memory_size(),
kernel_path: PathBuf::new(),
kernel_cmdline: String::new(),
initrd_path: None,
enable_rosetta: false,
serial_console: true,
virtio_console: true,
shared_dirs: Vec::new(),
networking: true,
vsock: true,
guest_cid: None,
balloon: true, block_devices: Vec::new(),
bridge_nic_mac: None,
backend: VmBackend::default(),
debug_console_socket: None,
}
}
}
impl VmmConfig {
pub(super) fn to_vm_config(&self) -> VmConfig {
let mut builder = VmConfig::builder()
.vcpu_count(self.vcpu_count)
.memory_size(self.memory_size)
.kernel_path(self.kernel_path.to_string_lossy())
.kernel_cmdline(&self.kernel_cmdline)
.enable_rosetta(self.enable_rosetta);
if let Some(initrd_path) = &self.initrd_path {
builder = builder.initrd_path(initrd_path.to_string_lossy());
}
builder.build()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
#[repr(u8)]
pub enum VmBackend {
Hv = 0,
#[default]
Vz = 1,
}
impl VmBackend {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Hv => "hv",
Self::Vz => "vz",
}
}
#[must_use]
pub fn from_str_ascii(s: &str) -> Option<Self> {
match s {
"hv" => Some(Self::Hv),
"vz" => Some(Self::Vz),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VmmState {
Created,
Initializing,
Running,
Paused,
Stopping,
Stopped,
Failed,
}