1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! Product-facing VM creation options ([`VmOptions`] / [`ImageRef`]).
//!
//! Managed Runtime entry points take [`VmOptions`] only.
use std::path::PathBuf;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::secrets::Secret;
use crate::security::SecurityOptions;
use crate::volumes::VolumeMount;
/// Source of the guest root filesystem / base disk.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ImageRef {
/// Pull/ensure an OCI image reference (e.g. `python:slim`).
Oci(String),
/// Host directory used as virtiofs root (debug / custom rootfs).
Rootfs(PathBuf),
/// Existing base disk image path (raw/ext4); Runtime creates a QCOW2 overlay.
BaseDisk(PathBuf),
}
impl From<&str> for ImageRef {
fn from(s: &str) -> Self {
Self::Oci(s.to_owned())
}
}
impl From<String> for ImageRef {
fn from(s: String) -> Self {
Self::Oci(s)
}
}
impl ImageRef {
/// Human-readable label for logs / `VmState.image`.
#[must_use]
pub fn label(&self) -> String {
match self {
Self::Oci(r) => r.clone(),
Self::Rootfs(p) | Self::BaseDisk(p) => p.display().to_string(),
}
}
}
/// Guest networking mode for a managed VM.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum NetworkSpec {
/// No virtio-net. Guest is offline. Ports and secrets are invalid.
Disabled,
/// gvproxy virtio-net. Empty `allow_net` means unrestricted egress.
Enabled {
/// Hostname / CIDR allow-list. Empty = full egress.
#[serde(default)]
allow_net: Vec<String>,
},
}
impl Default for NetworkSpec {
fn default() -> Self {
Self::Enabled {
allow_net: Vec::new(),
}
}
}
impl NetworkSpec {
/// Whether virtio-net / gvproxy is attached.
#[must_use]
pub const fn is_enabled(&self) -> bool {
matches!(self, Self::Enabled { .. })
}
/// Egress allow-list (empty if disabled or unrestricted).
#[must_use]
pub fn allow_net(&self) -> &[String] {
match self {
Self::Enabled { allow_net } => allow_net,
Self::Disabled => &[],
}
}
}
/// Options for creating a managed VM.
///
/// Construct only via [`VmOptions::from_image`] (image is required).
#[derive(Debug, Clone)]
pub struct VmOptions {
/// Root image / rootfs / base disk.
pub image: ImageRef,
/// Optional unique name.
pub name: Option<String>,
/// Optional agent identity.
pub agent_id: Option<String>,
/// Optional tenant identity.
pub tenant_id: Option<String>,
/// vCPUs (default 1).
pub vcpus: u8,
/// RAM in MiB (default 512).
pub ram_mib: u32,
/// Publish specs (`host:guest`, ephemeral forms). Resolved at boot.
pub ports: Vec<String>,
/// Host-only secrets for MITM (memory only).
pub secrets: Vec<Secret>,
/// Guest network (gvproxy or offline).
pub network: NetworkSpec,
/// Volume mounts (bind or named) resolved at create.
pub volumes: Vec<VolumeMount>,
/// Optional first command run by the CLI after the agent is ready.
///
/// Not PID 1. Merged from OCI `ENTRYPOINT`+`CMD` when unset.
pub command: Option<Vec<String>>,
/// Workload environment (`KEY=VALUE`) — applied to **exec**, not VM boot.
///
/// Stored on the handle for Phase A; not passed as libkrun boot env.
pub env: Vec<String>,
/// Workload working directory for Phase A exec defaults.
pub workdir: Option<String>,
/// Workload user `uid[:gid]` for Phase A (parsed later).
pub user: Option<String>,
/// Auto-remove VM state when stopped.
pub auto_remove: bool,
/// Wait for guest agent after create (`Duration::ZERO` = skip).
pub ready_timeout: Duration,
/// Detach: persist so create/restart skip watchdog, parent-death, and Runtime Drop SIGTERM.
pub detach: bool,
/// Isolation policy (Landlock / jailer). Default: fail-closed Landlock on Linux.
pub security: SecurityOptions,
/// Stop after this many idle seconds (`None` = never). Default off.
pub auto_stop_secs: Option<u64>,
/// Delete a stopped VM after this many idle seconds (`None` = never). Default off.
pub auto_delete_secs: Option<u64>,
}
impl VmOptions {
/// Sole constructor — image is required (K27).
#[must_use]
pub fn from_image(image: impl Into<ImageRef>) -> Self {
Self {
image: image.into(),
name: None,
agent_id: None,
tenant_id: None,
vcpus: 1,
ram_mib: 512,
ports: Vec::new(),
secrets: Vec::new(),
network: NetworkSpec::default(),
volumes: Vec::new(),
command: None,
env: Vec::new(),
workdir: None,
user: None,
auto_remove: false,
ready_timeout: Duration::from_secs(30),
detach: false,
security: SecurityOptions::default(),
auto_stop_secs: None,
auto_delete_secs: None,
}
}
/// Set VM name.
#[must_use]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
/// Set agent id.
#[must_use]
pub fn agent_id(mut self, id: impl Into<String>) -> Self {
self.agent_id = Some(id.into());
self
}
/// Set tenant id.
#[must_use]
pub fn tenant_id(mut self, id: impl Into<String>) -> Self {
self.tenant_id = Some(id.into());
self
}
/// Set vCPU count.
#[must_use]
pub const fn vcpus(mut self, n: u8) -> Self {
self.vcpus = n;
self
}
/// Set RAM in MiB.
#[must_use]
pub const fn ram_mib(mut self, mib: u32) -> Self {
self.ram_mib = mib;
self
}
/// Add a port publish string.
#[must_use]
pub fn port(mut self, spec: impl Into<String>) -> Self {
self.ports.push(spec.into());
self
}
/// Set egress allow-list (implies [`NetworkSpec::Enabled`]).
#[must_use]
pub fn allow_net(mut self, hosts: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.network = NetworkSpec::Enabled {
allow_net: hosts.into_iter().map(Into::into).collect(),
};
self
}
/// Set network mode.
#[must_use]
pub fn network(mut self, spec: NetworkSpec) -> Self {
self.network = spec;
self
}
/// Attach secrets for MITM.
#[must_use]
pub fn secrets(mut self, secrets: impl IntoIterator<Item = Secret>) -> Self {
self.secrets = secrets.into_iter().collect();
self
}
/// First command after agent ready (`None` = no auto-exec).
#[must_use]
pub fn command(mut self, cmd: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.command = Some(cmd.into_iter().map(Into::into).collect());
self
}
/// Add a volume mount (bind or named).
#[must_use]
pub fn volume(mut self, mount: VolumeMount) -> Self {
self.volumes.push(mount);
self
}
/// Bind a host directory at `guest_path` (convenience).
#[must_use]
pub fn bind_volume(
mut self,
host_path: impl Into<PathBuf>,
guest_path: impl Into<String>,
) -> Self {
self.volumes.push(VolumeMount::bind(host_path, guest_path));
self
}
/// Attach a named volume at `guest_path` (convenience).
#[must_use]
pub fn named_volume(mut self, name: impl Into<String>, guest_path: impl Into<String>) -> Self {
self.volumes.push(VolumeMount::named(name, guest_path));
self
}
/// Auto-remove when stopped.
#[must_use]
pub const fn auto_remove(mut self, yes: bool) -> Self {
self.auto_remove = yes;
self
}
/// Guest agent ready wait.
#[must_use]
pub const fn ready_timeout(mut self, d: Duration) -> Self {
self.ready_timeout = d;
self
}
/// Detached spawn (no watchdog / parent-death; survives Runtime Drop).
#[must_use]
pub const fn detach(mut self, yes: bool) -> Self {
self.detach = yes;
self
}
/// Workload environment (`KEY=VALUE`) for Phase A exec defaults.
#[must_use]
pub fn env(mut self, vars: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.env = vars.into_iter().map(Into::into).collect();
self
}
/// Workload working directory for Phase A exec defaults.
#[must_use]
pub fn workdir(mut self, path: impl Into<String>) -> Self {
self.workdir = Some(path.into());
self
}
/// Workload user for Phase A (`uid[:gid]` or `name[:group]`).
#[must_use]
pub fn user(mut self, user: impl Into<String>) -> Self {
self.user = Some(user.into());
self
}
/// Isolation policy (K22 Landlock fail-closed on Linux by default).
#[must_use]
pub const fn security(mut self, opts: SecurityOptions) -> Self {
self.security = opts;
self
}
/// Auto-stop after idle seconds (default off).
#[must_use]
pub const fn auto_stop_secs(mut self, secs: Option<u64>) -> Self {
self.auto_stop_secs = secs;
self
}
/// Auto-delete stopped VMs after idle seconds (default off).
#[must_use]
pub const fn auto_delete_secs(mut self, secs: Option<u64>) -> Self {
self.auto_delete_secs = secs;
self
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "tests")]
mod tests {
use super::*;
use crate::ImageRef;
#[test]
fn from_image_defaults() {
let o = VmOptions::from_image("python:slim");
assert_eq!(o.image, ImageRef::Oci("python:slim".into()));
assert_eq!(o.vcpus, 1);
assert_eq!(o.ram_mib, 512);
assert!(o.network.is_enabled());
assert!(o.ports.is_empty());
assert!(o.env.is_empty());
assert!(o.workdir.is_none());
assert!(o.user.is_none());
assert!(o.agent_id.is_none());
assert!(o.tenant_id.is_none());
assert!(!o.auto_remove);
assert!(!o.detach);
}
#[test]
fn fluent_chain() {
let o = VmOptions::from_image("alpine")
.name("n1")
.agent_id("agt")
.tenant_id("ten")
.vcpus(2)
.ram_mib(1024)
.port("8080:80")
.allow_net(["example.com"])
.env(["A=1", "B=2"])
.workdir("/work")
.user("1000:1000")
.auto_remove(true)
.detach(true);
assert_eq!(o.name.as_deref(), Some("n1"));
assert_eq!(o.agent_id.as_deref(), Some("agt"));
assert_eq!(o.tenant_id.as_deref(), Some("ten"));
assert_eq!(o.vcpus, 2);
assert_eq!(o.ram_mib, 1024);
assert_eq!(o.ports, vec!["8080:80"]);
assert_eq!(o.network.allow_net(), &["example.com".to_owned()]);
assert_eq!(o.env, vec!["A=1", "B=2"]);
assert_eq!(o.workdir.as_deref(), Some("/work"));
assert_eq!(o.user.as_deref(), Some("1000:1000"));
assert!(o.auto_remove);
assert!(o.detach);
}
#[test]
fn image_ref_from_str() {
assert_eq!(ImageRef::from("x"), ImageRef::Oci("x".into()));
assert_eq!(ImageRef::Rootfs("/tmp/r".into()).label(), "/tmp/r");
}
}