use super::{Provider, Unavailable};
use crate::context::Context;
use crate::host::{Host, HostOs};
pub(super) fn kvm_problem(host: &Host) -> Option<Unavailable> {
let device = &host.devices.kvm;
if !device.exists() {
return Some(Unavailable::NoKvm);
}
match std::fs::OpenOptions::new().read(true).write(true).open(device) {
Ok(_) => None,
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => Some(Unavailable::KvmDenied { group: kvm_group(host) }),
Err(e) => Some(Unavailable::KvmUnusable { error: e.to_string() }),
}
}
pub(crate) fn holds_virtualization(host: &Host) -> bool {
if host.os != HostOs::Linux {
return false;
}
let Ok(modules) = std::fs::read_to_string(&host.devices.proc_modules) else {
return false;
};
let at_load = std::fs::read_to_string(&host.devices.kvm_virt_at_load).ok();
holds(&modules, at_load.as_deref())
}
fn holds(modules: &str, enable_virt_at_load: Option<&str>) -> bool {
let loaded = modules.lines().any(|l| matches!(l.split_whitespace().next(), Some("kvm_intel" | "kvm_amd")));
loaded && enable_virt_at_load.is_some_and(|v| matches!(v.trim(), "Y" | "y" | "1"))
}
#[must_use]
pub(crate) fn cpu_module(host: &Host) -> Option<&'static str> {
let modules = std::fs::read_to_string(&host.devices.proc_modules).ok()?;
modules.lines().find_map(|l| match l.split_whitespace().next() {
Some("kvm_intel") => Some("kvm_intel"),
Some("kvm_amd") => Some("kvm_amd"),
_ => None,
})
}
#[must_use]
pub(crate) fn loadable(host: &Host) -> bool {
host.devices.sys_module.join("kvm/refcnt").is_file()
}
#[must_use]
pub(crate) fn in_use(host: &Host) -> bool {
let module = |name: &str| host.devices.sys_module.join(name);
std::iter::once("kvm").chain(cpu_module(host)).any(|name| {
let held = std::fs::read_to_string(module(name).join("refcnt"))
.ok()
.and_then(|r| r.trim().parse::<usize>().ok());
let by_modules = std::fs::read_dir(module(name).join("holders")).map_or(0, Iterator::count);
held.is_none_or(|held| held > by_modules)
})
}
#[must_use]
pub(crate) fn shares_from_next_load(host: &Host) -> bool {
let Ok(entries) = std::fs::read_dir(&host.devices.modprobe_d) else {
return false;
};
entries
.filter_map(Result::ok)
.filter(|e| e.path().extension().is_some_and(|x| x == "conf"))
.filter_map(|e| std::fs::read_to_string(e.path()).ok())
.any(|text| text.lines().any(shares_on_load))
}
fn shares_on_load(line: &str) -> bool {
let mut words = line.split_whitespace();
words.next() == Some("options") && words.next() == Some("kvm") && words.any(|w| matches!(w.split_once('='), Some(("enable_virt_at_load", "0" | "N" | "n"))))
}
#[must_use]
pub fn kvm_group(host: &Host) -> String {
let groups = std::fs::read_to_string(&host.devices.etc_group).unwrap_or_default();
group_of(device_gid(&host.devices.kvm), &groups)
}
#[cfg(unix)]
fn device_gid(path: &std::path::Path) -> Option<u32> {
use std::os::unix::fs::MetadataExt as _;
std::fs::metadata(path).ok().map(|m| m.gid())
}
#[cfg(not(unix))]
fn device_gid(_path: &std::path::Path) -> Option<u32> {
None
}
fn group_of(gid: Option<u32>, etc_group: &str) -> String {
gid.filter(|g| *g != 0)
.and_then(|gid| {
etc_group.lines().find_map(|l| {
let mut fields = l.split(':');
let name = fields.next()?;
(fields.nth(1)?.parse::<u32>().ok()? == gid).then(|| name.to_owned())
})
})
.unwrap_or_else(|| "kvm".to_owned())
}
const LIBVIRT_GROUPS: [&str; 2] = ["libvirt", "kvm"];
#[must_use]
pub fn uses_kvm(provider: Provider, os: HostOs) -> bool {
provider == Provider::Libvirt || (provider == Provider::Qemu && os == HostOs::Linux)
}
#[must_use]
pub fn login_groups(provider: Provider, host: &Host) -> Vec<String> {
match provider {
Provider::Libvirt => {
let mut groups: Vec<String> = LIBVIRT_GROUPS.map(str::to_owned).to_vec();
let device = kvm_group(host);
if !groups.contains(&device) {
groups.push(device);
}
groups
}
Provider::Qemu if uses_kvm(provider, host.os) => vec![kvm_group(host)],
_ => Vec::new(),
}
}
pub(super) fn new_login_group(provider: Provider, host: &Host) -> String {
if provider == Provider::Libvirt {
"libvirt".to_owned()
} else {
kvm_group(host)
}
}
pub async fn groups_pending(cx: &Context) -> Vec<String> {
if cx.host.os != HostOs::Linux {
return Vec::new();
}
let Ok(user) = cx.read_ok("id", &["-un"]).await else {
return Vec::new();
};
let user = user.trim();
match (cx.read_ok("id", &["-nG", user]).await, cx.read_ok("id", &["-nG"]).await) {
(Ok(account), Ok(session)) => pending_groups(&account, &session),
_ => Vec::new(),
}
}
fn pending_groups(account: &str, session: &str) -> Vec<String> {
let session: Vec<&str> = session.split_whitespace().collect();
account.split_whitespace().filter(|g| !session.contains(g)).map(str::to_owned).collect()
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use isothrum_runner::fake::{FakeProcesses, Reply};
use super::*;
fn linux(root: &std::path::Path) -> Host {
crate::testing::linux_host(root)
}
#[test]
fn kvm_holds_the_cpus_virtualization_once_its_module_turns_it_on_as_it_loads() {
let modules =
"kvm_intel 413696 0 - Live 0x0000000000000000\nkvm 1404928 1 kvm_intel, Live 0x0000000000000000\nirqbypass 12288 1 kvm, Live 0x0000000000000000\n";
assert!(holds(modules, Some("Y\n")));
assert!(holds(&modules.replace("kvm_intel", "kvm_amd"), Some("Y\n")));
assert!(!holds(modules, Some("N\n")), "kvm.enable_virt_at_load=0: only while a KVM guest runs");
assert!(!holds(modules, None), "before Linux 6.12: only while a KVM guest runs");
assert!(
!holds(
"vboxdrv 696320 2 vboxnetadp, Live 0x0000000000000000\nkvm 1404928 0 - Live 0x0000000000000000\n",
Some("Y\n")
),
"no CPU module"
);
}
#[test]
fn kvm_holding_virtualization_is_read_from_the_hosts_files_on_linux_only() {
let dir = tempfile::tempdir().unwrap();
let mut host = linux(dir.path());
assert!(!holds_virtualization(&host), "no /proc/modules: can't be told");
crate::testing::kvm_holding(&host, "kvm_amd 1 0 - Live 0x0\n");
assert!(holds_virtualization(&host));
host.os = HostOs::Macos;
assert!(!holds_virtualization(&host));
}
#[test]
fn the_kvm_group_is_the_one_dev_kvm_belongs_to() {
let etc_group = "root:x:0:\nkvm:x:993:\nlibvirt:x:994:ada\nhypervisors:x:1200:\n";
assert_eq!(group_of(Some(993), etc_group), "kvm");
assert_eq!(group_of(Some(1200), etc_group), "hypervisors", "a distribution's own name for it");
assert_eq!(group_of(Some(0), etc_group), "kvm", "root's: a user doesn't join it");
assert_eq!(group_of(Some(4242), etc_group), "kvm", "a group /etc/group doesn't name");
assert_eq!(group_of(None, etc_group), "kvm", "no /dev/kvm");
}
#[test]
fn a_kvm_device_missing_or_not_the_users_or_unusable_says_which() {
let dir = tempfile::tempdir().unwrap();
let host = linux(dir.path());
assert_eq!(kvm_problem(&host), Some(Unavailable::NoKvm));
std::fs::create_dir_all(host.devices.kvm.parent().unwrap()).unwrap();
std::fs::write(&host.devices.kvm, "").unwrap();
assert_eq!(kvm_problem(&host), None);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
let mut host = host;
host.devices.kvm = dir.path().to_path_buf();
assert!(matches!(kvm_problem(&host), Some(Unavailable::KvmUnusable { .. })), "{:?}", kvm_problem(&host));
let denied = dir.path().join("denied");
std::fs::write(&denied, "").unwrap();
std::fs::set_permissions(&denied, std::fs::Permissions::from_mode(0o400)).unwrap();
host.devices.kvm = denied;
if let Some(problem) = kvm_problem(&host) {
assert!(matches!(problem, Unavailable::KvmDenied { .. }), "{problem:?}");
}
}
}
#[test]
fn libvirt_and_qemu_on_linux_boot_through_kvm_and_join_its_groups() {
let dir = tempfile::tempdir().unwrap();
let host = linux(dir.path());
assert!(uses_kvm(Provider::Libvirt, HostOs::Linux) && uses_kvm(Provider::Qemu, HostOs::Linux));
assert!(!uses_kvm(Provider::Qemu, HostOs::Macos) && !uses_kvm(Provider::Qemu, HostOs::Windows) && !uses_kvm(Provider::Virtualbox, HostOs::Linux));
assert_eq!(login_groups(Provider::Libvirt, &host), ["libvirt", "kvm"]);
assert_eq!(login_groups(Provider::Qemu, &host), ["kvm"]);
assert_eq!(login_groups(Provider::Virtualbox, &host), Vec::<String>::new());
let mac = Host {
os: HostOs::Macos,
..host.clone()
};
assert_eq!(login_groups(Provider::Qemu, &mac), Vec::<String>::new());
assert_eq!(new_login_group(Provider::Libvirt, &host), "libvirt");
assert_eq!(new_login_group(Provider::Qemu, &host), "kvm");
}
#[test]
fn the_groups_pending_are_the_accounts_the_session_lacks() {
assert_eq!(
pending_groups("vagrant sudo docker libvirt kvm\n", "vagrant sudo\n"),
["docker", "libvirt", "kvm"]
);
assert_eq!(pending_groups("vagrant sudo libvirt\n", "vagrant libvirt sudo\n"), Vec::<String>::new());
assert_eq!(pending_groups("", "vagrant"), Vec::<String>::new());
}
#[tokio::test(start_paused = true)]
async fn the_groups_pending_are_asked_of_id_on_linux_only() {
let dir = tempfile::tempdir().unwrap();
let tools = Arc::new(
FakeProcesses::script()
.expect("id -un", Reply::ok("ada"))
.expect("id -nG ada", Reply::ok("ada sudo libvirt kvm"))
.expect("id -nG", Reply::ok("ada sudo"))
.build(),
);
let cx = Context::new(tools, linux(dir.path()));
assert_eq!(groups_pending(&cx).await, ["libvirt", "kvm"]);
let unknown = Context::new(Arc::new(FakeProcesses::script().expect("id -un", Reply::missing()).build()), linux(dir.path()));
assert_eq!(groups_pending(&unknown).await, Vec::<String>::new());
let nothing = Arc::new(FakeProcesses::script().build());
let mac = Context::new(
nothing,
Host {
os: HostOs::Macos,
..linux(dir.path())
},
);
assert_eq!(groups_pending(&mac).await, Vec::<String>::new());
}
}