Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 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,11 @@ 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.
Requires `--restore-kind`.
* `--restore-kind <same-vm|clone>`: Whether the VM restored with
`--restore-snapshot` is the VM that was saved, continuing where it left off
(`same-vm`), or a new copy of it, such as a VM created from a template
snapshot or a rollback (`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
13 changes: 13 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,19 @@ 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.

`RestoreVM` also requires `kind`, which says whether the restored VM is the VM
that was saved or a clone of it. `RESTORE_KIND_UNSPECIFIED` is rejected.

* `RESTORE_KIND_SAME_VM`: the restored VM is the VM that was saved, continuing
where it left off, for example after the host saved it to disk
(guest-transparent hibernation).
* `RESTORE_KIND_CLONE`: the restored VM is a new copy of the saved VM, for
example a VM created from a template snapshot or a rollback to an earlier
snapshot.

`kind` is independent of `resume`, which only controls whether the restored
VM starts running.

## Virtio-console file output

`VmService` can write a virtio-console stream directly to a host file. Set the
Expand Down
13 changes: 11 additions & 2 deletions Guide/src/user_guide/openvmm/snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ Use `shutdown` to exit OpenVMM after saving.

## Restoring a snapshot

To restore, pass the snapshot directory with `--restore-snapshot`:
To restore, pass the snapshot directory with `--restore-snapshot`, and say
with `--restore-kind` whether the restored VM is the VM that was saved
(`same-vm`) or a new copy of it (`clone`):

```bash
cargo run -- \
Expand All @@ -78,9 +80,16 @@ 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
```

Use `same-vm` when the VM continues where it left off, for example after it
was saved to disk to free host resources. Use `clone` when the restored VM is
a new copy, for example one of several VMs created from a template snapshot,
or a rollback to an earlier snapshot. `--restore-kind` is required with
`--restore-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,
}

/// Saved state for the VM worker to restore, and how to restore it.
#[derive(MeshPayload)]
pub struct VmRestore {
/// The saved state.
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>,
/// The saved state to restore, if any.
pub restore: Option<VmRestore>,
/// 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
37 changes: 36 additions & 1 deletion openvmm/openvmm_entry/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,21 @@ Examples:
/// Restore VM from a snapshot directory (implies file-backed memory from
/// the snapshot's memory.bin). Cannot be used with --memory-backing-file.
/// The same boot and device flags used at save time must also be supplied.
/// Requires --restore-kind.
#[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 VM restored with --restore-snapshot is the VM that was
/// saved, continuing where it left off (`same-vm`), or a new copy of it,
/// e.g. a VM created from a template snapshot or a rollback (`clone`).
#[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 +1860,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 +6263,8 @@ mod tests {
"--snp-restricted-injection",
"--restore-snapshot",
"snapshot",
"--restore-kind",
"same-vm",
])
.unwrap();
assert_eq!(
Expand All @@ -6254,6 +6273,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();
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::VmRestore { 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::VmRestore {
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
14 changes: 14 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,18 @@ 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 (for example,
// guest-transparent hibernation).
RESTORE_KIND_SAME_VM = 1;
// A new copy of the saved VM (for example, a VM created from a template
// snapshot, or a rollback to an earlier snapshot).
RESTORE_KIND_CLONE = 2;
}

message RestoreVMRequest {
string source_dir = 1;
// Device and boot configuration must be compatible with the snapshot.
Expand All @@ -939,6 +951,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::VmRestore;
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(VmRestore {
saved_state,
kind: RestoreKind::SameVm,
}))
.await
}

async fn run_core_inner(
self,
saved_state: Option<ProtobufMessage>,
restore: Option<VmRestore>,
) -> 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
Loading
Loading