Skip to content

powershell_builder: new support crate #1328

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5392,6 +5392,7 @@ dependencies = [
"petri_artifacts_core",
"petri_artifacts_vmm_test",
"pipette_client",
"powershell_builder",
"prost",
"scsidisk_resources",
"serde",
Expand Down Expand Up @@ -5627,6 +5628,14 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"

[[package]]
name = "powershell_builder"
version = "0.0.0"
dependencies = [
"guid",
"jiff",
]

[[package]]
name = "prettyplease"
version = "0.1.25"
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pal_async = { path = "support/pal/pal_async" }
pal_async_test = { path = "support/pal/pal_async_test" }
pal_event = { path = "support/pal/pal_event" }
pal_uring = { path = "support/pal/pal_uring" }
powershell_builder = { path = "support/powershell_builder" }
safeatomic = { path = "support/safeatomic" }
serde_helpers = { path = "support/serde_helpers" }
sev_guest_device = { path = "support/sev_guest_device" }
Expand Down
1 change: 1 addition & 0 deletions petri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mesh_worker.workspace = true
mesh.workspace = true
pal_async.workspace = true
pal.workspace = true
powershell_builder.workspace = true
unix_socket.workspace = true
sparse_mmap.workspace = true

Expand Down
37 changes: 2 additions & 35 deletions petri/src/vm/hyperv/hvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

//! Functions for interacting with Hyper-V VMs.

use super::CommandError;
use super::vm::CommandError;
use anyhow::Context;
use guid::Guid;
use jiff::Timestamp;
use std::ffi::OsStr;
use std::process::Stdio;

pub fn hvc_start(vmid: &Guid) -> Result<(), CommandError> {
hvc_output(|cmd| cmd.arg("start").arg(vmid.to_string())).map(|_| ())
Expand Down Expand Up @@ -95,37 +92,7 @@ fn hvc_output(
f: impl FnOnce(&mut std::process::Command) -> &mut std::process::Command,
) -> Result<String, CommandError> {
let mut cmd = std::process::Command::new("hvc.exe");
cmd.stderr(Stdio::piped()).stdin(Stdio::null());
f(&mut cmd);

let hvc_cmd = format!(
"{} {}",
cmd.get_program().to_string_lossy(),
cmd.get_args()
.collect::<Vec<_>>()
.join(OsStr::new(" "))
.to_string_lossy()
);
tracing::debug!(hvc_cmd, "executing hvc command");

let start = Timestamp::now();
let output = cmd.output()?;
let time_elapsed = Timestamp::now() - start;

let hvc_stdout = String::from_utf8_lossy(&output.stdout).to_string();
let hvc_stderr = String::from_utf8_lossy(&output.stderr).to_string();
tracing::debug!(
hvc_cmd,
hvc_stdout,
hvc_stderr,
"hvc command exited in {:.3}s with status {}",
time_elapsed.total(jiff::Unit::Second).unwrap_or(-1.0),
output.status
);

if !output.status.success() {
return Err(CommandError::Command(output.status, hvc_stderr));
}

Ok(String::from_utf8(output.stdout)?.trim().to_owned())
super::vm::run_cmd(cmd, true)
}
15 changes: 0 additions & 15 deletions petri/src/vm/hyperv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use thiserror::Error;
use vm::HyperVVM;
use vmm_core_defs::HaltReason;

Expand Down Expand Up @@ -717,20 +716,6 @@ impl PetriVmHyperV {
}
}

/// Error running command
#[derive(Error, Debug)]
pub enum CommandError {
/// failed to launch command
#[error("failed to launch command")]
Launch(#[from] std::io::Error),
/// command exited with non-zero status
#[error("command exited with non-zero status ({0}): {1}")]
Command(std::process::ExitStatus, String),
/// command output is not utf-8
#[error("command output is not utf-8")]
Utf8(#[from] std::string::FromUtf8Error),
}

fn acl_read_for_vm(path: &Path, id: Option<guid::Guid>) -> anyhow::Result<()> {
let sid_arg = format!(
"NT VIRTUAL MACHINE\\{name}:R",
Expand Down
Loading
Loading