isoloom_spec/syntax/hardware.rs
1//! `hardware:`: what a machine asks of the hardware under it. Each target either gives it or
2//! refuses the machine with the reason.
3
4use schemars::JsonSchema;
5use serde::Deserialize;
6
7use crate::model::{Arch, Boot, Device, DeviceInput, Gpus, GpusInput};
8
9/// A virtual TPM 2.0 (BitLocker, Windows Hello, measured boot).
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, JsonSchema)]
11#[serde(rename_all = "lowercase")]
12pub enum Tpm {
13 /// Windows 11 gets one wherever the target can give one, and runs without where it can't;
14 /// every other OS gets none.
15 #[default]
16 Auto,
17 /// The machine needs one: a target that gives none refuses it. A VM only.
18 Required,
19 /// None, even on Windows 11.
20 None,
21}
22
23/// The hardware a machine asks for.
24#[derive(Debug, Clone, PartialEq, Default, Deserialize, JsonSchema)]
25#[serde(deny_unknown_fields)]
26pub struct Hardware {
27 /// The CPU architecture: `amd64` (the default) or `arm64`.
28 #[serde(default)]
29 pub arch: Arch,
30 /// A virtual TPM 2.0: `auto` (the default: Windows 11 gets one where the target can),
31 /// `required` (a VM only) or `none`.
32 #[serde(default)]
33 pub tpm: Tpm,
34 /// GPUs: a count (`1`), `all`, or `{ count, ids, driver, capabilities, options }`. A container
35 /// gets the host's; a VM gets them passed through or sized with them.
36 #[serde(default)]
37 #[schemars(with = "Option<GpusInput>")]
38 pub gpus: Option<Gpus>,
39 /// The host's devices (`/dev/fuse`, `host:guest[:permissions]`, or `{ host, guest,
40 /// permissions }`). A container only.
41 #[serde(default)]
42 #[schemars(with = "Vec<DeviceInput>")]
43 pub devices: Vec<Device>,
44 /// The machine runs VMs of its own: its VM gets the CPU's virtualization extensions. A VM
45 /// only.
46 #[serde(default)]
47 pub nested_virtualization: bool,
48 /// How it boots, when not the way its image does: its own kernel, firmware, boot order or
49 /// network boot. A VM only.
50 #[serde(default)]
51 pub boot: Option<Boot>,
52}
53
54impl Hardware {
55 /// What only a VM can give, as the field naming it (`hardware.tpm`), when the machine asks
56 /// for any.
57 #[must_use]
58 pub fn vm_only(&self) -> Option<&'static str> {
59 if self.tpm == Tpm::Required {
60 Some("tpm")
61 } else if self.nested_virtualization {
62 Some("nested_virtualization")
63 } else if self.boot.is_some() {
64 Some("boot")
65 } else {
66 None
67 }
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn hardware_reads_its_enums() {
77 let h: Hardware = serde_yaml_ng::from_str("{ arch: arm64, tpm: required }").unwrap();
78 assert_eq!((h.arch, h.tpm, h.vm_only()), (Arch::Arm64, Tpm::Required, Some("tpm")));
79 assert_eq!(Hardware::default().vm_only(), None);
80 assert!(serde_yaml_ng::from_str::<Hardware>("{ tpm: true }").is_err());
81 }
82}