Skip to content

Commit bb98537

Browse files
committed
1 parent e725273 commit bb98537

File tree

6 files changed

+68
-56
lines changed

6 files changed

+68
-56
lines changed

src/snapshot-editor/src/utils.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ pub enum UtilsError {
2626
}
2727

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

3434
let snapshot: Result<Snapshot<MicrovmState>, UtilsError> = Snapshot::load(&mut snapshot_reader, snapshot_len).map_err(UtilsError::VmStateLoad);
35-
snapshot
35+
match snapshot {
36+
Ok(snapshot) => {
37+
let version = snapshot.version();
38+
Ok((snapshot.data, version.to_owned()))
39+
}
40+
Err(e) => {
41+
return Err(e);
42+
}
43+
}
44+
3645
}
3746

3847
// This method is used only in aarch64 code so far

src/vmm/src/persist.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ mod tests {
731731
#[cfg(target_arch = "x86_64")]
732732
acpi_dev_state: vmm.acpi_device_manager.save(),
733733
};
734+
let vm_info = microvm_state.vm_info.clone();
735+
let device_states = microvm_state.device_states.clone();
734736

735737
let mut buf = vec![0; 10000];
736738

@@ -742,10 +744,10 @@ mod tests {
742744
Snapshot::load(&mut buf.as_slice(), buf.len()).unwrap();
743745
let restored_microvm_state = restored_snapshot.data;
744746

745-
assert_eq!(restored_microvm_state.vm_info, microvm_state.vm_info);
747+
assert_eq!(restored_microvm_state.vm_info, vm_info);
746748
assert_eq!(
747749
restored_microvm_state.device_states,
748-
microvm_state.device_states
750+
device_states
749751
)
750752
}
751753

src/vmm/src/snapshot/mod.rs

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,16 @@ impl SnapshotHdr {
8282
}
8383

8484
pub fn load<R: Read>(reader: &mut R) -> Result<Self, SnapshotError> {
85-
let hdr: SnapshotHdr = bincode::DefaultOptions::new()
86-
.with_limit(VM_STATE_DESERIALIZE_LIMIT)
87-
.with_fixint_encoding()
88-
.allow_trailing_bytes() // need this because we deserialize header and snapshot from the same file, so after
89-
// reading the header, there will be trailing bytes.
90-
.deserialize_from(reader)
91-
.map_err(|err| SnapshotError::Serde(err.to_string()))?;
85+
let hdr: SnapshotHdr = deserialize(reader)?;
9286

9387
Ok(hdr)
9488
}
9589

9690
pub fn store<W: Write>(&self, writer: &mut W) -> Result<(), SnapshotError> {
97-
bincode::serialize_into(writer, self).map_err(|err| SnapshotError::Serde(err.to_string()))
91+
match serialize(writer, self) {
92+
Ok(_) => Ok(()),
93+
Err(e) => Err(e)
94+
}
9895
}
9996
}
10097

@@ -105,33 +102,17 @@ pub struct Snapshot<Data> {
105102
pub data: Data,
106103
}
107104

