Created
May 15, 2017 11:08
-
-
Save jn0/64df59d88f109219fa712c35ab91f384 to your computer and use it in GitHub Desktop.
yet another tool to detect hypervisor from guest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Which Hypervisor | |
verbose=no | |
exe="$0" | |
say() { echo "$@" >&2; } | |
verb_say() { [ "$verbose" = 'yes' ] && say "$@"; } | |
error() { | |
local -i rc=$1 | |
shift | |
say "$@" | |
exit $rc | |
} | |
show_help() { | |
say "$exe [-v|--verbose] [-h|--help]" | |
exit 0 | |
} | |
while [ -n "$1" ]; do | |
case "$1" in | |
-h|--help) show_help;; | |
-v|--verbose) verbose=yes; shift;; | |
*) error 1 "What is '$1'?";; | |
esac | |
done | |
usable() { | |
local cmd=$1 | |
local bin=$(type -p "$cmd") | |
if [ -e "$bin" -a -x "$bin" ]; then | |
verb_say "Can use '$cmd' as '$bin'." | |
echo "$bin" | |
else | |
verb_say "No usable '$cmd'." | |
return 1 | |
fi | |
} | |
grep=$(usable grep) || error 1 "Cannot grep here." | |
cat=$(usable cat) || error 1 "Cannot cat here." | |
dmidecode=$(usable dmidecode) | |
dmesg=$(usable dmesg) | |
lspci=$(usable lspci) | |
lshw=$(usable lshw) | |
sysdv=$(usable systemd-detect-virt) | |
facter=$(usable facter) | |
hostnamectl=$(usable hostnamectl) | |
dmesg_has() { | |
[ -n "$dmesg" ] && { "$dmesg" 2>/dev/null | grep -q "$@"; return $?; } | |
grep -q "$@" '/var/log/dmesg' 2>/dev/null | |
} | |
test_if_cmd_has() { | |
local cmd="$1" ; shift | |
[ -n "$cmd" ] && { "$cmd" 2>/dev/null | grep "$@" && return; } | |
return 1 | |
} | |
test_if_cmd_arg_has() { | |
local cmd="$1" arg="$2" ; shift 2 | |
[ -n "$cmd" ] && { "$cmd" "$arg" 2>/dev/null | grep "$@" && return; } | |
return 1 | |
} | |
test_if_cmd_arg2_has() { | |
local cmd="$1" arg1="$2" arg2="$3" ; shift 3 | |
[ -n "$cmd" ] && { "$cmd" "$arg1" "$arg2" 2>/dev/null | grep "$@" && return; } | |
return 1 | |
} | |
hostnamectl_has() { test_if_cmd_arg_has "$hostnamectl" status -q "$@"; } | |
lspci_has() { test_if_cmd_has "$lspci" -q "$@"; } | |
sysdv_has() { test_if_cmd_has "$sysdv" -q "$@"; } | |
facter_has() { test_if_cmd_arg_has "$facter" virtual -q "$@"; } | |
sys_prod_name() { test_if_cmd_arg2_has "$dmidecode" -s system-product-name -q "$@"; } | |
sys_mfgr_name() { test_if_cmd_arg2_has "$dmidecode" -s system-manufacturer -q "$@"; } | |
hw_class_system() { test_if_cmd_arg2_has "$lshw" -class system -q "$@"; } | |
hw_class_system_product() { test_if_cmd_arg2_has "$lshw" -class system 'product:'| cut -d: -f2- | grep -q "$@"; } | |
hw_class_system_vendor() { test_if_cmd_arg2_has "$lshw" -class system 'vendor:'| cut -d: -f2- | grep -q "$@"; } | |
product_is() { sys_prod_name "$@" || hw_class_system_product "$@"; } | |
vendor_is() { sys_mfgr_name "$@" || hw_class_system_vendor "$@"; } | |
is_ms_vm() { | |
{ product_is 'Virtual Machine' && vendor_is 'Microsoft Corporation'; } \ | |
|| hostnamectl_has 'Virtualization: microsoft' \ | |
|| sysdv_has microsoft | |
} | |
check_vmware() { product_is 'VMware Virtual Platform'; } | |
check_virtualbox() { product_is 'VirtualBox'; } | |
check_qemu_kvm() { product_is 'KVM' || hostnamectl_has 'Virtualization: kvm'; } | |
check_qemu() { vendor_is QEMU || dmesg_has QEMU; } | |
check_ms_hyperv() { | |
facter_has hyperv \ | |
|| lspci_has 'Microsoft Corporation Hyper-V' \ | |
|| { is_ms_vm && dmesg_has -w 'Hyper-V'; } | |
} | |
check_ms_virtualpc() { is_ms_vm && ! dmesg_has -w 'Hyper-V'; } | |
check_xen() { sys_prod_name 'HVM domU' || dmesg_has Xen || hostnamectl_has 'Virtualization: xen'; } | |
check_openvz() { hostnamectl_has 'Virtualization: openvz' || [ -e /proc/user_beancounters ]; } | |
check_virtuozzo() { [ -e /dev/mem ] && [ ! -r /dev/mem ] && [ -d /proc/vz ]; } | |
check_lxc() { sysdv_has lxc; } | |
check_docker() { [ -e /proc/1/cgroup ] && grep -q docker /proc/1/cgroup; } | |
other_vm() { dmesg_has "Hypervisor detected"; } | |
declare -a hyptype=() | |
declare -i n=0 | |
for func in $(declare -F | cut -d' ' -f3- | grep '^check_'); do | |
vmtype=${func:6} | |
verb_say -n "$vmtype: " | |
"$func" && { verb_say yes; hyptype[${#hyptype[*]}]=$vmtype; let n+=1; } || verb_say no | |
done | |
[ "$verbose" = 'yes' ] && echo -n "hypervisor: " | |
(( n == 0 )) && { other_vm && echo other || echo no; } || echo "${hyptype[*]}" | |
# EOF # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment