diff --git a/src/main.rs b/src/main.rs index a1233eab1..7b3451c49 100755 --- a/src/main.rs +++ b/src/main.rs @@ -285,7 +285,7 @@ fn main() { vm_config.disks, ); - if let Err(e) = vmm::boot_kernel(vm_config) { + if let Err(e) = vmm::start_vm_loop(vm_config) { println!("Guest boot failed: {}", e); process::exit(1); } diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index 58a53b933..cf20ee0b5 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -3,11 +3,9 @@ // SPDX-License-Identifier: Apache-2.0 // -extern crate kvm_ioctls; #[macro_use] extern crate log; -use kvm_ioctls::*; use std::fmt::{self, Display}; use std::result; @@ -40,21 +38,9 @@ impl Display for Error { } } -struct Vmm { - kvm: Kvm, -} - -impl Vmm { - fn new() -> Result { - let kvm = Kvm::new().expect("new KVM instance creation failed"); - Ok(Vmm { kvm }) - } -} - -pub fn boot_kernel(config: VmConfig) -> Result<()> { +pub fn start_vm_loop(config: VmConfig) -> Result<()> { loop { - let vmm = Vmm::new()?; - let mut vm = Vm::new(&vmm.kvm, &config).map_err(Error::VmNew)?; + let mut vm = Vm::new(&config).map_err(Error::VmNew)?; if vm.start().map_err(Error::VmStart)? == ExitBehaviour::Shutdown { break; diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index ce6c553cc..6fe8d6f5d 100755 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -211,6 +211,9 @@ pub enum Error { /// Failed to join on vCPU threads ThreadCleanup, + + /// Failed to create a new KVM instance + KvmNew(io::Error), } pub type Result = result::Result; @@ -533,7 +536,8 @@ fn get_host_cpu_phys_bits() -> u8 { } impl<'a> Vm<'a> { - pub fn new(kvm: &Kvm, config: &'a VmConfig) -> Result { + pub fn new(config: &'a VmConfig) -> Result { + let kvm = Kvm::new().map_err(Error::KvmNew)?; let kernel = File::open(&config.kernel.path).map_err(Error::KernelFile)?; let fd = kvm.create_vm().map_err(Error::VmCreate)?; let fd = Arc::new(fd);