From 452475c280fb4ecf54bc1b971b7721b456f86b95 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Wed, 1 Apr 2020 17:22:40 +0200 Subject: [PATCH] vmm: Add migration helpers Signed-off-by: Sebastien Boeuf --- vmm/src/migration.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 vmm/src/migration.rs diff --git a/vmm/src/migration.rs b/vmm/src/migration.rs new file mode 100644 index 000000000..a3408dc6b --- /dev/null +++ b/vmm/src/migration.rs @@ -0,0 +1,61 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 + +use anyhow::anyhow; +use std::fs::File; +use std::io::BufReader; +use std::path::PathBuf; +use url::Url; +use vm_migration::{MigratableError, Snapshot}; + +pub const VM_SNAPSHOT_FILE: &str = "vm.json"; + +pub fn url_to_path(url: &Url) -> std::result::Result { + match url.scheme() { + "file" => url + .to_file_path() + .map_err(|_| { + MigratableError::MigrateSend(anyhow!("Could not convert file URL to a file path")) + }) + .and_then(|path| { + if !path.is_dir() { + return Err(MigratableError::MigrateSend(anyhow!( + "Destination is not a directory" + ))); + } + Ok(path) + }), + + _ => Err(MigratableError::MigrateSend(anyhow!( + "URL scheme is not file: {}", + url.scheme() + ))), + } +} + +pub fn recv_vm_snapshot(source_url: &str) -> std::result::Result { + let url = Url::parse(source_url).map_err(|e| { + MigratableError::MigrateSend(anyhow!("Could not parse destination URL: {}", e)) + })?; + + match url.scheme() { + "file" => { + let mut vm_snapshot_path = url_to_path(&url)?; + vm_snapshot_path.push(VM_SNAPSHOT_FILE); + + // Try opening the snapshot file + let vm_snapshot_file = + File::open(vm_snapshot_path).map_err(|e| MigratableError::MigrateSend(e.into()))?; + let vm_snapshot_reader = BufReader::new(vm_snapshot_file); + let vm_snapshot = serde_json::from_reader(vm_snapshot_reader) + .map_err(|e| MigratableError::MigrateReceive(e.into()))?; + + Ok(vm_snapshot) + } + _ => Err(MigratableError::MigrateSend(anyhow!( + "Unsupported VM transport URL scheme: {}", + url.scheme() + ))), + } +}