[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
1 changed files with 61 additions and 55 deletions

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,65 +50,59 @@ 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
if [ -d "/proc/vz" ] && [ ! -d "/proc/bc" ]; then if [ -d "/proc/vz" ] && [ ! -d "/proc/bc" ]; then
echo openvz echo openvz
exit exit
fi fi
if [ -e "/proc/1/environ" ] && if [ -e "/proc/1/environ" ] &&
tr '\000' '\n' < "/proc/1/environ" | grep -Eiq '^container='; then tr '\000' '\n' < "/proc/1/environ" | grep -Eiq '^container='; then
echo lxc echo lxc
exit exit
fi 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
if grep -q -i "xen" /sys/hypervisor/type; then if grep -q -i "xen" /sys/hypervisor/type; then
echo virtual_by_xen echo virtual_by_xen
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 fi
elif grep -q -i 'virtualbox' /sys/class/dmi/id/product_name; then done
echo "virtual_by_virtualbox" fi
exit echo "virtual_by_unknown"
fi exit
fi else
echo "physical"
if [ -r /sys/class/dmi/id/sys_vendor ]; then exit
if grep -q -i 'qemu' /sys/class/dmi/id/sys_vendor; then fi
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 fi
echo "virtual_by_unknown"
exit
else
echo "physical"
exit
fi
fi
;; ;;
esac esac