nutanix: Don't match Nutanix-based baremetal systems
[virt-what.git] / virt-what.in
1 #!/bin/sh -
2 # @configure_input@
3 # Copyright (C) 2008-2022 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 Alibaba Cloud
117 if echo "$dmi" | grep -q 'Manufacturer: Alibaba'; then
118     # Check for Alibaba Cloud ECS Bare Metal (EBM) Instance
119     if [ "x$root" = "x" ] && ( { echo -e "GET /latest/meta-datainstance/instance-type HTTP/1.0\r\nHost: 100.100.100.200\r\n\r" >&3; grep -sq 'ebm' <&3 ; } 3<> /dev/tcp/100.100.100.200/80 ) 2>/dev/null ; then
120         echo "alibaba_cloud-ebm"
121     else
122         echo "alibaba_cloud"
123     fi
124 fi
125
126 # Check for VMware.
127 # cpuid check added by Chetan Loke.
128
129 if [ "$cpuid" = "VMwareVMware" ]; then
130     echo vmware
131 elif echo "$dmi" | grep -q 'Manufacturer: VMware'; then
132     echo vmware
133 fi
134
135 # Check for Hyper-V.
136 # http://blogs.msdn.com/b/sqlosteam/archive/2010/10/30/is-this-real-the-metaphysics-of-hardware-virtualization.aspx
137 if [ "$cpuid" = "Microsoft Hv" ]; then
138     echo hyperv
139 fi
140
141 # Check for VirtualPC.
142 # The negative check for cpuid is to distinguish this from Hyper-V
143 # which also has the same manufacturer string in the SM-BIOS data.
144 if [ "$cpuid" != "Microsoft Hv" ] &&
145     echo "$dmi" | grep -q 'Manufacturer: Microsoft Corporation' &&
146     echo "$dmi" | grep -q 'Product Name: Virtual Machine'; then
147     echo virtualpc
148 fi
149
150 # Check for VirtualBox.
151 # Added by Laurent Léonard.
152 if echo "$dmi" | grep -q 'Manufacturer: innotek GmbH'; then
153     echo virtualbox
154 fi
155
156 # Check for bhyve.
157 if [ "$cpuid" = "bhyve bhyve " ]; then
158   echo bhyve
159 elif echo "$dmi" | grep -q "Vendor: BHYVE"; then
160   echo bhyve
161 fi
162
163 # Check for OpenVZ / Virtuozzo.
164 # Added by Evgeniy Sokolov.
165 # /proc/vz - always exists if OpenVZ kernel is running (inside and outside
166 # container)
167 # /proc/bc - exists on node, but not inside container.
168 if [ -d "${root}/proc/vz" -a ! -d "${root}/proc/bc" ]; then
169     echo openvz
170 fi
171
172 # Check for LXC containers
173 # http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
174 # Added by Marc Fournier
175 if [ -e "${root}/proc/1/environ" ] &&
176     tr '\000' '\n' < "${root}/proc/1/environ" |
177         grep -Eiq '^container=lxc'; then
178     echo lxc
179 fi
180
181 # Check for Illumos LX
182 if [ -e "${root}/proc/1/environ" ] &&
183     tr '\0' '\n' < "${root}/proc/1/environ" | grep -q '^container=zone$' &&
184     [ -e "${root}/proc/version" ] &&
185     grep -q 'BrandZ virtual linux' < "${root}/proc/version"; then
186     echo illumos-lx
187 fi
188
189 # Check for Docker.
190 if [ -f "${root}/.dockerenv" ] || [ -f "${root}/.dockerinit" ] || \
191    grep -qF /docker/ "${root}/proc/self/cgroup" 2>/dev/null; then
192     echo docker
193 fi
194
195 # Check for OCI.
196 if [ -e "${root}/proc/1/environ" ] &&
197     cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container=oci'; then
198     echo oci
199 fi
200
201 # Check for Podman.
202 if [ -e "${root}/proc/1/environ" ] &&
203     cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container=podman'; then
204     echo podman
205 elif grep -qF /libpod- "${root}/proc/self/cgroup" 2>/dev/null; then
206     echo podman
207 fi
208
209 # Check for Linux-VServer
210 if test -e "${root}/proc/self/status" \
211    && cat "${root}/proc/self/status" | grep -q "VxID: [0-9]*"; then
212     echo linux_vserver
213     if grep -q "VxID: 0$" "${root}/proc/self/status"; then
214         echo linux_vserver-host
215     else
216         echo linux_vserver-guest
217     fi
218 fi
219
220 # Check for UML.
221 # Added by Laurent Léonard.
222 if have_cpuinfo && grep -q 'UML' "${root}/proc/cpuinfo"; then
223     echo uml
224 fi
225
226 # Check for IBM PowerVM Lx86 Linux/x86 emulator.
227 if have_cpuinfo && grep -q '^vendor_id.*PowerVM Lx86' "${root}/proc/cpuinfo"
228 then
229     echo powervm_lx86
230 fi
231
232 # Check for Hitachi Virtualization Manager (HVM) Virtage logical partitioning.
233 if echo "$dmi" | grep -q 'Manufacturer.*HITACHI' &&
234    echo "$dmi" | grep -q 'Product.* LPAR'; then
235     echo virtage
236 fi
237
238 # Check for IBM SystemZ.
239 if have_cpuinfo && grep -q '^vendor_id.*IBM/S390' "${root}/proc/cpuinfo"; then
240     echo ibm_systemz
241     if [ -f "${root}/proc/sysinfo" ]; then
242         if grep -q 'VM.*Control Program.*KVM/Linux' "${root}/proc/sysinfo"; then
243             echo ibm_systemz-kvm
244         elif grep -q 'VM.*Control Program.*z/VM' "${root}/proc/sysinfo"; then
245             echo ibm_systemz-zvm
246         elif grep -q '^LPAR' "${root}/proc/sysinfo"; then
247             echo ibm_systemz-lpar
248         else
249             # This is unlikely to be correct.
250             echo ibm_systemz-direct
251         fi
252     fi
253 fi
254
255 # Check for Parallels.
256 if echo "$dmi" | grep -q 'Vendor: Parallels'; then
257     echo parallels
258     skip_qemu_kvm=true
259 fi
260
261 # Check for Nutanix AHV.
262 if echo "$dmi" | grep -q 'Manufacturer: Nutanix' &&
263    echo "$dmi" | grep -q 'Product Name: AHV'; then
264     echo nutanix_ahv
265 fi
266
267 # Check for oVirt/RHEV.
268 if echo "$dmi" | grep -q 'Manufacturer: oVirt'; then
269     echo ovirt
270 fi
271 if echo "$dmi" | grep -q 'Product Name: RHEV Hypervisor'; then
272     echo rhev
273 fi
274
275 # Google Cloud
276 if echo "$dmi" | grep -q 'Product Name: Google Compute Engine'; then
277     echo google_cloud
278 fi
279
280 # Red Hat's hypervisor.
281 if echo "$dmi" | grep -q 'Manufacturer: Red Hat'; then
282     echo redhat
283 fi
284
285 # Check for Xen.
286
287 if [ "$cpuid" = "XenVMMXenVMM" ] &&
288     ! echo "$dmi" | grep -q 'No SMBIOS nor DMI entry point found, sorry'; then
289     echo xen; echo xen-hvm
290     skip_qemu_kvm=true
291 elif [ -d "${root}/proc/xen" ]; then
292     echo xen
293     if grep -q "control_d" "${root}/proc/xen/capabilities" 2>/dev/null; then
294         echo xen-dom0
295     else
296         echo xen-domU
297     fi
298     skip_qemu_kvm=true
299     skip_lkvm=true
300 elif [ -f "${root}/sys/hypervisor/type" ] &&
301     grep -q "xen" "${root}/sys/hypervisor/type"; then
302     # Ordinary kernel with pv_ops.  There does not seem to be
303     # enough information at present to tell whether this is dom0
304     # or domU.  XXX
305     echo xen
306 elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
307     if [ -d "${root}/proc/device-tree/hypervisor" ] &&
308         grep -q "xen" "${root}/proc/device-tree/hypervisor/compatible"; then
309         echo xen
310         skip_qemu_kvm=true
311         skip_lkvm=true
312     elif [ -d "${root}/proc/device-tree/hypervisor" ] &&
313         grep -q "vmware" "${root}/proc/device-tree/hypervisor/compatible"; then
314         echo vmware
315         skip_lkvm=true
316     fi
317 elif [ "$arch" = "ia64" ]; then
318     if [ -d "${root}/sys/bus/xen" -a ! -d "${root}/sys/bus/xen-backend" ]; then
319         # PV-on-HVM drivers installed in a Xen guest.
320         echo xen
321         echo xen-hvm
322     else
323         # There is no virt leaf on IA64 HVM.  This is a last-ditch
324         # attempt to detect something is virtualized by using a
325         # timing attack.
326         virt-what-ia64-xen-rdtsc-test > /dev/null 2>&1
327         case "$?" in
328             0) ;; # not virtual
329             1) # Could be some sort of virt, or could just be a bit slow.
330                 echo virt
331         esac
332     fi
333 fi
334
335 # Check for QEMU/KVM.
336 #
337 # Parallels exports KVMKVMKVM leaf, so skip this test if we've already
338 # seen that it's Parallels.  Xen uses QEMU as the device model, so
339 # skip this test if we know it is Xen.
340
341 if ! "$skip_qemu_kvm"; then
342     if [ "$cpuid" = "KVMKVMKVM" ]; then
343         echo kvm
344     elif [ "$cpuid" = "TCGTCGTCGTCG" ]; then
345         echo qemu
346         skip_lkvm=true
347     elif echo "$dmi" | grep -q 'Product Name: KVM'; then
348         echo kvm
349         skip_lkvm=true
350     elif echo "$dmi" | grep -q 'Manufacturer: KVM'; then
351         echo kvm
352         skip_lkvm=true
353     elif echo "$dmi" | grep -q 'Manufacturer: Amazon EC2' &&
354         echo "$dmi" | grep -q 'System is a virtual machine'; then
355         # This is for AWS Graviton (Arm) systems which don't have CPUID.
356         echo kvm
357         skip_lkvm=true
358     elif echo "$dmi" | grep -q 'Manufacturer: Alibaba Cloud' &&
359         echo "$dmi" | grep -q 'System is a virtual machine'; then
360         # This is for Alibaba Arm systems which don't have CPUID.
361         echo kvm
362         skip_lkvm=true
363     elif echo "$dmi" | grep -q 'Manufacturer: QEMU'; then
364         # The test for KVM above failed, so now we know we're
365         # not using KVM acceleration.
366         echo qemu
367         skip_lkvm=true
368     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
369         if [ -d "${root}/proc/device-tree" ] &&
370             ls "${root}/proc/device-tree" | grep -q "fw-cfg"; then
371             # We don't have enough information to determine if we're
372             # using KVM acceleration or not.
373             echo qemu
374             skip_lkvm=true
375         fi
376     elif [ -d ${root}/proc/device-tree/hypervisor ] &&
377          grep -q "linux,kvm" /proc/device-tree/hypervisor/compatible; then
378         # We are running as a spapr KVM guest on ppc64
379         echo kvm
380         skip_lkvm=true
381     elif use_sysctl; then
382         # SmartOS KVM
383         product=$(sysctl -n hw.product)
384         if echo "$product" | grep -q 'SmartDC HVM'; then
385             echo kvm
386         fi
387     else
388         # This is known to fail for qemu with the explicit -cpu
389         # option, since /proc/cpuinfo will not contain the QEMU
390         # string. QEMU 2.10 added a new CPUID leaf, so this
391         # problem only triggered for older QEMU
392         if have_cpuinfo && grep -q 'QEMU' "${root}/proc/cpuinfo"; then
393             echo qemu
394         fi
395     fi
396 fi
397
398 if ! "$skip_lkvm"; then
399     if [ "$cpuid" = "LKVMLKVMLKVM" ]; then
400         echo lkvm
401     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
402         if [ -d "${root}/proc/device-tree" ] &&
403             grep -q "dummy-virt" "${root}/proc/device-tree/compatible"; then
404             echo lkvm
405         fi
406     fi
407 fi
408
409 # Check ppc64 lpar, kvm or powerkvm
410
411 # example /proc/cpuinfo line indicating 'not baremetal'
412 # platform  : pSeries
413 #
414 # example /proc/ppc64/lparcfg systemtype line
415 # system_type=IBM pSeries (emulated by qemu)
416
417 if [ "$arch" = "ppc64" ] || [ "$arch" = "ppc64le" ] ; then
418     if have_cpuinfo && grep -q 'platform.**pSeries' "${root}/proc/cpuinfo"; then
419         if grep -q 'model.*emulated by qemu' "${root}/proc/cpuinfo"; then
420                 echo ibm_power-kvm
421         else
422             # Assume LPAR, now detect shared or dedicated
423             if grep -q 'shared_processor_mode=1' "${root}/proc/ppc64/lparcfg"; then
424                 echo ibm_power-lpar_shared
425             else
426                 echo ibm_power-lpar_dedicated
427             fi
428         # detect powerkvm?
429         fi
430     fi
431 fi
432
433 # Check for OpenBSD/VMM
434 if [ "$cpuid" = "OpenBSDVMM58" ]; then
435         echo vmm
436 fi
437
438 # Check for LDoms
439 if [ "${arch#sparc}" != "$arch" ] && [ -e "${root}/dev/mdesc" ]; then
440     echo ldoms
441     if [ -d "${root}/sys/class/vlds/ctrl" ] && \
442              [ -d "${root}/sys/class/vlds/sp" ]; then
443         echo ldoms-control
444     else
445         echo ldoms-guest
446     fi
447     MDPROP="${root}/usr/lib/ldoms/mdprop.py"
448     if [ -x "${MDPROP}" ]; then
449         if [ -n "$($MDPROP -v iodevice device-type=pciex)" ]; then
450             echo ldoms-root
451             echo ldoms-io
452         elif [ -n "$($MDPROP -v iov-device vf-id=0)" ]; then
453             echo ldoms-io
454         fi
455     fi
456 fi
457
458 # Check for AWS.
459 # AWS on Xen.
460 if echo "$dmi" | grep -Eq 'Version: [0-9]+\.[0-9]+\.amazon'; then
461     echo aws
462 # AWS on baremetal or KVM.
463 elif echo "$dmi" | grep -q 'Vendor: Amazon EC2'; then
464     echo aws
465 fi
466
467 # UpCloud
468 if echo "$dmi" | grep -q 'Manufacturer: UpCloud'; then
469     echo upcloud
470 fi