hypervisor: Remove panic from Hypervisor::check_required_extensions()

Remove the panic by replacing the .expect() with a cleaner error
handling.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-05-19 13:25:28 +00:00 committed by Sebastien Boeuf
parent a335cbb8d5
commit 84454f142d
2 changed files with 10 additions and 4 deletions

View File

@ -54,6 +54,11 @@ pub enum HypervisorError {
///
#[error("Incompatible API version")]
IncompatibleApiVersion,
///
/// Checking extensions failed
///
#[error("Checking extensions:{0}")]
CheckExtensions(#[source] anyhow::Error),
}
///

View File

@ -70,9 +70,9 @@ pub use kvm_ioctls;
pub use kvm_ioctls::{Cap, Kvm};
#[cfg(target_arch = "aarch64")]
use std::mem;
use thiserror::Error;
#[cfg(feature = "tdx")]
use vmm_sys_util::{ioctl::ioctl_with_val, ioctl_expr, ioctl_ioc_nr, ioctl_iowr_nr};
///
/// Export generically-named wrappers of kvm-bindings for Unix-based platforms
///
@ -465,8 +465,9 @@ pub struct KvmHypervisor {
kvm: Kvm,
}
/// Enum for KVM related error
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum KvmError {
#[error("Capability missing: {0:?}")]
CapabilityMissing(Cap),
}
pub type KvmResult<T> = result::Result<T, KvmError>;
@ -561,8 +562,8 @@ impl hypervisor::Hypervisor for KvmHypervisor {
}
fn check_required_extensions(&self) -> hypervisor::Result<()> {
check_required_kvm_extensions(&self.kvm).expect("Missing KVM capabilities");
Ok(())
check_required_kvm_extensions(&self.kvm)
.map_err(|e| hypervisor::HypervisorError::CheckExtensions(e.into()))
}
#[cfg(target_arch = "x86_64")]