Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hypestv: support relaying serial ports to new windows #374

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,18 @@ dependencies = [
"crossbeam-utils",
]

[[package]]
name = "console_relay"
version = "0.0.0"
dependencies = [
"anyhow",
"futures",
"getrandom",
"pal_async",
"term",
"unix_socket",
]

[[package]]
name = "consomme"
version = "0.0.0"
Expand Down Expand Up @@ -2955,9 +2967,11 @@ dependencies = [
"anyhow",
"clap",
"clap_dyn_complete",
"console_relay",
"diag_client",
"dirs",
"futures",
"futures-concurrency",
"guid",
"inspect",
"mesh",
Expand Down Expand Up @@ -4417,6 +4431,7 @@ dependencies = [
"chipset_resources",
"clap",
"clap_dyn_complete",
"console_relay",
"debug_worker_defs",
"diag_client",
"dirs",
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ cache_topology = { path = "support/cache_topology" }
closeable_mutex = { path = "support/closeable_mutex" }
ci_logger = { path = "support/ci_logger" }
clap_dyn_complete = { path = "support/clap_dyn_complete" }
console_relay = { path = "support/console_relay" }
safe_intrinsics = { path = "support/safe_intrinsics" }
debug_ptr = { path = "support/debug_ptr" }
fast_select = { path = "support/fast_select" }
Expand Down
9 changes: 6 additions & 3 deletions Guide/src/dev_guide/dev_tools/hypestv.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ After this, all commands will implicitly operate on `tdxvm`. Use `select` again
to work on another VM.

To enable serial port output, use the `serial` command. This can be used at any
time, even while the VM is not running. E.g., to enable serial port output for
COM2:
time, even while the VM is not running. E.g., to open a separate window for
interactive use of COM1 and enable logging serial port output for COM2:

```
tdxvm [off]> serial 2 output
tdxvm [off]> serial 1 term
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another useful addition to this sort of tool: having a .hypestv_init file (a-la .gdbinit) which lets you configure a set of pre-baked commands to run when doing various kinds of vm development (e.g: depending on the guest that's running, or the vm sku, etc...).

the concern, of course, is that this opens up an avenue to "script" this dev tool...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that similar to this, we'd have the concept of a workspace that could be loaded or saved by name. And maybe if you name it the same as a VM, it gets loaded automatically or something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like a .vmname file or something like that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I could probably get behind a scheme like auto-loading .hypestv-<vmname>.

tdxvm [off]> serial 2 log
```

Start a VM with `start`. This is an asynchronous command: you can continue to
Expand All @@ -61,6 +62,7 @@ on the prompt may not be accurate until you type another command or press Enter.

```
tdxvm [off]> start
serial port 1 connected
serial port 2 connected
VM started
tdxvm [off]>
Expand Down Expand Up @@ -92,6 +94,7 @@ VM.

```
tdxvm [running]> kill
serial port 1 disconnected
serial port 2 disconnected
VM killed
tdxvm [stopping]>
Expand Down
2 changes: 2 additions & 0 deletions hyperv/tools/hypestv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rust-version.workspace = true

[dependencies]
clap_dyn_complete.workspace = true
console_relay.workspace = true
diag_client.workspace = true
guid.workspace = true
inspect.workspace = true
Expand All @@ -18,6 +19,7 @@ anyhow.workspace = true
clap.workspace = true
dirs.workspace = true
futures.workspace = true
futures-concurrency.workspace = true
parking_lot.workspace = true
rustyline = { workspace = true, features = ["derive"] }
shell-words.workspace = true
Expand Down
45 changes: 34 additions & 11 deletions hyperv/tools/hypestv/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use hyperv::run_hvc;
use mesh::rpc::RpcSend;
use pal_async::DefaultDriver;
use rustyline_printer::Printer;
use std::fmt::Display;
use std::path::PathBuf;
use std::sync::Arc;
use vm::Vm;

Expand All @@ -42,6 +44,10 @@ pub(crate) enum InteractiveCommand {
/// Detach from the active VM.
Detach,

/// Quit the interactive shell.
#[clap(visible_alias = "q")]
Quit,

#[clap(flatten)]
Vm(VmCommand),
}
Expand Down Expand Up @@ -74,12 +80,12 @@ pub(crate) enum VmCommand {
force: bool,
},

/// Sets the serial output mode.
/// Gets or sets the serial output mode.
Serial {
/// The serial port to configure (1 = COM1, etc.).
port: u32,
port: Option<u32>,
/// The serial output mode.
mode: SerialMode,
mode: Option<SerialMode>,
},

/// Inspect host or paravisor state.
Expand All @@ -102,20 +108,28 @@ pub(crate) enum VmCommand {
},
}

#[derive(ValueEnum, Clone)]
#[derive(ValueEnum, Copy, Clone)]
pub(crate) enum SerialMode {
/// The serial port is disconnected.
Off,
/// The serial port output is connected to the host's console.
Output,
// TODO: add Console mode for interactive console, and Terminal mode for
// launching a terminal emulator.
/// The serial port output is logged to standard output.
Log,
/// The serial port input and output are connected to a new terminal
/// emulator window.
Term,
// TODO: add Console mode for interactive console.
}

impl Display for SerialMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.pad(self.to_possible_value().unwrap().get_name())
}
}

pub(crate) enum Request {
Prompt(mesh::rpc::Rpc<(), String>),
Inspect(mesh::rpc::Rpc<(InspectTarget, String), anyhow::Result<inspect::Node>>),
Command(mesh::rpc::Rpc<InteractiveCommand, anyhow::Result<()>>),
Command(mesh::rpc::Rpc<InteractiveCommand, anyhow::Result<bool>>),
}

pub(crate) enum InspectTarget {
Expand All @@ -130,10 +144,15 @@ pub(crate) enum InspectTarget {
struct CommandLine {
/// The initial VM name. Use select to change the active VM.
vm: Option<String>,
#[clap(long, hide(true))]
relay_console_path: Option<PathBuf>,
}

pub async fn main(driver: DefaultDriver) -> anyhow::Result<()> {
let command_line = CommandLine::parse();
if let Some(relay_console_path) = command_line.relay_console_path {
return console_relay::relay_console(&relay_console_path);
}

let mut rl = rustyline::Editor::<_, rustyline::history::FileHistory>::with_config(
rustyline::Config::builder()
Expand Down Expand Up @@ -215,7 +234,8 @@ pub async fn main(driver: DefaultDriver) -> anyhow::Result<()> {

match parse(&mut template, trimmed) {
Ok(cmd) => match block_on(send.call_failable(Request::Command, cmd)) {
Ok(()) => {}
Ok(true) => {}
Ok(false) => break,
Err(err) => {
eprintln!("{:#}", err);
}
Expand Down Expand Up @@ -275,8 +295,11 @@ pub async fn main(driver: DefaultDriver) -> anyhow::Result<()> {
.handle_command(cmd)
.await?;
}
InteractiveCommand::Quit => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, I'm surprised this wasn't here in v0. how were you quitting before this? like, via ctrl-d?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ctrl-d.

return Ok(false);
}
}
Ok(())
Ok(true)
})
.await
}
Expand Down
Loading