Add support for Nutanix Acropolis Hypervisor (AHV) (RHBZ#1756381).
[virt-what.git] / virt-what.in
1 #!/bin/sh -
2 # @configure_input@
3 # Copyright (C) 2008-2019 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 # 'virt-what' tries to detect the type of virtualization being
20 # used (or none at all if we're running on bare-metal).  It prints
21 # out one or more lines each being a 'fact' about the virtualization.
22 #
23 # Please see also the manual page virt-what(1).
24 # This script should be run as root.
25 #
26 # The following resources were useful in writing this script:
27 # . http://dmo.ca/blog/detecting-virtualization-on-linux/
28
29 # Do not allow unset variables, and set defaults.
30 set -u
31 root=''
32 skip_qemu_kvm=false
33 skip_lkvm=false
34
35 VERSION="@VERSION@"
36
37 have_cpuinfo () {
38     test -e "${root}/proc/cpuinfo"
39 }
40
41 use_sysctl() {
42     # Lacking /proc, on some systems sysctl can be used instead.
43     OS=$(uname) || fail "failed to get operating system name"
44
45     [ "$OS" = "OpenBSD" ]
46 }
47
48 fail () {
49     echo "virt-what: $1" >&2
50     exit 1
51 }
52
53 usage () {
54     echo "virt-what [options]"
55     echo "Options:"
56     echo "  --help          Display this help"
57     echo "  --version       Display version and exit"
58     exit 0
59 }
60
61 # Handle the command line arguments, if any.
62 while test $# -gt 0; do
63     case "$1" in
64         --help) usage ;;
65         --test-root=*)
66             # Deliberately undocumented: used for 'make check'.
67             root=$(echo "$1" | sed 's/.*=//')
68             shift 1
69             test -z "$root" && fail "--test-root option requires a value"
70             ;;
71         -v|--version) echo "$VERSION"; exit 0 ;;
72         --) shift; break ;;
73         *) fail "unrecognized option '$1'";;
74     esac
75 done
76 test $# -gt 0 && fail "extra operand '$1'"
77
78 # Add /sbin and /usr/sbin to the path so we can find system
79 # binaries like dmidecode.
80 # Add /usr/libexec to the path so we can find the helper binary.
81 prefix=@prefix@
82 exec_prefix=@exec_prefix@
83 PATH="${root}@libexecdir@:${root}/sbin:${root}/usr/sbin:${PATH}"
84 export PATH
85
86 # Check we're running as root.
87 EFFUID=$(id -u) || fail "failed to get current user id"
88
89 if [ "x$root" = "x" ] && [ "$EFFUID" -ne 0 ]; then
90     fail "this script must be run as root"
91 fi
92
93 # Try to locate the CPU-ID helper program
94 CPUID_HELPER=$(which virt-what-cpuid-helper 2>/dev/null)
95 if [ -z "$CPUID_HELPER" ] ; then
96     fail "virt-what-cpuid-helper program not found in \$PATH"
97 fi
98
99 # Many fullvirt hypervisors give an indication through CPUID.  Use the
100 # helper program to get this information.
101
102 cpuid=$(virt-what-cpuid-helper)
103
104 # Check for various products in the BIOS information.
105 # Note that dmidecode doesn't exist on all architectures.  On the ones
106 # it does not, then this will return an error, which is ignored (error
107 # message redirected into the $dmi variable).
108
109 dmi=$(LANG=C dmidecode 2>&1)
110
111 # Architecture.
112 # Note for the purpose of testing, we only call uname with -m option.
113
114 arch=$(uname -m | sed -e 's/i.86/i386/' | sed -e 's/arm.*/arm/')
115
116 # Check for VMware.
117 # cpuid check added by Chetan Loke.
118
119 if [ "$cpuid" = "VMwareVMware" ]; then
120     echo vmware
121 elif echo "$dmi" | grep -q 'Manufacturer: VMware'; then
122     echo vmware
123 fi
124
125 # Check for Hyper-V.
126 # http://blogs.msdn.com/b/sqlosteam/archive/2010/10/30/is-this-real-the-metaphysics-of-hardware-virtualization.aspx
127 if [ "$cpuid" = "Microsoft Hv" ]; then
128     echo hyperv
129 fi
130
131 # Check for VirtualPC.
132 # The negative check for cpuid is to distinguish this from Hyper-V
133 # which also has the same manufacturer string in the SM-BIOS data.
134 if [ "$cpuid" != "Microsoft Hv" ] &&
135     echo "$dmi" | grep -q 'Manufacturer: Microsoft Corporation'; then
136     echo virtualpc
137 fi
138
139 # Check for VirtualBox.
140 # Added by Laurent Léonard.
141 if echo "$dmi" | grep -q 'Manufacturer: innotek GmbH'; then
142     echo virtualbox
143 fi
144
145 # Check for bhyve.
146 if [ "$cpuid" = "bhyve bhyve " ]; then
147   echo bhyve
148 elif echo "$dmi" | grep -q "Vendor: BHYVE"; then
149   echo bhyve
150 fi
151
152 # Check for OpenVZ / Virtuozzo.
153 # Added by Evgeniy Sokolov.
154 # /proc/vz - always exists if OpenVZ kernel is running (inside and outside
155 # container)
156 # /proc/bc - exists on node, but not inside container.
157
158 if [ -d "${root}/proc/vz" -a ! -d "${root}/proc/bc" ]; then
159     echo openvz
160 fi
161
162 # Check for LXC containers
163 # http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
164 # Added by Marc Fournier
165
166 if [ -e "${root}/proc/1/environ" ] &&
167     cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container='; then
168     echo lxc
169 fi
170
171 # Check for Linux-VServer
172 if test -e "${root}/proc/self/status" \
173    && cat "${root}/proc/self/status" | grep -q "VxID: [0-9]*"; then
174     echo linux_vserver
175     if grep -q "VxID: 0$" "${root}/proc/self/status"; then
176         echo linux_vserver-host
177     else
178         echo linux_vserver-guest
179     fi
180 fi
181
182 # Check for UML.
183 # Added by Laurent Léonard.
184 if have_cpuinfo && grep -q 'UML' "${root}/proc/cpuinfo"; then
185     echo uml
186 fi
187
188 # Check for IBM PowerVM Lx86 Linux/x86 emulator.
189 if have_cpuinfo && grep -q '^vendor_id.*PowerVM Lx86' "${root}/proc/cpuinfo"
190 then
191     echo powervm_lx86
192 fi
193
194 # Check for Hitachi Virtualization Manager (HVM) Virtage logical partitioning.
195 if echo "$dmi" | grep -q 'Manufacturer.*HITACHI' &&
196    echo "$dmi" | grep -q 'Product.* LPAR'; then
197     echo virtage
198 fi
199
200 # Check for IBM SystemZ.
201 if have_cpuinfo && grep -q '^vendor_id.*IBM/S390' "${root}/proc/cpuinfo"; then
202     echo ibm_systemz
203     if [ -f "${root}/proc/sysinfo" ]; then
204         if grep -q 'VM.*Control Program.*KVM/Linux' "${root}/proc/sysinfo"; then
205             echo ibm_systemz-kvm
206         elif grep -q 'VM.*Control Program.*z/VM' "${root}/proc/sysinfo"; then
207             echo ibm_systemz-zvm
208         elif grep -q '^LPAR' "${root}/proc/sysinfo"; then
209             echo ibm_systemz-lpar
210         else
211             # This is unlikely to be correct.
212             echo ibm_systemz-direct
213         fi
214     fi
215 fi
216
217 # Check for Parallels.
218 if echo "$dmi" | grep -q 'Vendor: Parallels'; then
219     echo parallels
220     skip_qemu_kvm=true
221 fi
222
223 # Check for Nutanix AHV.
224 # This is sufficiently different from KVM and has Viridian extensions,
225 # so skip the KVM test.
226 if echo "$dmi" | grep -q 'Manufacturer: Nutanix'; then
227     echo nutanix_ahv
228     skip_qemu_kvm=true
229 fi
230
231 # Check for oVirt/RHEV.
232 if echo "$dmi" | grep -q 'Manufacturer: oVirt'; then
233     echo ovirt
234 fi
235 if echo "$dmi" | grep -q 'Product Name: RHEV Hypervisor'; then
236     echo rhev
237 fi
238
239 # Check for Xen.
240
241 if [ "$cpuid" = "XenVMMXenVMM" ] &&
242     ! echo "$dmi" | grep -q 'No SMBIOS nor DMI entry point found, sorry'; then
243     echo xen; echo xen-hvm
244     skip_qemu_kvm=true
245 elif [ -d "${root}/proc/xen" ]; then
246     echo xen
247     if grep -q "control_d" "${root}/proc/xen/capabilities" 2>/dev/null; then
248         echo xen-dom0
249     else
250         echo xen-domU
251     fi
252     skip_qemu_kvm=true
253     skip_lkvm=true
254 elif [ -f "${root}/sys/hypervisor/type" ] &&
255     grep -q "xen" "${root}/sys/hypervisor/type"; then
256     # Ordinary kernel with pv_ops.  There does not seem to be
257     # enough information at present to tell whether this is dom0
258     # or domU.  XXX
259     echo xen
260 elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
261     if [ -d "${root}/proc/device-tree/hypervisor" ] &&
262         grep -q "xen" "${root}/proc/device-tree/hypervisor/compatible"; then
263         echo xen
264         skip_qemu_kvm=true
265         skip_lkvm=true
266     fi
267 elif [ "$arch" = "ia64" ]; then
268     if [ -d "${root}/sys/bus/xen" -a ! -d "${root}/sys/bus/xen-backend" ]; then
269         # PV-on-HVM drivers installed in a Xen guest.
270         echo xen
271         echo xen-hvm
272     else
273         # There is no virt leaf on IA64 HVM.  This is a last-ditch
274         # attempt to detect something is virtualized by using a
275         # timing attack.
276         virt-what-ia64-xen-rdtsc-test > /dev/null 2>&1
277         case "$?" in
278             0) ;; # not virtual
279             1) # Could be some sort of virt, or could just be a bit slow.
280                 echo virt
281         esac
282     fi
283 fi
284
285 # Check for QEMU/KVM.
286 #
287 # Parallels exports KVMKVMKVM leaf, so skip this test if we've already
288 # seen that it's Parallels.  Xen uses QEMU as the device model, so
289 # skip this test if we know it is Xen.
290
291 if ! "$skip_qemu_kvm"; then
292     if [ "$cpuid" = "KVMKVMKVM" ]; then
293         echo kvm
294     elif [ "$cpuid" = "TCGTCGTCGTCG" ]; then
295         echo qemu
296         skip_lkvm=true
297     elif echo "$dmi" | grep -q 'Product Name: KVM'; then
298         echo kvm
299         skip_lkvm=true
300     elif echo "$dmi" | grep -q 'Manufacturer: QEMU'; then
301         # The test for KVM above failed, so now we know we're
302         # not using KVM acceleration.
303         echo qemu
304         skip_lkvm=true
305     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
306         if [ -d "${root}/proc/device-tree" ] &&
307             ls "${root}/proc/device-tree" | grep -q "fw-cfg"; then
308             # We don't have enough information to determine if we're
309             # using KVM acceleration or not.
310             echo qemu
311             skip_lkvm=true
312         fi
313     elif [ -d ${root}/proc/device-tree/hypervisor ] &&
314          grep -q "linux,kvm" /proc/device-tree/hypervisor/compatible; then
315         # We are running as a spapr KVM guest on ppc64
316         echo kvm
317         skip_lkvm=true
318     elif use_sysctl; then
319         # SmartOS KVM
320         product=$(sysctl -n hw.product)
321         if echo "$product" | grep -q 'SmartDC HVM'; then
322             echo kvm
323         fi
324     else
325         # This is known to fail for qemu with the explicit -cpu
326         # option, since /proc/cpuinfo will not contain the QEMU
327         # string. QEMU 2.10 added a new CPUID leaf, so this
328         # problem only triggered for older QEMU
329         if have_cpuinfo && grep -q 'QEMU' "${root}/proc/cpuinfo"; then
330             echo qemu
331         fi
332     fi
333 fi
334
335 if ! "$skip_lkvm"; then
336     if [ "$cpuid" = "LKVMLKVMLKVM" ]; then
337         echo lkvm
338     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
339         if [ -d "${root}/proc/device-tree" ] &&
340             grep -q "dummy-virt" "${root}/proc/device-tree/compatible"; then
341             echo lkvm
342         fi
343     fi
344 fi
345
346 # Check for Docker.
347 if [ -f "${root}/.dockerinit" ]; then
348     echo docker
349 fi
350
351 # Check ppc64 lpar, kvm or powerkvm
352
353 # example /proc/cpuinfo line indicating 'not baremetal'
354 # platform  : pSeries
355 #
356 # example /proc/ppc64/lparcfg systemtype line
357 # system_type=IBM pSeries (emulated by qemu)
358
359 if [ "$arch" = "ppc64" ] || [ "$arch" = "ppc64le" ] ; then
360     if have_cpuinfo && grep -q 'platform.**pSeries' "${root}/proc/cpuinfo"; then
361         if grep -q 'model.*emulated by qemu' "${root}/proc/cpuinfo"; then
362                 echo ibm_power-kvm
363         else
364             # Assume LPAR, now detect shared or dedicated
365             if grep -q 'shared_processor_mode=1' "${root}/proc/ppc64/lparcfg"; then
366                 echo ibm_power-lpar_shared
367             else
368                 echo ibm_power-lpar_dedicated
369             fi
370         # detect powerkvm?
371         fi
372     fi
373 fi
374
375 # Check for OpenBSD/VMM
376 if [ "$cpuid" = "OpenBSDVMM58" ]; then
377         echo vmm
378 fi
379
380 # Check for LDoms
381 if [ "${arch#sparc}" != "$arch" ] && [ -e "${root}/dev/mdesc" ]; then
382     echo ldoms
383     if [ -d "${root}/sys/class/vlds/ctrl" ] && \
384              [ -d "${root}/sys/class/vlds/sp" ]; then
385         echo ldoms-control
386     else
387         echo ldoms-guest
388     fi
389     MDPROP="${root}/usr/lib/ldoms/mdprop.py"
390     if [ -x "${MDPROP}" ]; then
391         if [ -n "$($MDPROP -v iodevice device-type=pciex)" ]; then
392             echo ldoms-root
393             echo ldoms-io
394         elif [ -n "$($MDPROP -v iov-device vf-id=0)" ]; then
395             echo ldoms-io
396         fi
397     fi
398 fi
399
400 # Check for AWS.
401 # AWS on Xen.
402 if echo "$dmi" | grep -q 'Version: [0-9]\.[0-9]\.amazon'; then
403     echo aws
404 # AWS on baremetal or KVM.
405 elif echo "$dmi" | grep -q 'Vendor: Amazon EC2'; then
406     echo aws
407 fi