From 154548a0e6b81d791b4dac1425f9200f26f0e3cf Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Sun, 8 Nov 2026 11:27:19 -0800 Subject: [PATCH] microvm: let a restore attach a listener to a disconnected boot console A microVM snapshot records its boot console's attachment, and a restore must request that attachment again. A snapshot that `--virtio-console none` captured restores only with `none`, whose device discards the guest's output, so a restored guest's kernel console, and what its init writes there, such as a panic, an oops, or an out-of-memory kill, reach no one. NVX captures its templates with `none`, so that a capture waits for no reader, and so its clones keep no record of why they fail once restored (microsoft/nvx#996). A restore may now attach a listener at a fresh endpoint to a disconnected boot console, `--virtio-console listen=`: a Unix socket beside the snapshot directory, or a named pipe in the `//./pipe/openvmm-microvm-` namespace, as for a saved listener. The device is the one that the snapshot saved, whose saved state records that it discards output while disconnected, so the listener's attachment, mode `Listen` with the `DiscardWhileDisconnected` policy, keeps doing so: a client reads what the guest writes once it connected, and the guest never waits for a client that went away. Only the endpoint changes, so the restore checks the snapshot's machine contract against the saved disconnected attachment. The virtio-console resolver accepts that attachment. The management RPC's restore takes the device's disconnect policy from the attachment, as the command line's does, rather than from the endpoint. A restore still refuses any other change of a disconnected console: a client, the terminal, or a TCP endpoint, which no microVM console takes, and a disconnected control console stays disconnected. Tests: unit tests of the restore of a disconnected attachment with a socket listener on Linux and a pipe listener on Windows, of the attachments that it refuses, of the disconnected control console, of the contract's alignment, and of the resolver's validation. The tests of the restore and of the resolver fail without the change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Guide/src/reference/openvmm/management/cli.md | 7 +- Guide/src/user_guide/openvmm/snapshots.md | 14 +- openvmm/openvmm_entry/src/microvm/config.rs | 13 +- openvmm/openvmm_entry/src/microvm/console.rs | 299 ++++++++++++++++++ openvmm/openvmm_entry/src/microvm/mod.rs | 2 + openvmm/openvmm_entry/src/microvm/restore.rs | 65 ++++ openvmm/openvmm_entry/src/ttrpc/microvm.rs | 21 +- .../virtio_console/src/resolver/attachment.rs | 54 ++++ 8 files changed, 454 insertions(+), 21 deletions(-) diff --git a/Guide/src/reference/openvmm/management/cli.md b/Guide/src/reference/openvmm/management/cli.md index 43f6a290023..a314642337b 100644 --- a/Guide/src/reference/openvmm/management/cli.md +++ b/Guide/src/reference/openvmm/management/cli.md @@ -1056,7 +1056,12 @@ Serial devices can be configured to appear as different devices inside the guest attached to `hvc1`. A guest-requested exit writes the boot console output that the device accepted before OpenVMM exits. * `none`: keep the device present and explicitly discard guest TX while - disconnected. + disconnected. A restore caller can supply + `--virtio-console listen=`, a Unix socket beside the + snapshot directory or a `//./pipe/openvmm-microvm-` pipe, to read the + restored guest's console. The listener keeps discarding guest TX while no + client is connected, so the guest never waits for a client. A restore + refuses every other replacement of a disconnected console. A microVM refuses TCP endpoints, `listen=tcp:IP:PORT` and `connect=tcp:IP:PORT`, and a snapshot whose boot console has one. Unless diff --git a/Guide/src/user_guide/openvmm/snapshots.md b/Guide/src/user_guide/openvmm/snapshots.md index 5fb1b41b6a8..6509552ddac 100644 --- a/Guide/src/user_guide/openvmm/snapshots.md +++ b/Guide/src/user_guide/openvmm/snapshots.md @@ -159,9 +159,17 @@ timeout; only the listener endpoint identity may change. Restore callers use listener, `--microvm-control-console listen=` with `--microvm-control-auth-stdin`. OpenVMM validates an approved replacement without reopening or canonicalizing the captured listener path, so the source -generation's private directory need not survive until restore. Client, -inherited-provider, and disconnected attachments retain their stricter saved -identity requirements. +generation's private directory need not survive until restore. Client and +inherited-provider attachments retain their stricter saved identity +requirements. A disconnected boot console, which `--virtio-console none` +captures, may take a listener at restore, +`--virtio-console listen=`: a Unix socket beside the snapshot +directory, or a named pipe in the `//./pipe/openvmm-microvm-` namespace, +as a saved listener must be. The device is the one that the snapshot saved, so +the listener keeps discarding guest TX while no client is connected, and a +restored guest never waits for a client that went away. A disconnected console +takes no other attachment, and a disconnected control console stays +disconnected. MicroVM orchestrators can add `--restore-ready-path `. OpenVMM connects to an existing Unix domain socket on Linux or named pipe on Windows and writes diff --git a/openvmm/openvmm_entry/src/microvm/config.rs b/openvmm/openvmm_entry/src/microvm/config.rs index d759623c89c..7536fa8ffc9 100644 --- a/openvmm/openvmm_entry/src/microvm/config.rs +++ b/openvmm/openvmm_entry/src/microvm/config.rs @@ -9,6 +9,7 @@ use super::console::ConsoleEndpoint; use super::console::effective_microvm_console; use super::console::effective_microvm_control_console; use super::console::microvm_console_attachments_share_endpoint; +use super::console::microvm_console_disconnect_policy; use super::console::microvm_console_socket_cleanup; use super::console::microvm_control_broker_config; use super::console::validate_microvm_console_attachment_namespace; @@ -55,7 +56,6 @@ use std::cell::RefCell; use std::path::PathBuf; use std::time::Duration; use virtio_resources::console::attachment::VirtioConsoleDisconnectPolicy; -use virtio_resources::console::attachment::VirtioConsoleReconnectPolicy; use virtio_resources::console::control::VirtioControlConsoleBrokerConfig; use vm_manifest_builder::MachineArch; use vm_resource::IntoResource; @@ -653,12 +653,11 @@ impl<'a> MicrovmConfigBuilder<'a> { ) -> virtio_resources::console::VirtioConsoleHandle { virtio_resources::console::VirtioConsoleHandle { backend, - disconnect_policy: if self.active - && !self.console.as_ref().is_some_and(|(_, attachment, _)| { - attachment.reconnect_policy - == VirtioConsoleReconnectPolicy::DiscardWhileDisconnected - }) { - VirtioConsoleDisconnectPolicy::Retain + disconnect_policy: if self.active { + self.console.as_ref().map_or( + VirtioConsoleDisconnectPolicy::Retain, + |(_, attachment, _)| microvm_console_disconnect_policy(attachment), + ) } else { VirtioConsoleDisconnectPolicy::Discard }, diff --git a/openvmm/openvmm_entry/src/microvm/console.rs b/openvmm/openvmm_entry/src/microvm/console.rs index 5f60188ca5e..92577a1fdd5 100644 --- a/openvmm/openvmm_entry/src/microvm/console.rs +++ b/openvmm/openvmm_entry/src/microvm/console.rs @@ -65,6 +65,66 @@ pub(super) fn microvm_console_listener_replacement_matches( && saved.reconnect_timeout_ms == requested.reconnect_timeout_ms } +/// Returns whether `requested` is a listener that a restore attached to +/// `saved`, a disconnected boot console: a Unix socket or named pipe that +/// keeps discarding guest output while no client is connected, so that only +/// the endpoint differs from the saved attachment. +pub(super) fn microvm_console_discarding_listener_matches( + saved: &openvmm_helpers::snapshot::microvm::SnapshotAttachment, + requested: &openvmm_helpers::snapshot::microvm::SnapshotAttachment, +) -> bool { + saved.stable_id == MICROVM_CONSOLE_STABLE_ID + && saved.kind == MICROVM_CONSOLE_ATTACHMENT_KIND + && saved.reconnect_policy == "discard-while-disconnected" + && saved.identity_kind == "disconnected" + && saved.stable_id == requested.stable_id + && saved.kind == requested.kind + && saved.required == requested.required + && saved.reconnect_policy == requested.reconnect_policy + && matches!( + requested.identity_kind.as_str(), + "unix-socket" | "named-pipe" + ) + && saved.length == requested.length + && saved.reconnect_timeout_ms == requested.reconnect_timeout_ms +} + +/// Returns the attachment of a listener at `requested`, a Unix socket or named +/// pipe, that a restore attaches to a disconnected boot console. Like the +/// disconnected attachment, it discards guest output while no client is +/// connected, so the guest never waits for a client. +fn microvm_console_discarding_listener( + requested: &SerialConfigCli, +) -> anyhow::Result { + use virtio_resources::console::attachment::VirtioConsoleReconnectPolicy; + + anyhow::ensure!( + matches!(requested, SerialConfigCli::Pipe(_)), + "a restore attaches only a socket or pipe listener to a disconnected virtio-console" + ); + let (effective, mut attachment, mut snapshot) = microvm_console_attachment_from_cli(requested)?; + attachment.reconnect_policy = VirtioConsoleReconnectPolicy::DiscardWhileDisconnected; + snapshot.reconnect_policy = "discard-while-disconnected".to_owned(); + Ok((effective, attachment, snapshot)) +} + +/// Returns what the boot console's device does with guest output while no +/// client is connected to its host endpoint: a disconnected attachment, or a +/// listener that a restore attached to one, discards it, and every other +/// attachment retains it until a client connects. +pub(crate) fn microvm_console_disconnect_policy( + attachment: &virtio_resources::console::attachment::VirtioConsoleAttachment, +) -> virtio_resources::console::attachment::VirtioConsoleDisconnectPolicy { + use virtio_resources::console::attachment::VirtioConsoleDisconnectPolicy; + use virtio_resources::console::attachment::VirtioConsoleReconnectPolicy; + + if attachment.reconnect_policy == VirtioConsoleReconnectPolicy::DiscardWhileDisconnected { + VirtioConsoleDisconnectPolicy::Discard + } else { + VirtioConsoleDisconnectPolicy::Retain + } +} + pub(crate) struct MicrovmConsoleSocketCleanup { #[cfg(unix)] path: PathBuf, @@ -529,6 +589,20 @@ fn microvm_console_attachment_from_snapshot_with_identity( "snapshot microVM console identity is not canonical" ); if let Some(requested) = requested { + // A restore may attach a socket or pipe listener to a disconnected + // boot console, which then keeps discarding guest output while no + // client is connected. + if !control_console + && attachment.reconnect_policy == "discard-while-disconnected" + && matches!(requested, SerialConfigCli::Pipe(_)) + { + let listener = microvm_console_discarding_listener(requested)?; + anyhow::ensure!( + microvm_console_discarding_listener_matches(attachment, &listener.2), + "restore-time virtio-console listener does not match the snapshot attachment contract" + ); + return Ok(listener); + } let requested = if control_console { microvm_control_console_attachment_from_cli(requested)? } else { @@ -713,6 +787,9 @@ mod tests { use super::*; use clap::Parser as _; use test_with_tracing::test; + use virtio_resources::console::attachment::VirtioConsoleAttachmentMode; + use virtio_resources::console::attachment::VirtioConsoleBackendKind; + use virtio_resources::console::attachment::VirtioConsoleDisconnectPolicy; use virtio_resources::console::attachment::VirtioConsoleReconnectPolicy; fn socket_attachment(path: &Path) -> openvmm_helpers::snapshot::microvm::SnapshotAttachment { @@ -1088,6 +1165,228 @@ mod tests { assert_eq!(restored_snapshot, snapshot); } + /// Asserts that `restored`, what a restore attached to a disconnected + /// boot console `snapshot`, is a listener at `endpoint` of the backend + /// `backend_kind`, which discards guest output while no client is + /// connected, and which differs from the saved attachment in its endpoint + /// alone. + fn assert_discarding_listener( + snapshot: &openvmm_helpers::snapshot::microvm::SnapshotAttachment, + restored: &ConsoleEndpoint, + endpoint: &Path, + backend_kind: VirtioConsoleBackendKind, + ) { + let (config, resource, restored_attachment) = restored; + let SerialConfigCli::Pipe(path) = config else { + panic!("restore did not attach a listener"); + }; + assert!(path.ends_with(endpoint.file_name().unwrap())); + assert_eq!(resource.mode, VirtioConsoleAttachmentMode::Listen); + assert_eq!(resource.backend_kind, backend_kind); + assert_eq!( + resource.reconnect_policy, + VirtioConsoleReconnectPolicy::DiscardWhileDisconnected + ); + assert!(!resource.required); + assert_eq!(resource.reconnect_timeout_ms, 0); + assert_eq!( + microvm_console_disconnect_policy(resource), + VirtioConsoleDisconnectPolicy::Discard + ); + assert_eq!( + restored_attachment.reconnect_policy, + "discard-while-disconnected" + ); + assert_ne!(restored_attachment.identity, snapshot.identity); + assert!(microvm_console_discarding_listener_matches( + snapshot, + restored_attachment + )); + assert!(!microvm_console_listener_replacement_matches( + snapshot, + restored_attachment + )); + } + + #[cfg(unix)] + #[test] + fn disconnected_attachment_takes_a_restore_listener_that_keeps_discarding() { + let (_, _, snapshot) = microvm_console_attachment_from_cli(&SerialConfigCli::None).unwrap(); + let directory = tempfile::tempdir().unwrap(); + let endpoint = directory.path().join("clone.console"); + + let restored = microvm_console_attachment_from_snapshot( + &snapshot, + Some(&SerialConfigCli::Pipe(endpoint.clone())), + ) + .unwrap(); + + assert_discarding_listener( + &snapshot, + &restored, + &endpoint, + VirtioConsoleBackendKind::UnixSocket, + ); + assert_eq!(restored.2.identity_kind, "unix-socket"); + // The listener's socket must be beside the snapshot, as a listener + // that a snapshot recorded must be. + validate_microvm_console_attachment_namespace( + &restored.2, + &directory.path().join("snapshot"), + ) + .unwrap(); + let elsewhere = tempfile::tempdir().unwrap(); + assert!( + validate_microvm_console_attachment_namespace( + &restored.2, + &elsewhere.path().join("snapshot") + ) + .is_err() + ); + } + + #[cfg(windows)] + #[test] + fn disconnected_attachment_takes_a_restore_pipe_listener_that_keeps_discarding() { + let (_, _, snapshot) = microvm_console_attachment_from_cli(&SerialConfigCli::None).unwrap(); + let endpoint = PathBuf::from("//./pipe/openvmm-microvm-clone-console"); + + let restored = microvm_console_attachment_from_snapshot( + &snapshot, + Some(&SerialConfigCli::Pipe(endpoint.clone())), + ) + .unwrap(); + + assert_discarding_listener( + &snapshot, + &restored, + &endpoint, + VirtioConsoleBackendKind::NamedPipe, + ); + assert_eq!(restored.2.identity_kind, "named-pipe"); + assert!( + microvm_console_attachment_from_snapshot( + &snapshot, + Some(&SerialConfigCli::Pipe(PathBuf::from("//./pipe/unrelated"))) + ) + .is_err() + ); + } + + #[test] + fn disconnected_attachment_refuses_every_other_restore_attachment() { + let (_, _, snapshot) = microvm_console_attachment_from_cli(&SerialConfigCli::None).unwrap(); + #[cfg(unix)] + let directory = tempfile::tempdir().unwrap(); + #[cfg(unix)] + let client = directory.path().join("client.console"); + #[cfg(windows)] + let client = PathBuf::from("//./pipe/openvmm-microvm-client-console"); + for (requested, what) in [ + ( + SerialConfigCli::Tcp("127.0.0.1:5555".parse().unwrap()), + "a TCP listener", + ), + ( + SerialConfigCli::ConnectTcp("127.0.0.1:5555".parse().unwrap()), + "a TCP client", + ), + ( + SerialConfigCli::ConnectPipe(client), + "a socket or pipe client", + ), + (SerialConfigCli::Console, "the terminal"), + ] { + assert!( + microvm_console_attachment_from_snapshot(&snapshot, Some(&requested)).is_err(), + "a disconnected boot console took {what} at a restore" + ); + } + } + + #[cfg(unix)] + #[test] + fn disconnected_control_console_refuses_a_restore_listener() { + use std::os::unix::fs::PermissionsExt as _; + + let (_, _, snapshot) = + microvm_control_console_attachment_from_cli(&SerialConfigCli::None).unwrap(); + let directory = tempfile::tempdir().unwrap(); + std::fs::set_permissions(directory.path(), std::fs::Permissions::from_mode(0o700)).unwrap(); + let requested = SerialConfigCli::Pipe(directory.path().join("control.sock")); + + assert!( + microvm_console_attachment_from_snapshot_with_identity( + &snapshot, + Some(&requested), + MICROVM_CONTROL_CONSOLE_STABLE_ID, + MICROVM_CONTROL_CONSOLE_ATTACHMENT_KIND, + true, + ) + .is_err() + ); + } + + #[test] + fn only_a_disconnected_boot_console_takes_a_discarding_listener() { + let disconnected = microvm_console_attachment_from_cli(&SerialConfigCli::None) + .unwrap() + .2; + let listener = openvmm_helpers::snapshot::microvm::SnapshotAttachment { + reconnect_policy: "discard-while-disconnected".to_owned(), + identity_kind: "unix-socket".to_owned(), + identity: b"/snapshots/clone.console".to_vec(), + ..disconnected.clone() + }; + assert!(microvm_console_discarding_listener_matches( + &disconnected, + &listener + )); + + // A listener that retains output, or whose contract changes beyond + // its endpoint, is no discarding replacement. + let retaining = openvmm_helpers::snapshot::microvm::SnapshotAttachment { + reconnect_policy: "recreate-listener".to_owned(), + ..listener.clone() + }; + let required = openvmm_helpers::snapshot::microvm::SnapshotAttachment { + required: true, + ..listener.clone() + }; + let tcp = openvmm_helpers::snapshot::microvm::SnapshotAttachment { + identity_kind: "tcp".to_owned(), + identity: b"127.0.0.1:5555".to_vec(), + ..listener.clone() + }; + for requested in [&retaining, &required, &tcp, &disconnected] { + assert!(!microvm_console_discarding_listener_matches( + &disconnected, + requested + )); + } + + // Neither does a saved listener, nor the control console. + let saved_listener = socket_attachment(Path::new("source.sock")); + assert!(!microvm_console_discarding_listener_matches( + &saved_listener, + &listener + )); + let control = openvmm_helpers::snapshot::microvm::SnapshotAttachment { + stable_id: MICROVM_CONTROL_CONSOLE_STABLE_ID.to_owned(), + kind: MICROVM_CONTROL_CONSOLE_ATTACHMENT_KIND.to_owned(), + ..disconnected + }; + let control_listener = openvmm_helpers::snapshot::microvm::SnapshotAttachment { + stable_id: MICROVM_CONTROL_CONSOLE_STABLE_ID.to_owned(), + kind: MICROVM_CONTROL_CONSOLE_ATTACHMENT_KIND.to_owned(), + ..listener + }; + assert!(!microvm_console_discarding_listener_matches( + &control, + &control_listener + )); + } + #[test] fn control_attachment_has_distinct_identity_and_rejects_tcp() { let (_, resource, snapshot) = diff --git a/openvmm/openvmm_entry/src/microvm/mod.rs b/openvmm/openvmm_entry/src/microvm/mod.rs index fb905a4508f..5f87179a957 100644 --- a/openvmm/openvmm_entry/src/microvm/mod.rs +++ b/openvmm/openvmm_entry/src/microvm/mod.rs @@ -29,6 +29,8 @@ pub(crate) use console::microvm_console_attachment_from_cli; #[cfg(any(feature = "ttrpc", feature = "grpc"))] pub(crate) use console::microvm_console_attachment_from_snapshot; #[cfg(any(feature = "ttrpc", feature = "grpc"))] +pub(crate) use console::microvm_console_disconnect_policy; +#[cfg(any(feature = "ttrpc", feature = "grpc"))] pub(crate) use console::microvm_console_socket_cleanup; #[cfg(any(feature = "ttrpc", feature = "grpc"))] pub(crate) use console::validate_microvm_console_attachment_namespace; diff --git a/openvmm/openvmm_entry/src/microvm/restore.rs b/openvmm/openvmm_entry/src/microvm/restore.rs index f9ef23c349e..ca6ecf9b168 100644 --- a/openvmm/openvmm_entry/src/microvm/restore.rs +++ b/openvmm/openvmm_entry/src/microvm/restore.rs @@ -5,6 +5,7 @@ use super::console::MICROVM_CONSOLE_STABLE_ID; use super::console::MICROVM_CONTROL_CONSOLE_STABLE_ID; +use super::console::microvm_console_discarding_listener_matches; use super::console::microvm_console_listener_replacement_matches; use super::filesystem::validate_microvm_filesystem_slot; use crate::Options; @@ -78,6 +79,23 @@ fn align_restore_console_listener_attachments( } } +/// Returns the boot console attachment from which a restore builds the +/// machine contract that the snapshot must match: the saved disconnected +/// attachment, if the restore attached a listener to it, which discards guest +/// output while no client is connected as the disconnected attachment does, +/// and `expected`, the restore's own attachment, otherwise. +fn restore_console_contract_attachment<'a>( + saved: &'a [SnapshotAttachment], + expected: Option<&'a SnapshotAttachment>, +) -> Option<&'a SnapshotAttachment> { + let expected = expected?; + saved + .iter() + .find(|attachment| attachment.stable_id == MICROVM_CONSOLE_STABLE_ID) + .filter(|saved| microvm_console_discarding_listener_matches(saved, expected)) + .or(Some(expected)) +} + /// Restore-time inputs that shape a microVM configuration. #[derive(Default)] pub(crate) struct MicrovmRestore { @@ -698,6 +716,8 @@ pub(crate) fn validate_restore_contract( // A dormant-slot snapshot captured no filesystem, so a filesystem that the // restore attaches to the slot is not part of the snapshot's contract. let filesystem = filesystem.filter(|_| saved_contract.microvm_filesystem.is_some()); + let console_attachment = + restore_console_contract_attachment(&saved_contract.attachments, console_attachment); let mut expected_contract = openvmm_helpers::snapshot::microvm::microvm_machine_contract( expected_hypervisor, openvmm_helpers::snapshot::microvm::MICROVM_BOOT_LAYOUT_VERSION, @@ -833,6 +853,51 @@ pub(crate) mod tests { assert_eq!(expected, vec![saved_attachment]); } + #[test] + fn a_restore_listener_of_a_disconnected_boot_console_keeps_the_saved_contract() { + let saved = SnapshotAttachment { + stable_id: MICROVM_CONSOLE_STABLE_ID.to_owned(), + kind: "virtio-console".to_owned(), + required: false, + reconnect_policy: "discard-while-disconnected".to_owned(), + identity_kind: "disconnected".to_owned(), + identity: b"discard".to_vec(), + length: 0, + reconnect_timeout_ms: 0, + }; + let listener = SnapshotAttachment { + identity_kind: "unix-socket".to_owned(), + identity: b"/snapshots/clone.console".to_vec(), + ..saved.clone() + }; + let saved_attachments = [saved.clone()]; + + // The restore's contract records the saved disconnected attachment, + // whose device discards output as the listener's does. + assert_eq!( + restore_console_contract_attachment(&saved_attachments, Some(&listener)), + Some(&saved) + ); + + // Any other attachment is checked as the restore supplies it. + let retaining = SnapshotAttachment { + reconnect_policy: "recreate-listener".to_owned(), + ..listener.clone() + }; + assert_eq!( + restore_console_contract_attachment(&saved_attachments, Some(&retaining)), + Some(&retaining) + ); + assert_eq!( + restore_console_contract_attachment(&[], Some(&listener)), + Some(&listener) + ); + assert_eq!( + restore_console_contract_attachment(&saved_attachments, None), + None + ); + } + #[test] fn generation_id_is_the_entropy_prefix() { let mut entropy = [0x5a; RESTORE_ENTROPY_LEN]; diff --git a/openvmm/openvmm_entry/src/ttrpc/microvm.rs b/openvmm/openvmm_entry/src/ttrpc/microvm.rs index a48b5b9385d..fc080cdc0cd 100644 --- a/openvmm/openvmm_entry/src/ttrpc/microvm.rs +++ b/openvmm/openvmm_entry/src/ttrpc/microvm.rs @@ -939,7 +939,7 @@ impl CreateVm { &snapshot_attachment, &restore.path, )?; - let (backend, disconnect_policy) = match endpoint_config { + let backend = match endpoint_config { SerialConfigCli::Pipe(path) => { let backend = bind_serial_without_cleanup(&path).with_context(|| { format!( @@ -949,9 +949,9 @@ impl CreateVm { })?; self.resources.console_socket_cleanup = crate::microvm::microvm_console_socket_cleanup(path)?; - (backend, VirtioConsoleDisconnectPolicy::Retain) + backend } - SerialConfigCli::ConnectPipe(path) => ( + SerialConfigCli::ConnectPipe(path) => { crate::serial_io::connect::connect_serial_with_timeout( &path, Duration::from_millis( @@ -963,15 +963,16 @@ impl CreateVm { "failed to reconnect virtio console client: {}", path.display() ) - })?, - VirtioConsoleDisconnectPolicy::Retain, - ), - SerialConfigCli::None => ( - DisconnectedSerialBackendHandle.into_resource(), - VirtioConsoleDisconnectPolicy::Discard, - ), + })? + } + SerialConfigCli::None => DisconnectedSerialBackendHandle.into_resource(), _ => unreachable!("saved microVM console was validated as an attachment"), }; + // A listener that the restore attached to a disconnected console + // discards guest output while no client is connected, as the + // saved device does. + let disconnect_policy = + crate::microvm::microvm_console_disconnect_policy(&resource_attachment); config.virtio_devices.push(( openvmm_defs::config::VirtioBus::Mmio, virtio_resources::console::VirtioConsoleHandle { diff --git a/vm/devices/virtio/virtio_console/src/resolver/attachment.rs b/vm/devices/virtio/virtio_console/src/resolver/attachment.rs index 7e0c725dbb8..3cee53e6999 100644 --- a/vm/devices/virtio/virtio_console/src/resolver/attachment.rs +++ b/vm/devices/virtio/virtio_console/src/resolver/attachment.rs @@ -25,6 +25,7 @@ pub(super) fn validate_attachment( ( VirtioConsoleAttachmentMode::Listen, VirtioConsoleReconnectPolicy::RecreateListener + | VirtioConsoleReconnectPolicy::DiscardWhileDisconnected ) | ( VirtioConsoleAttachmentMode::Connect, VirtioConsoleReconnectPolicy::ReconnectClient @@ -59,3 +60,56 @@ pub(super) fn validate_attachment( } Ok(()) } + +#[cfg(test)] +mod tests { + use super::validate_attachment; + use test_with_tracing::test; + use virtio_resources::console::attachment::VirtioConsoleAttachment; + use virtio_resources::console::attachment::VirtioConsoleAttachmentMode; + use virtio_resources::console::attachment::VirtioConsoleBackendKind; + use virtio_resources::console::attachment::VirtioConsoleReconnectPolicy; + + fn discarding_listener() -> VirtioConsoleAttachment { + VirtioConsoleAttachment { + stable_id: "console:microvm-virtio0".to_owned(), + backend_kind: VirtioConsoleBackendKind::UnixSocket, + mode: VirtioConsoleAttachmentMode::Listen, + endpoint_identity: "/snapshots/clone.console".to_owned(), + reconnect_policy: VirtioConsoleReconnectPolicy::DiscardWhileDisconnected, + required: false, + reconnect_timeout_ms: 0, + } + } + + #[test] + fn a_listener_may_discard_guest_output_while_disconnected() { + validate_attachment(Some(&discarding_listener())).unwrap(); + + // It stays optional, with no reconnect timeout, as a disconnected + // attachment does. + for attachment in [ + VirtioConsoleAttachment { + required: true, + ..discarding_listener() + }, + VirtioConsoleAttachment { + reconnect_timeout_ms: 5000, + ..discarding_listener() + }, + ] { + assert!(validate_attachment(Some(&attachment)).is_err()); + } + } + + #[test] + fn a_client_never_discards_guest_output() { + let client = VirtioConsoleAttachment { + mode: VirtioConsoleAttachmentMode::Connect, + required: true, + reconnect_timeout_ms: 5000, + ..discarding_listener() + }; + assert!(validate_attachment(Some(&client)).is_err()); + } +}