@@ -34,15 +34,13 @@ use crate::utils::u64_to_usize;
34
34
use crate :: vmm_config:: boot_source:: BootSourceConfig ;
35
35
use crate :: vmm_config:: instance_info:: InstanceInfo ;
36
36
use crate :: vmm_config:: machine_config:: { HugePageConfig , MachineConfigError , MachineConfigUpdate } ;
37
- use crate :: vmm_config:: snapshot:: {
38
- CreateSnapshotParams , LoadSnapshotParams , MemBackendType , SnapshotType ,
39
- } ;
37
+ use crate :: vmm_config:: snapshot:: { CreateSnapshotParams , LoadSnapshotParams , MemBackendType } ;
40
38
use crate :: vstate:: kvm:: KvmState ;
41
39
use crate :: vstate:: memory;
42
- use crate :: vstate:: memory:: { GuestMemoryExtension , GuestMemoryState , GuestRegionMmap , MemoryError } ;
40
+ use crate :: vstate:: memory:: { GuestMemoryState , GuestRegionMmap , MemoryError } ;
43
41
use crate :: vstate:: vcpu:: { VcpuSendEventError , VcpuState } ;
44
42
use crate :: vstate:: vm:: VmState ;
45
- use crate :: { EventManager , Vmm , mem_size_mib , vstate} ;
43
+ use crate :: { EventManager , Vmm , vstate} ;
46
44
47
45
/// Holds information related to the VM that is not part of VmState.
48
46
#[ derive( Clone , Debug , Default , Deserialize , PartialEq , Eq , Serialize ) ]
@@ -164,7 +162,8 @@ pub fn create_snapshot(
164
162
165
163
snapshot_state_to_file ( & microvm_state, & params. snapshot_path ) ?;
166
164
167
- snapshot_memory_to_file ( vmm, & params. mem_file_path , params. snapshot_type ) ?;
165
+ vmm. vm
166
+ . snapshot_memory_to_file ( & params. mem_file_path , params. snapshot_type ) ?;
168
167
169
168
// We need to mark queues as dirty again for all activated devices. The reason we
170
169
// do it here is because we don't mark pages as dirty during runtime
@@ -210,81 +209,6 @@ fn snapshot_state_to_file(
210
209
. map_err ( |err| SnapshotBackingFile ( "sync_all" , err) )
211
210
}
212
211
213
- /// Takes a snapshot of the virtual machine running inside the given [`Vmm`] and saves it to
214
- /// `mem_file_path`.
215
- ///
216
- /// If `snapshot_type` is [`SnapshotType::Diff`], and `mem_file_path` exists and is a snapshot file
217
- /// of matching size, then the diff snapshot will be directly merged into the existing snapshot.
218
- /// Otherwise, existing files are simply overwritten.
219
- fn snapshot_memory_to_file (
220
- vmm : & Vmm ,
221
- mem_file_path : & Path ,
222
- snapshot_type : SnapshotType ,
223
- ) -> Result < ( ) , CreateSnapshotError > {
224
- use self :: CreateSnapshotError :: * ;
225
-
226
- // Need to check this here, as we create the file in the line below
227
- let file_existed = mem_file_path. exists ( ) ;
228
-
229
- let mut file = OpenOptions :: new ( )
230
- . write ( true )
231
- . create ( true )
232
- . truncate ( false )
233
- . open ( mem_file_path)
234
- . map_err ( |err| MemoryBackingFile ( "open" , err) ) ?;
235
-
236
- // Determine what size our total memory area is.
237
- let mem_size_mib = mem_size_mib ( vmm. vm . guest_memory ( ) ) ;
238
- let expected_size = mem_size_mib * 1024 * 1024 ;
239
-
240
- if file_existed {
241
- let file_size = file
242
- . metadata ( )
243
- . map_err ( |e| MemoryBackingFile ( "get_metadata" , e) ) ?
244
- . len ( ) ;
245
-
246
- // Here we only truncate the file if the size mismatches.
247
- // - For full snapshots, the entire file's contents will be overwritten anyway. We have to
248
- // avoid truncating here to deal with the edge case where it represents the snapshot file
249
- // from which this very microVM was loaded (as modifying the memory file would be
250
- // reflected in the mmap of the file, meaning a truncate operation would zero out guest
251
- // memory, and thus corrupt the VM).
252
- // - For diff snapshots, we want to merge the diff layer directly into the file.
253
- if file_size != expected_size {
254
- file. set_len ( 0 )
255
- . map_err ( |err| MemoryBackingFile ( "truncate" , err) ) ?;
256
- }
257
- }
258
-
259
- // Set the length of the file to the full size of the memory area.
260
- file. set_len ( expected_size)
261
- . map_err ( |e| MemoryBackingFile ( "set_length" , e) ) ?;
262
-
263
- match snapshot_type {
264
- SnapshotType :: Diff => {
265
- let dirty_bitmap = vmm. vm . get_dirty_bitmap ( ) . map_err ( DirtyBitmap ) ?;
266
- vmm. vm
267
- . guest_memory ( )
268
- . dump_dirty ( & mut file, & dirty_bitmap)
269
- . map_err ( Memory )
270
- }
271
- SnapshotType :: Full => {
272
- let dump_res = vmm. vm . guest_memory ( ) . dump ( & mut file) . map_err ( Memory ) ;
273
- if dump_res. is_ok ( ) {
274
- vmm. vm . reset_dirty_bitmap ( ) ;
275
- vmm. vm . guest_memory ( ) . reset_dirty ( ) ;
276
- }
277
-
278
- dump_res
279
- }
280
- } ?;
281
-
282
- file. flush ( )
283
- . map_err ( |err| MemoryBackingFile ( "flush" , err) ) ?;
284
- file. sync_all ( )
285
- . map_err ( |err| MemoryBackingFile ( "sync_all" , err) )
286
- }
287
-
288
212
/// Validates that snapshot CPU vendor matches the host CPU vendor.
289
213
///
290
214
/// # Errors
0 commit comments