openvmm_helpers/
hypervisor.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Hypervisor resource construction and auto-detection for OpenVMM entry
5//! points.
6
7use hypervisor_resources::HypervisorKind;
8use vm_resource::Resource;
9
10/// Returns a [`Resource<HypervisorKind>`] for the first available hypervisor
11/// backend.
12///
13/// Backends are checked in registration order (highest priority first).
14pub fn choose_hypervisor() -> anyhow::Result<Resource<HypervisorKind>> {
15    for probe in hypervisor_resources::probes() {
16        if let Some(resource) = probe.try_new_resource()? {
17            return Ok(resource);
18        }
19    }
20    anyhow::bail!("no hypervisor available");
21}
22
23/// Returns a [`Resource<HypervisorKind>`] for the named backend.
24///
25/// This validates that the name matches a registered probe and checks
26/// availability.
27pub fn hypervisor_resource(name: &str) -> anyhow::Result<Resource<HypervisorKind>> {
28    let probe = hypervisor_resources::probe_by_name(name)
29        .ok_or_else(|| anyhow::anyhow!("unknown hypervisor: {name}"))?;
30    probe
31        .try_new_resource()?
32        .ok_or_else(|| anyhow::anyhow!("hypervisor {name} is not available"))
33}