Skip to content

Commit

Permalink
vmm: Propagate boot_kernel errors properly
Browse files Browse the repository at this point in the history
So that our error traces are more meaningful.

Signed-off-by: Samuel Ortiz <[email protected]>
  • Loading branch information
Samuel Ortiz committed May 10, 2019
1 parent 43965ed commit 2c94529
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,7 @@ fn main() {
)
.unwrap();

vmm::boot_kernel(vm_config).unwrap();
if let Err(e) = vmm::boot_kernel(vm_config) {
println!("Guest boot failed: {}", e);
}
}
36 changes: 32 additions & 4 deletions vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,38 @@
extern crate kvm_ioctls;

use kvm_ioctls::*;
use std::fmt::{self, Display};
use std::result;

pub mod vm;

use self::vm::{Result, Vm, VmConfig};
use self::vm::{Vm, VmConfig};

/// Errors associated with VM management
#[derive(Debug)]
pub enum Error {
/// Cannot create a new VM.
VmNew(vm::Error),

/// Cannot start a VM.
VmStart(vm::Error),

/// Cannot load a kernel.
LoadKernel(vm::Error),
}
pub type Result<T> = result::Result<T, Error>;

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;

match self {
VmNew(e) => write!(f, "Can not create a new virtual machine: {:?}", e),
VmStart(e) => write!(f, "Can not start a new virtual machine: {:?}", e),
LoadKernel(e) => write!(f, "Can not load a guest kernel: {:?}", e),
}
}
}

struct Vmm {
kvm: Kvm,
Expand All @@ -24,10 +52,10 @@ impl Vmm {

pub fn boot_kernel(config: VmConfig) -> Result<()> {
let vmm = Vmm::new()?;
let mut vm = Vm::new(&vmm.kvm, config)?;
let mut vm = Vm::new(&vmm.kvm, config).map_err(Error::VmNew)?;

let entry = vm.load_kernel()?;
vm.start(entry)?;
let entry = vm.load_kernel().map_err(Error::LoadKernel)?;
vm.start(entry).map_err(Error::VmStart)?;

Ok(())
}

0 comments on commit 2c94529

Please sign in to comment.