1use mesh::payload::Protobuf;
7use mesh::payload::Timestamp;
8
9pub mod publish;
10pub mod restore;
11
12pub use publish::check_snapshot_destination;
13pub use publish::write_snapshot;
14pub use restore::read_snapshot;
15
16pub const MANIFEST_VERSION: u32 = 1;
18
19#[derive(Clone, Protobuf)]
21#[mesh(package = "openvmm.snapshot")]
22pub struct SnapshotManifest {
23 #[mesh(1)]
25 pub version: u32,
26 #[mesh(2)]
28 pub created_at: Timestamp,
29 #[mesh(3)]
31 pub openvmm_version: String,
32 #[mesh(4)]
34 pub memory_size_bytes: u64,
35 #[mesh(5)]
37 pub vp_count: u32,
38 #[mesh(6)]
40 pub page_size: u32,
41 #[mesh(7)]
43 pub architecture: String,
44 #[mesh(18, encoding = "mesh::payload::encoding::ZeroCopyEncoding")]
47 pub snapshot_id: guid::Guid,
48 }
51
52pub fn validate_manifest(
58 manifest: &SnapshotManifest,
59 expected_arch: &str,
60 expected_memory_size: u64,
61 expected_vp_count: u32,
62 expected_page_size: u32,
63) -> anyhow::Result<()> {
64 if manifest.version != MANIFEST_VERSION {
65 anyhow::bail!(
66 "snapshot manifest version {} is not supported (expected {})",
67 manifest.version,
68 MANIFEST_VERSION,
69 );
70 }
71
72 if manifest.architecture != expected_arch {
73 anyhow::bail!(
74 "snapshot architecture '{}' doesn't match expected '{}'",
75 manifest.architecture,
76 expected_arch,
77 );
78 }
79
80 if manifest.memory_size_bytes != expected_memory_size {
81 anyhow::bail!(
82 "snapshot memory size ({} bytes) doesn't match expected ({} bytes)",
83 manifest.memory_size_bytes,
84 expected_memory_size,
85 );
86 }
87
88 if manifest.vp_count != expected_vp_count {
89 anyhow::bail!(
90 "snapshot VP count ({}) doesn't match expected ({})",
91 manifest.vp_count,
92 expected_vp_count,
93 );
94 }
95
96 if manifest.page_size != expected_page_size {
97 anyhow::bail!(
98 "snapshot page size ({}) doesn't match expected ({})",
99 manifest.page_size,
100 expected_page_size,
101 );
102 }
103
104 Ok(())
105}
106
107#[derive(Default)]
109pub struct SnapshotExpectations {
110 pub snapshot_id: Option<guid::Guid>,
113}
114
115pub fn validate_expectations(
117 manifest: &SnapshotManifest,
118 expectations: &SnapshotExpectations,
119) -> anyhow::Result<()> {
120 if let Some(expected) = expectations.snapshot_id {
121 if manifest.snapshot_id != expected {
122 anyhow::bail!("snapshot ID doesn't match the expected snapshot ID");
123 }
124 }
125
126 Ok(())
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 const TEST_ID: guid::Guid = guid::guid!("3099921d-f0e6-48aa-9d58-8fd4811ce4d2");
134 const OTHER_ID: guid::Guid = guid::guid!("7b2a8c1e-5f4d-4e3a-9b6c-0d1e2f3a4b5c");
135
136 fn test_manifest() -> SnapshotManifest {
138 SnapshotManifest {
139 version: MANIFEST_VERSION,
140 created_at: Timestamp {
141 seconds: 1234567890,
142 nanos: 0,
143 },
144 openvmm_version: "test-0.1.0".to_string(),
145 memory_size_bytes: 1024,
146 vp_count: 2,
147 page_size: 4096,
148 architecture: "x86_64".to_string(),
149 snapshot_id: TEST_ID,
150 }
151 }
152
153 #[test]
154 fn write_read_roundtrip() {
155 let dir = tempfile::tempdir().unwrap();
156 let snap_dir = dir.path().join("snap");
157
158 let mem_path = dir.path().join("memory.bin");
160 std::fs::write(&mem_path, b"FAKEMEM").unwrap();
161
162 let manifest = test_manifest();
163 let state = b"saved-state-data";
164
165 write_snapshot(&snap_dir, &manifest, state, &mem_path).unwrap();
166
167 let (read_manifest, read_state) = read_snapshot(&snap_dir).unwrap();
168 assert_eq!(read_manifest.version, manifest.version);
169 assert_eq!(read_manifest.memory_size_bytes, manifest.memory_size_bytes);
170 assert_eq!(read_manifest.vp_count, manifest.vp_count);
171 assert_eq!(read_manifest.architecture, manifest.architecture);
172 assert_eq!(read_manifest.snapshot_id, manifest.snapshot_id);
173 assert_eq!(read_state, state);
174
175 assert!(snap_dir.join("memory.bin").exists());
177 }
178
179 #[test]
180 fn decode_manifest_without_snapshot_id() {
181 #[derive(Protobuf)]
185 #[mesh(package = "openvmm.snapshot")]
186 struct OldManifest {
187 #[mesh(1)]
188 version: u32,
189 #[mesh(8)]
190 state_size_bytes: u64,
191 }
192
193 let bytes = mesh::payload::encode(OldManifest {
194 version: 6,
195 state_size_bytes: 4096,
196 });
197 let manifest: SnapshotManifest = mesh::payload::decode(&bytes).unwrap();
198 assert!(manifest.snapshot_id.is_zero());
199 let err = validate_manifest(&manifest, "x86_64", 1024, 2, 4096).unwrap_err();
200 assert!(
201 err.to_string().contains("version 6 is not supported"),
202 "unexpected error: {err}"
203 );
204 }
205
206 #[test]
207 fn write_snapshot_creates_dir() {
208 let dir = tempfile::tempdir().unwrap();
209 let snap_dir = dir.path().join("a").join("b").join("c");
210
211 let mem_path = dir.path().join("memory.bin");
212 std::fs::write(&mem_path, b"MEM").unwrap();
213
214 write_snapshot(&snap_dir, &test_manifest(), b"state", &mem_path).unwrap();
215
216 assert!(snap_dir.join("manifest.bin").exists());
217 assert!(snap_dir.join("state.bin").exists());
218 assert!(snap_dir.join("memory.bin").exists());
219 }
220
221 #[test]
222 fn write_snapshot_rejects_existing_directory() {
223 let dir = tempfile::tempdir().unwrap();
226 let snap_dir = dir.path().join("snap");
227 std::fs::create_dir_all(&snap_dir).unwrap();
228
229 let mem_path = snap_dir.join("memory.bin");
230 std::fs::write(&mem_path, b"MEM").unwrap();
231
232 let err = write_snapshot(&snap_dir, &test_manifest(), b"state", &mem_path).unwrap_err();
233 assert!(
234 err.to_string().contains("already exists"),
235 "unexpected error: {err}"
236 );
237 assert_eq!(std::fs::read(&mem_path).unwrap(), b"MEM");
238 assert!(!snap_dir.join("state.bin").exists());
239 assert!(!snap_dir.join("manifest.bin").exists());
240 }
241
242 #[test]
243 fn write_snapshot_rejects_existing_snapshot() {
244 let dir = tempfile::tempdir().unwrap();
245 let snap_dir = dir.path().join("snap");
246 let mem_path = dir.path().join("memory.bin");
247 std::fs::write(&mem_path, b"MEM").unwrap();
248
249 let mut first = test_manifest();
250 first.snapshot_id = OTHER_ID;
251 write_snapshot(&snap_dir, &first, b"first", &mem_path).unwrap();
252
253 let second = test_manifest();
254 let err = write_snapshot(&snap_dir, &second, b"second", &mem_path).unwrap_err();
255 assert!(
256 err.to_string().contains("already exists"),
257 "unexpected error: {err}"
258 );
259
260 let (manifest, state) = read_snapshot(&snap_dir).unwrap();
262 assert_eq!(manifest.snapshot_id, OTHER_ID);
263 assert_eq!(state, b"first");
264 assert!(snap_dir.join("memory.bin").exists());
265 }
266
267 #[test]
268 fn write_snapshot_cleans_up_on_failure() {
269 let dir = tempfile::tempdir().unwrap();
270 let snap_dir = dir.path().join("snap");
271 let mem_path = dir.path().join("missing.bin");
274
275 write_snapshot(&snap_dir, &test_manifest(), b"state", &mem_path).unwrap_err();
276
277 assert!(!snap_dir.exists());
279 }
280
281 #[test]
282 fn check_snapshot_destination_rejects_file() {
283 let dir = tempfile::tempdir().unwrap();
284 let not_a_dir = dir.path().join("file");
285 std::fs::write(¬_a_dir, b"x").unwrap();
286 let err = check_snapshot_destination(¬_a_dir).unwrap_err();
287 assert!(
288 err.to_string().contains("already exists"),
289 "unexpected error: {err}"
290 );
291 }
292
293 #[test]
294 fn read_snapshot_missing_file() {
295 let dir = tempfile::tempdir().unwrap();
296 let result = read_snapshot(dir.path());
298 assert!(result.is_err());
299 }
300
301 #[test]
302 fn validate_manifest_ok() {
303 let manifest = test_manifest();
304 validate_manifest(&manifest, "x86_64", 1024, 2, 4096).unwrap();
305 }
306
307 #[test]
308 fn validate_manifest_wrong_arch() {
309 let manifest = test_manifest();
310 let err = validate_manifest(&manifest, "aarch64", 1024, 2, 4096).unwrap_err();
311 assert!(
312 err.to_string().contains("architecture"),
313 "unexpected error: {err}"
314 );
315 }
316
317 #[test]
318 fn validate_manifest_wrong_memory_size() {
319 let manifest = test_manifest();
320 let err = validate_manifest(&manifest, "x86_64", 9999, 2, 4096).unwrap_err();
321 assert!(
322 err.to_string().contains("memory size"),
323 "unexpected error: {err}"
324 );
325 }
326
327 #[test]
328 fn validate_manifest_wrong_vp_count() {
329 let manifest = test_manifest();
330 let err = validate_manifest(&manifest, "x86_64", 1024, 99, 4096).unwrap_err();
331 assert!(
332 err.to_string().contains("VP count"),
333 "unexpected error: {err}"
334 );
335 }
336
337 #[test]
338 fn validate_manifest_wrong_page_size() {
339 let manifest = test_manifest();
340 let err = validate_manifest(&manifest, "x86_64", 1024, 2, 65536).unwrap_err();
341 assert!(
342 err.to_string().contains("page size"),
343 "unexpected error: {err}"
344 );
345 }
346
347 fn expect_id(id: guid::Guid) -> SnapshotExpectations {
348 SnapshotExpectations {
349 snapshot_id: Some(id),
350 }
351 }
352
353 #[test]
354 fn validate_expectations_snapshot_id_matches() {
355 let manifest = test_manifest();
356 validate_expectations(&manifest, &expect_id(TEST_ID)).unwrap();
357 }
358
359 #[test]
360 fn validate_expectations_skipped_when_not_provided() {
361 let manifest = test_manifest();
362 validate_expectations(&manifest, &SnapshotExpectations::default()).unwrap();
363 }
364
365 #[test]
366 fn validate_expectations_snapshot_id_mismatch() {
367 let manifest = test_manifest();
368 let err = validate_expectations(&manifest, &expect_id(OTHER_ID)).unwrap_err();
369 assert!(
370 err.to_string().contains("snapshot ID"),
371 "unexpected error: {err}"
372 );
373 }
374
375 #[test]
376 fn validate_expectations_snapshot_id_missing_from_manifest() {
377 let mut manifest = test_manifest();
378 manifest.snapshot_id = guid::Guid::ZERO;
379 let err = validate_expectations(&manifest, &expect_id(TEST_ID)).unwrap_err();
380 assert!(
381 err.to_string().contains("snapshot ID"),
382 "unexpected error: {err}"
383 );
384 }
385
386 #[test]
387 fn validate_manifest_wrong_version() {
388 let mut manifest = test_manifest();
389 manifest.version = 999;
390 let err = validate_manifest(&manifest, "x86_64", 1024, 2, 4096).unwrap_err();
391 assert!(
392 err.to_string().contains("version"),
393 "unexpected error: {err}"
394 );
395 }
396}