Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustify snapshot module #4691

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/firecracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,11 @@ fn print_snapshot_data_format(snapshot_path: &str) -> Result<(), SnapshotVersion
let mut snapshot_reader =
File::open(snapshot_path).map_err(SnapshotVersionError::OpenSnapshot)?;

let data_format_version = Snapshot::get_format_version(&mut snapshot_reader)
.map_err(SnapshotVersionError::SnapshotVersion)?;
// TODO: Need to fix this
// let data_format_version = Snapshot::get_format_version(&mut snapshot_reader)
// .map_err(SnapshotVersionError::SnapshotVersion)?;

println!("v{}", data_format_version);
// println!("v{}", data_format_version);
Ok(())
}

Expand Down
13 changes: 8 additions & 5 deletions src/snapshot-editor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::PathBuf;
use fc_utils::u64_to_usize;
use semver::Version;
use vmm::persist::MicrovmState;
use vmm::snapshot::Snapshot;
use vmm::snapshot::{Snapshot, SnapshotError, SnapshotHdr};

// Some errors are only used in aarch64 code
#[allow(unused)]
Expand All @@ -26,11 +26,13 @@ pub enum UtilsError {
}

#[allow(unused)]
pub fn open_vmstate(snapshot_path: &PathBuf) -> Result<(MicrovmState, Version), UtilsError> {
pub fn open_vmstate(snapshot_path: &PathBuf) -> Result<Snapshot<MicrovmState>, UtilsError> {
let mut snapshot_reader = File::open(snapshot_path).map_err(UtilsError::VmStateFileOpen)?;
let metadata = std::fs::metadata(snapshot_path).map_err(UtilsError::VmStateFileMeta)?;
let snapshot_len = u64_to_usize(metadata.len());
Snapshot::load(&mut snapshot_reader, snapshot_len).map_err(UtilsError::VmStateLoad)

let snapshot: Result<Snapshot<MicrovmState>, UtilsError> = Snapshot::load(&mut snapshot_reader, snapshot_len).map_err(UtilsError::VmStateLoad);
snapshot
}

// This method is used only in aarch64 code so far
Expand All @@ -46,9 +48,10 @@ pub fn save_vmstate(
.truncate(true)
.open(output_path)
.map_err(UtilsError::OutputFileOpen)?;
let mut snapshot = Snapshot::new(version);
let snapshot_hdr = SnapshotHdr::new(version);
let snapshot = Snapshot::new(snapshot_hdr, microvm_state);
snapshot
.save(&mut output_file, &microvm_state)
.save(&mut output_file)
.map_err(UtilsError::VmStateSave)?;
Ok(())
}
24 changes: 14 additions & 10 deletions src/vmm/src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::device_manager::persist::ACPIDeviceManagerState;
use crate::device_manager::persist::{DevicePersistError, DeviceStates};
use crate::logger::{info, warn};
use crate::resources::VmResources;
use crate::snapshot::Snapshot;
use crate::snapshot::{Snapshot, SnapshotHdr};
use crate::vmm_config::boot_source::BootSourceConfig;
use crate::vmm_config::instance_info::InstanceInfo;
use crate::vmm_config::machine_config::{HugePageConfig, MachineConfigUpdate, VmConfigError};
Expand Down Expand Up @@ -191,9 +191,10 @@ fn snapshot_state_to_file(
.open(snapshot_path)
.map_err(|err| SnapshotBackingFile("open", err))?;

let snapshot = Snapshot::new(SNAPSHOT_VERSION);
let snapshot_hdr = SnapshotHdr::new(SNAPSHOT_VERSION);
let snapshot = Snapshot::new(snapshot_hdr, microvm_state);
snapshot
.save(&mut snapshot_file, microvm_state)
.save(&mut snapshot_file)
.map_err(SerializeMicrovmState)?;
snapshot_file
.flush()
Expand Down Expand Up @@ -476,15 +477,14 @@ pub enum SnapshotStateFromFileError {
fn snapshot_state_from_file(
snapshot_path: &Path,
) -> Result<MicrovmState, SnapshotStateFromFileError> {
let snapshot = Snapshot::new(SNAPSHOT_VERSION);
let mut snapshot_reader =
File::open(snapshot_path).map_err(SnapshotStateFromFileError::Open)?;
let metadata = std::fs::metadata(snapshot_path).map_err(SnapshotStateFromFileError::Meta)?;
let snapshot_len = u64_to_usize(metadata.len());
let state: MicrovmState = snapshot
.load_with_version_check(&mut snapshot_reader, snapshot_len)
let state: Snapshot<MicrovmState> = Snapshot::load(&mut snapshot_reader, snapshot_len)
.map_err(SnapshotStateFromFileError::Load)?;
Ok(state)

Ok(state.data)
}

/// Error type for [`guest_memory_from_file`].
Expand Down Expand Up @@ -733,10 +733,14 @@ mod tests {
};

let mut buf = vec![0; 10000];
Snapshot::serialize(&mut buf.as_mut_slice(), &microvm_state).unwrap();

let restored_microvm_state: MicrovmState =
Snapshot::deserialize(&mut buf.as_slice()).unwrap();
let snapshot_hdr = SnapshotHdr::new(Version::new(1, 0, 42));
let snapshot = Snapshot::new(snapshot_hdr, microvm_state);
snapshot.save(&mut buf.as_mut_slice()).unwrap();

let restored_snapshot: Snapshot<MicrovmState> =
Snapshot::load(&mut buf.as_slice(), buf.len()).unwrap();
let restored_microvm_state = restored_snapshot.data;

assert_eq!(restored_microvm_state.vm_info, microvm_state.vm_info);
assert_eq!(
Expand Down
Loading
Loading