Show the detected machine type in device inventory
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
//! uninstall commands; registry entries are evidence, not verified MSI state.
|
||||
use anyhow::{Result, ensure};
|
||||
use chrono::Utc;
|
||||
use rmm_domain::inventory_document::DeviceType;
|
||||
use serde_json::{Value, json};
|
||||
use std::{
|
||||
mem::{size_of, zeroed},
|
||||
@@ -64,6 +65,82 @@ fn text(key: HKEY, name: &str) -> Result<Option<String>> {
|
||||
Ok(Some(String::from_utf16(&data[..count - 1])?))
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
fn non_microsoft_hypervisor() -> bool {
|
||||
use std::arch::x86_64::__cpuid;
|
||||
if __cpuid(1).ecx & (1 << 31) == 0 {
|
||||
return false;
|
||||
}
|
||||
let vendor = __cpuid(0x4000_0000);
|
||||
// The Microsoft hypervisor can also be active on physical VBS machines.
|
||||
[vendor.ebx, vendor.ecx, vendor.edx]
|
||||
!= [
|
||||
u32::from_le_bytes(*b"Micr"),
|
||||
u32::from_le_bytes(*b"osof"),
|
||||
u32::from_le_bytes(*b"t Hv"),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "x86_64"))]
|
||||
fn non_microsoft_hypervisor() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn classify_machine(
|
||||
hyper_v_host: bool,
|
||||
guest_hypervisor: bool,
|
||||
manufacturer: Option<&str>,
|
||||
product: Option<&str>,
|
||||
) -> Option<DeviceType> {
|
||||
if hyper_v_host {
|
||||
return Some(DeviceType::Hypervisor);
|
||||
}
|
||||
let manufacturer = manufacturer.unwrap_or_default().trim().to_ascii_lowercase();
|
||||
let product = product.unwrap_or_default().trim().to_ascii_lowercase();
|
||||
let virtual_firmware = [
|
||||
"qemu",
|
||||
"vmware",
|
||||
"virtualbox",
|
||||
"xen",
|
||||
"parallels",
|
||||
"bhyve",
|
||||
"kvm",
|
||||
"virtual machine",
|
||||
"virtual platform",
|
||||
]
|
||||
.iter()
|
||||
.any(|prefix| manufacturer.starts_with(prefix) || product.starts_with(prefix));
|
||||
if guest_hypervisor || virtual_firmware {
|
||||
return Some(DeviceType::Virtual);
|
||||
}
|
||||
(!manufacturer.is_empty() && !product.is_empty()).then_some(DeviceType::Physical)
|
||||
}
|
||||
|
||||
fn machine_type() -> Option<DeviceType> {
|
||||
let hyper_v_host = open(
|
||||
HKEY_LOCAL_MACHINE,
|
||||
r"SYSTEM\CurrentControlSet\Services\vmms",
|
||||
KEY_WOW64_64KEY,
|
||||
)
|
||||
.is_ok();
|
||||
let bios = open(
|
||||
HKEY_LOCAL_MACHINE,
|
||||
r"HARDWARE\DESCRIPTION\System\BIOS",
|
||||
KEY_WOW64_64KEY,
|
||||
)
|
||||
.ok();
|
||||
let read = |name| {
|
||||
bios.as_ref()
|
||||
.and_then(|key| text(key.0, name).ok().flatten())
|
||||
};
|
||||
classify_machine(
|
||||
hyper_v_host,
|
||||
non_microsoft_hypervisor(),
|
||||
read("SystemManufacturer").as_deref(),
|
||||
read("SystemProductName").as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
fn hardware() -> Result<Value> {
|
||||
let mut system: SYSTEM_INFO = unsafe { zeroed() };
|
||||
unsafe {
|
||||
@@ -91,7 +168,7 @@ fn hardware() -> Result<Value> {
|
||||
Ok(
|
||||
json!({"cpu_model":cpu_model, "logical_processors_visible":system.dwNumberOfProcessors,
|
||||
"memory_total_bytes":memory.ullTotalPhys,"memory_available_bytes":memory.ullAvailPhys,
|
||||
"memory_load_percent":memory.dwMemoryLoad}),
|
||||
"memory_load_percent":memory.dwMemoryLoad,"device_type":machine_type()}),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -197,7 +274,7 @@ pub fn observations() -> Vec<Value> {
|
||||
[
|
||||
(
|
||||
"windows.hardware",
|
||||
"windows.win32:GetNativeSystemInfo+GlobalMemoryStatusEx;registry:CentralProcessor",
|
||||
"windows.win32:GetNativeSystemInfo+GlobalMemoryStatusEx;registry:CentralProcessor,BIOS,vmms;cpuid",
|
||||
hardware as Provider,
|
||||
),
|
||||
(
|
||||
@@ -226,3 +303,34 @@ pub fn observations() -> Vec<Value> {
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn machine_type_distinguishes_hyper_v_hosts_from_guests() {
|
||||
let guest = (Some("Microsoft Corporation"), Some("Virtual Machine"));
|
||||
assert_eq!(
|
||||
classify_machine(false, false, guest.0, guest.1),
|
||||
Some(DeviceType::Virtual)
|
||||
);
|
||||
assert_eq!(
|
||||
classify_machine(true, false, guest.0, guest.1),
|
||||
Some(DeviceType::Hypervisor)
|
||||
);
|
||||
assert_eq!(
|
||||
classify_machine(false, false, Some("QEMU"), Some("Standard PC")),
|
||||
Some(DeviceType::Virtual)
|
||||
);
|
||||
assert_eq!(
|
||||
classify_machine(false, true, None, None),
|
||||
Some(DeviceType::Virtual)
|
||||
);
|
||||
assert_eq!(
|
||||
classify_machine(false, false, Some("Dell Inc."), Some("Precision")),
|
||||
Some(DeviceType::Physical)
|
||||
);
|
||||
assert_eq!(classify_machine(false, false, None, None), None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user