From d73971e407fa3d4c6a80eb69322fa627ca069a68 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Mon, 7 Sep 2020 10:10:58 +0000 Subject: [PATCH] hypervisor: kvm: check API compatibility Signed-off-by: Wei Liu --- hypervisor/src/hypervisor.rs | 5 +++++ hypervisor/src/kvm/mod.rs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/hypervisor/src/hypervisor.rs b/hypervisor/src/hypervisor.rs index 055a3db64..4fcceae71 100644 --- a/hypervisor/src/hypervisor.rs +++ b/hypervisor/src/hypervisor.rs @@ -55,6 +55,11 @@ pub enum HypervisorError { /// #[error("Failed to get the list of supported MSRs: {0}")] GetMsrList(#[source] anyhow::Error), + /// + /// API version is not compatible + /// + #[error("Incompatible API version")] + IncompatibleApiVersion, } /// diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index f06a5046b..2efc75402 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -345,6 +345,12 @@ impl KvmHypervisor { /// Create a hypervisor based on Kvm pub fn new() -> hypervisor::Result { let kvm_obj = Kvm::new().map_err(|e| hypervisor::HypervisorError::VmCreate(e.into()))?; + let api_version = kvm_obj.get_api_version(); + + if api_version != kvm_bindings::KVM_API_VERSION as i32 { + return Err(hypervisor::HypervisorError::IncompatibleApiVersion); + } + Ok(KvmHypervisor { kvm: kvm_obj }) } }