vmm: Add a VmRestore command

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2020-02-26 00:03:06 +01:00 committed by Rob Bradford
parent 39d4f817f0
commit 92c73c3b78
4 changed files with 72 additions and 0 deletions

View File

@ -98,6 +98,9 @@ pub enum ApiError {
/// The VM could not be snapshotted. /// The VM could not be snapshotted.
VmSnapshot(VmError), VmSnapshot(VmError),
/// The VM could not restored.
VmRestore(VmError),
/// The VMM could not shutdown. /// The VMM could not shutdown.
VmmShutdown(VmError), VmmShutdown(VmError),
@ -155,6 +158,13 @@ pub struct VmSnapshotConfig {
pub destination_url: String, pub destination_url: String,
} }
#[derive(Clone, Deserialize, Serialize)]
pub struct VmRestoreConfig {
/// The snapshot restore source URL.
/// This is where the VMM is going to get its snapshot to restore itself from.
pub source_url: String,
}
pub enum ApiResponsePayload { pub enum ApiResponsePayload {
/// No data is sent on the channel. /// No data is sent on the channel.
Empty, Empty,
@ -235,6 +245,9 @@ pub enum ApiRequest {
/// Take a VM snapshot /// Take a VM snapshot
VmSnapshot(Arc<VmSnapshotConfig>, Sender<ApiResponse>), VmSnapshot(Arc<VmSnapshotConfig>, Sender<ApiResponse>),
/// Restore from a VM snapshot
VmRestore(Arc<VmRestoreConfig>, Sender<ApiResponse>),
} }
pub fn vm_create( pub fn vm_create(
@ -341,6 +354,24 @@ pub fn vm_snapshot(
Ok(()) Ok(())
} }
pub fn vm_restore(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
data: Arc<VmRestoreConfig>,
) -> ApiResult<()> {
let (response_sender, response_receiver) = channel();
// Send the VM restore request.
api_sender
.send(ApiRequest::VmRestore(data, response_sender))
.map_err(ApiError::RequestSend)?;
api_evt.write(1).map_err(ApiError::EventFdWrite)?;
response_receiver.recv().map_err(ApiError::ResponseRecv)??;
Ok(())
}
pub fn vm_info(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResult<VmInfo> { pub fn vm_info(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResult<VmInfo> {
let (response_sender, response_receiver) = channel(); let (response_sender, response_receiver) = channel();

View File

@ -292,6 +292,10 @@ impl Vmm {
Ok(()) Ok(())
} }
fn vm_restore(&mut self, _source_url: &str) -> result::Result<(), VmError> {
Ok(())
}
fn vm_shutdown(&mut self) -> result::Result<(), VmError> { fn vm_shutdown(&mut self) -> result::Result<(), VmError> {
if let Some(ref mut vm) = self.vm.take() { if let Some(ref mut vm) = self.vm.take() {
vm.shutdown() vm.shutdown()
@ -601,6 +605,14 @@ impl Vmm {
sender.send(response).map_err(Error::ApiResponseSend)?; sender.send(response).map_err(Error::ApiResponseSend)?;
} }
ApiRequest::VmRestore(snapshot_data, sender) => {
let response = self
.vm_restore(&snapshot_data.source_url)
.map_err(ApiError::VmRestore)
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
}
ApiRequest::VmmShutdown(sender) => { ApiRequest::VmmShutdown(sender) => {
let response = self let response = self
.vmm_shutdown() .vmm_shutdown()

View File

@ -2,10 +2,13 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
use crate::config::VmConfig;
use crate::vm::{VmSnapshot, VM_SNAPSHOT_ID};
use anyhow::anyhow; use anyhow::anyhow;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use url::Url; use url::Url;
use vm_migration::{MigratableError, Snapshot}; use vm_migration::{MigratableError, Snapshot};
@ -59,3 +62,23 @@ pub fn recv_vm_snapshot(source_url: &str) -> std::result::Result<Snapshot, Migra
))), ))),
} }
} }
pub fn vm_config_from_snapshot(
snapshot: &Snapshot,
) -> std::result::Result<Arc<Mutex<VmConfig>>, MigratableError> {
if let Some(vm_section) = snapshot
.snapshot_data
.get(&format!("{}-section", VM_SNAPSHOT_ID))
{
let vm_snapshot: VmSnapshot =
serde_json::from_slice(&vm_section.snapshot).map_err(|e| {
MigratableError::Restore(anyhow!("Could not deserialize VM snapshot {}", e))
})?;
return Ok(vm_snapshot.config);
}
Err(MigratableError::Restore(anyhow!(
"Could not find VM config snapshot section"
)))
}

View File

@ -139,6 +139,9 @@ pub enum Error {
/// VM is not created /// VM is not created
VmNotCreated, VmNotCreated,
/// VM is already created
VmAlreadyCreated,
/// VM is not running /// VM is not running
VmNotRunning, VmNotRunning,
@ -184,6 +187,9 @@ pub enum Error {
/// Cannot snapshot VM /// Cannot snapshot VM
Snapshot(MigratableError), Snapshot(MigratableError),
/// Cannot restore VM
Restore(MigratableError),
/// Cannot send VM snapshot /// Cannot send VM snapshot
SnapshotSend(MigratableError), SnapshotSend(MigratableError),
} }