Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-vmm): refresh apt before installing QEMU and print the plan that runs (#4972)

injaneity8ac7727281ec
diff --git a/libs/cua/crates/cua-vmm/src/auto.rs b/libs/cua/crates/cua-vmm/src/auto.rs
index ac2759c762..678f7fe304 100644
--- a/libs/cua/crates/cua-vmm/src/auto.rs
+++ b/libs/cua/crates/cua-vmm/src/auto.rs
@@ -129,6 +129,64 @@ impl DoctorReport {
}
}
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum QemuPackageManager {
+ Apt,
+ Dnf,
+}
+
+impl QemuPackageManager {
+ pub fn detect() -> Option<(Self, PathBuf)> {
+ [Self::Apt, Self::Dnf]
+ .into_iter()
+ .find_map(|manager| host::which(manager.program()).map(|path| (manager, path)))
+ }
+
+ fn program(self) -> &'static str {
+ match self {
+ Self::Apt => "apt-get",
+ Self::Dnf => "dnf",
+ }
+ }
+
+ fn packages(self) -> &'static [&'static str] {
+ match self {
+ Self::Apt => &[
+ "qemu-system-x86",
+ "qemu-system-arm",
+ "qemu-utils",
+ "qemu-efi-aarch64",
+ "ovmf",
+ ],
+ Self::Dnf => &["qemu-kvm", "qemu-img", "edk2-ovmf", "edk2-aarch64"],
+ }
+ }
+
+ pub fn commands(self, program: &str) -> Vec<Vec<&str>> {
+ let install = [&["-n", program, "install", "-y"], self.packages()].concat();
+ match self {
+ Self::Apt => vec![vec!["-n", program, "update"], install],
+ Self::Dnf => vec![install],
+ }
+ }
+
+ pub fn render(self) -> String {
+ self.commands(self.program())
+ .iter()
+ .map(|args| format!("sudo {}", args.join(" ")))
+ .collect::<Vec<_>>()
+ .join(" && ")
+ }
+
+ pub fn linux_hint() -> String {
+ format!(
+ "Debian/Ubuntu: `{}`; Fedora: `{}`",
+ Self::Apt.render(),
+ Self::Dnf.render()
+ )
+ }
+}
+
/// Inspect the host. Read-only: never installs or starts anything.
pub async fn doctor() -> DoctorReport {
let host_os = HostOs::current();
@@ -185,18 +243,13 @@ pub async fn doctor() -> DoctorReport {
.missing
.push(format!("qemu-system-{native} / qemu-img"));
let step = match host_os {
- HostOs::Macos if host::which("brew").is_some() => Some("brew install qemu"),
- HostOs::Linux if host::which("apt-get").is_some() => {
- Some("sudo apt-get install qemu-system qemu-utils")
- }
- HostOs::Linux if host::which("dnf").is_some() => {
- Some("sudo dnf install qemu-kvm qemu-img")
- }
+ HostOs::Macos if host::which("brew").is_some() => Some("brew install qemu".into()),
+ HostOs::Linux => QemuPackageManager::detect().map(|(manager, _)| manager.render()),
_ => None,
};
if let Some(s) = step {
qemu_status.provisionable = true;
- qemu_status.provisioning.push(s.to_string());
+ qemu_status.provisioning.push(s);
}
}
@@ -615,4 +668,73 @@ mod tests {
assert_eq!(json["backends"].as_array().unwrap().len(), 4);
eprintln!("{}", serde_json::to_string_pretty(&r).unwrap());
}
+
+ #[test]
+ fn apt_qemu_plan_refreshes_before_install() {
+ let commands = QemuPackageManager::Apt.commands("/usr/bin/apt-get");
+ assert_eq!(
+ commands,
+ [
+ vec!["-n", "/usr/bin/apt-get", "update"],
+ vec![
+ "-n",
+ "/usr/bin/apt-get",
+ "install",
+ "-y",
+ "qemu-system-x86",
+ "qemu-system-arm",
+ "qemu-utils",
+ "qemu-efi-aarch64",
+ "ovmf",
+ ],
+ ]
+ );
+ }
+
+ #[test]
+ fn apt_qemu_plan_render_matches_command_arguments() {
+ assert_eq!(
+ QemuPackageManager::Apt.render(),
+ "sudo -n apt-get update && sudo -n apt-get install -y qemu-system-x86 \
+ qemu-system-arm qemu-utils qemu-efi-aarch64 ovmf"
+ );
+ }
+
+ #[test]
+ fn dnf_qemu_plan_render_matches_command_arguments() {
+ assert_eq!(
+ QemuPackageManager::Dnf.commands("dnf"),
+ [vec![
+ "-n",
+ "dnf",
+ "install",
+ "-y",
+ "qemu-kvm",
+ "qemu-img",
+ "edk2-ovmf",
+ "edk2-aarch64",
+ ]]
+ );
+ assert_eq!(
+ QemuPackageManager::Dnf.render(),
+ "sudo -n dnf install -y qemu-kvm qemu-img edk2-ovmf edk2-aarch64"
+ );
+ }
+
+ #[test]
+ fn qemu_plan_preserves_both_guest_architectures() {
+ let apt = QemuPackageManager::Apt.render();
+ let dnf = QemuPackageManager::Dnf.render();
+ for package in [
+ "qemu-system-x86",
+ "ovmf",
+ "qemu-system-arm",
+ "qemu-efi-aarch64",
+ ] {
+ assert!(apt.contains(package), "{package}");
+ }
+ for package in ["qemu-kvm", "edk2-ovmf", "edk2-aarch64"] {
+ assert!(dnf.contains(package), "{package}");
+ }
+ }
}
diff --git a/libs/cua/crates/cua-vmm/src/qemu/mod.rs b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
index a88f2fce7d..69ffd69ba1 100644
--- a/libs/cua/crates/cua-vmm/src/qemu/mod.rs
+++ b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
@@ -28,6 +28,7 @@ use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
+use crate::auto::QemuPackageManager;
use crate::error::{Result, VmmError};
use crate::exec::{GuestExec, SshExec};
use crate::host;
@@ -271,8 +272,10 @@ impl QemuRuntime {
}
Err(VmmError::missing(
name,
- "install QEMU (macOS: `brew install qemu`; Debian/Ubuntu: `apt install qemu-system qemu-utils`; \
- Fedora: `dnf install qemu-kvm qemu-img`) or enable allow_install to let cua provision it",
+ format!(
+ "install QEMU (macOS: `brew install qemu`; {}) or enable allow_install to let cua provision it",
+ QemuPackageManager::linux_hint()
+ ),
))
}
@@ -339,7 +342,10 @@ impl QemuRuntime {
let fw = firmware::find_uefi(arch, &bin).ok_or_else(|| {
VmmError::missing(
format!("UEFI firmware for {arch}"),
- "install edk2 (Homebrew qemu ships it; Debian: `apt install qemu-efi-aarch64 ovmf`)",
+ format!(
+ "install edk2 (Homebrew qemu ships it; {})",
+ QemuPackageManager::linux_hint()
+ ),
)
})?;
match fw.vars_template {
@@ -892,45 +898,13 @@ pub async fn install_qemu() -> Result<()> {
host::run(brew, &["install", "qemu"]).await?;
}
host::HostOs::Linux => {
- if let Some(apt) = host::which("apt-get") {
- let apt = apt.display().to_string();
- tracing::info!("installing QEMU with apt (sudo -n)");
- host::run(
- "sudo",
- &[
- "-n",
- &apt,
- "install",
- "-y",
- "qemu-system-x86",
- "qemu-system-arm",
- "qemu-utils",
- "qemu-efi-aarch64",
- "ovmf",
- ],
- )
- .await?;
- } else if let Some(dnf) = host::which("dnf") {
- let dnf = dnf.display().to_string();
- host::run(
- "sudo",
- &[
- "-n",
- &dnf,
- "install",
- "-y",
- "qemu-kvm",
- "qemu-img",
- "edk2-ovmf",
- "edk2-aarch64",
- ],
- )
- .await?;
- } else {
- return Err(VmmError::missing(
- "QEMU",
- "no supported package manager found (apt-get/dnf)",
- ));
+ let (manager, program) = QemuPackageManager::detect().ok_or_else(|| {
+ VmmError::missing("QEMU", "no supported package manager found (apt-get/dnf)")
+ })?;
+ let program = program.display().to_string();
+ tracing::info!("installing QEMU with {manager:?} (sudo -n)");
+ for args in manager.commands(&program) {
+ host::run("sudo", &args).await?;
}
}
host::HostOs::Windows => {
@@ -1560,4 +1534,16 @@ mod tests {
.is_ok()
);
}
+
+ #[test]
+ fn missing_qemu_hints_show_the_install_plan() {
+ assert_eq!(
+ QemuPackageManager::linux_hint(),
+ format!(
+ "Debian/Ubuntu: `{}`; Fedora: `{}`",
+ QemuPackageManager::Apt.render(),
+ QemuPackageManager::Dnf.render()
+ )
+ );
+ }
}