vmm: Switch MemoryManager::send() to url_to_path()

This continues the work in cc78a597cd

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-03-12 13:13:33 +00:00 committed by Sebastien Boeuf
parent cc78a597cd
commit ab4b30edd3

View File

@ -28,7 +28,6 @@ use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::path::PathBuf; use std::path::PathBuf;
use std::result; use std::result;
use std::sync::{Arc, Barrier, Mutex}; use std::sync::{Arc, Barrier, Mutex};
use url::Url;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use vm_allocator::GsiApic; use vm_allocator::GsiApic;
use vm_allocator::SystemAllocator; use vm_allocator::SystemAllocator;
@ -2042,60 +2041,31 @@ impl Transportable for MemoryManager {
_snapshot: &Snapshot, _snapshot: &Snapshot,
destination_url: &str, destination_url: &str,
) -> result::Result<(), MigratableError> { ) -> result::Result<(), MigratableError> {
let url = Url::parse(destination_url).map_err(|e| { let vm_memory_snapshot_path = url_to_path(destination_url)?;
MigratableError::MigrateSend(anyhow!("Could not parse destination URL: {}", e))
})?;
match url.scheme() { if let Some(guest_memory) = &*self.snapshot.lock().unwrap() {
"file" => { for region in self.snapshot_memory_regions.iter() {
let vm_memory_snapshot_path = url if let Some(content) = &region.content {
.to_file_path() let mut memory_region_path = vm_memory_snapshot_path.clone();
.map_err(|_| { memory_region_path.push(content);
MigratableError::MigrateSend(anyhow!(
"Could not convert file URL to a file path: {}",
destination_url
))
})
.and_then(|path| {
if !path.is_dir() {
return Err(MigratableError::MigrateSend(anyhow!(
"Destination is not a directory"
)));
}
Ok(path)
})?;
if let Some(guest_memory) = &*self.snapshot.lock().unwrap() { // Create the snapshot file for the region
for region in self.snapshot_memory_regions.iter() { let mut memory_region_file = OpenOptions::new()
if let Some(content) = &region.content { .read(true)
let mut memory_region_path = vm_memory_snapshot_path.clone(); .write(true)
memory_region_path.push(content); .create_new(true)
.open(memory_region_path)
.map_err(|e| MigratableError::MigrateSend(e.into()))?;
// Create the snapshot file for the region guest_memory
let mut memory_region_file = OpenOptions::new() .write_all_to(
.read(true) region.start_addr,
.write(true) &mut memory_region_file,
.create_new(true) region.size as usize,
.open(memory_region_path) )
.map_err(|e| MigratableError::MigrateSend(e.into()))?; .map_err(|e| MigratableError::MigrateSend(e.into()))?;
guest_memory
.write_all_to(
region.start_addr,
&mut memory_region_file,
region.size as usize,
)
.map_err(|e| MigratableError::MigrateSend(e.into()))?;
}
}
} }
} }
_ => {
return Err(MigratableError::MigrateSend(anyhow!(
"Unsupported VM transport URL scheme: {}",
url.scheme()
)))
}
} }
Ok(()) Ok(())
} }