vmm: memory_manager: Make the snapshot source directory an Option

This allows the code to be reused when creating the VM from a snapshot
when doing VM migration.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-11-04 14:58:18 +00:00 committed by Samuel Ortiz
parent 523029449e
commit dfe2dadb3e
3 changed files with 48 additions and 42 deletions

View File

@ -397,7 +397,7 @@ impl Vmm {
&snapshot, &snapshot,
exit_evt, exit_evt,
reset_evt, reset_evt,
source_url, Some(source_url),
restore_cfg.prefault, restore_cfg.prefault,
&self.seccomp_action, &self.seccomp_action,
self.hypervisor.clone(), self.hypervisor.clone(),

View File

@ -760,54 +760,60 @@ impl MemoryManager {
snapshot: &Snapshot, snapshot: &Snapshot,
vm: Arc<dyn hypervisor::Vm>, vm: Arc<dyn hypervisor::Vm>,
config: &MemoryConfig, config: &MemoryConfig,
source_url: &str, source_url: Option<&str>,
prefault: bool, prefault: bool,
phys_bits: u8, phys_bits: u8,
) -> Result<Arc<Mutex<MemoryManager>>, Error> { ) -> Result<Arc<Mutex<MemoryManager>>, Error> {
let url = Url::parse(source_url).unwrap(); let mm = MemoryManager::new(vm, config, prefault, phys_bits)?;
/* url must be valid dir which is verified in recv_vm_snapshot() */
let vm_snapshot_path = url.to_file_path().unwrap();
if let Some(mem_section) = snapshot if let Some(source_url) = source_url {
.snapshot_data let url = Url::parse(source_url).unwrap();
.get(&format!("{}-section", MEMORY_MANAGER_SNAPSHOT_ID)) /* url must be valid dir which is verified in recv_vm_snapshot() */
{ let vm_snapshot_path = url.to_file_path().unwrap();
let mem_snapshot: MemoryManagerSnapshotData =
match serde_json::from_slice(&mem_section.snapshot) { if let Some(mem_section) = snapshot
Ok(snapshot) => snapshot, .snapshot_data
Err(error) => { .get(&format!("{}-section", MEMORY_MANAGER_SNAPSHOT_ID))
return Err(Error::Restore(MigratableError::Restore(anyhow!( {
"Could not deserialize MemoryManager {}", let mem_snapshot: MemoryManagerSnapshotData =
error match serde_json::from_slice(&mem_section.snapshot) {
)))) Ok(snapshot) => snapshot,
Err(error) => {
return Err(Error::Restore(MigratableError::Restore(anyhow!(
"Could not deserialize MemoryManager {}",
error
))))
}
};
// Here we turn the content file name into a content file path as
// this will be needed to copy the content of the saved memory
// region into the newly created memory region.
// We simply ignore the content files that are None, as they
// represent regions that have been directly saved by the user, with
// no need for saving into a dedicated external file. For these
// files, the VmConfig already contains the information on where to
// find them.
let mut saved_regions = mem_snapshot.memory_regions;
for region in saved_regions.iter_mut() {
if let Some(content) = &mut region.content {
let mut memory_region_path = vm_snapshot_path.clone();
memory_region_path.push(content.clone());
*content = memory_region_path;
} }
};
// Here we turn the content file name into a content file path as
// this will be needed to copy the content of the saved memory
// region into the newly created memory region.
// We simply ignore the content files that are None, as they
// represent regions that have been directly saved by the user, with
// no need for saving into a dedicated external file. For these
// files, the VmConfig already contains the information on where to
// find them.
let mut saved_regions = mem_snapshot.memory_regions;
for region in saved_regions.iter_mut() {
if let Some(content) = &mut region.content {
let mut memory_region_path = vm_snapshot_path.clone();
memory_region_path.push(content.clone());
*content = memory_region_path;
} }
}
let mm = MemoryManager::new(vm, config, prefault, phys_bits)?; mm.lock().unwrap().fill_saved_regions(saved_regions)?;
mm.lock().unwrap().fill_saved_regions(saved_regions)?;
Ok(mm) Ok(mm)
} else {
Err(Error::Restore(MigratableError::Restore(anyhow!(
"Could not find {}-section from snapshot",
MEMORY_MANAGER_SNAPSHOT_ID
))))
}
} else { } else {
Err(Error::Restore(MigratableError::Restore(anyhow!( Ok(mm)
"Could not find {}-section from snapshot",
MEMORY_MANAGER_SNAPSHOT_ID
))))
} }
} }

View File

@ -680,7 +680,7 @@ impl Vm {
snapshot: &Snapshot, snapshot: &Snapshot,
exit_evt: EventFd, exit_evt: EventFd,
reset_evt: EventFd, reset_evt: EventFd,
source_url: &str, source_url: Option<&str>,
prefault: bool, prefault: bool,
seccomp_action: &SeccompAction, seccomp_action: &SeccompAction,
hypervisor: Arc<dyn hypervisor::Hypervisor>, hypervisor: Arc<dyn hypervisor::Hypervisor>,