-
Notifications
You must be signed in to change notification settings - Fork 10
/
sys-is-vm
executable file
·29 lines (24 loc) · 1.11 KB
/
sys-is-vm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/sh -u
# sys-is-vm (part of ossobv/vcutil) // wdoekes/2021 // Public Domain
#
# Quick and dirty script that guesses whether we're a virtual machine.
#
# Return value 0 (success) means VM, return value 1 (failure) means hardware.
#
# Note that it only detects Xen/QEMU/KVM based VMs. For LXC containers it
# will guess that you're on (the host) hardware.
#
test -t 2; QUIET=$? # silent if stderr is not a tty
chassis=$(cat /sys/class/dmi/id/chassis_vendor)
test "$chassis" = "QEMU" -o "$chassis" = "Xen"; chassis_is_not_vm=$?
test $QUIET = 1 && exit $chassis_is_not_vm # 0=is_vm, 1=is_not_vm
cpu_model=$(sed -ne '/^model name/{s/^[^:]*:[[:blank:]]*//p;q}' /proc/cpuinfo)
test "$cpu_model" = "Common KVM processor"; cpu_is_not_vm=$?
if test $cpu_is_not_vm = 0 -a $chassis_is_not_vm = 1; then
echo "WARNING: CPU looks like VM, but chassis does not" >&2
fi
is_vm_str=$(test $chassis_is_not_vm = 0 && echo true || echo false)
echo "chassis: $chassis [/sys/class/dmi/id/chassis_vendor]" >&2
echo "cpu: $cpu_model [/proc/cpuinfo]" >&2
echo "sys-is-vm: $is_vm_str" >&2
exit $chassis_is_not_vm # 0=is_vm, 1=is_not_vm