2019-02-22 16:04:35 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
|
|
|
extern crate kvm_ioctls;
|
2019-05-19 02:24:47 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2019-02-22 16:04:35 +00:00
|
|
|
|
|
|
|
use kvm_ioctls::*;
|
2019-05-10 08:46:27 +00:00
|
|
|
use std::fmt::{self, Display};
|
|
|
|
use std::result;
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
pub mod config;
|
2019-09-04 13:55:14 +00:00
|
|
|
pub mod device_manager;
|
2019-02-28 13:16:58 +00:00
|
|
|
pub mod vm;
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-05-23 19:48:05 +00:00
|
|
|
use self::config::VmConfig;
|
2019-08-29 13:51:45 +00:00
|
|
|
use self::vm::{ExitBehaviour, Vm};
|
2019-05-10 08:46:27 +00:00
|
|
|
|
|
|
|
/// 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),
|
|
|
|
}
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-02-28 13:16:58 +00:00
|
|
|
struct Vmm {
|
|
|
|
kvm: Kvm,
|
|
|
|
}
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-02-28 13:16:58 +00:00
|
|
|
impl Vmm {
|
|
|
|
fn new() -> Result<Self> {
|
|
|
|
let kvm = Kvm::new().expect("new KVM instance creation failed");
|
|
|
|
Ok(Vmm { kvm })
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-03-07 13:56:43 +00:00
|
|
|
pub fn boot_kernel(config: VmConfig) -> Result<()> {
|
2019-08-29 13:51:45 +00:00
|
|
|
loop {
|
|
|
|
let vmm = Vmm::new()?;
|
|
|
|
let mut vm = Vm::new(&vmm.kvm, &config).map_err(Error::VmNew)?;
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-09-23 23:03:05 +00:00
|
|
|
if vm.start().map_err(Error::VmStart)? == ExitBehaviour::Shutdown {
|
2019-08-29 13:51:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-06 17:37:41 +00:00
|
|
|
|
|
|
|
#[cfg(not(feature = "acpi"))]
|
|
|
|
break;
|
2019-08-29 13:51:45 +00:00
|
|
|
}
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-02-28 13:16:58 +00:00
|
|
|
Ok(())
|
2019-02-22 16:04:35 +00:00
|
|
|
}
|