Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions Guide/src/reference/openvmm/management/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ describes the source definitions.
* `--restore-snapshot <DIR>`: Restore RAM and device state from a snapshot.
Pass the same boot and device flags used at save time. Uses
`<DIR>/memory.bin` and conflicts with explicit memory backing files.
* `--restore-kind <same-vm|clone>`: 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.
Expand Down
4 changes: 4 additions & 0 deletions Guide/src/reference/openvmm/management/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Guide/src/user_guide/openvmm/snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 5 additions & 2 deletions openvmm/openvmm_core/src/worker/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;

Expand Down
25 changes: 23 additions & 2 deletions openvmm/openvmm_defs/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,36 @@ cfg_select! {

pub const VM_WORKER: WorkerId<VmWorkerParameters> = 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 (for example,
/// guest-transparent hibernation).
SameVm,
/// A new copy of the saved VM (for example, a VM created from a template
/// snapshot, or a rollback to an earlier snapshot).
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 {
/// The hypervisor to use.
pub hypervisor: Resource<HypervisorKind>,
/// The initial configuration.
pub cfg: Config,
/// The saved state.
pub saved_state: Option<ProtobufMessage>,
/// Restore the VM from saved state instead of booting it, if set.
pub restore: Option<RestoreParameters>,
/// File-backed guest RAM handle. When set, guest memory uses this
/// fd/handle instead of allocating anonymous memory.
pub shared_memory: Option<SharedMemoryFd>,
Expand Down
18 changes: 17 additions & 1 deletion openvmm/openvmm_entry/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

/// 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<RestoreKindCli>,

/// 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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -6246,6 +6260,8 @@ mod tests {
"--snp-restricted-injection",
"--restore-snapshot",
"snapshot",
"--restore-kind",
"same-vm",
])
.unwrap();
assert_eq!(
Expand Down
26 changes: 21 additions & 5 deletions openvmm/openvmm_entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 19 additions & 3 deletions openvmm/openvmm_entry/src/ttrpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ struct RestoreParameters {
/// expectations and the host.
manifest: openvmm_helpers::snapshot::SnapshotManifest,
state_bytes: Vec<u8>,
kind: openvmm_defs::worker::RestoreKind,
}

struct PreparedRestore {
Expand All @@ -780,6 +781,7 @@ fn prepare_snapshot_restore(
source_dir,
manifest,
state_bytes,
kind: _,
} = restore;
openvmm_helpers::snapshot::validate_manifest(
manifest,
Expand Down Expand Up @@ -1052,6 +1054,16 @@ impl VmService {
&mut self,
request: vmservice::RestoreVmRequest,
) -> anyhow::Result<vmservice::RestoreVmResponse> {
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"));
Expand Down Expand Up @@ -1094,6 +1106,7 @@ impl VmService {
source_dir,
manifest,
state_bytes,
kind,
}),
)
.await?
Expand Down Expand Up @@ -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),
)
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions openvmm/openvmm_ttrpc_vmservice/src/vmservice.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
15 changes: 11 additions & 4 deletions petri/src/vm/openvmm/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ProtobufMessage>,
restore: Option<RestoreParameters>,
) -> anyhow::Result<(PetriVmOpenVmm, PetriVmRuntimeConfig)> {
let has_guest_shares = !self.guest_shares.is_empty();
let Self {
Expand Down Expand Up @@ -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")?;

Expand Down
5 changes: 3 additions & 2 deletions petri/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,15 +26,15 @@ impl Worker {
host: &WorkerHost,
cfg: Config,
shared_memory: Option<openvmm_defs::worker::SharedMemoryFd>,
saved_state: Option<mesh::payload::message::ProtobufMessage>,
restore: Option<RestoreParameters>,
) -> anyhow::Result<(Self, mesh::Receiver<HaltReason>)> {
let (vm_rpc, rpc_recv) = mesh::channel();
let (notify_send, notify_recv) = mesh::channel();

let params = VmWorkerParameters {
hypervisor: openvmm_helpers::hypervisor::choose_hypervisor()?,
cfg,
saved_state,
restore,
shared_memory,
rpc: rpc_recv,
notify: notify_send,
Expand Down
4 changes: 4 additions & 0 deletions vmm_tests/vmm_tests/tests/tests/ttrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,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
Expand All @@ -1754,6 +1755,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
Expand All @@ -1775,6 +1777,7 @@ async fn test_ttrpc_interface(
// doesn't matter.
expected_snapshot_id: Some(snapshot_id.to_uppercase()),
resume: false,
kind: vmservice::RestoreKind::SameVm as i32,
Comment thread
ayusharora221204 marked this conversation as resolved.
Resolved
},
)
.await
Expand Down Expand Up @@ -1857,6 +1860,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
Expand Down
Loading