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

Added support for using a custom QEMU binary #92

Merged
merged 1 commit into from
Oct 25, 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
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub struct Target {
/// Arch to run
#[serde(default = "Target::default_arch")]
pub arch: String,
/// Command used to launch QEMU
pub qemu_command: Option<String>,
/// Command to run inside virtual machine.
pub command: String,

Expand Down Expand Up @@ -139,6 +141,7 @@ impl Default for Target {
kernel_args: None,
rootfs: Self::default_rootfs(),
arch: Self::default_arch(),
qemu_command: None,
command: "".into(),
vm: VMConfig::default(),
}
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ struct Args {
/// Arch to run
#[clap(short, long, default_value = ARCH, conflicts_with = "config")]
arch: String,
/// Command to use to launch QEMU. Can be a full path or a PATH-resolved binary. If none is
/// provided, we default to `qemu-system-$ARCH`, where $ARCH is the value of the `arch`
/// argument
#[clap(short, long, conflicts_with = "config")]
qemu_command: Option<String>,
/// Command to run in kernel mode. `-` to get an interactive shell.
#[clap(conflicts_with = "config")]
command: Vec<String>,
Expand Down Expand Up @@ -113,6 +118,7 @@ fn config(args: &Args) -> Result<Vmtest> {
rootfs: args.rootfs.clone(),
arch: args.arch.clone(),
kernel_args: args.kargs.clone(),
qemu_command: args.qemu_command.clone(),
command: args.command.join(" "),
vm: VMConfig::default(),
}],
Expand Down
29 changes: 27 additions & 2 deletions src/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ffi::{OsStr, OsString};
use std::fs;
use std::hash::Hash;
use std::hash::Hasher;
use std::io::{BufRead, BufReader, Read, Write};
use std::io::{BufRead, BufReader, ErrorKind, Read, Write};
use std::marker::Send;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::net::UnixStream;
Expand Down Expand Up @@ -655,8 +655,13 @@ impl Qemu {
let qmp_sock = gen_sock("qmp");
let command_sock = gen_sock("cmdout");
let (init, guest_init) = gen_init(&target.rootfs).context("Failed to generate init")?;
let program = target
.qemu_command
.unwrap_or_else(|| format!("qemu-system-{}", target.arch));
Self::verify_qemu_exists(&program)?;

let mut c = Command::new(format!("qemu-system-{}", target.arch));
// Start the main QEMU process
let mut c = Command::new(program);

c.args(QEMU_DEFAULT_ARGS)
.stderr(Stdio::piped())
Expand Down Expand Up @@ -974,6 +979,26 @@ impl Qemu {
err
}

fn verify_qemu_exists(qemu_program: &str) -> anyhow::Result<()> {
if let Err(e) = Command::new(&qemu_program)
.arg("--help")
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
if let ErrorKind::NotFound = e.kind() {
Err(e).context(format!(
"Did not find QEMU binary {qemu_program}. Make sure QEMU is installed"
))
} else {
warn!("Failed to verify that qemu is installed due to error, continuing... {e}");
Ok(())
}
} else {
Ok(())
}
}

/// Boot the VM and connect to QGA
///
/// Returns the child process and QGA wrapper on success, an error otherwise.
Expand Down
Loading