Skip to main content

arcbox_computer_runtime/config/
runtime.rs

1use serde::{Deserialize, Serialize};
2
3use crate::error::{ComputerError, Result};
4
5use super::JailerConfig;
6
7/// Top-level configuration of the sandbox runtime, as the composer's
8/// config file spells it out (`/etc/arcbox/vmm.toml` in the System VM).
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct RuntimeConfig {
11    pub firecracker: ComputerConfig,
12    pub network: NetworkConfig,
13    pub grpc: GrpcConfig,
14    pub defaults: DefaultVmConfig,
15}
16
17/// The `[firecracker]` keys this runtime reads.
18///
19/// The section is named for the VMM because that is what a deployed
20/// `vmm.toml` has always called it, and the on-disk vocabulary is frozen
21/// (`computer/AGENTS.md`) — but nothing here is Firecracker's. The keys
22/// that configure a VMM adapter (its binaries, its process-level flags,
23/// the sandbox datapath) belong to whoever builds that adapter, and are
24/// read out of this same section by the composer; serde ignores what it
25/// does not know, so one section serves both halves and the file's shape
26/// is unchanged.
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct ComputerConfig {
29    /// Jailer isolation for every sandbox VMM (absent = no isolation).
30    #[serde(default)]
31    pub jailer: Option<JailerConfig>,
32    /// Root data directory (VMs, snapshots, images).
33    pub data_dir: String,
34    /// Spare pre-warmed restore slots kept per snapshot id (CORE-78).
35    ///
36    /// A slot pre-executes the fixed host-side restore setup — jailer
37    /// chroot, Firecracker spawn, kernel/vmstate/mem staging, dm-snapshot
38    /// — so a restore only claims it and issues LoadSnapshot. Only
39    /// snapshots that have been restored at least once are pooled, for at
40    /// most two distinct snapshot ids (LRU-evicted). `0` disables pooling.
41    #[serde(default = "default_pool_size")]
42    pub pool_size: usize,
43    /// Serve eligible Creates from warm template snapshots (CORE-77).
44    ///
45    /// The first create of a template shape cold-boots and checkpoints the
46    /// idle guest; every later create of the same shape restores from that
47    /// snapshot instead of booting a kernel. Requires jailer isolation;
48    /// only networked creates without a custom boot recipe participate.
49    /// `false` restores plain cold boots for every create.
50    #[serde(default = "default_warm_create")]
51    pub warm_create: bool,
52    /// Where to look for the `dmsetup` binary that drives dm-snapshot CoW
53    /// rootfs images. The first entry that exists and answers
54    /// `dmsetup version` wins; none usable disables CoW (every sandbox
55    /// copies its rootfs). `PATH` is never searched. `None` — the field
56    /// absent from the file — leaves the choice to the composer, whose
57    /// fallback is the library's stock-distro list; an explicit list is
58    /// used exactly as written, so `[]` is how a config disables CoW.
59    #[serde(default)]
60    pub dmsetup_candidates: Option<Vec<String>>,
61}
62
63fn default_pool_size() -> usize {
64    1
65}
66
67fn default_warm_create() -> bool {
68    true
69}
70
71/// Network IP-pool settings for sandbox TAP interfaces.
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct NetworkConfig {
74    /// IP CIDR pool from which guest addresses are allocated.
75    pub cidr: String,
76    /// Default gateway advertised to guests.
77    pub gateway: String,
78    /// DNS servers advertised to guests.
79    pub dns: Vec<String>,
80}
81
82/// gRPC server transport configuration.
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct GrpcConfig {
85    /// Unix-domain socket path (primary transport).
86    pub unix_socket: String,
87    /// Optional TCP address (`host:port`). Empty = disabled.
88    pub tcp_addr: String,
89}
90
91/// Default VM resource values used when a create request omits a field.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct DefaultVmConfig {
94    pub vcpus: u64,
95    pub memory_mib: u64,
96    pub kernel: String,
97    pub rootfs: String,
98    /// Select the agent as PID 1 with `init=` naming [`crate::VM_AGENT_PATH`].
99    /// Converted images retain their distribution's `/sbin/init`.
100    pub boot_args: String,
101}
102
103impl Default for RuntimeConfig {
104    fn default() -> Self {
105        Self {
106            firecracker: ComputerConfig {
107                jailer: None,
108                data_dir: "/var/lib/firecracker-vmm".into(),
109                pool_size: default_pool_size(),
110                warm_create: default_warm_create(),
111                dmsetup_candidates: None,
112            },
113            network: NetworkConfig {
114                cidr: "172.20.0.0/16".into(),
115                gateway: "172.20.0.1".into(),
116                dns: vec!["1.1.1.1".into(), "8.8.8.8".into()],
117            },
118            grpc: GrpcConfig {
119                unix_socket: "/run/firecracker-vmm/vmm.sock".into(),
120                tcp_addr: String::new(),
121            },
122            defaults: DefaultVmConfig {
123                vcpus: 1,
124                memory_mib: 512,
125                kernel: "/var/lib/firecracker-vmm/kernels/vmlinux".into(),
126                rootfs: "/var/lib/firecracker-vmm/images/ubuntu-22.04.ext4".into(),
127                boot_args: format!(
128                    "console=ttyS0 reboot=k panic=1 pci=off init={}",
129                    crate::VM_AGENT_PATH
130                ),
131            },
132        }
133    }
134}
135
136impl RuntimeConfig {
137    /// Load configuration from a TOML file.
138    pub fn from_file(path: &str) -> Result<Self> {
139        let content =
140            std::fs::read_to_string(path).map_err(|e| ComputerError::Config(e.to_string()))?;
141        toml::from_str(&content).map_err(|e| ComputerError::Config(e.to_string()))
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn test_default_config_has_sane_values() {
151        let cfg = RuntimeConfig::default();
152        assert_eq!(cfg.defaults.vcpus, 1);
153        assert_eq!(cfg.defaults.memory_mib, 512);
154        assert!(cfg.defaults.boot_args.contains("console=ttyS0"));
155        assert!(
156            cfg.defaults
157                .boot_args
158                .split_whitespace()
159                .any(|arg| arg == format!("init={}", crate::VM_AGENT_PATH)),
160            "the default command line must select vm-agent as PID 1"
161        );
162        assert_ne!(cfg.network.cidr, "");
163        assert_ne!(cfg.firecracker.data_dir, "");
164    }
165
166    #[test]
167    fn pool_size_defaults_to_one_spare_slot() {
168        assert_eq!(RuntimeConfig::default().firecracker.pool_size, 1);
169        // A config written before the knob existed still loads with the default.
170        let cfg: ComputerConfig = toml::from_str("data_dir = \"/var/lib/vmm\"\n").unwrap();
171        assert_eq!(cfg.pool_size, 1);
172    }
173
174    #[test]
175    fn warm_create_defaults_on_and_parses_the_escape_hatch() {
176        assert!(RuntimeConfig::default().firecracker.warm_create);
177        // A config written before the knob existed still loads with the default.
178        let cfg: ComputerConfig = toml::from_str("data_dir = \"/var/lib/vmm\"\n").unwrap();
179        assert!(cfg.warm_create);
180        // The escape hatch is reachable by config alone.
181        let cfg: ComputerConfig =
182            toml::from_str("data_dir = \"/var/lib/vmm\"\nwarm_create = false\n").unwrap();
183        assert!(!cfg.warm_create);
184    }
185
186    #[test]
187    fn dmsetup_candidates_absent_is_none_and_explicit_lists_parse_as_written() {
188        // A config written before the field existed still loads; the
189        // absence is preserved so the composer can supply its own list.
190        let cfg: ComputerConfig = toml::from_str("data_dir = \"/var/lib/vmm\"\n").unwrap();
191        assert_eq!(cfg.dmsetup_candidates, None);
192        // An explicit list is used exactly as written.
193        let cfg: ComputerConfig = toml::from_str(
194            "data_dir = \"/var/lib/vmm\"\n\
195             dmsetup_candidates = [\"/opt/arcbox/dmsetup\", \"/sbin/dmsetup\"]\n",
196        )
197        .unwrap();
198        assert_eq!(
199            cfg.dmsetup_candidates.as_deref(),
200            Some(
201                &[
202                    "/opt/arcbox/dmsetup".to_string(),
203                    "/sbin/dmsetup".to_string()
204                ][..]
205            )
206        );
207        // An empty list is a deliberate "no dmsetup" — CoW off.
208        let cfg: ComputerConfig = toml::from_str(
209            "data_dir = \"/var/lib/vmm\"\n\
210             dmsetup_candidates = []\n",
211        )
212        .unwrap();
213        assert_eq!(cfg.dmsetup_candidates.as_deref(), Some(&[][..]));
214    }
215
216    #[test]
217    fn test_vmm_config_json_roundtrip() {
218        let cfg = RuntimeConfig::default();
219        let json = serde_json::to_string(&cfg).unwrap();
220        let decoded: RuntimeConfig = serde_json::from_str(&json).unwrap();
221        assert_eq!(decoded.defaults.vcpus, cfg.defaults.vcpus);
222        assert_eq!(decoded.defaults.memory_mib, cfg.defaults.memory_mib);
223        assert_eq!(decoded.network.cidr, cfg.network.cidr);
224        assert_eq!(decoded.network.gateway, cfg.network.gateway);
225    }
226
227    #[test]
228    fn test_from_file_missing_returns_config_error() {
229        let result = RuntimeConfig::from_file("/nonexistent/arcbox-test-config.toml");
230        assert!(result.is_err());
231        assert!(matches!(
232            result.unwrap_err(),
233            crate::error::ComputerError::Config(_)
234        ));
235    }
236}