Skip to content

Commit 6925509

Browse files
tmt: Treat VM creation as critical section
Use a Mutex guard for VM creation as CI sometimes is failing with ``` Error: 0: Failed to create libvirt domain 1: Failed to start libvirt domain: error: Failed to start domain 'bootc-..' error: failed to create directory '/run/user/1001/libvirt/qemu/run/swtpm': File exists ``` Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent 54609eb commit 6925509

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

crates/xtask/src/tmt.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::mpsc;
1+
use std::sync::{Arc, Mutex, mpsc};
22
use std::time::Duration;
33
use std::usize;
44

@@ -382,6 +382,7 @@ fn run_plan(
382382
vm_mem_mb: String,
383383
base_log_dir: Utf8PathBuf,
384384
bcvk_has_log_dir: bool,
385+
libvirt_lock: Arc<Mutex<()>>,
385386
) -> RunPlanResult {
386387
let sh = match Shell::new() {
387388
Ok(sh) => sh,
@@ -407,13 +408,24 @@ fn run_plan(
407408

408409
// Launch VM with bcvk
409410
let firmware_args_slice = firmware_args.as_slice();
411+
412+
let guard = match libvirt_lock.lock() {
413+
Ok(g) => g,
414+
Err(e) => {
415+
eprintln!("Mutex lock failed for plan {plan}: {e:#}");
416+
return RunPlanResult::new(plan, false, None, None);
417+
}
418+
};
419+
410420
let launch_result = cmd!(
411421
sh,
412422
"bcvk libvirt run --name {vm_name} --memory {vm_mem_mb} --cpus {vm_cpu} --detach {firmware_args_slice...} {COMMON_INST_ARGS...} {plan_bcvk_opts...} {log_dir_args...} {image}"
413423
)
414424
.run()
415425
.context("Launching VM with bcvk");
416426

427+
drop(guard);
428+
417429
if let Err(e) = launch_result {
418430
eprintln!("Failed to launch VM for plan {}: {:#}", plan, e);
419431
return RunPlanResult::new(plan, false, None, None);
@@ -796,6 +808,8 @@ pub(crate) fn run_tmt(sh: &Shell, args: &RunTmtArgs) -> Result<()> {
796808

797809
let (tx, rx) = mpsc::channel::<RunPlanResult>();
798810

811+
let libvirt_lock = Arc::new(Mutex::new(()));
812+
799813
// Run each plan in its own VM
800814
for plan in plans {
801815
let plan_name = sanitize_plan_name(plan);
@@ -854,6 +868,7 @@ pub(crate) fn run_tmt(sh: &Shell, args: &RunTmtArgs) -> Result<()> {
854868
let vm_mem = vm_mem.to_string();
855869
let vm_cpu = vm_cpu.to_string();
856870
let base_log_dir = base_log_dir.clone();
871+
let libvirt_lock = libvirt_lock.clone();
857872

858873
let tx_clone = tx.clone();
859874
std::thread::spawn(move || {
@@ -871,6 +886,7 @@ pub(crate) fn run_tmt(sh: &Shell, args: &RunTmtArgs) -> Result<()> {
871886
vm_mem,
872887
base_log_dir,
873888
bcvk_has_log_dir,
889+
libvirt_lock,
874890
);
875891

876892
if let Err(e) = tx_clone.send(result) {

0 commit comments

Comments
 (0)