Skip to main content

isothrum_machine/providers/
helper.rs

1//! The programs a hypervisor's VMs need beside it on this computer ([`helpers`]): the one list
2//! its detection reads it installed by, a setup's install of it brings
3//! ([`Helper::package`]), and a run's needs ask for. A hypervisor installed without one reads
4//! as not installed, so a setup installs it again, which brings the program.
5
6use super::{Provider, virtiofsd};
7use crate::context::Context;
8use crate::host::{HostOs, LinuxPackages};
9
10/// A program a hypervisor's VMs need beside it, which its install brings.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum Helper {
14    /// `mkisofs`: vagrant-qemu gives a VM on a private network its settings in a cloud-init seed,
15    /// an ISO Vagrant builds with `mkisofs` on Linux (`hdiutil` on macOS, which every Mac has).
16    /// Without it, `vagrant up` fails at the first VM ("The executable 'mkisofs' Vagrant is
17    /// trying to run was not found").
18    Mkisofs,
19    /// A virtiofsd the hypervisor finds ([`virtiofsd::virtiofsd`]), which libvirt and
20    /// vagrant-qemu start to share a folder of this computer with a VM on Linux.
21    Virtiofsd,
22}
23
24/// The programs `provider`'s VMs need beside its hypervisor on `os`, in the order they are
25/// asked for: QEMU's on Linux `mkisofs` and a virtiofsd, libvirt's a virtiofsd.
26#[must_use]
27pub fn helpers(provider: Provider, os: HostOs) -> &'static [Helper] {
28    match (provider, os) {
29        (Provider::Qemu, HostOs::Linux) => &[Helper::Mkisofs, Helper::Virtiofsd],
30        (Provider::Libvirt, HostOs::Linux) => &[Helper::Virtiofsd],
31        _ => &[],
32    }
33}
34
35impl Helper {
36    /// The program's name.
37    #[must_use]
38    pub fn program(self) -> &'static str {
39        match self {
40            Helper::Mkisofs => "mkisofs",
41            Helper::Virtiofsd => "virtiofsd",
42        }
43    }
44
45    /// The package that brings it through `packages`: `genisoimage` has `/usr/bin/mkisofs` on
46    /// Debian, Ubuntu and Fedora (Debian's `xorriso` has no `mkisofs`, only `xorrisofs`);
47    /// `virtiofsd` is its own package from Debian 12, Ubuntu 23.10 and Fedora 36 on (QEMU's
48    /// before), with the vhost-user descriptor libvirt finds it by.
49    #[must_use]
50    pub fn package(self, packages: LinuxPackages) -> &'static str {
51        match (self, packages) {
52            (Helper::Mkisofs, LinuxPackages::Apt | LinuxPackages::Dnf) => "genisoimage",
53            (Helper::Virtiofsd, LinuxPackages::Apt | LinuxPackages::Dnf) => "virtiofsd",
54        }
55    }
56
57    /// Whether `provider` finds it on the context's host: `mkisofs` starts, or the hypervisor
58    /// finds a virtiofsd where it looks.
59    pub async fn found(self, cx: &Context, provider: Provider) -> bool {
60        match self {
61            Helper::Mkisofs => cx.tool_starts("mkisofs", &["-version"]).await,
62            Helper::Virtiofsd => virtiofsd::virtiofsd(provider, &cx.host.devices).is_some(),
63        }
64    }
65}
66
67/// The first of `provider`'s [`helpers`] the context's host hasn't; `None` when it has them all.
68pub(crate) async fn missing(cx: &Context, provider: Provider) -> Option<Helper> {
69    for &helper in helpers(provider, cx.host.os) {
70        if !helper.found(cx, provider).await {
71            return Some(helper);
72        }
73    }
74    None
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    /// No helper.
82    const NONE: [Helper; 0] = [];
83
84    #[test]
85    fn qemu_on_linux_needs_mkisofs_and_virtiofsd_libvirt_virtiofsd_and_no_other() {
86        assert_eq!(helpers(Provider::Qemu, HostOs::Linux), [Helper::Mkisofs, Helper::Virtiofsd]);
87        assert_eq!(helpers(Provider::Libvirt, HostOs::Linux), [Helper::Virtiofsd]);
88        // A Mac builds a VM's seed with hdiutil and shares no folder with a QEMU VM.
89        assert_eq!(helpers(Provider::Qemu, HostOs::Macos), NONE);
90        for p in [
91            Provider::Virtualbox,
92            Provider::VmwareDesktop,
93            Provider::Hyperv,
94            Provider::Utm,
95            Provider::Parallels,
96        ] {
97            for os in crate::host::ALL_OSES {
98                assert_eq!(helpers(p, os), NONE, "{p:?} on {os:?}");
99            }
100        }
101    }
102
103    #[test]
104    fn each_helper_comes_in_a_package_of_every_linux_package_manager() {
105        for helper in [Helper::Mkisofs, Helper::Virtiofsd] {
106            for packages in [LinuxPackages::Apt, LinuxPackages::Dnf] {
107                assert!(!helper.package(packages).is_empty(), "{helper:?} through {packages:?}");
108            }
109        }
110        assert_eq!(Helper::Mkisofs.package(LinuxPackages::Apt), "genisoimage", "xorriso has no mkisofs");
111    }
112}