2019-02-22 16:04:35 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2020-02-18 15:44:41 +00:00
|
|
|
extern crate anyhow;
|
2019-12-31 10:49:11 +00:00
|
|
|
extern crate arc_swap;
|
2020-06-02 02:29:54 +00:00
|
|
|
extern crate hypervisor;
|
2020-07-06 15:41:45 +00:00
|
|
|
extern crate option_parser;
|
2019-09-18 09:14:49 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2019-05-19 02:24:47 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2019-09-27 09:40:50 +00:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate serde_json;
|
2020-02-04 16:44:12 +00:00
|
|
|
extern crate tempfile;
|
2020-02-25 00:09:54 +00:00
|
|
|
extern crate url;
|
2019-09-25 12:09:33 +00:00
|
|
|
extern crate vmm_sys_util;
|
2020-05-05 12:09:35 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate credibility;
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-11-04 18:06:45 +00:00
|
|
|
use crate::api::{ApiError, ApiRequest, ApiResponse, ApiResponsePayload, VmInfo, VmmPingResponse};
|
2020-04-14 09:21:24 +00:00
|
|
|
use crate::config::{
|
2020-04-28 15:21:22 +00:00
|
|
|
DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, RestoreConfig, VmConfig, VsockConfig,
|
2020-04-14 09:21:24 +00:00
|
|
|
};
|
2020-06-23 09:39:39 +00:00
|
|
|
use crate::migration::{get_vm_snapshot, recv_vm_snapshot};
|
2020-03-20 16:57:03 +00:00
|
|
|
use crate::seccomp_filters::{get_seccomp_filter, Thread};
|
2019-10-08 13:53:39 +00:00
|
|
|
use crate::vm::{Error as VmError, Vm, VmState};
|
2019-09-25 12:09:33 +00:00
|
|
|
use libc::EFD_NONBLOCK;
|
2020-07-30 21:21:58 +00:00
|
|
|
use seccomp::{SeccompAction, SeccompFilter};
|
2020-06-11 16:50:25 +00:00
|
|
|
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
2020-05-15 18:31:35 +00:00
|
|
|
use std::fs::File;
|
2019-09-25 12:09:33 +00:00
|
|
|
use std::io;
|
2020-05-15 18:31:35 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
2020-02-14 09:55:19 +00:00
|
|
|
use std::path::PathBuf;
|
2019-09-26 17:00:43 +00:00
|
|
|
use std::sync::mpsc::{Receiver, RecvError, SendError, Sender};
|
2019-12-05 14:50:38 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-09-25 12:40:14 +00:00
|
|
|
use std::{result, thread};
|
2020-10-26 16:11:02 +00:00
|
|
|
use thiserror::Error;
|
2020-04-02 06:53:32 +00:00
|
|
|
use vm_migration::{Pausable, Snapshottable, Transportable};
|
2019-09-25 12:09:33 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-03-07 13:56:43 +00:00
|
|
|
|
2019-09-25 12:14:15 +00:00
|
|
|
pub mod api;
|
2019-05-23 19:48:05 +00:00
|
|
|
pub mod config;
|
2019-11-11 13:55:50 +00:00
|
|
|
pub mod cpu;
|
2019-09-04 13:55:14 +00:00
|
|
|
pub mod device_manager;
|
2020-05-05 10:19:43 +00:00
|
|
|
pub mod device_tree;
|
2020-01-14 11:02:42 +00:00
|
|
|
pub mod interrupt;
|
2019-12-19 15:47:36 +00:00
|
|
|
pub mod memory_manager;
|
2020-02-25 00:09:54 +00:00
|
|
|
pub mod migration;
|
2020-03-20 14:57:17 +00:00
|
|
|
pub mod seccomp_filters;
|
2019-02-28 13:16:58 +00:00
|
|
|
pub mod vm;
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-11-06 17:20:55 +00:00
|
|
|
#[cfg(feature = "acpi")]
|
|
|
|
mod acpi;
|
|
|
|
|
2019-09-25 12:40:14 +00:00
|
|
|
/// Errors associated with VMM management
|
2020-10-26 16:11:02 +00:00
|
|
|
#[derive(Debug, Error)]
|
2019-09-25 12:40:14 +00:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2019-05-10 08:46:27 +00:00
|
|
|
pub enum Error {
|
2019-09-25 12:40:14 +00:00
|
|
|
/// API request receive error
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error receiving API request: {0}")]
|
|
|
|
ApiRequestRecv(#[source] RecvError),
|
2019-09-25 12:40:14 +00:00
|
|
|
|
|
|
|
/// API response send error
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error sending API request: {0}")]
|
|
|
|
ApiResponseSend(#[source] SendError<ApiResponse>),
|
2019-09-25 12:40:14 +00:00
|
|
|
|
2019-09-18 09:14:49 +00:00
|
|
|
/// Cannot bind to the UNIX domain socket path
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error binding to UNIX domain socket: {0}")]
|
|
|
|
Bind(#[source] io::Error),
|
2019-09-18 09:14:49 +00:00
|
|
|
|
2019-09-25 12:40:14 +00:00
|
|
|
/// Cannot clone EventFd.
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error cloning EventFd: {0}")]
|
|
|
|
EventFdClone(#[source] io::Error),
|
2019-09-25 12:40:14 +00:00
|
|
|
|
2019-09-25 12:09:33 +00:00
|
|
|
/// Cannot create EventFd.
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error creating EventFd: {0}")]
|
|
|
|
EventFdCreate(#[source] io::Error),
|
2019-09-25 12:09:33 +00:00
|
|
|
|
2019-09-25 12:40:14 +00:00
|
|
|
/// Cannot read from EventFd.
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error reading from EventFd: {0}")]
|
|
|
|
EventFdRead(#[source] io::Error),
|
2019-05-10 08:46:27 +00:00
|
|
|
|
2019-09-25 12:40:14 +00:00
|
|
|
/// Cannot create epoll context.
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error creating epoll context: {0}")]
|
|
|
|
Epoll(#[source] io::Error),
|
2019-05-10 08:46:27 +00:00
|
|
|
|
2019-09-18 09:14:49 +00:00
|
|
|
/// Cannot create HTTP thread
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error spawning HTTP thread: {0}")]
|
|
|
|
HttpThreadSpawn(#[source] io::Error),
|
2019-09-18 09:14:49 +00:00
|
|
|
|
2019-09-25 12:40:14 +00:00
|
|
|
/// Cannot handle the VM STDIN stream
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error handling VM stdin: {0:?}")]
|
2019-09-25 12:40:14 +00:00
|
|
|
Stdin(VmError),
|
2019-05-10 08:46:27 +00:00
|
|
|
|
2019-10-01 14:41:50 +00:00
|
|
|
/// Cannot reboot the VM
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error rebooting VM: {0:?}")]
|
2019-10-01 14:41:50 +00:00
|
|
|
VmReboot(VmError),
|
2019-09-30 14:24:15 +00:00
|
|
|
|
2019-09-30 09:53:49 +00:00
|
|
|
/// Cannot shut a VM down
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error shutting down VM: {0:?}")]
|
2019-09-30 09:53:49 +00:00
|
|
|
VmShutdown(VmError),
|
2019-09-25 12:45:23 +00:00
|
|
|
|
|
|
|
/// Cannot create VMM thread
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error spawning VMM thread {0:?}")]
|
|
|
|
VmmThreadSpawn(#[source] io::Error),
|
2019-10-08 13:23:29 +00:00
|
|
|
|
|
|
|
/// Cannot shut the VMM down
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error shutting down VMM: {0:?}")]
|
2019-10-08 13:23:29 +00:00
|
|
|
VmmShutdown(VmError),
|
2020-02-14 09:55:19 +00:00
|
|
|
|
|
|
|
// Error following "exe" link
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error following \"exe\" link: {0}")]
|
|
|
|
ExePathReadLink(#[source] io::Error),
|
2020-03-20 14:57:17 +00:00
|
|
|
|
|
|
|
/// Cannot create seccomp filter
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error creating seccomp filter: {0}")]
|
2020-03-20 14:57:17 +00:00
|
|
|
CreateSeccompFilter(seccomp::SeccompError),
|
|
|
|
|
|
|
|
/// Cannot apply seccomp filter
|
2020-10-26 16:11:02 +00:00
|
|
|
#[error("Error applying seccomp filter: {0}")]
|
2020-03-20 14:57:17 +00:00
|
|
|
ApplySeccompFilter(seccomp::Error),
|
2019-05-10 08:46:27 +00:00
|
|
|
}
|
2019-09-25 12:40:14 +00:00
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
2019-02-22 16:04:35 +00:00
|
|
|
|
2019-09-25 12:25:08 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum EpollDispatch {
|
|
|
|
Exit,
|
|
|
|
Reset,
|
|
|
|
Stdin,
|
|
|
|
Api,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EpollContext {
|
2020-05-15 18:31:35 +00:00
|
|
|
epoll_file: File,
|
2019-09-25 12:25:08 +00:00
|
|
|
dispatch_table: Vec<Option<EpollDispatch>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EpollContext {
|
|
|
|
pub fn new() -> result::Result<EpollContext, io::Error> {
|
2020-05-15 18:31:35 +00:00
|
|
|
let epoll_fd = epoll::create(true)?;
|
|
|
|
// Use 'File' to enforce closing on 'epoll_fd'
|
|
|
|
let epoll_file = unsafe { File::from_raw_fd(epoll_fd) };
|
2019-09-25 12:25:08 +00:00
|
|
|
|
|
|
|
// Initial capacity needs to be large enough to hold:
|
|
|
|
// * 1 exit event
|
|
|
|
// * 1 reset event
|
|
|
|
// * 1 stdin event
|
|
|
|
// * 1 API event
|
|
|
|
let mut dispatch_table = Vec::with_capacity(5);
|
|
|
|
dispatch_table.push(None);
|
|
|
|
|
|
|
|
Ok(EpollContext {
|
2020-05-15 18:31:35 +00:00
|
|
|
epoll_file,
|
2019-09-25 12:25:08 +00:00
|
|
|
dispatch_table,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_stdin(&mut self) -> result::Result<(), io::Error> {
|
|
|
|
let dispatch_index = self.dispatch_table.len() as u64;
|
|
|
|
epoll::ctl(
|
2020-05-15 18:31:35 +00:00
|
|
|
self.epoll_file.as_raw_fd(),
|
2019-09-25 12:25:08 +00:00
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
|
|
|
libc::STDIN_FILENO,
|
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
self.dispatch_table.push(Some(EpollDispatch::Stdin));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_event<T>(&mut self, fd: &T, token: EpollDispatch) -> result::Result<(), io::Error>
|
|
|
|
where
|
|
|
|
T: AsRawFd,
|
|
|
|
{
|
|
|
|
let dispatch_index = self.dispatch_table.len() as u64;
|
|
|
|
epoll::ctl(
|
2020-05-15 18:31:35 +00:00
|
|
|
self.epoll_file.as_raw_fd(),
|
2019-09-25 12:25:08 +00:00
|
|
|
epoll::ControlOptions::EPOLL_CTL_ADD,
|
|
|
|
fd.as_raw_fd(),
|
|
|
|
epoll::Event::new(epoll::Events::EPOLLIN, dispatch_index),
|
|
|
|
)?;
|
|
|
|
self.dispatch_table.push(Some(token));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRawFd for EpollContext {
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
2020-05-15 18:31:35 +00:00
|
|
|
self.epoll_file.as_raw_fd()
|
2019-09-25 12:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:48:25 +00:00
|
|
|
pub struct PciDeviceInfo {
|
|
|
|
pub id: String,
|
|
|
|
pub bdf: u32,
|
|
|
|
}
|
|
|
|
|
2020-06-11 16:50:25 +00:00
|
|
|
impl Serialize for PciDeviceInfo {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
// Transform the PCI b/d/f into a standardized string.
|
|
|
|
let segment = (self.bdf >> 16) & 0xffff;
|
|
|
|
let bus = (self.bdf >> 8) & 0xff;
|
|
|
|
let device = (self.bdf >> 3) & 0x1f;
|
|
|
|
let function = self.bdf & 0x7;
|
|
|
|
let bdf_str = format!(
|
|
|
|
"{:04x}:{:02x}:{:02x}.{:01x}",
|
|
|
|
segment, bus, device, function
|
|
|
|
);
|
|
|
|
|
|
|
|
// Serialize the structure.
|
|
|
|
let mut state = serializer.serialize_struct("PciDeviceInfo", 2)?;
|
|
|
|
state.serialize_field("id", &self.id)?;
|
|
|
|
state.serialize_field("bdf", &bdf_str)?;
|
|
|
|
state.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 12:45:23 +00:00
|
|
|
pub fn start_vmm_thread(
|
2019-11-21 18:32:39 +00:00
|
|
|
vmm_version: String,
|
2019-09-26 16:19:00 +00:00
|
|
|
http_path: &str,
|
2019-09-25 12:45:23 +00:00
|
|
|
api_event: EventFd,
|
2019-09-26 16:19:00 +00:00
|
|
|
api_sender: Sender<ApiRequest>,
|
2019-09-25 12:45:23 +00:00
|
|
|
api_receiver: Receiver<ApiRequest>,
|
2020-07-30 21:21:58 +00:00
|
|
|
seccomp_action: &SeccompAction,
|
2020-06-02 02:29:54 +00:00
|
|
|
hypervisor: Arc<dyn hypervisor::Hypervisor>,
|
2019-09-25 12:45:23 +00:00
|
|
|
) -> Result<thread::JoinHandle<Result<()>>> {
|
2019-09-26 16:19:00 +00:00
|
|
|
let http_api_event = api_event.try_clone().map_err(Error::EventFdClone)?;
|
|
|
|
|
2020-03-20 14:57:17 +00:00
|
|
|
// Retrieve seccomp filter
|
|
|
|
let vmm_seccomp_filter =
|
2020-07-30 21:21:58 +00:00
|
|
|
get_seccomp_filter(seccomp_action, Thread::Vmm).map_err(Error::CreateSeccompFilter)?;
|
2020-03-20 14:57:17 +00:00
|
|
|
|
2020-02-14 09:55:19 +00:00
|
|
|
// Find the path that the "/proc/<pid>/exe" symlink points to. Must be done before spawning
|
|
|
|
// a thread as Rust does not put the child threads in the same thread group which prevents the
|
|
|
|
// link from being followed as per PTRACE_MODE_READ_FSCREDS (see proc(5) and ptrace(2)). The
|
|
|
|
// alternative is to run always with CAP_SYS_PTRACE but that is not a good idea.
|
|
|
|
let self_path = format!("/proc/{}/exe", std::process::id());
|
|
|
|
let vmm_path = std::fs::read_link(PathBuf::from(self_path)).map_err(Error::ExePathReadLink)?;
|
2020-07-30 22:33:52 +00:00
|
|
|
let vmm_seccomp_action = seccomp_action.clone();
|
2019-09-26 16:19:00 +00:00
|
|
|
let thread = thread::Builder::new()
|
2019-09-25 12:45:23 +00:00
|
|
|
.name("vmm".to_string())
|
|
|
|
.spawn(move || {
|
2020-03-20 14:57:17 +00:00
|
|
|
// Apply seccomp filter for VMM thread.
|
|
|
|
SeccompFilter::apply(vmm_seccomp_filter).map_err(Error::ApplySeccompFilter)?;
|
|
|
|
|
2020-07-30 22:33:52 +00:00
|
|
|
let mut vmm = Vmm::new(
|
|
|
|
vmm_version.to_string(),
|
|
|
|
api_event,
|
|
|
|
vmm_path,
|
|
|
|
vmm_seccomp_action,
|
|
|
|
hypervisor,
|
|
|
|
)?;
|
2019-09-25 12:45:23 +00:00
|
|
|
|
2019-10-08 13:53:39 +00:00
|
|
|
vmm.control_loop(Arc::new(api_receiver))
|
2019-09-25 12:45:23 +00:00
|
|
|
})
|
2019-09-26 16:19:00 +00:00
|
|
|
.map_err(Error::VmmThreadSpawn)?;
|
|
|
|
|
|
|
|
// The VMM thread is started, we can start serving HTTP requests
|
2020-07-30 21:21:58 +00:00
|
|
|
api::start_http_thread(http_path, http_api_event, api_sender, seccomp_action)?;
|
2019-09-26 16:19:00 +00:00
|
|
|
|
|
|
|
Ok(thread)
|
2019-09-25 12:45:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 12:40:14 +00:00
|
|
|
pub struct Vmm {
|
|
|
|
epoll: EpollContext,
|
|
|
|
exit_evt: EventFd,
|
|
|
|
reset_evt: EventFd,
|
|
|
|
api_evt: EventFd,
|
2019-11-21 18:32:39 +00:00
|
|
|
version: String,
|
2019-09-25 12:40:14 +00:00
|
|
|
vm: Option<Vm>,
|
2019-12-05 14:50:38 +00:00
|
|
|
vm_config: Option<Arc<Mutex<VmConfig>>>,
|
2020-02-14 09:55:19 +00:00
|
|
|
vmm_path: PathBuf,
|
2020-07-30 22:33:52 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-06-02 02:29:54 +00:00
|
|
|
hypervisor: Arc<dyn hypervisor::Hypervisor>,
|
2019-09-25 12:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vmm {
|
2020-06-02 02:29:54 +00:00
|
|
|
fn new(
|
|
|
|
vmm_version: String,
|
|
|
|
api_evt: EventFd,
|
|
|
|
vmm_path: PathBuf,
|
2020-07-30 22:33:52 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-06-02 02:29:54 +00:00
|
|
|
hypervisor: Arc<dyn hypervisor::Hypervisor>,
|
|
|
|
) -> Result<Self> {
|
2019-09-25 12:40:14 +00:00
|
|
|
let mut epoll = EpollContext::new().map_err(Error::Epoll)?;
|
|
|
|
let exit_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdCreate)?;
|
|
|
|
let reset_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdCreate)?;
|
|
|
|
|
|
|
|
if unsafe { libc::isatty(libc::STDIN_FILENO as i32) } != 0 {
|
|
|
|
epoll.add_stdin().map_err(Error::Epoll)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
epoll
|
|
|
|
.add_event(&exit_evt, EpollDispatch::Exit)
|
|
|
|
.map_err(Error::Epoll)?;
|
|
|
|
|
|
|
|
epoll
|
|
|
|
.add_event(&reset_evt, EpollDispatch::Reset)
|
|
|
|
.map_err(Error::Epoll)?;
|
|
|
|
|
|
|
|
epoll
|
|
|
|
.add_event(&api_evt, EpollDispatch::Api)
|
|
|
|
.map_err(Error::Epoll)?;
|
|
|
|
|
|
|
|
Ok(Vmm {
|
|
|
|
epoll,
|
|
|
|
exit_evt,
|
|
|
|
reset_evt,
|
|
|
|
api_evt,
|
2019-11-21 18:32:39 +00:00
|
|
|
version: vmm_version,
|
2019-09-25 12:40:14 +00:00
|
|
|
vm: None,
|
2019-09-30 14:17:28 +00:00
|
|
|
vm_config: None,
|
2020-02-14 09:55:19 +00:00
|
|
|
vmm_path,
|
2020-07-30 22:33:52 +00:00
|
|
|
seccomp_action,
|
2020-06-02 02:29:54 +00:00
|
|
|
hypervisor,
|
2019-09-25 12:40:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:03:38 +00:00
|
|
|
fn vm_boot(&mut self) -> result::Result<(), VmError> {
|
|
|
|
// Create a new VM is we don't have one yet.
|
|
|
|
if self.vm.is_none() {
|
|
|
|
let exit_evt = self.exit_evt.try_clone().map_err(VmError::EventFdClone)?;
|
|
|
|
let reset_evt = self.reset_evt.try_clone().map_err(VmError::EventFdClone)?;
|
|
|
|
|
|
|
|
if let Some(ref vm_config) = self.vm_config {
|
2020-02-14 09:55:19 +00:00
|
|
|
let vm = Vm::new(
|
|
|
|
Arc::clone(vm_config),
|
|
|
|
exit_evt,
|
|
|
|
reset_evt,
|
|
|
|
self.vmm_path.clone(),
|
2020-07-30 22:33:52 +00:00
|
|
|
&self.seccomp_action,
|
2020-06-02 02:29:54 +00:00
|
|
|
self.hypervisor.clone(),
|
2020-02-14 09:55:19 +00:00
|
|
|
)?;
|
2019-10-01 15:03:38 +00:00
|
|
|
self.vm = Some(vm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can boot the VM.
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
|
|
|
vm.boot()
|
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotCreated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:16:58 +00:00
|
|
|
fn vm_pause(&mut self) -> result::Result<(), VmError> {
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
2019-11-22 13:54:52 +00:00
|
|
|
vm.pause().map_err(VmError::Pause)
|
2019-10-10 15:16:58 +00:00
|
|
|
} else {
|
2019-10-10 16:00:44 +00:00
|
|
|
Err(VmError::VmNotRunning)
|
2019-10-10 15:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:34:55 +00:00
|
|
|
fn vm_resume(&mut self) -> result::Result<(), VmError> {
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
2019-11-22 13:54:52 +00:00
|
|
|
vm.resume().map_err(VmError::Resume)
|
2019-10-10 15:34:55 +00:00
|
|
|
} else {
|
2019-10-10 16:00:44 +00:00
|
|
|
Err(VmError::VmNotRunning)
|
2019-10-10 15:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 06:53:32 +00:00
|
|
|
fn vm_snapshot(&mut self, destination_url: &str) -> result::Result<(), VmError> {
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
|
|
|
vm.snapshot()
|
|
|
|
.map_err(VmError::Snapshot)
|
|
|
|
.and_then(|snapshot| {
|
|
|
|
vm.send(&snapshot, destination_url)
|
|
|
|
.map_err(VmError::SnapshotSend)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
2019-11-24 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:50:19 +00:00
|
|
|
fn vm_restore(&mut self, restore_cfg: RestoreConfig) -> result::Result<(), VmError> {
|
2020-04-02 06:56:47 +00:00
|
|
|
if self.vm.is_some() || self.vm_config.is_some() {
|
|
|
|
return Err(VmError::VmAlreadyCreated);
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:50:19 +00:00
|
|
|
let source_url = restore_cfg.source_url.as_path().to_str();
|
|
|
|
if source_url.is_none() {
|
|
|
|
return Err(VmError::RestoreSourceUrlPathToStr);
|
|
|
|
}
|
|
|
|
// Safe to unwrap as we checked it was Some(&str).
|
|
|
|
let source_url = source_url.unwrap();
|
|
|
|
|
2020-06-23 09:39:39 +00:00
|
|
|
let snapshot = recv_vm_snapshot(source_url).map_err(VmError::Restore)?;
|
|
|
|
let vm_snapshot = get_vm_snapshot(&snapshot).map_err(VmError::Restore)?;
|
2020-04-02 06:56:47 +00:00
|
|
|
|
2020-06-23 09:39:39 +00:00
|
|
|
self.vm_config = Some(Arc::clone(&vm_snapshot.config));
|
2020-04-02 06:56:47 +00:00
|
|
|
|
|
|
|
let exit_evt = self.exit_evt.try_clone().map_err(VmError::EventFdClone)?;
|
|
|
|
let reset_evt = self.reset_evt.try_clone().map_err(VmError::EventFdClone)?;
|
|
|
|
|
|
|
|
let vm = Vm::new_from_snapshot(
|
2020-06-23 09:39:39 +00:00
|
|
|
&snapshot,
|
2020-04-02 06:56:47 +00:00
|
|
|
exit_evt,
|
|
|
|
reset_evt,
|
|
|
|
self.vmm_path.clone(),
|
|
|
|
source_url,
|
2020-04-07 13:54:33 +00:00
|
|
|
restore_cfg.prefault,
|
2020-07-30 22:33:52 +00:00
|
|
|
&self.seccomp_action,
|
2020-06-02 02:29:54 +00:00
|
|
|
self.hypervisor.clone(),
|
2020-04-02 06:56:47 +00:00
|
|
|
)?;
|
|
|
|
self.vm = Some(vm);
|
|
|
|
|
|
|
|
// Now we can restore the rest of the VM.
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-06-23 09:39:39 +00:00
|
|
|
vm.restore(snapshot).map_err(VmError::Restore)
|
2020-04-02 06:56:47 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotCreated)
|
|
|
|
}
|
2020-02-25 23:03:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 15:03:38 +00:00
|
|
|
fn vm_shutdown(&mut self) -> result::Result<(), VmError> {
|
2019-10-08 09:20:09 +00:00
|
|
|
if let Some(ref mut vm) = self.vm.take() {
|
2019-10-01 15:03:38 +00:00
|
|
|
vm.shutdown()
|
|
|
|
} else {
|
2019-10-10 16:00:44 +00:00
|
|
|
Err(VmError::VmNotRunning)
|
2019-10-01 15:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 14:41:50 +00:00
|
|
|
fn vm_reboot(&mut self) -> result::Result<(), VmError> {
|
2019-09-27 14:48:16 +00:00
|
|
|
// Without ACPI, a reset is equivalent to a shutdown
|
2020-10-30 13:34:16 +00:00
|
|
|
// On AArch64, before ACPI is supported, we simply jump over this check and continue to reset.
|
|
|
|
#[cfg(all(target_arch = "x86_64", not(feature = "acpi")))]
|
2019-09-27 14:48:16 +00:00
|
|
|
{
|
2019-11-05 06:03:29 +00:00
|
|
|
if self.vm.is_some() {
|
2020-04-26 02:39:11 +00:00
|
|
|
self.exit_evt.write(1).unwrap();
|
|
|
|
return Ok(());
|
2019-09-27 14:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First we stop the current VM and create a new one.
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
|
|
|
let config = vm.get_config();
|
2019-11-05 06:03:29 +00:00
|
|
|
self.vm_shutdown()?;
|
2019-09-27 14:48:16 +00:00
|
|
|
|
2019-10-01 14:41:50 +00:00
|
|
|
let exit_evt = self.exit_evt.try_clone().map_err(VmError::EventFdClone)?;
|
|
|
|
let reset_evt = self.reset_evt.try_clone().map_err(VmError::EventFdClone)?;
|
2019-09-27 14:48:16 +00:00
|
|
|
|
2020-02-19 16:10:13 +00:00
|
|
|
// The Linux kernel fires off an i8042 reset after doing the ACPI reset so there may be
|
|
|
|
// an event sitting in the shared reset_evt. Without doing this we get very early reboots
|
|
|
|
// during the boot process.
|
|
|
|
if self.reset_evt.read().is_ok() {
|
|
|
|
warn!("Spurious second reset event received. Ignoring.");
|
|
|
|
}
|
2020-06-02 02:29:54 +00:00
|
|
|
self.vm = Some(Vm::new(
|
|
|
|
config,
|
|
|
|
exit_evt,
|
|
|
|
reset_evt,
|
|
|
|
self.vmm_path.clone(),
|
2020-07-30 22:33:52 +00:00
|
|
|
&self.seccomp_action,
|
2020-06-02 02:29:54 +00:00
|
|
|
self.hypervisor.clone(),
|
|
|
|
)?);
|
2019-09-27 14:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then we start the new VM.
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
2019-10-01 14:41:50 +00:00
|
|
|
vm.boot()?;
|
2019-09-30 14:24:15 +00:00
|
|
|
} else {
|
2019-10-01 14:41:50 +00:00
|
|
|
return Err(VmError::VmNotCreated);
|
2019-09-27 14:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-01 14:41:50 +00:00
|
|
|
fn vm_info(&self) -> result::Result<VmInfo, VmError> {
|
2019-10-01 12:07:15 +00:00
|
|
|
match &self.vm_config {
|
|
|
|
Some(config) => {
|
|
|
|
let state = match &self.vm {
|
2019-10-01 14:41:50 +00:00
|
|
|
Some(vm) => vm.get_state()?,
|
2019-10-01 12:07:15 +00:00
|
|
|
None => VmState::Created,
|
|
|
|
};
|
|
|
|
|
2020-08-25 06:44:21 +00:00
|
|
|
let config = Arc::clone(config);
|
|
|
|
|
|
|
|
let mut memory_actual_size = config.lock().unwrap().memory.total_size();
|
|
|
|
if let Some(vm) = &self.vm {
|
2020-10-14 10:04:42 +00:00
|
|
|
memory_actual_size -= vm.balloon_size();
|
2020-08-25 06:44:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 12:07:15 +00:00
|
|
|
Ok(VmInfo {
|
2020-08-25 06:44:21 +00:00
|
|
|
config,
|
2019-10-01 12:07:15 +00:00
|
|
|
state,
|
2020-08-25 06:44:21 +00:00
|
|
|
memory_actual_size,
|
2019-10-01 12:07:15 +00:00
|
|
|
})
|
|
|
|
}
|
2019-10-01 14:41:50 +00:00
|
|
|
None => Err(VmError::VmNotCreated),
|
2019-10-01 12:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 18:06:45 +00:00
|
|
|
fn vmm_ping(&self) -> result::Result<VmmPingResponse, ApiError> {
|
|
|
|
Ok(VmmPingResponse {
|
2019-11-21 18:32:39 +00:00
|
|
|
version: self.version.clone(),
|
2019-11-04 18:06:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:24:39 +00:00
|
|
|
fn vm_delete(&mut self) -> result::Result<(), VmError> {
|
|
|
|
if self.vm_config.is_none() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:41:35 +00:00
|
|
|
// If a VM is booted, we first try to shut it down.
|
|
|
|
if self.vm.is_some() {
|
|
|
|
self.vm_shutdown()?;
|
|
|
|
}
|
2019-10-01 15:24:39 +00:00
|
|
|
|
2019-10-08 09:20:09 +00:00
|
|
|
self.vm_config = None;
|
2019-10-01 15:24:39 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-08 13:23:29 +00:00
|
|
|
fn vmm_shutdown(&mut self) -> result::Result<(), VmError> {
|
|
|
|
self.vm_delete()
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:52:23 +00:00
|
|
|
fn vm_resize(
|
|
|
|
&mut self,
|
|
|
|
desired_vcpus: Option<u8>,
|
|
|
|
desired_ram: Option<u64>,
|
2020-10-14 10:04:42 +00:00
|
|
|
desired_balloon: Option<u64>,
|
2020-01-10 15:52:23 +00:00
|
|
|
) -> result::Result<(), VmError> {
|
2019-11-26 16:46:10 +00:00
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-10-14 10:04:42 +00:00
|
|
|
if let Err(e) = vm.resize(desired_vcpus, desired_ram, desired_balloon) {
|
2020-01-17 17:22:09 +00:00
|
|
|
error!("Error when resizing VM: {:?}", e);
|
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-26 16:46:10 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:34:15 +00:00
|
|
|
fn vm_resize_zone(&mut self, id: String, desired_ram: u64) -> result::Result<(), VmError> {
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
|
|
|
if let Err(e) = vm.resize_zone(id, desired_ram) {
|
|
|
|
error!("Error when resizing VM: {:?}", e);
|
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:40:27 +00:00
|
|
|
fn vm_add_device(&mut self, device_cfg: DeviceConfig) -> result::Result<Vec<u8>, VmError> {
|
2020-02-27 13:00:46 +00:00
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-06-11 15:40:27 +00:00
|
|
|
let info = vm.add_device(device_cfg).map_err(|e| {
|
2020-02-27 13:00:46 +00:00
|
|
|
error!("Error when adding new device to the VM: {:?}", e);
|
2020-06-11 15:40:27 +00:00
|
|
|
e
|
|
|
|
})?;
|
|
|
|
serde_json::to_vec(&info).map_err(VmError::SerializeJson)
|
2020-02-27 13:00:46 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 10:49:15 +00:00
|
|
|
fn vm_remove_device(&mut self, id: String) -> result::Result<(), VmError> {
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
|
|
|
if let Err(e) = vm.remove_device(id) {
|
2020-03-23 16:35:58 +00:00
|
|
|
error!("Error when removing new device to the VM: {:?}", e);
|
2020-03-09 10:49:15 +00:00
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:40:27 +00:00
|
|
|
fn vm_add_disk(&mut self, disk_cfg: DiskConfig) -> result::Result<Vec<u8>, VmError> {
|
2020-03-23 16:21:58 +00:00
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-06-11 15:40:27 +00:00
|
|
|
let info = vm.add_disk(disk_cfg).map_err(|e| {
|
2020-03-23 16:21:58 +00:00
|
|
|
error!("Error when adding new disk to the VM: {:?}", e);
|
2020-06-11 15:40:27 +00:00
|
|
|
e
|
|
|
|
})?;
|
|
|
|
serde_json::to_vec(&info).map_err(VmError::SerializeJson)
|
2020-03-23 16:21:58 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:40:27 +00:00
|
|
|
fn vm_add_fs(&mut self, fs_cfg: FsConfig) -> result::Result<Vec<u8>, VmError> {
|
2020-04-14 09:21:24 +00:00
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-06-11 15:40:27 +00:00
|
|
|
let info = vm.add_fs(fs_cfg).map_err(|e| {
|
2020-04-14 09:21:24 +00:00
|
|
|
error!("Error when adding new fs to the VM: {:?}", e);
|
2020-06-11 15:40:27 +00:00
|
|
|
e
|
|
|
|
})?;
|
|
|
|
serde_json::to_vec(&info).map_err(VmError::SerializeJson)
|
2020-04-14 09:21:24 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:40:27 +00:00
|
|
|
fn vm_add_pmem(&mut self, pmem_cfg: PmemConfig) -> result::Result<Vec<u8>, VmError> {
|
2020-03-23 16:21:58 +00:00
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-06-11 15:40:27 +00:00
|
|
|
let info = vm.add_pmem(pmem_cfg).map_err(|e| {
|
2020-03-23 16:21:58 +00:00
|
|
|
error!("Error when adding new pmem device to the VM: {:?}", e);
|
2020-06-11 15:40:27 +00:00
|
|
|
e
|
|
|
|
})?;
|
|
|
|
serde_json::to_vec(&info).map_err(VmError::SerializeJson)
|
2020-03-23 16:21:58 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:40:27 +00:00
|
|
|
fn vm_add_net(&mut self, net_cfg: NetConfig) -> result::Result<Vec<u8>, VmError> {
|
2020-03-23 16:21:58 +00:00
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-06-11 15:40:27 +00:00
|
|
|
let info = vm.add_net(net_cfg).map_err(|e| {
|
2020-03-23 16:21:58 +00:00
|
|
|
error!("Error when adding new network device to the VM: {:?}", e);
|
2020-06-11 15:40:27 +00:00
|
|
|
e
|
|
|
|
})?;
|
|
|
|
serde_json::to_vec(&info).map_err(VmError::SerializeJson)
|
2020-03-23 16:21:58 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 15:40:27 +00:00
|
|
|
fn vm_add_vsock(&mut self, vsock_cfg: VsockConfig) -> result::Result<Vec<u8>, VmError> {
|
2020-04-28 15:21:22 +00:00
|
|
|
if let Some(ref mut vm) = self.vm {
|
2020-06-11 15:40:27 +00:00
|
|
|
let info = vm.add_vsock(vsock_cfg).map_err(|e| {
|
2020-04-28 15:21:22 +00:00
|
|
|
error!("Error when adding new vsock device to the VM: {:?}", e);
|
2020-06-11 15:40:27 +00:00
|
|
|
e
|
|
|
|
})?;
|
|
|
|
serde_json::to_vec(&info).map_err(VmError::SerializeJson)
|
2020-04-28 15:21:22 +00:00
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 10:20:13 +00:00
|
|
|
fn vm_counters(&mut self) -> result::Result<Vec<u8>, VmError> {
|
|
|
|
if let Some(ref mut vm) = self.vm {
|
|
|
|
let info = vm.counters().map_err(|e| {
|
|
|
|
error!("Error when getting counters from the VM: {:?}", e);
|
|
|
|
e
|
|
|
|
})?;
|
|
|
|
serde_json::to_vec(&info).map_err(VmError::SerializeJson)
|
|
|
|
} else {
|
|
|
|
Err(VmError::VmNotRunning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 13:53:39 +00:00
|
|
|
fn control_loop(&mut self, api_receiver: Arc<Receiver<ApiRequest>>) -> Result<()> {
|
2019-09-25 12:40:14 +00:00
|
|
|
const EPOLL_EVENTS_LEN: usize = 100;
|
|
|
|
|
|
|
|
let mut events = vec![epoll::Event::new(epoll::Events::empty(), 0); EPOLL_EVENTS_LEN];
|
|
|
|
let epoll_fd = self.epoll.as_raw_fd();
|
|
|
|
|
|
|
|
'outer: loop {
|
|
|
|
let num_events = match epoll::wait(epoll_fd, -1, &mut events[..]) {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == io::ErrorKind::Interrupted {
|
|
|
|
// It's well defined from the epoll_wait() syscall
|
|
|
|
// documentation that the epoll loop can be interrupted
|
|
|
|
// before any of the requested events occurred or the
|
|
|
|
// timeout expired. In both those cases, epoll_wait()
|
|
|
|
// returns an error of type EINTR, but this should not
|
|
|
|
// be considered as a regular error. Instead it is more
|
|
|
|
// appropriate to retry, by calling into epoll_wait().
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return Err(Error::Epoll(e));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for event in events.iter().take(num_events) {
|
|
|
|
let dispatch_idx = event.data as usize;
|
|
|
|
|
|
|
|
if let Some(dispatch_type) = self.epoll.dispatch_table[dispatch_idx] {
|
|
|
|
match dispatch_type {
|
|
|
|
EpollDispatch::Exit => {
|
|
|
|
// Consume the event.
|
|
|
|
self.exit_evt.read().map_err(Error::EventFdRead)?;
|
2019-10-08 13:23:29 +00:00
|
|
|
self.vmm_shutdown().map_err(Error::VmmShutdown)?;
|
2019-09-25 12:40:14 +00:00
|
|
|
|
|
|
|
break 'outer;
|
|
|
|
}
|
|
|
|
EpollDispatch::Reset => {
|
|
|
|
// Consume the event.
|
|
|
|
self.reset_evt.read().map_err(Error::EventFdRead)?;
|
2019-10-08 13:53:39 +00:00
|
|
|
self.vm_reboot().map_err(Error::VmReboot)?;
|
2019-09-25 12:40:14 +00:00
|
|
|
}
|
|
|
|
EpollDispatch::Stdin => {
|
|
|
|
if let Some(ref vm) = self.vm {
|
|
|
|
vm.handle_stdin().map_err(Error::Stdin)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EpollDispatch::Api => {
|
|
|
|
// Consume the event.
|
|
|
|
self.api_evt.read().map_err(Error::EventFdRead)?;
|
|
|
|
|
|
|
|
// Read from the API receiver channel
|
|
|
|
let api_request = api_receiver.recv().map_err(Error::ApiRequestRecv)?;
|
|
|
|
|
|
|
|
match api_request {
|
|
|
|
ApiRequest::VmCreate(config, sender) => {
|
2019-09-30 14:17:28 +00:00
|
|
|
// We only store the passed VM config.
|
|
|
|
// The VM will be created when being asked to boot it.
|
|
|
|
let response = if self.vm_config.is_none() {
|
|
|
|
self.vm_config = Some(config);
|
|
|
|
Ok(ApiResponsePayload::Empty)
|
|
|
|
} else {
|
|
|
|
Err(ApiError::VmAlreadyCreated)
|
2019-09-25 12:40:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-10-01 15:24:39 +00:00
|
|
|
ApiRequest::VmDelete(sender) => {
|
2019-10-01 15:43:40 +00:00
|
|
|
let response = self
|
|
|
|
.vm_delete()
|
|
|
|
.map_err(ApiError::VmDelete)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
2019-10-01 15:24:39 +00:00
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-09-30 09:53:49 +00:00
|
|
|
ApiRequest::VmBoot(sender) => {
|
2019-09-30 14:17:28 +00:00
|
|
|
// If we don't have a config, we can not boot a VM.
|
|
|
|
if self.vm_config.is_none() {
|
|
|
|
sender
|
|
|
|
.send(Err(ApiError::VmMissingConfig))
|
|
|
|
.map_err(Error::ApiResponseSend)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:43:40 +00:00
|
|
|
let response = self
|
|
|
|
.vm_boot()
|
|
|
|
.map_err(ApiError::VmBoot)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
2019-09-25 12:40:14 +00:00
|
|
|
|
2019-09-30 14:24:15 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
2019-09-25 12:40:14 +00:00
|
|
|
}
|
2019-09-27 14:48:16 +00:00
|
|
|
ApiRequest::VmShutdown(sender) => {
|
2019-10-01 15:43:40 +00:00
|
|
|
let response = self
|
|
|
|
.vm_shutdown()
|
|
|
|
.map_err(ApiError::VmShutdown)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
2019-09-27 14:48:16 +00:00
|
|
|
|
2019-09-30 14:24:15 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
2019-09-27 14:48:16 +00:00
|
|
|
}
|
|
|
|
ApiRequest::VmReboot(sender) => {
|
2019-10-01 15:43:40 +00:00
|
|
|
let response = self
|
|
|
|
.vm_reboot()
|
|
|
|
.map_err(ApiError::VmReboot)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
2019-09-27 14:48:16 +00:00
|
|
|
|
2019-10-01 12:07:15 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
|
|
|
ApiRequest::VmInfo(sender) => {
|
2019-10-01 15:43:40 +00:00
|
|
|
let response = self
|
|
|
|
.vm_info()
|
|
|
|
.map_err(ApiError::VmInfo)
|
|
|
|
.map(ApiResponsePayload::VmInfo);
|
2019-10-01 12:07:15 +00:00
|
|
|
|
2019-09-27 14:48:16 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-11-04 18:06:45 +00:00
|
|
|
ApiRequest::VmmPing(sender) => {
|
|
|
|
let response = self.vmm_ping().map(ApiResponsePayload::VmmPing);
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-10-10 15:16:58 +00:00
|
|
|
ApiRequest::VmPause(sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_pause()
|
|
|
|
.map_err(ApiError::VmPause)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-10-10 15:34:55 +00:00
|
|
|
ApiRequest::VmResume(sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_resume()
|
|
|
|
.map_err(ApiError::VmResume)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-11-24 17:39:06 +00:00
|
|
|
ApiRequest::VmSnapshot(snapshot_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_snapshot(&snapshot_data.destination_url)
|
|
|
|
.map_err(ApiError::VmSnapshot)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-04-07 12:50:19 +00:00
|
|
|
ApiRequest::VmRestore(restore_data, sender) => {
|
2020-02-25 23:03:06 +00:00
|
|
|
let response = self
|
2020-04-07 12:50:19 +00:00
|
|
|
.vm_restore(restore_data.as_ref().clone())
|
2020-02-25 23:03:06 +00:00
|
|
|
.map_err(ApiError::VmRestore)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-10-08 13:23:29 +00:00
|
|
|
ApiRequest::VmmShutdown(sender) => {
|
|
|
|
let response = self
|
|
|
|
.vmm_shutdown()
|
|
|
|
.map_err(ApiError::VmmShutdown)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
|
|
|
|
break 'outer;
|
|
|
|
}
|
2019-11-26 16:46:10 +00:00
|
|
|
ApiRequest::VmResize(resize_data, sender) => {
|
|
|
|
let response = self
|
2020-01-10 15:52:23 +00:00
|
|
|
.vm_resize(
|
|
|
|
resize_data.desired_vcpus,
|
|
|
|
resize_data.desired_ram,
|
2020-10-14 10:04:42 +00:00
|
|
|
resize_data.desired_balloon,
|
2020-01-10 15:52:23 +00:00
|
|
|
)
|
2019-11-26 16:46:10 +00:00
|
|
|
.map_err(ApiError::VmResize)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-09-10 15:34:15 +00:00
|
|
|
ApiRequest::VmResizeZone(resize_zone_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_resize_zone(
|
|
|
|
resize_zone_data.id.clone(),
|
|
|
|
resize_zone_data.desired_ram,
|
|
|
|
)
|
|
|
|
.map_err(ApiError::VmResizeZone)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-02-27 13:00:46 +00:00
|
|
|
ApiRequest::VmAddDevice(add_device_data, sender) => {
|
|
|
|
let response = self
|
2020-03-11 15:17:51 +00:00
|
|
|
.vm_add_device(add_device_data.as_ref().clone())
|
2020-02-27 13:00:46 +00:00
|
|
|
.map_err(ApiError::VmAddDevice)
|
2020-06-11 15:40:27 +00:00
|
|
|
.map(ApiResponsePayload::VmAction);
|
2020-02-27 13:00:46 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-03-09 10:49:15 +00:00
|
|
|
ApiRequest::VmRemoveDevice(remove_device_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_remove_device(remove_device_data.id.clone())
|
|
|
|
.map_err(ApiError::VmRemoveDevice)
|
|
|
|
.map(|_| ApiResponsePayload::Empty);
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-03-23 16:21:58 +00:00
|
|
|
ApiRequest::VmAddDisk(add_disk_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_add_disk(add_disk_data.as_ref().clone())
|
|
|
|
.map_err(ApiError::VmAddDisk)
|
2020-06-11 15:40:27 +00:00
|
|
|
.map(ApiResponsePayload::VmAction);
|
2020-03-23 16:21:58 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-04-14 09:21:24 +00:00
|
|
|
ApiRequest::VmAddFs(add_fs_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_add_fs(add_fs_data.as_ref().clone())
|
|
|
|
.map_err(ApiError::VmAddFs)
|
2020-06-11 15:40:27 +00:00
|
|
|
.map(ApiResponsePayload::VmAction);
|
2020-04-14 09:21:24 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-03-23 16:21:58 +00:00
|
|
|
ApiRequest::VmAddPmem(add_pmem_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_add_pmem(add_pmem_data.as_ref().clone())
|
|
|
|
.map_err(ApiError::VmAddPmem)
|
2020-06-11 15:40:27 +00:00
|
|
|
.map(ApiResponsePayload::VmAction);
|
2020-03-23 16:21:58 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-03-23 16:21:58 +00:00
|
|
|
ApiRequest::VmAddNet(add_net_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_add_net(add_net_data.as_ref().clone())
|
|
|
|
.map_err(ApiError::VmAddNet)
|
2020-06-11 15:40:27 +00:00
|
|
|
.map(ApiResponsePayload::VmAction);
|
2020-03-23 16:21:58 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-04-28 15:21:22 +00:00
|
|
|
ApiRequest::VmAddVsock(add_vsock_data, sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_add_vsock(add_vsock_data.as_ref().clone())
|
|
|
|
.map_err(ApiError::VmAddVsock)
|
2020-06-11 15:40:27 +00:00
|
|
|
.map(ApiResponsePayload::VmAction);
|
2020-04-28 15:21:22 +00:00
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2020-06-24 10:20:13 +00:00
|
|
|
ApiRequest::VmCounters(sender) => {
|
|
|
|
let response = self
|
|
|
|
.vm_counters()
|
|
|
|
.map_err(ApiError::VmInfo)
|
|
|
|
.map(ApiResponsePayload::VmAction);
|
|
|
|
|
|
|
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
|
|
|
}
|
2019-09-25 12:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 13:53:39 +00:00
|
|
|
Ok(())
|
2019-09-25 12:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-12 11:53:47 +00:00
|
|
|
|
|
|
|
const CPU_MANAGER_SNAPSHOT_ID: &str = "cpu-manager";
|
|
|
|
const MEMORY_MANAGER_SNAPSHOT_ID: &str = "memory-manager";
|
|
|
|
const DEVICE_MANAGER_SNAPSHOT_ID: &str = "device-manager";
|