Skip to main content

arcbox_hypervisor/
config.rs

1//! VM configuration types.
2
3use crate::types::CpuArch;
4
5/// Virtual machine configuration.
6#[derive(Debug, Clone)]
7pub struct VmConfig {
8    /// Number of virtual CPUs.
9    pub vcpu_count: u32,
10    /// Memory size in bytes.
11    pub memory_size: u64,
12    /// CPU architecture (defaults to native).
13    pub arch: CpuArch,
14    /// Path to the kernel image.
15    pub kernel_path: Option<String>,
16    /// Kernel command line arguments.
17    pub kernel_cmdline: Option<String>,
18    /// Path to the initial ramdisk.
19    pub initrd_path: Option<String>,
20    /// Enable Rosetta 2 translation (macOS ARM only).
21    pub enable_rosetta: bool,
22}
23
24impl Default for VmConfig {
25    fn default() -> Self {
26        Self {
27            vcpu_count: 1,
28            memory_size: 512 * 1024 * 1024, // 512MB
29            arch: CpuArch::native(),
30            kernel_path: None,
31            kernel_cmdline: None,
32            initrd_path: None,
33            enable_rosetta: false,
34        }
35    }
36}
37
38impl VmConfig {
39    /// Creates a new builder for VM configuration.
40    #[must_use]
41    pub fn builder() -> VmConfigBuilder {
42        VmConfigBuilder::default()
43    }
44}
45
46/// Builder for [`VmConfig`].
47#[derive(Debug, Default)]
48pub struct VmConfigBuilder {
49    config: VmConfig,
50}
51
52impl VmConfigBuilder {
53    /// Sets the number of vCPUs.
54    #[must_use]
55    pub const fn vcpu_count(mut self, count: u32) -> Self {
56        self.config.vcpu_count = count;
57        self
58    }
59
60    /// Sets the memory size in bytes.
61    #[must_use]
62    pub const fn memory_size(mut self, size: u64) -> Self {
63        self.config.memory_size = size;
64        self
65    }
66
67    /// Sets the CPU architecture.
68    #[must_use]
69    pub const fn arch(mut self, arch: CpuArch) -> Self {
70        self.config.arch = arch;
71        self
72    }
73
74    /// Sets the kernel path.
75    #[must_use]
76    pub fn kernel_path(mut self, path: impl Into<String>) -> Self {
77        self.config.kernel_path = Some(path.into());
78        self
79    }
80
81    /// Sets the kernel command line.
82    #[must_use]
83    pub fn kernel_cmdline(mut self, cmdline: impl Into<String>) -> Self {
84        self.config.kernel_cmdline = Some(cmdline.into());
85        self
86    }
87
88    /// Sets the initrd path.
89    #[must_use]
90    pub fn initrd_path(mut self, path: impl Into<String>) -> Self {
91        self.config.initrd_path = Some(path.into());
92        self
93    }
94
95    /// Enables Rosetta 2 translation.
96    #[must_use]
97    pub const fn enable_rosetta(mut self, enable: bool) -> Self {
98        self.config.enable_rosetta = enable;
99        self
100    }
101
102    /// Builds the configuration.
103    #[must_use]
104    pub fn build(self) -> VmConfig {
105        self.config
106    }
107}