[explorer/machine_type] Simplify Linux + basic OpenBSD support.

By abstracting away vendor-dependent pattern matching for the linux code, we can
re-use that and be reasonably sure about OpenBSD machines being virtualised when
we can identify the undelrying virtualisation technology.
It remains to be solved how to tell if an OpenBSD machine is physical; in that
case previous cdist behaviour ("unknown") remains.

For NetBSD something similar to OpenBSD could be done, with different sysctls:
hw.machine and hw.model wary of adding those without testing though, so for
NetBSD previous cdist behaviour ("unknown") remains.
https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7.i386+NetBSD-9.0
This commit is contained in:
evilham 2020-05-18 16:35:50 +02:00
parent 7ca2bfc14a
commit cf44c4a01b

View file

@ -22,6 +22,18 @@
os=$("$__explorer/os") os=$("$__explorer/os")
vendor_string_to_machine_type() {
for vendor in vmware bochs kvm qemu virtualbox bhyve; do
if echo "${1}" | grep -q -i "${vendor}"; then
if [ "${vendor}" = "bochs" -o "${vendor}" = "qemu" ]; then
vendor="kvm"
fi
echo "virtual_by_${vendor}"
exit
fi
done
}
case "$os" in case "$os" in
"freebsd") "freebsd")
# FreeBSD does not have /proc/cpuinfo even when procfs is used. # FreeBSD does not have /proc/cpuinfo even when procfs is used.
@ -38,6 +50,18 @@ case "$os" in
fi fi
;; ;;
"openbsd")
# OpenBSD can also use the sysctl's: hw.vendor or hw.product.
# Note we can be reasonably sure about a machine being virtualised
# as long as we can identify the virtualisation technology.
# But not so much about it being physical...
# Patches are welcome / reach out if you have better ideas.
for sysctl in hw.vendor hw.product; do
# This exits if we can make a reasonable judgement
vendor_string_to_machine_type "$(sysctl -n "${sysctl}")"
done
;;
*) *)
# Defaulting to linux for compatibility with previous cdist behaviour # Defaulting to linux for compatibility with previous cdist behaviour
@ -54,7 +78,7 @@ fi
if [ -r /proc/cpuinfo ]; then if [ -r /proc/cpuinfo ]; then
# this should only exist on virtual guest machines, # this should only exist on virtual guest machines,
# tested on vmware, xen, kvm # tested on vmware, xen, kvm, bhyve
if grep -q "hypervisor" /proc/cpuinfo; then if grep -q "hypervisor" /proc/cpuinfo; then
# this file is aviable in xen guest systems # this file is aviable in xen guest systems
if [ -r /sys/hypervisor/type ]; then if [ -r /sys/hypervisor/type ]; then
@ -63,32 +87,14 @@ if [ -r /proc/cpuinfo ]; then
exit exit
fi fi
else else
if [ -r /sys/class/dmi/id/product_name ]; then for vendor_file in /sys/class/dmi/id/product_name \
if grep -q -i 'vmware' /sys/class/dmi/id/product_name; then /sys/class/dmi/id/sys_vendor \
echo "virtual_by_vmware" /sys/class/dmi/id/chasis_vendor; do
exit if [ -r ${vendor_file} ]; then
elif grep -q -i 'bochs' /sys/class/dmi/id/product_name; then # This exits if we can make a reasonable judgement
echo "virtual_by_kvm" vendor_string_to_machine_type "$(cat "${vendor_file}")"
exit
elif grep -q -i 'virtualbox' /sys/class/dmi/id/product_name; then
echo "virtual_by_virtualbox"
exit
fi
fi
if [ -r /sys/class/dmi/id/sys_vendor ]; then
if grep -q -i 'qemu' /sys/class/dmi/id/sys_vendor; then
echo "virtual_by_kvm"
exit
fi
fi
if [ -r /sys/class/dmi/id/chassis_vendor ]; then
if grep -q -i 'qemu' /sys/class/dmi/id/chassis_vendor; then
echo "virtual_by_kvm"
exit
fi
fi fi
done
fi fi
echo "virtual_by_unknown" echo "virtual_by_unknown"
exit exit