herolib-virt 0.3.13

Virtualization and container management for herolib (buildah, nerdctl, kubernetes)
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use crate::cloudhv::errors::{CloudHypervisorError, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

const DEFAULT_BINARY_PATHS: &[&str] = &[
    "/usr/local/bin/cloud-hypervisor",
    "/usr/bin/cloud-hypervisor",
    "cloud-hypervisor", // Will search in PATH
];

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CloudHypervisorConfig {
    pub binary_path: PathBuf,
    pub api_socket_path: PathBuf,
    pub log_file: Option<PathBuf>,
    pub seccomp: bool,
    pub landlock: bool,
}

impl CloudHypervisorConfig {
    /// Create a new config with the specified binary path
    /// If the path doesn't exist, returns an error
    pub fn new(binary_path: impl Into<PathBuf>) -> Self {
        Self {
            binary_path: binary_path.into(),
            api_socket_path: PathBuf::from("/tmp/ch-api.sock"),
            log_file: None,
            seccomp: true,
            landlock: true,
        }
    }

    /// Create a new config with automatic binary path detection
    /// Tries default paths and returns error if binary not found
    pub fn new_with_defaults() -> Result<Self> {
        let binary_path = Self::find_binary()?;
        Ok(Self {
            binary_path,
            api_socket_path: PathBuf::from("/tmp/ch-api.sock"),
            log_file: None,
            seccomp: true,
            landlock: true,
        })
    }

    /// Create a new config, using provided path if it exists, otherwise try defaults
    pub fn new_smart(binary_path: Option<impl Into<PathBuf>>) -> Result<Self> {
        let path = if let Some(p) = binary_path {
            let path = p.into();
            if path.exists() {
                path
            } else {
                return Err(CloudHypervisorError::Config(format!(
                    "Specified binary not found: {}",
                    path.display()
                )));
            }
        } else {
            Self::find_binary()?
        };

        Ok(Self {
            binary_path: path,
            api_socket_path: PathBuf::from("/tmp/ch-api.sock"),
            log_file: None,
            seccomp: true,
            landlock: true,
        })
    }

    /// Find the cloud-hypervisor binary in default locations
    fn find_binary() -> Result<PathBuf> {
        for path_str in DEFAULT_BINARY_PATHS {
            let path = PathBuf::from(path_str);
            
            // For relative paths, check if it's in PATH
            if !path.is_absolute() {
                if let Ok(which_path) = which::which(path_str) {
                    return Ok(which_path);
                }
            } else if path.exists() {
                return Ok(path);
            }
        }

        Err(CloudHypervisorError::Config(
            format!(
                "Cloud Hypervisor binary not found. Searched in: {}. \
                Please install cloud-hypervisor or specify the binary path explicitly.",
                DEFAULT_BINARY_PATHS.join(", ")
            )
        ))
    }

    pub fn with_api_socket(mut self, path: impl Into<PathBuf>) -> Self {
        self.api_socket_path = path.into();
        self
    }

    pub fn with_log_file(mut self, path: impl Into<PathBuf>) -> Self {
        self.log_file = Some(path.into());
        self
    }

    pub fn with_seccomp(mut self, enabled: bool) -> Self {
        self.seccomp = enabled;
        self
    }

    pub fn with_landlock(mut self, enabled: bool) -> Self {
        self.landlock = enabled;
        self
    }

    pub fn validate(&self) -> Result<()> {
        if !self.binary_path.exists() {
            return Err(CloudHypervisorError::Config(format!(
                "Binary not found: {}",
                self.binary_path.display()
            )));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VMConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payload: Option<PayloadConfig>,
    pub cpus: CpuConfig,
    pub memory: MemoryConfig,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub disks: Vec<DiskConfig>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub net: Vec<NetConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rng: Option<RngConfig>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub fs: Vec<FsConfig>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub vhost_user_net: Vec<VhostUserNetConfig>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub vhost_user_blk: Vec<VhostUserBlkConfig>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub vfio: Vec<VfioConfig>,
    pub console: ConsoleConfig,
    pub serial: SerialConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PayloadConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kernel: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub initramfs: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cmdline: Option<String>,
}

impl Default for VMConfig {
    fn default() -> Self {
        Self {
            payload: None,
            cpus: CpuConfig::default(),
            memory: MemoryConfig::default(),
            disks: vec![],
            net: vec![],
            rng: None,
            fs: vec![],
            vhost_user_net: vec![],
            vhost_user_blk: vec![],
            vfio: vec![],
            console: ConsoleConfig::default(),
            serial: SerialConfig::default(),
        }
    }
}

impl VMConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_kernel(mut self, path: impl Into<PathBuf>) -> Self {
        let payload = self.payload.get_or_insert_with(|| PayloadConfig {
            kernel: None,
            initramfs: None,
            cmdline: None,
        });
        payload.kernel = Some(path.into().display().to_string());
        self
    }

    pub fn with_initramfs(mut self, path: impl Into<PathBuf>) -> Self {
        let payload = self.payload.get_or_insert_with(|| PayloadConfig {
            kernel: None,
            initramfs: None,
            cmdline: None,
        });
        payload.initramfs = Some(path.into().display().to_string());
        self
    }

    pub fn with_cmdline(mut self, args: Vec<String>) -> Self {
        let payload = self.payload.get_or_insert_with(|| PayloadConfig {
            kernel: None,
            initramfs: None,
            cmdline: None,
        });
        payload.cmdline = Some(args.join(" "));
        self
    }

    pub fn with_cpus(mut self, cpus: u32) -> Self {
        self.cpus.boot_vcpus = cpus;
        self.cpus.max_vcpus = cpus;
        self
    }

    pub fn with_memory(mut self, size_mb: u64) -> Self {
        self.memory.size = size_mb * 1024 * 1024;
        self
    }

    pub fn with_memory_shared(mut self, shared: bool) -> Self {
        self.memory.shared = shared;
        self
    }

    pub fn with_rng(mut self, src: RngSource) -> Self {
        self.rng = Some(RngConfig { src });
        self
    }

    pub fn add_virtio_blk(&mut self, path: impl Into<PathBuf>, readonly: bool) -> &mut Self {
        self.disks.push(DiskConfig {
            path: path.into(),
            readonly,
            direct: true,
            vhost_user: false,
            socket: None,
        });
        self
    }

    pub fn add_virtio_net(&mut self, tap: impl Into<String>) -> &mut Self {
        self.net.push(NetConfig {
            tap: Some(tap.into()),
            mac: None,
            vhost_user: false,
            socket: None,
        });
        self
    }

    pub fn add_virtio_net_with_mac(&mut self, tap: impl Into<String>, mac: impl Into<String>) -> &mut Self {
        self.net.push(NetConfig {
            tap: Some(tap.into()),
            mac: Some(mac.into()),
            vhost_user: false,
            socket: None,
        });
        self
    }

    pub fn add_virtio_rng(&mut self) -> &mut Self {
        self.rng = Some(RngConfig { src: RngSource::Urandom });
        self
    }

    pub fn add_virtio_fs(&mut self, tag: impl Into<String>, socket_path: impl Into<PathBuf>) -> &mut Self {
        self.fs.push(FsConfig {
            tag: tag.into(),
            socket: socket_path.into(),
            num_queues: 1,
            queue_size: 1024,
            cache_size: 8 * 1024 * 1024 * 1024,
        });
        self
    }

    pub fn add_vhost_user_net(&mut self, socket: impl Into<PathBuf>) -> &mut Self {
        self.vhost_user_net.push(VhostUserNetConfig {
            socket: socket.into(),
            mac: None,
            num_queues: 2,
            queue_size: 256,
        });
        self
    }

    pub fn add_vhost_user_blk(&mut self, socket: impl Into<PathBuf>) -> &mut Self {
        self.vhost_user_blk.push(VhostUserBlkConfig {
            socket: socket.into(),
            num_queues: 1,
            queue_size: 128,
            readonly: false,
        });
        self
    }

    pub fn add_vfio(&mut self, device: impl Into<String>) -> &mut Self {
        self.vfio.push(VfioConfig {
            device: device.into(),
        });
        self
    }

    pub fn build(self) -> Result<VMSpec> {
        if self.payload.is_none() || self.payload.as_ref().unwrap().kernel.is_none() {
            return Err(CloudHypervisorError::Config(
                "Kernel path is required".to_string(),
            ));
        }

        if self.cpus.boot_vcpus == 0 {
            return Err(CloudHypervisorError::Config(
                "At least one CPU is required".to_string(),
            ));
        }

        if self.memory.size == 0 {
            return Err(CloudHypervisorError::Config(
                "Memory size must be greater than 0".to_string(),
            ));
        }

        Ok(VMSpec {
            config: self,
            id: uuid::Uuid::new_v4().to_string(),
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VMSpec {
    pub config: VMConfig,
    pub id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpuConfig {
    pub boot_vcpus: u32,
    pub max_vcpus: u32,
}

impl Default for CpuConfig {
    fn default() -> Self {
        Self {
            boot_vcpus: 1,
            max_vcpus: 1,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    pub size: u64,
    pub mergeable: bool,
    pub hotplug_method: HotplugMethod,
    pub hotplug_size: Option<u64>,
    pub hotplugged_size: Option<u64>,
    pub shared: bool,
    pub hugepages: bool,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            size: 512 * 1024 * 1024,
            mergeable: false,
            hotplug_method: HotplugMethod::Acpi,
            hotplug_size: None,
            hotplugged_size: None,
            shared: false,
            hugepages: false,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HotplugMethod {
    Acpi,
    VirtioMem,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiskConfig {
    pub path: PathBuf,
    pub readonly: bool,
    pub direct: bool,
    pub vhost_user: bool,
    pub socket: Option<PathBuf>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tap: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mac: Option<String>,
    #[serde(skip_serializing_if = "is_false")]
    pub vhost_user: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub socket: Option<PathBuf>,
}

fn is_false(b: &bool) -> bool {
    !b
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RngConfig {
    pub src: RngSource,
}

impl Default for RngConfig {
    fn default() -> Self {
        Self {
            src: RngSource::Urandom,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RngSource {
    Urandom,
    Random,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FsConfig {
    pub tag: String,
    pub socket: PathBuf,
    pub num_queues: usize,
    pub queue_size: u16,
    pub cache_size: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VhostUserNetConfig {
    pub socket: PathBuf,
    pub mac: Option<String>,
    pub num_queues: usize,
    pub queue_size: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VhostUserBlkConfig {
    pub socket: PathBuf,
    pub num_queues: usize,
    pub queue_size: u16,
    pub readonly: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VfioConfig {
    pub device: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsoleConfig {
    pub mode: ConsoleMode,
}

impl Default for ConsoleConfig {
    fn default() -> Self {
        Self {
            mode: ConsoleMode::Off,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConsoleMode {
    Off,
    Tty,
    File,
    Null,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerialConfig {
    pub mode: SerialMode,
}

impl Default for SerialConfig {
    fn default() -> Self {
        Self {
            mode: SerialMode::Null,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SerialMode {
    Off,
    Tty,
    File,
    Null,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceConfig {
    pub id: String,
    pub device_type: DeviceType,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum DeviceType {
    Disk(DiskConfig),
    Net(NetConfig),
    Fs(FsConfig),
    VhostUserNet(VhostUserNetConfig),
    VhostUserBlk(VhostUserBlkConfig),
    Vfio(VfioConfig),
}