diff --git a/Guide/src/reference/openvmm/management/cli.md b/Guide/src/reference/openvmm/management/cli.md index f51ad2a3ee8..acad57e6f38 100644 --- a/Guide/src/reference/openvmm/management/cli.md +++ b/Guide/src/reference/openvmm/management/cli.md @@ -62,6 +62,8 @@ describes the source definitions. * `--restore-snapshot `: Restore RAM and device state from a snapshot. Pass the same boot and device flags used at save time. Uses `/memory.bin` and conflicts with explicit memory backing files. +* `--restore-kind `: Required with `--restore-snapshot`. Whether + the restored VM is the saved VM (`same-vm`) or a new copy of it (`clone`). * `--hv`: Exposes Hyper-V enlightenments. VMBus is enabled by default when `--hv` is active; pass `--no-vmbus` to suppress VMBus while keeping enlightenments. diff --git a/Guide/src/reference/openvmm/management/grpc.md b/Guide/src/reference/openvmm/management/grpc.md index 92e616a0035..41055fb4e8a 100644 --- a/Guide/src/reference/openvmm/management/grpc.md +++ b/Guide/src/reference/openvmm/management/grpc.md @@ -67,6 +67,10 @@ as not provided, so the ID isn't checked. The restored snapshot's ID is returned in `RestoreVMResponse.snapshot_id`. Set `resume` to start the restored VM immediately; otherwise it is created paused. +`kind` is required: `RESTORE_KIND_SAME_VM` if the restored VM is the VM that +was saved, or `RESTORE_KIND_CLONE` if it is a new copy (e.g. from a template +snapshot, or a rollback). + ## Virtio-console file output `VmService` can write a virtio-console stream directly to a host file. Set the diff --git a/Guide/src/user_guide/openvmm/snapshots.md b/Guide/src/user_guide/openvmm/snapshots.md index 726b6d871df..b0b39e2fc97 100644 --- a/Guide/src/user_guide/openvmm/snapshots.md +++ b/Guide/src/user_guide/openvmm/snapshots.md @@ -78,9 +78,13 @@ cargo run -- \ --disk memdiff:file:path/to/disk.vhdx,on=scsi0 \ --memory size=4096M \ --processors 4 \ - --restore-snapshot path/to/snapshot-dir + --restore-snapshot path/to/snapshot-dir \ + --restore-kind same-vm ``` +`--restore-kind` is required: `same-vm` if the restored VM is the VM that was +saved, or `clone` if it is a new copy (e.g. from a template snapshot). + `--restore-snapshot` automatically opens `memory.bin` from the snapshot directory, so `file=...` should not be specified in `--memory` (the two options are mutually exclusive). diff --git a/openvmm/openvmm_core/src/worker/dispatch.rs b/openvmm/openvmm_core/src/worker/dispatch.rs index 5a854fc784f..1ff697dc420 100644 --- a/openvmm/openvmm_core/src/worker/dispatch.rs +++ b/openvmm/openvmm_core/src/worker/dispatch.rs @@ -345,8 +345,11 @@ impl Worker for VmWorker { shared_memory, ))?; let saved_state = parameters - .saved_state - .map(|m| m.parse()) + .restore + .map(|restore| { + tracing::info!(kind = ?restore.kind, "restoring VM from saved state"); + restore.saved_state.parse() + }) .transpose() .context("failed to decode saved state")?; diff --git a/openvmm/openvmm_defs/src/worker.rs b/openvmm/openvmm_defs/src/worker.rs index e8d7f031b2b..379c9041130 100644 --- a/openvmm/openvmm_defs/src/worker.rs +++ b/openvmm/openvmm_defs/src/worker.rs @@ -25,6 +25,25 @@ cfg_select! { pub const VM_WORKER: WorkerId = WorkerId::new("VmWorker"); +/// Whether a VM restored from saved state is the VM that was saved, or a +/// clone of it. +#[derive(MeshPayload, Debug, Clone, Copy, PartialEq, Eq)] +pub enum RestoreKind { + /// The same VM that was saved, continuing where it left off. + SameVm, + /// A new copy of the saved VM. + Clone, +} + +/// Parameters for restoring the VM from saved state. +#[derive(MeshPayload)] +pub struct RestoreParameters { + /// The saved state to restore. + pub saved_state: ProtobufMessage, + /// Whether the restored VM is the saved VM or a clone of it. + pub kind: RestoreKind, +} + /// Launch parameters for the VM worker. #[derive(MeshPayload)] pub struct VmWorkerParameters { @@ -32,8 +51,8 @@ pub struct VmWorkerParameters { pub hypervisor: Resource, /// The initial configuration. pub cfg: Config, - /// The saved state. - pub saved_state: Option, + /// Restore the VM from saved state instead of booting it, if set. + pub restore: Option, /// File-backed guest RAM handle. When set, guest memory uses this /// fd/handle instead of allocating anonymous memory. pub shared_memory: Option, diff --git a/openvmm/openvmm_entry/src/cli_args.rs b/openvmm/openvmm_entry/src/cli_args.rs index bfa2cd95ddf..47e9183dd50 100644 --- a/openvmm/openvmm_entry/src/cli_args.rs +++ b/openvmm/openvmm_entry/src/cli_args.rs @@ -254,10 +254,15 @@ Examples: #[clap( long, value_name = "DIR", - conflicts_with_all = ["deprecated_memory_backing_file", "numa"] + conflicts_with_all = ["deprecated_memory_backing_file", "numa"], + requires = "restore_kind" )] pub restore_snapshot: Option, + /// Whether the restored VM is the saved VM or a copy of it (required with --restore-snapshot) + #[clap(long, value_name = "KIND", requires = "restore_snapshot")] + pub restore_kind: Option, + /// use private anonymous memory for guest RAM #[clap(long = "private-memory", hide = true, conflicts_with_all = ["deprecated_memory_backing_file", "restore_snapshot", "numa"])] pub deprecated_private_memory: bool, @@ -1852,6 +1857,15 @@ pub enum SecureBootTemplateCli { UefiCa, } +/// Kind of snapshot restore selected by `--restore-kind`. +#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)] +pub enum RestoreKindCli { + /// The VM that was saved, continuing where it left off. + SameVm, + /// A new copy of the saved VM. + Clone, +} + /// TPM reference implementation version selected by `--tpm`. #[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)] pub enum TpmVersionCli { @@ -6246,6 +6260,8 @@ mod tests { "--snp-restricted-injection", "--restore-snapshot", "snapshot", + "--restore-kind", + "same-vm", ]) .unwrap(); assert_eq!( @@ -6254,6 +6270,22 @@ mod tests { ); } + #[test] + fn test_restore_kind_required_with_restore_snapshot() { + assert!(Options::try_parse_from(["openvmm", "--restore-snapshot", "snapshot"]).is_err()); + assert!(Options::try_parse_from(["openvmm", "--restore-kind", "clone"]).is_err()); + + let opt = Options::try_parse_from([ + "openvmm", + "--restore-snapshot", + "snapshot", + "--restore-kind", + "clone", + ]) + .unwrap(); + assert_eq!(opt.restore_kind, Some(RestoreKindCli::Clone)); + } + #[test] fn test_isolation_options_allow_vbs_uefi() { let opt = Options::try_parse_from(["openvmm", "--isolation", "vbs", "--uefi"]).unwrap(); diff --git a/openvmm/openvmm_entry/src/lib.rs b/openvmm/openvmm_entry/src/lib.rs index 6971b75ddec..c55bbb4a5d0 100644 --- a/openvmm/openvmm_entry/src/lib.rs +++ b/openvmm/openvmm_entry/src/lib.rs @@ -36,6 +36,7 @@ use cli_args::EndpointConfigCli; use cli_args::IgvmPersonalityCli; use cli_args::NicConfigCli; use cli_args::ProvisionCondition; +use cli_args::RestoreKindCli; use cli_args::SerialConfigCli; use cli_args::TpmVersionCli; use cli_args::UefiConsoleModeCli; @@ -3058,9 +3059,19 @@ async fn run_control_inner( let vm_worker = { let vm_host = mesh.make_host("vm", opt.log_file.clone()).await?; - let (shared_memory, saved_state) = if let Some(snapshot_dir) = &opt.restore_snapshot { - let (fd, state_msg) = prepare_snapshot_restore(snapshot_dir, &opt)?; - (Some(fd), Some(state_msg)) + let (shared_memory, restore) = if let Some(snapshot_dir) = &opt.restore_snapshot { + let (fd, saved_state) = prepare_snapshot_restore(snapshot_dir, &opt)?; + let kind = match opt + .restore_kind + .context("--restore-snapshot requires --restore-kind")? + { + RestoreKindCli::SameVm => openvmm_defs::worker::RestoreKind::SameVm, + RestoreKindCli::Clone => openvmm_defs::worker::RestoreKind::Clone, + }; + ( + Some(fd), + Some(openvmm_defs::worker::RestoreParameters { saved_state, kind }), + ) } else { let shared_memory = opt .memory_backing_file() @@ -3080,7 +3091,7 @@ async fn run_control_inner( None => openvmm_helpers::hypervisor::choose_hypervisor()?, }, cfg: vm_config, - saved_state, + restore, shared_memory, rpc: rpc_recv, notify: notify_send, @@ -3328,7 +3339,12 @@ mod tests { let mut args = vec!["openvmm", "--single-process"]; args.extend(&boot); if restore { - args.extend(["--restore-snapshot", snapshot.to_str().unwrap()]); + args.extend([ + "--restore-snapshot", + snapshot.to_str().unwrap(), + "--restore-kind", + "same-vm", + ]); } let opt = Options::try_parse_from(&args).unwrap(); let mesh = VmmMesh::new(&driver, true).unwrap(); diff --git a/openvmm/openvmm_entry/src/ttrpc/mod.rs b/openvmm/openvmm_entry/src/ttrpc/mod.rs index 9cb040c729f..716d83f4242 100644 --- a/openvmm/openvmm_entry/src/ttrpc/mod.rs +++ b/openvmm/openvmm_entry/src/ttrpc/mod.rs @@ -760,6 +760,7 @@ struct RestoreParameters { /// expectations and the host. manifest: openvmm_helpers::snapshot::SnapshotManifest, state_bytes: Vec, + kind: openvmm_defs::worker::RestoreKind, } struct PreparedRestore { @@ -780,6 +781,7 @@ fn prepare_snapshot_restore( source_dir, manifest, state_bytes, + kind: _, } = restore; openvmm_helpers::snapshot::validate_manifest( manifest, @@ -1052,6 +1054,16 @@ impl VmService { &mut self, request: vmservice::RestoreVmRequest, ) -> anyhow::Result { + let kind = match request.kind() { + vmservice::RestoreKind::Unspecified => { + return Err(anyhow!(Code::InvalidArgument).context(format!( + "unspecified or invalid restore kind {}", + request.kind + ))); + } + vmservice::RestoreKind::SameVm => openvmm_defs::worker::RestoreKind::SameVm, + vmservice::RestoreKind::Clone => openvmm_defs::worker::RestoreKind::Clone, + }; let source_dir = PathBuf::from(request.source_dir); if source_dir.as_os_str().is_empty() { return Err(anyhow!(Code::InvalidArgument).context("missing snapshot source directory")); @@ -1094,6 +1106,7 @@ impl VmService { source_dir, manifest, state_bytes, + kind, }), ) .await? @@ -1583,13 +1596,16 @@ impl VmService { .await .context("spawning vm process failed")?; - let (shared_memory, saved_state, active_memory_backing_file, snapshot_id) = + let (shared_memory, worker_restore, active_memory_backing_file, snapshot_id) = if let Some(restore) = &restore { let prepared = prepare_snapshot_restore(restore, config_mem_size, config_proc_count)?; ( Some(prepared.shared_memory), - Some(prepared.saved_state), + Some(openvmm_defs::worker::RestoreParameters { + saved_state: prepared.saved_state, + kind: restore.kind, + }), Some(restore.source_dir.join("memory.bin")), Some(prepared.snapshot_id), ) @@ -1612,7 +1628,7 @@ impl VmService { VmWorkerParameters { hypervisor: openvmm_helpers::hypervisor::choose_hypervisor()?, cfg: config, - saved_state, + restore: worker_restore, shared_memory, rpc: recv, notify: notify_send, diff --git a/openvmm/openvmm_ttrpc_vmservice/src/vmservice.proto b/openvmm/openvmm_ttrpc_vmservice/src/vmservice.proto index 17e60f892ec..1da88401013 100644 --- a/openvmm/openvmm_ttrpc_vmservice/src/vmservice.proto +++ b/openvmm/openvmm_ttrpc_vmservice/src/vmservice.proto @@ -929,6 +929,16 @@ message SaveVMResponse { string snapshot_id = 1; } +// Whether the restored VM is the VM that was saved, or a clone of it. +enum RestoreKind { + // Rejected: the caller must choose. + RESTORE_KIND_UNSPECIFIED = 0; + // The same VM that was saved, continuing where it left off. + RESTORE_KIND_SAME_VM = 1; + // A new copy of the saved VM. + RESTORE_KIND_CLONE = 2; +} + message RestoreVMRequest { string source_dir = 1; // Device and boot configuration must be compatible with the snapshot. @@ -939,6 +949,8 @@ message RestoreVMRequest { optional string expected_snapshot_id = 3; // Resume the restored VM immediately. Otherwise it remains paused. bool resume = 4; + // Required. Whether the restored VM is the saved VM or a clone of it. + RestoreKind kind = 5; } message RestoreVMResponse { diff --git a/petri/src/vm/openvmm/start.rs b/petri/src/vm/openvmm/start.rs index 51793a3cd53..706025e12e8 100644 --- a/petri/src/vm/openvmm/start.rs +++ b/petri/src/vm/openvmm/start.rs @@ -16,6 +16,8 @@ use mesh_process::Mesh; use mesh_process::ProcessConfig; use mesh_worker::WorkerHost; use openvmm_defs::config::DeviceVtl; +use openvmm_defs::worker::RestoreKind; +use openvmm_defs::worker::RestoreParameters; use pal_async::pipe::PolledPipe; use pal_async::task::Spawn; use petri_artifacts_common::tags::MachineArch; @@ -37,19 +39,24 @@ impl PetriVmConfigOpenVmm { /// Launch a fresh worker that restores from `saved_state`, mirroring /// production `--restore-snapshot`. Exercises the path the in-process - /// pulse (`verify_save_restore`) cannot reach. + /// pulse (`verify_save_restore`) cannot reach. The restored VM is the VM + /// that was saved, not a clone of it. pub(crate) async fn run_restore( self, saved_state: &[u8], ) -> anyhow::Result<(PetriVmOpenVmm, PetriVmRuntimeConfig)> { let saved_state: ProtobufMessage = mesh::payload::decode(saved_state) .context("failed to decode serialized saved state")?; - self.run_core_inner(Some(saved_state)).await + self.run_core_inner(Some(RestoreParameters { + saved_state, + kind: RestoreKind::SameVm, + })) + .await } async fn run_core_inner( self, - saved_state: Option, + restore: Option, ) -> anyhow::Result<(PetriVmOpenVmm, PetriVmRuntimeConfig)> { let has_guest_shares = !self.guest_shares.is_empty(); let Self { @@ -197,7 +204,7 @@ impl PetriVmConfigOpenVmm { ); } - let (worker, halt_notif) = Worker::launch(&host, config, shared_memory, saved_state) + let (worker, halt_notif) = Worker::launch(&host, config, shared_memory, restore) .await .context("failed to launch vm worker")?; diff --git a/petri/src/worker.rs b/petri/src/worker.rs index 2f0d8a6406c..cbd29601513 100644 --- a/petri/src/worker.rs +++ b/petri/src/worker.rs @@ -11,6 +11,7 @@ use mesh_worker::WorkerHost; use openvmm_defs::config::Config; use openvmm_defs::rpc::PulseSaveRestoreError; use openvmm_defs::rpc::VmRpc; +use openvmm_defs::worker::RestoreParameters; use openvmm_defs::worker::VM_WORKER; use openvmm_defs::worker::VmWorkerParameters; use vmm_core_defs::HaltReason; @@ -25,7 +26,7 @@ impl Worker { host: &WorkerHost, cfg: Config, shared_memory: Option, - saved_state: Option, + restore: Option, ) -> anyhow::Result<(Self, mesh::Receiver)> { let (vm_rpc, rpc_recv) = mesh::channel(); let (notify_send, notify_recv) = mesh::channel(); @@ -33,7 +34,7 @@ impl Worker { let params = VmWorkerParameters { hypervisor: openvmm_helpers::hypervisor::choose_hypervisor()?, cfg, - saved_state, + restore, shared_memory, rpc: rpc_recv, notify: notify_send, diff --git a/vmm_tests/vmm_tests/tests/tests/ttrpc.rs b/vmm_tests/vmm_tests/tests/tests/ttrpc.rs index 7c8ca2060e8..9aa5766465b 100644 --- a/vmm_tests/vmm_tests/tests/tests/ttrpc.rs +++ b/vmm_tests/vmm_tests/tests/tests/ttrpc.rs @@ -1724,6 +1724,29 @@ async fn test_ttrpc_interface( waiter.await.unwrap_err(); + for kind in [vmservice::RestoreKind::Unspecified as i32, 99] { + let err = client + .call() + .start( + vmservice::Vm::RestoreVm, + vmservice::RestoreVmRequest { + source_dir: snapshot_path.to_string_lossy().into_owned(), + config: restore_config.clone(), + expected_snapshot_id: None, + resume: false, + kind, + }, + ) + .await + .unwrap_err(); + assert_eq!( + err.code, + mesh_rpc::service::Code::InvalidArgument as i32, + "RestoreVm with restore kind {kind}: {}", + err.message + ); + } + let err = client .call() .start( @@ -1733,6 +1756,7 @@ async fn test_ttrpc_interface( config: restore_config.clone(), expected_snapshot_id: Some("not-a-guid".to_string()), resume: false, + kind: vmservice::RestoreKind::SameVm as i32, }, ) .await @@ -1754,6 +1778,7 @@ async fn test_ttrpc_interface( config: restore_config.clone(), expected_snapshot_id: Some(wrong_snapshot_id), resume: false, + kind: vmservice::RestoreKind::SameVm as i32, }, ) .await @@ -1775,6 +1800,7 @@ async fn test_ttrpc_interface( // doesn't matter. expected_snapshot_id: Some(snapshot_id.to_uppercase()), resume: false, + kind: vmservice::RestoreKind::Clone as i32, }, ) .await @@ -1857,6 +1883,7 @@ async fn test_ttrpc_interface( config: restore_config, expected_snapshot_id: Some(second_snapshot_id.clone()), resume: true, + kind: vmservice::RestoreKind::SameVm as i32, }, ) .await