Skip to content

Commit

Permalink
xtask: attempt to open /dev/kvm
Browse files Browse the repository at this point in the history
We're seeing test failures where KVM is present but we aren't able to use it.

See actions/runner-images#7670 (comment).
  • Loading branch information
tamird authored Nov 9, 2023
1 parent e68fa14 commit ec26bf5
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions xtask/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
env::consts::{ARCH, OS},
ffi::OsString,
fmt::Write as _,
fs::{copy, create_dir_all, metadata, File},
fs::{copy, create_dir_all, OpenOptions},
io::{BufRead as _, BufReader, ErrorKind, Write as _},
path::{Path, PathBuf},
process::{Child, ChildStdin, Command, Output, Stdio},
Expand Down Expand Up @@ -284,9 +284,12 @@ pub fn run(opts: Options) -> Result<()> {
let tmp_dir = tempfile::tempdir().context("tempdir failed")?;

let initrd_image = tmp_dir.path().join("qemu-initramfs.img");
let initrd_image_file = File::create(&initrd_image).with_context(|| {
format!("failed to create {} for writing", initrd_image.display())
})?;
let initrd_image_file = OpenOptions::new()
.create(true)
.open(&initrd_image)
.with_context(|| {
format!("failed to create {} for writing", initrd_image.display())
})?;

let mut gen_init_cpio = Command::new(&gen_init_cpio);
let mut gen_init_cpio_child = gen_init_cpio
Expand Down Expand Up @@ -351,19 +354,21 @@ pub fn run(opts: Options) -> Result<()> {
}
if guest_arch == ARCH {
match OS {
"linux" => match metadata("/dev/kvm") {
Ok(metadata) => {
use std::os::unix::fs::FileTypeExt as _;
if metadata.file_type().is_char_device() {
"linux" => {
const KVM: &str = "/dev/kvm";
match OpenOptions::new().read(true).write(true).open(KVM) {
Ok(_file) => {
qemu.args(["-accel", "kvm"]);
}
Err(error) => match error.kind() {
ErrorKind::NotFound | ErrorKind::PermissionDenied => {}
_kind => {
return Err(error)
.with_context(|| format!("failed to open {KVM}"));
}
},
}
Err(error) => {
if error.kind() != ErrorKind::NotFound {
Err(error).context("failed to check existence of /dev/kvm")?;
}
}
},
}
"macos" => {
qemu.args(["-accel", "hvf"]);
}
Expand Down

0 comments on commit ec26bf5

Please sign in to comment.