use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use super::Provider;
use crate::host::{Devices, HostOs};
pub(crate) const VHOST_USER_DIRS: [&str; 2] = ["/usr/share/qemu/vhost-user", "/etc/qemu/vhost-user"];
pub(crate) const VAGRANT_QEMU_FALLBACKS: [&str; 2] = ["/usr/libexec/virtiofsd", "/usr/lib/qemu/virtiofsd"];
#[must_use]
pub fn needs_virtiofsd(provider: Provider, os: HostOs) -> bool {
super::helpers(provider, os).contains(&super::Helper::Virtiofsd)
}
#[must_use]
pub fn virtiofsd(provider: Provider, devices: &Devices) -> Option<PathBuf> {
match provider {
Provider::Libvirt => described_in(&devices.vhost_user),
Provider::Qemu => devices.virtiofsd.iter().find(|f| runnable(f)).cloned(),
_ => None,
}
}
pub(crate) fn vagrant_qemu_candidates(path: Option<&OsStr>, fallbacks: &[impl AsRef<Path>]) -> Vec<PathBuf> {
let on_path = path
.into_iter()
.flat_map(std::env::split_paths)
.filter(|d| !d.as_os_str().is_empty())
.map(|d| d.join("virtiofsd"));
on_path.chain(fallbacks.iter().map(|f| f.as_ref().to_path_buf())).collect()
}
fn runnable(file: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::metadata(file).is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
}
#[cfg(not(unix))]
{
file.is_file()
}
}
fn described_in(dirs: &[impl AsRef<Path>]) -> Option<PathBuf> {
let mut descriptors = std::collections::BTreeMap::new();
for dir in dirs {
for entry in std::fs::read_dir(dir).into_iter().flatten().flatten() {
let path = entry.path();
if path.extension().is_some_and(|e| e == "json") {
descriptors.insert(entry.file_name(), path);
}
}
}
descriptors.values().find_map(|path| fs_binary(&std::fs::read_to_string(path).ok()?))
}
fn fs_binary(json: &str) -> Option<PathBuf> {
let descriptor: serde_json::Value = serde_json::from_str(json).ok()?;
if descriptor.get("type").and_then(serde_json::Value::as_str) != Some("fs") {
return None;
}
let binary = PathBuf::from(descriptor.get("binary")?.as_str()?);
binary.is_file().then_some(binary)
}
#[cfg(test)]
mod tests {
use super::*;
fn descriptor(kind: &str, binary: &Path) -> String {
serde_json::json!({ "description": "virtiofsd", "type": kind, "binary": binary }).to_string()
}
#[test]
fn libvirt_finds_the_virtiofsd_its_packages_descriptor_names() {
let root = tempfile::tempdir().unwrap();
let (share, etc) = (root.path().join("share"), root.path().join("etc"));
std::fs::create_dir_all(&share).unwrap();
let binary = root.path().join("virtiofsd");
assert_eq!(described_in(&[&share, &etc]), None, "no descriptor: none found");
std::fs::write(share.join("50-virtiofsd.json"), descriptor("fs", &binary)).unwrap();
assert_eq!(described_in(&[&share, &etc]), None, "a descriptor whose program is gone");
std::fs::write(&binary, "").unwrap();
assert_eq!(described_in(&[&share, &etc]), Some(binary.clone()));
std::fs::create_dir_all(&etc).unwrap();
std::fs::write(etc.join("50-virtiofsd.json"), "").unwrap();
assert_eq!(described_in(&[&share, &etc]), None);
}
#[test]
fn libvirt_finds_the_virtiofsd_of_debians_package_beside_qemus_gpu_descriptor() {
let root = tempfile::tempdir().unwrap();
let share = root.path().join("usr/share/qemu/vhost-user");
std::fs::create_dir_all(&share).unwrap();
let (gpu, binary) = (root.path().join("vhost-user-gpu"), root.path().join("virtiofsd"));
std::fs::write(&gpu, "").unwrap();
std::fs::write(&binary, "").unwrap();
let gpu_json = format!("{{\n \"description\": \"QEMU vhost-user-gpu\",\n \"type\": \"gpu\",\n \"binary\": {gpu:?}\n}}\n");
std::fs::write(share.join("50-qemu-gpu.json"), gpu_json).unwrap();
let fs_json = format!(
"{{\n \"description\": \"virtiofsd vhost-user-fs\",\n \"type\": \"fs\",\n \"binary\": {binary:?},\n \"features\": [\n \"migrate-precopy\",\n \"separate-options\"\n ]\n}}\n"
);
std::fs::write(share.join("50-virtiofsd.json"), fs_json).unwrap();
assert_eq!(described_in(&[&share, &root.path().join("etc/qemu/vhost-user")]), Some(binary));
}
#[test]
fn a_descriptor_of_another_vhost_user_device_isnt_a_virtiofsd() {
let root = tempfile::tempdir().unwrap();
let binary = root.path().join("vhost-user-gpu");
std::fs::write(&binary, "").unwrap();
std::fs::write(root.path().join("50-qemu-gpu.json"), descriptor("gpu", &binary)).unwrap();
std::fs::write(root.path().join("README"), descriptor("fs", &binary)).unwrap();
assert_eq!(described_in(&[root.path()]), None);
}
#[cfg(unix)]
fn executable(file: &Path) {
use std::os::unix::fs::PermissionsExt as _;
std::fs::write(file, "#!/bin/sh\n").unwrap();
std::fs::set_permissions(file, std::fs::Permissions::from_mode(0o755)).unwrap();
}
#[cfg(unix)]
#[test]
fn qemu_finds_the_virtiofsd_vagrant_qemu_starts_where_it_looks() {
let root = tempfile::tempdir().unwrap();
let (bin, libexec, qemu) = (root.path().join("bin"), root.path().join("libexec"), root.path().join("qemu"));
for d in [&bin, &libexec, &qemu] {
std::fs::create_dir_all(d).unwrap();
}
let mut devices = Devices::under(root.path());
let fallbacks = [libexec.join("virtiofsd"), qemu.join("virtiofsd")];
let mut looked_at = |path: &OsStr| {
devices.virtiofsd = vagrant_qemu_candidates(Some(path), &fallbacks);
virtiofsd(Provider::Qemu, &devices)
};
let path = std::env::join_paths([&bin]).unwrap();
assert_eq!(looked_at(&path), None, "none anywhere");
std::fs::write(&fallbacks[1], "").unwrap();
assert_eq!(looked_at(&path), None, "not executable");
executable(&fallbacks[1]);
assert_eq!(looked_at(&path), Some(fallbacks[1].clone()), "QEMU's own");
executable(&fallbacks[0]);
assert_eq!(looked_at(&path), Some(fallbacks[0].clone()), "the package's first");
executable(&bin.join("virtiofsd"));
assert_eq!(looked_at(&path), Some(bin.join("virtiofsd")), "the PATH's first");
std::fs::remove_file(bin.join("virtiofsd")).unwrap();
std::fs::create_dir(bin.join("virtiofsd")).unwrap();
let with_empty = std::env::join_paths([Path::new(""), &bin]).unwrap();
assert_eq!(looked_at(&with_empty), Some(fallbacks[0].clone()));
let no_fallbacks: [PathBuf; 0] = [];
assert_eq!(vagrant_qemu_candidates(None, &no_fallbacks), Vec::<PathBuf>::new());
}
#[test]
fn each_provider_looks_where_it_starts_its_virtiofsd_from() {
let root = tempfile::tempdir().unwrap();
let devices = Devices::under(root.path());
let binary = root.path().join("usr/libexec/virtiofsd");
std::fs::create_dir_all(binary.parent().unwrap()).unwrap();
std::fs::write(&binary, "").unwrap();
let share = &devices.vhost_user[0];
std::fs::create_dir_all(share).unwrap();
std::fs::write(share.join("50-virtiofsd.json"), descriptor("fs", &binary)).unwrap();
assert_eq!(virtiofsd(Provider::Libvirt, &devices), Some(binary.clone()));
let only_described = Devices {
virtiofsd: Vec::new(),
..devices.clone()
};
assert_eq!(virtiofsd(Provider::Qemu, &only_described), None);
let only_on_disk = Devices {
vhost_user: Vec::new(),
..devices
};
assert_eq!(virtiofsd(Provider::Libvirt, &only_on_disk), None);
assert_eq!(virtiofsd(Provider::Virtualbox, &only_on_disk), None);
}
#[test]
fn libvirt_and_qemu_on_linux_are_held_to_a_virtiofsd() {
assert!(needs_virtiofsd(Provider::Libvirt, HostOs::Linux) && needs_virtiofsd(Provider::Qemu, HostOs::Linux));
for (p, os) in [
(Provider::Qemu, HostOs::Macos),
(Provider::Virtualbox, HostOs::Linux),
(Provider::Libvirt, HostOs::Macos),
] {
assert!(!needs_virtiofsd(p, os), "{p:?} on {os:?}");
}
}
}