108-
impl<Data: for<'a> Deserialize<'a>> Snapshot<Data> {
109-
/// Helper function to deserialize an object from a reader
110-
pub fn deserialize<T, O>(reader: &mut T) -> Result<O, SnapshotError>
111-
where
112-
T: Read,
113-
O: DeserializeOwned + Debug,
114-
{
115-
// flags below are those used by default by bincode::deserialize_from, plus `with_limit`.
116-
bincode::DefaultOptions::new()
117-
.with_limit(VM_STATE_DESERIALIZE_LIMIT)
118-
.with_fixint_encoding()
119-
.allow_trailing_bytes() // need this because we deserialize header and snapshot from the same file, so after
120-
// reading the header, there will be trailing bytes.
121-
.deserialize_from(reader)
122-
.map_err(|err| SnapshotError::Serde(err.to_string()))
123-
}
124-
105+
impl<Data: DeserializeOwned> Snapshot<Data> {
125106
pub fn load_unchecked<R: Read>(reader: &mut R) -> Result<Self, SnapshotError>
126107
where
127108
Data: DeserializeOwned + Debug,
128109
{
129-
let hdr: SnapshotHdr = Self::deserialize(reader)?;
110+
let hdr: SnapshotHdr = deserialize(reader)?;
130111
if hdr.magic != SNAPSHOT_MAGIC_ID {
131112
return Err(SnapshotError::InvalidMagic(hdr.magic));
132113
}
133114

134-
let data: Data = Self::deserialize(reader)?;
115+
let data: Data = deserialize(reader)?;
135116
Ok(Self { header: hdr, data })
136117
}
137118

@@ -156,7 +137,7 @@ impl<Data: for<'a> Deserialize<'a>> Snapshot<Data> {
156137
// 2 statements is important, we first get the checksum computed on the read bytes
157138
// then read the stored checksum.
158139
let computed_checksum = crc_reader.checksum();
159-
let stored_checksum: u64 = Self::deserialize(&mut crc_reader)?;
140+
let stored_checksum: u64 = deserialize(&mut crc_reader)?;
160141
if computed_checksum != stored_checksum {
161142
return Err(SnapshotError::Crc64(computed_checksum));
162143
}
@@ -167,29 +148,11 @@ impl<Data: for<'a> Deserialize<'a>> Snapshot<Data> {
167148
}
168149

169150
impl<Data: Serialize + Debug> Snapshot<Data> {
170-
/// Helper function to serialize an object to a writer
171-
pub fn serialize<T, O>(writer: &mut T, data: &O) -> Result<usize, SnapshotError>
172-
where
173-
T: Write,
174-
O: Serialize + Debug,
175-
{
176-
let mut buffer = Vec::new();
177-
bincode::serialize_into(&mut buffer, data)
178-
.map_err(|err| SnapshotError::Serde(err.to_string()))?;
179-
180-
writer
181-
.write_all(&buffer)
182-
.map_err(|err| SnapshotError::Serde(err.to_string()))?;
183-
184-
Ok(buffer.len())
185-
// bincode::serialize_into(writer, data).map_err(|err| SnapshotError::Serde(err.to_string()))
186-
}
187-
188151
pub fn save<W: Write>(&self, mut writer: &mut W) -> Result<usize, SnapshotError> {
189152
// Write magic value and snapshot version
190-
Self::serialize(&mut writer, &SnapshotHdr::new(self.header.version.clone()))?;
153+
serialize(&mut writer, &SnapshotHdr::new(self.header.version.clone()))?;
191154
// Write data
192-
Self::serialize(&mut writer, &self.data)
155+
serialize(&mut writer, &self.data)
193156
}
194157

195158
pub fn save_with_crc<W: Write>(&self, writer: &mut W) -> Result<usize, SnapshotError> {
@@ -198,14 +161,52 @@ impl<Data: Serialize + Debug> Snapshot<Data> {
198161

199162
// Now write CRC value
200163
let checksum = crc_writer.checksum();
201-
Self::serialize(&mut crc_writer, &checksum)
164+
serialize(&mut crc_writer, &checksum)
202165
}
203166
}
204167

205168
impl<Data> Snapshot<Data> {
206169
pub fn new(header: SnapshotHdr, data: Data) -> Self {
207170
Snapshot { header, data }
208171
}
172+
173+
pub fn version(&self) -> Version {
174+
self.header.version.clone()
175+
}
176+
}
177+
178+
/// Helper function to deserialize an object from a reader
179+
fn deserialize<T, O>(reader: &mut T) -> Result<O, SnapshotError>
180+
where
181+
T: Read,
182+
O: DeserializeOwned + Debug,
183+
{
184+
// flags below are those used by default by bincode::deserialize_from, plus `with_limit`.
185+
bincode::DefaultOptions::new()
186+
.with_limit(VM_STATE_DESERIALIZE_LIMIT)
187+
.with_fixint_encoding()
188+
.allow_trailing_bytes() // need this because we deserialize header and snapshot from the same file, so after
189+
// reading the header, there will be trailing bytes.
190+
.deserialize_from(reader)
191+
.map_err(|err| SnapshotError::Serde(err.to_string()))
192+
}
193+
194+
/// Helper function to serialize an object to a writer
195+
fn serialize<T, O>(writer: &mut T, data: &O) -> Result<usize, SnapshotError>
196+
where
197+
T: Write,
198+
O: Serialize + Debug,
199+
{
200+
let mut buffer = Vec::new();
201+
bincode::serialize_into(&mut buffer, data)
202+
.map_err(|err| SnapshotError::Serde(err.to_string()))?;
203+
204+
writer
205+
.write_all(&buffer)
206+
.map_err(|err| SnapshotError::Serde(err.to_string()))?;
207+
208+
Ok(buffer.len())
209+
// bincode::serialize_into(writer, data).map_err(|err| SnapshotError::Serde(err.to_string()))
209210
}
210211

211212
#[cfg(test)]

src/vmm/src/vmm_config/boot_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub(crate) mod tests {
132132
};
133133

134134
let mut snapshot_data = vec![0u8; 1000];
135-
Snapshot::serialize(&mut snapshot_data.as_mut_slice(), &boot_src_cfg).unwrap();
135+
Snapshot::serialize::<&BootSourceConfig>(&mut snapshot_data.as_mut_slice(), &boot_src_cfg).unwrap();
136136
let restored_boot_cfg = Snapshot::deserialize(&mut snapshot_data.as_slice()).unwrap();
137137
assert_eq!(boot_src_cfg, restored_boot_cfg);
138138
}

src/vmm/src/vstate/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ mod tests {
603603
fn check_serde(guest_memory: &GuestMemoryMmap) {
604604
let mut snapshot_data = vec![0u8; 10000];
605605
let original_state = guest_memory.describe();
606-
Snapshot::serialize(&mut snapshot_data.as_mut_slice(), &original_state).unwrap();
606+
Snapshot::serialize::<GuestMemoryState>(&mut snapshot_data.as_mut_slice(), &original_state).unwrap();
607607
let restored_state = Snapshot::deserialize(&mut snapshot_data.as_slice()).unwrap();
608608
assert_eq!(original_state, restored_state);
609609
}

src/vmm/src/vstate/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ pub(crate) mod tests {
579579
let (mut vm, _) = setup_vm(0x1000);
580580
vm.setup_irqchip().unwrap();
581581
let state = vm.save_state().unwrap();
582-
Snapshot::serialize(&mut snapshot_data.as_mut_slice(), &state).unwrap();
582+
Snapshot::serialize::<&mut [u8], VmState>(&mut snapshot_data.as_mut_slice(), &state).unwrap();
583583
let restored_state: VmState = Snapshot::deserialize(&mut snapshot_data.as_slice()).unwrap();
584584

585585
vm.restore_state(&restored_state).unwrap();

0 commit comments

Comments
 (0)