Skip to content
Merged
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
21 changes: 16 additions & 5 deletions crates/kit/src/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::process::{Child, Command, Output, Stdio};
use std::sync::Arc;
use std::time::Duration;

use camino::Utf8PathBuf;
use camino::{Utf8Path, Utf8PathBuf};
use cap_std_ext::cmdext::CapStdExtCommandExt;
use color_eyre::eyre::{eyre, Context};
use color_eyre::Result;
Expand Down Expand Up @@ -382,10 +382,21 @@ fn spawn(
config.memory_mb
);

let qemu = std::env::var("QEMU_BIN").ok().unwrap_or_else(|| {
let arch = std::env::consts::ARCH;
format!("qemu-system-{arch}")
});
let qemu = std::env::var("QEMU_BIN")
.ok()
.map(Ok)
.unwrap_or_else(|| -> Result<_> {
// RHEL only supports non-emulated, and qemu is an implementation detail
// of higher level virt.
let libexec_qemu = Utf8Path::new("/usr/libexec/qemu-kvm");
if libexec_qemu.try_exists()? {
Ok(libexec_qemu.to_string())
} else {
let arch = std::env::consts::ARCH;
Ok(format!("qemu-system-{arch}"))
}
})
.context("Checking for qemu")?;
Comment on lines +385 to +399

Choose a reason for hiding this comment

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

medium

While the functional-style chain with map(Ok).unwrap_or_else is correct, it can be a bit dense and harder to read for logic involving fallbacks and potential errors. Refactoring this into an immediately-invoked closure with a more traditional if/else structure makes the sequence of checks clearer and improves overall readability.

Suggested change
let qemu = std::env::var("QEMU_BIN")
.ok()
.map(Ok)
.unwrap_or_else(|| -> Result<_> {
// RHEL only supports non-emulated, and qemu is an implementation detail
// of higher level virt.
let libexec_qemu = Utf8Path::new("/usr/libexec/qemu-kvm");
if libexec_qemu.try_exists()? {
Ok(libexec_qemu.to_string())
} else {
let arch = std::env::consts::ARCH;
Ok(format!("qemu-system-{arch}"))
}
})
.context("Checking for qemu")?;
let qemu = (|| -> Result<_> {
if let Some(qemu_bin) = std::env::var("QEMU_BIN").ok() {
Ok(qemu_bin)
} else {
// RHEL only supports non-emulated, and qemu is an implementation detail
// of higher level virt.
let libexec_qemu = Utf8Path::new("/usr/libexec/qemu-kvm");
if libexec_qemu.try_exists()? {
Ok(libexec_qemu.to_string())
} else {
let arch = std::env::consts::ARCH;
Ok(format!("qemu-system-{arch}"))
}
}
})()
.context("Checking for qemu")?;


let mut cmd = Command::new(qemu);
// SAFETY: This API is safe to call in a forked child.
Expand Down