cloud-hypervisor/vmm/src/lib.rs
Sebastien Boeuf e5e651895b config: Reorganize command line parsing
The command line parsing of the user input was not properly
abstracted from the vmm specific code. In the case of --net,
the parsing was done when the device manager was adding devices.

In order to fix this confusion, this patch introduces a new
module "config" dedicated to the translation of a VmParams
structure into a VmCfg structure. The former is built based
on the input provided by the user, while the latter is the
result of the parsing of every options.

VmCfg is meant to be consumed by the vmm specific code, and
it is also a fully public structure so that it can directly
be built from a testing environment.

Fixes #31

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
Signed-off-by: Rob Bradford <robert.bradford@intel.com>
2019-05-24 17:08:52 +01:00

66 lines
1.4 KiB
Rust

// Copyright © 2019 Intel Corporation
//
// 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;
pub mod config;
pub mod vm;
use self::config::VmConfig;
use self::vm::Vm;
/// 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,
}
impl Vmm {
fn new() -> Result<Self> {
let kvm = Kvm::new().expect("new KVM instance creation failed");
Ok(Vmm { kvm })
}
}
pub fn boot_kernel(config: VmConfig) -> Result<()> {
let vmm = Vmm::new()?;
let mut vm = Vm::new(&vmm.kvm, config).map_err(Error::VmNew)?;
let entry = vm.load_kernel().map_err(Error::LoadKernel)?;
vm.start(entry).map_err(Error::VmStart)?;
Ok(())
}