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): make /dev/kvm usable in cua runtime setup qemu

injaneity8f9a41d1fc5c
diff --git a/libs/cua/crates/cua-daemon/src/local.rs b/libs/cua/crates/cua-daemon/src/local.rs
index b8bf326a6d..2a3fce5f28 100644
--- a/libs/cua/crates/cua-daemon/src/local.rs
+++ b/libs/cua/crates/cua-daemon/src/local.rs
@@ -1288,9 +1288,25 @@ pub fn doctor_checks(r: &DoctorReport) -> Vec<cua_proto::daemon::v1::RuntimeChec
pub async fn setup(
components: &[String],
dry_run: bool,
+) -> Result<Vec<cua_proto::daemon::v1::SetupStep>> {
+ setup_with(
+ doctor_report().await,
+ components,
+ dry_run,
+ cua_vmm::qemu::install_qemu,
+ kvm_step,
+ )
+ .await
+}
+
+async fn setup_with(
+ report: DoctorReport,
+ components: &[String],
+ dry_run: bool,
+ install_qemu: impl AsyncFn() -> cua_vmm::Result<()>,
+ kvm: impl AsyncFn(bool) -> Option<cua_proto::daemon::v1::SetupStep>,
) -> Result<Vec<cua_proto::daemon::v1::SetupStep>> {
use cua_proto::daemon::v1::{CheckStatus, SetupStep};
- let report = doctor_report().await;
let wanted: Vec<String> = if components.is_empty() {
report
.backends
@@ -1334,7 +1350,7 @@ pub async fn setup(
continue;
}
let result = match kind {
- BackendKind::Qemu => cua_vmm::qemu::install_qemu().await,
+ BackendKind::Qemu => install_qemu().await,
BackendKind::Lume => cua_vmm::lume::install_lume().await,
_ => Err(VmmError::Unsupported {
backend: "container",
@@ -1354,9 +1370,42 @@ pub async fn setup(
},
});
}
+ let qemu_installed = report.qemu.qemu_img.is_some()
+ && (report.qemu.binaries)
+ .get(report.host.arch.qemu())
+ .is_some_and(Option::is_some);
+ let qemu_present = match steps.iter().find(|s| s.name == "qemu") {
+ Some(s) => s.status != CheckStatus::Error as i32,
+ None => components.is_empty() && qemu_installed,
+ };
+ if qemu_present {
+ steps.extend(kvm(dry_run).await);
+ }
Ok(steps)
}
+async fn kvm_step(dry_run: bool) -> Option<cua_proto::daemon::v1::SetupStep> {
+ use cua_proto::daemon::v1::{CheckStatus, SetupStep};
+ if cua_vmm::host::HostOs::current() != cua_vmm::host::HostOs::Linux {
+ return None;
+ }
+ let denied = cua_vmm::host::kvm_usable()
+ .is_err_and(|e| e.kind() == std::io::ErrorKind::PermissionDenied);
+ let user = std::env::var("USER").ok();
+ let run = async |program: &str, args: &[&str]| cua_vmm::host::run(program, args).await;
+ let (status, detail) =
+ match cua_vmm::qemu::grant_kvm(cua_vmm::host::kvm_usable, user, dry_run, run).await {
+ Ok(detail) if dry_run && denied => (CheckStatus::Installable, detail),
+ Ok(detail) => (CheckStatus::Ok, detail),
+ Err(e) => (CheckStatus::Error, Some(e.to_string())),
+ };
+ detail.map(|detail| SetupStep {
+ name: "kvm".into(),
+ status: status as i32,
+ detail,
+ })
+}
+
// ------------------------------------------------------------------ images
/// A local image.
@@ -1884,4 +1933,115 @@ mod tests {
assert_eq!(r.image, ImageSource::oci("localhost:5000/img:tag"));
assert!(place(" ", "linux").is_err());
}
+
+ fn qemu_report(ready: bool) -> cua_vmm::auto::DoctorReport {
+ use cua_vmm::auto::{BackendStatus, DoctorReport, HostReport};
+ DoctorReport {
+ host: HostReport {
+ os: cua_vmm::host::HostOs::Linux,
+ arch: cua_vmm::Arch::X86_64,
+ kvm: false,
+ accel: Default::default(),
+ },
+ backends: vec![BackendStatus {
+ backend: super::BackendKind::Qemu,
+ ready,
+ provisionable: true,
+ detail: String::new(),
+ missing: vec![],
+ provisioning: vec!["apt-get install qemu".into()],
+ }],
+ qemu: Default::default(),
+ lume: Default::default(),
+ container: Default::default(),
+ }
+ }
+
+ fn kvm_step() -> Option<cua_proto::daemon::v1::SetupStep> {
+ Some(cua_proto::daemon::v1::SetupStep {
+ name: "kvm".into(),
+ status: cua_proto::daemon::v1::CheckStatus::Ok as i32,
+ detail: "added".into(),
+ })
+ }
+
+ async fn setup_steps(
+ report: cua_vmm::auto::DoctorReport,
+ components: &[&str],
+ dry_run: bool,
+ installs: bool,
+ ) -> Vec<(String, cua_proto::daemon::v1::CheckStatus)> {
+ let components: Vec<String> = components.iter().map(|c| c.to_string()).collect();
+ let install = async || match installs {
+ true => Ok(()),
+ false => Err(super::VmmError::PermissionDenied("sudo".into())),
+ };
+ let kvm = async |_| kvm_step();
+ super::setup_with(report, &components, dry_run, install, kvm)
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|s| (s.name, s.status.try_into().unwrap()))
+ .collect()
+ }
+
+ fn steps(
+ steps: &[(&str, cua_proto::daemon::v1::CheckStatus)],
+ ) -> Vec<(String, cua_proto::daemon::v1::CheckStatus)> {
+ steps.iter().map(|(n, s)| (n.to_string(), *s)).collect()
+ }
+
+ #[tokio::test]
+ async fn the_kvm_step_follows_qemu_once_it_is_present() {
+ use cua_proto::daemon::v1::CheckStatus::{Installable, Ok};
+ assert_eq!(
+ setup_steps(qemu_report(false), &[], false, true).await,
+ steps(&[("qemu", Ok), ("kvm", Ok)])
+ );
+ assert_eq!(
+ setup_steps(qemu_report(true), &["qemu"], false, true).await,
+ steps(&[("qemu", Ok), ("kvm", Ok)])
+ );
+ assert_eq!(
+ setup_steps(qemu_report(false), &["qemu"], true, true).await,
+ steps(&[("qemu", Installable), ("kvm", Ok)])
+ );
+ }
+
+ #[tokio::test]
+ async fn a_failed_qemu_install_has_no_kvm_step() {
+ use cua_proto::daemon::v1::CheckStatus::Error;
+ assert_eq!(
+ setup_steps(qemu_report(false), &["qemu"], false, false).await,
+ steps(&[("qemu", Error)])
+ );
+ }
+
+ #[tokio::test]
+ async fn components_without_qemu_have_no_kvm_step() {
+ use cua_proto::daemon::v1::CheckStatus::Error;
+ assert_eq!(
+ setup_steps(qemu_report(false), &["lume"], false, false).await,
+ steps(&[("lume", Error)])
+ );
+ assert!(
+ setup_steps(qemu_report(true), &[], false, true)
+ .await
+ .is_empty()
+ );
+ }
+
+ #[tokio::test]
+ async fn installed_qemu_denied_kvm_still_gets_a_kvm_step() {
+ use cua_proto::daemon::v1::CheckStatus::Ok;
+ let mut report = qemu_report(false);
+ report.backends[0].provisionable = false;
+ report.qemu.qemu_img = Some("/usr/bin/qemu-img".into());
+ let system = Some("/usr/bin/qemu-system-x86_64".into());
+ report.qemu.binaries.insert("x86_64".into(), system);
+ assert_eq!(
+ setup_steps(report, &[], false, true).await,
+ steps(&[("kvm", Ok)])
+ );
+ }
}
diff --git a/libs/cua/crates/cua-vmm/src/qemu/mod.rs b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
index 100600219d..c45a5cff8c 100644
--- a/libs/cua/crates/cua-vmm/src/qemu/mod.rs
+++ b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
@@ -474,6 +474,32 @@ impl QemuRuntime {
st: &mut QemuState,
spec: &StartSpec,
loadvm: Option<String>,
+ ) -> Result<()> {
+ match self.launch_once(st, spec, loadvm.clone(), false).await {
+ Err(e) if st.accel == "kvm" && self.cfg.accel.is_none() && kvm_stalled(&e) => {
+ tracing::warn!(
+ sandbox = %st.name,
+ "KVM did not start a vCPU (QEMU stopped answering QMP; see `dmesg` for kvm errors), so retrying under slow TCG emulation"
+ );
+ if let Some(pid) = st.pid {
+ let _ = host::run("kill", &["-9", &pid.to_string()]).await;
+ if !Self::wait_exit(pid, Duration::from_secs(5)).await {
+ return Err(e);
+ }
+ }
+ st.pid = None;
+ self.launch_once(st, spec, loadvm, true).await
+ }
+ r => r,
+ }
+ }
+
+ async fn launch_once(
+ &self,
+ st: &mut QemuState,
+ spec: &StartSpec,
+ loadvm: Option<String>,
+ force_tcg: bool,
) -> Result<()> {
let bin = self.binary(st.arch).await?;
let dir = self.dir(&st.name);
@@ -490,7 +516,11 @@ impl QemuRuntime {
} else {
None
};
- st.accel = self.accel_for(st.arch, &host::kvm_usable())?;
+ st.accel = if force_tcg {
+ "tcg".into()
+ } else {
+ self.accel_for(st.arch, &host::kvm_usable())?
+ };
st.cpus = spec.cpus;
st.memory_mb = spec.memory_mb;
st.restrict_network = spec.restrict_network;
@@ -903,6 +933,10 @@ pub fn usernet_established(table: &str, host_port: u16, guest_port: u16) -> bool
})
}
+fn kvm_stalled(e: &VmmError) -> bool {
+ matches!(e, VmmError::Qmp(m) if m.contains("timed out"))
+}
+
fn unique_suffix() -> String {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
@@ -945,7 +979,49 @@ fn missing_tools(
))
}
+pub async fn grant_kvm(
+ probe: impl Fn() -> std::io::Result<()>,
+ user: Option<String>,
+ dry_run: bool,
+ run: impl AsyncFn(&str, &[&str]) -> Result<String>,
+) -> Result<Option<String>> {
+ let e = match probe() {
+ Ok(()) => return Ok(None),
+ Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => e,
+ Err(e) => return Ok(Some(host::kvm_hint(&e))),
+ };
+ let user = user.filter(|user| !user.is_empty());
+ if dry_run {
+ let user = user.as_deref().unwrap_or("$USER");
+ return Ok(Some(format!(
+ "would: add {user} to the kvm group and grant it /dev/kvm now (setfacl)"
+ )));
+ }
+ let denied = || VmmError::PermissionDenied(host::kvm_hint(&e));
+ let group = run("stat", &["-c", "%G", "/dev/kvm"]).await;
+ if group.map_err(|_| denied())?.trim() != "kvm" {
+ return Err(denied());
+ }
+ let user = match user {
+ Some(user) => user,
+ None => run("id", &["-un"]).await?.trim().to_string(),
+ };
+ run("sudo", &["-n", "usermod", "-aG", "kvm", &user])
+ .await
+ .map_err(|_| denied())?;
+ let acl = format!("u:{user}:rw");
+ let setfacl = run("sudo", &["-n", "setfacl", "-m", &acl, "/dev/kvm"]).await;
+ Ok(Some(match setfacl.is_ok() && probe().is_ok() {
+ true => format!("added {user} to the kvm group and granted /dev/kvm now"),
+ false => {
+ format!("added {user} to the kvm group; log out and back in (or run `newgrp kvm`)")
+ }
+ }))
+}
+
/// Install QEMU through the host package manager (explicit opt-in).
+const PACKAGE_ATTEMPTS: u32 = 3;
+
pub async fn install_qemu() -> Result<()> {
match host::HostOs::current() {
host::HostOs::Macos => {
@@ -965,7 +1041,28 @@ pub async fn install_qemu() -> Result<()> {
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?;
+ let refresh = args.last() == Some(&"update");
+ let mut attempt = 1;
+ loop {
+ match host::run("sudo", &args).await {
+ Ok(_) => break,
+ Err(e) if attempt < PACKAGE_ATTEMPTS => {
+ tracing::warn!(
+ "`sudo {}` failed (attempt {attempt}), retrying: {e}",
+ args.join(" ")
+ );
+ attempt += 1;
+ tokio::time::sleep(Duration::from_secs(3 * attempt as u64)).await;
+ }
+ Err(e) if refresh => {
+ tracing::warn!(
+ "package index refresh failed, installing from the current lists: {e}"
+ );
+ break;
+ }
+ Err(e) => return Err(e),
+ }
+ }
}
}
host::HostOs::Windows => {
@@ -1517,6 +1614,19 @@ mod tests {
assert_eq!(r.accel_for(Arch::host(), &missing).unwrap(), "tcg");
}
+ #[test]
+ fn only_a_qmp_timeout_counts_as_a_stalled_kvm_launch() {
+ assert!(kvm_stalled(&VmmError::Qmp(
+ "timed out waiting for QEMU".into()
+ )));
+ assert!(!kvm_stalled(&VmmError::Qmp(
+ "connection closed by QEMU".into()
+ )));
+ assert!(!kvm_stalled(&VmmError::other(
+ "qemu started but VM status is 'paused'"
+ )));
+ }
+
#[test]
fn explicit_tcg_override_is_allowed() {
let tmp = tempfile::tempdir().unwrap();
@@ -1748,6 +1858,189 @@ mod tests {
assert!(matches!(r.load("win"), Err(VmmError::NotFound(_))));
}
+ fn recording(
+ calls: &std::sync::Mutex<Vec<String>>,
+ group: &'static str,
+ failing: &'static str,
+ ) -> impl AsyncFn(&str, &[&str]) -> Result<String> {
+ async move |program: &str, args: &[&str]| {
+ calls
+ .lock()
+ .unwrap()
+ .push(format!("{program} {}", args.join(" ")));
+ match program {
+ "stat" => Ok(format!("{group}\n")),
+ "id" => Ok("alice\n".to_string()),
+ _ if args.contains(&failing) => {
+ Err(VmmError::PermissionDenied("a password is required".into()))
+ }
+ _ => Ok(String::new()),
+ }
+ }
+ }
+
+ fn denied() -> std::io::Result<()> {
+ Err(std::io::ErrorKind::PermissionDenied.into())
+ }
+
+ fn denied_until_setfacl(
+ calls: &std::sync::Mutex<Vec<String>>,
+ ) -> impl Fn() -> std::io::Result<()> {
+ move || match calls.lock().unwrap().iter().any(|c| c.contains("setfacl")) {
+ true => Ok(()),
+ false => denied(),
+ }
+ }
+
+ #[tokio::test]
+ async fn denied_kvm_adds_the_user_to_the_kvm_group_then_grants_it_now() {
+ let calls = Default::default();
+ let note = grant_kvm(
+ denied_until_setfacl(&calls),
+ Some("bob".into()),
+ false,
+ recording(&calls, "kvm", ""),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ calls.into_inner().unwrap(),
+ [
+ "stat -c %G /dev/kvm",
+ "sudo -n usermod -aG kvm bob",
+ "sudo -n setfacl -m u:bob:rw /dev/kvm",
+ ]
+ );
+ assert_eq!(note, "added bob to the kvm group and granted /dev/kvm now");
+ }
+
+ #[tokio::test]
+ async fn a_failed_setfacl_still_adds_the_group_with_a_relogin_note() {
+ let calls = Default::default();
+ let note = grant_kvm(
+ denied_until_setfacl(&calls),
+ Some("bob".into()),
+ false,
+ recording(&calls, "kvm", "setfacl"),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ let calls = calls.into_inner().unwrap();
+ assert!(calls.contains(&"sudo -n usermod -aG kvm bob".to_string()));
+ assert!(note.starts_with("added bob to the kvm group; "), "{note}");
+ assert!(note.contains("log out and back in"), "{note}");
+ assert!(note.contains("newgrp kvm"), "{note}");
+ }
+
+ #[tokio::test]
+ async fn kvm_still_denied_after_setfacl_keeps_the_relogin_note() {
+ let calls = Default::default();
+ let note = grant_kvm(
+ denied,
+ Some("bob".into()),
+ false,
+ recording(&calls, "kvm", ""),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(
+ calls.into_inner().unwrap().last().unwrap(),
+ "sudo -n setfacl -m u:bob:rw /dev/kvm"
+ );
+ assert!(note.contains("log out and back in"), "{note}");
+ }
+
+ #[tokio::test]
+ async fn an_empty_user_falls_back_to_id_un() {
+ let calls = Default::default();
+ let note = grant_kvm(
+ denied_until_setfacl(&calls),
+ Some(String::new()),
+ false,
+ recording(&calls, "kvm", ""),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ let calls = calls.into_inner().unwrap();
+ assert!(calls.contains(&"id -un".to_string()), "{calls:?}");
+ assert!(calls.contains(&"sudo -n usermod -aG kvm alice".to_string()));
+ assert!(note.starts_with("added alice "), "{note}");
+ }
+
+ #[tokio::test]
+ async fn denied_kvm_without_sudo_is_an_error_naming_the_usermod_fix() {
+ let calls = Default::default();
+ let err = grant_kvm(
+ denied,
+ Some("bob".into()),
+ false,
+ recording(&calls, "kvm", "usermod"),
+ )
+ .await
+ .unwrap_err();
+ assert_eq!(err.to_string(), host::kvm_hint(&denied().unwrap_err()));
+ assert!(err.to_string().contains("sudo usermod -aG kvm"), "{err}");
+ let calls = calls.into_inner().unwrap();
+ assert!(!calls.iter().any(|c| c.contains("setfacl")), "{calls:?}");
+ }
+
+ #[tokio::test]
+ async fn kvm_owned_by_another_group_is_the_hint_without_sudo() {
+ let calls = Default::default();
+ let err = grant_kvm(
+ denied,
+ Some("bob".into()),
+ false,
+ recording(&calls, "root", ""),
+ )
+ .await
+ .unwrap_err();
+ assert_eq!(err.to_string(), host::kvm_hint(&denied().unwrap_err()));
+ let calls = calls.into_inner().unwrap();
+ assert!(!calls.iter().any(|c| c.starts_with("sudo")), "{calls:?}");
+ }
+
+ #[tokio::test]
+ async fn missing_kvm_runs_nothing_and_is_only_a_note() {
+ let calls = Default::default();
+ let missing = || Err(std::io::ErrorKind::NotFound.into());
+ let note = grant_kvm(
+ missing,
+ Some("bob".into()),
+ false,
+ recording(&calls, "kvm", ""),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ assert!(note.contains("/dev/kvm is missing"), "{note}");
+ assert!(calls.lock().unwrap().is_empty());
+ }
+
+ #[tokio::test]
+ async fn dry_run_only_says_it_would_add_the_user_and_grant_kvm() {
+ let calls = Default::default();
+ let note = grant_kvm(
+ denied,
+ Some("bob".into()),
+ true,
+ recording(&calls, "kvm", ""),
+ )
+ .await
+ .unwrap()
+ .unwrap();
+ assert!(
+ note.starts_with("would: add bob to the kvm group"),
+ "{note}"
+ );
+ assert!(note.contains("grant it /dev/kvm now (setfacl)"), "{note}");
+ assert!(calls.lock().unwrap().is_empty());
+ }
+
#[test]
fn missing_qemu_hints_show_the_install_plan() {
assert_eq!(