From bb19c3d2b7903540f6ba5756b6e3a6f3811496e4 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Fri, 22 Jul 2022 11:10:16 +0000 Subject: [PATCH] hypervisor: pick the available hypervisor automatically Signed-off-by: Wei Liu --- hypervisor/src/lib.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hypervisor/src/lib.rs b/hypervisor/src/lib.rs index 118b8d414..3d36794cd 100644 --- a/hypervisor/src/lib.rs +++ b/hypervisor/src/lib.rs @@ -69,12 +69,18 @@ pub enum HypervisorType { pub fn new() -> std::result::Result, HypervisorError> { #[cfg(feature = "kvm")] - let hv = kvm::KvmHypervisor::new()?; + if kvm::KvmHypervisor::is_available()? { + return kvm::KvmHypervisor::new(); + } #[cfg(feature = "mshv")] - let hv = mshv::MshvHypervisor::new()?; + if mshv::MshvHypervisor::is_available()? { + return mshv::MshvHypervisor::new(); + } - Ok(hv) + Err(HypervisorError::HypervisorCreate(anyhow!( + "no supported hypervisor" + ))) } // Returns a `Vec` with a size in bytes at least as large as `size_in_bytes`.