Add detection of Red Hat's hypervisor
[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 Alibaba Cloud
117 if echo "$dmi" | grep -q 'Manufacturer: Alibaba'; then
118     # Check for Alibaba Cloud ECS Bare Metal (EBM) Instance
119     if ( { 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
169 if [ -d "${root}/proc/vz" -a ! -d "${root}/proc/bc" ]; then
170     echo openvz
171 fi
172
173 # Check for LXC containers
174 # http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
175 # Added by Marc Fournier
176
177 if [ -e "${root}/proc/1/environ" ] &&
178     cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container=lxc'; then
179     echo lxc
180 fi
181
182 # Check for Linux-VServer
183 if test -e "${root}/proc/self/status" \
184    && cat "${root}/proc/self/status" | grep -q "VxID: [0-9]*"; then
185     echo linux_vserver
186     if grep -q "VxID: 0$" "${root}/proc/self/status"; then
187         echo linux_vserver-host
188     else
189         echo linux_vserver-guest
190     fi
191 fi
192
193 # Check for UML.
194 # Added by Laurent Léonard.
195 if have_cpuinfo && grep -q 'UML' "${root}/proc/cpuinfo"; then
196     echo uml
197 fi
198
199 # Check for IBM PowerVM Lx86 Linux/x86 emulator.
200 if have_cpuinfo && grep -q '^vendor_id.*PowerVM Lx86' "${root}/proc/cpuinfo"
201 then
202     echo powervm_lx86
203 fi
204
205 # Check for Hitachi Virtualization Manager (HVM) Virtage logical partitioning.
206 if echo "$dmi" | grep -q 'Manufacturer.*HITACHI' &&
207    echo "$dmi" | grep -q 'Product.* LPAR'; then
208     echo virtage
209 fi
210
211 # Check for IBM SystemZ.
212 if have_cpuinfo && grep -q '^vendor_id.*IBM/S390' "${root}/proc/cpuinfo"; then
213     echo ibm_systemz
214     if [ -f "${root}/proc/sysinfo" ]; then
215         if grep -q 'VM.*Control Program.*KVM/Linux' "${root}/proc/sysinfo"; then
216             echo ibm_systemz-kvm
217         elif grep -q 'VM.*Control Program.*z/VM' "${root}/proc/sysinfo"; then
218             echo ibm_systemz-zvm
219         elif grep -q '^LPAR' "${root}/proc/sysinfo"; then
220             echo ibm_systemz-lpar
221         else
222             # This is unlikely to be correct.
223             echo ibm_systemz-direct
224         fi
225     fi
226 fi
227
228 # Check for Parallels.
229 if echo "$dmi" | grep -q 'Vendor: Parallels'; then
230     echo parallels
231     skip_qemu_kvm=true
232 fi
233
234 # Check for Nutanix AHV.
235 # This is sufficiently different from KVM and has Viridian extensions,
236 # so skip the KVM test.
237 if echo "$dmi" | grep -q 'Manufacturer: Nutanix'; then
238     echo nutanix_ahv
239     skip_qemu_kvm=true
240 fi
241
242 # Check for oVirt/RHEV.
243 if echo "$dmi" | grep -q 'Manufacturer: oVirt'; then
244     echo ovirt
245 fi
246 if echo "$dmi" | grep -q 'Product Name: RHEV Hypervisor'; then
247     echo rhev
248 fi
249
250 # Google Cloud
251 if echo "$dmi" | grep -q 'Product Name: Google Compute Engine'; then
252     echo google_cloud
253 fi
254
255 # Red Hat's hypervisor.
256 if echo "$dmi" | grep -q 'Manufacturer: Red Hat'; then
257     echo redhat
258 fi
259
260 # Check for Xen.
261
262 if [ "$cpuid" = "XenVMMXenVMM" ] &&
263     ! echo "$dmi" | grep -q 'No SMBIOS nor DMI entry point found, sorry'; then
264     echo xen; echo xen-hvm
265     skip_qemu_kvm=true
266 elif [ -d "${root}/proc/xen" ]; then
267     echo xen
268     if grep -q "control_d" "${root}/proc/xen/capabilities" 2>/dev/null; then
269         echo xen-dom0
270     else
271         echo xen-domU
272     fi
273     skip_qemu_kvm=true
274     skip_lkvm=true
275 elif [ -f "${root}/sys/hypervisor/type" ] &&
276     grep -q "xen" "${root}/sys/hypervisor/type"; then
277     # Ordinary kernel with pv_ops.  There does not seem to be
278     # enough information at present to tell whether this is dom0
279     # or domU.  XXX
280     echo xen
281 elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
282     if [ -d "${root}/proc/device-tree/hypervisor" ] &&
283         grep -q "xen" "${root}/proc/device-tree/hypervisor/compatible"; then
284         echo xen
285         skip_qemu_kvm=true
286         skip_lkvm=true
287     elif [ -d "${root}/proc/device-tree/hypervisor" ] &&
288         grep -q "vmware" "${root}/proc/device-tree/hypervisor/compatible"; then
289         echo vmware
290         skip_lkvm=true
291     fi
292 elif [ "$arch" = "ia64" ]; then
293     if [ -d "${root}/sys/bus/xen" -a ! -d "${root}/sys/bus/xen-backend" ]; then
294         # PV-on-HVM drivers installed in a Xen guest.
295         echo xen
296         echo xen-hvm
297     else
298         # There is no virt leaf on IA64 HVM.  This is a last-ditch
299         # attempt to detect something is virtualized by using a
300         # timing attack.
301         virt-what-ia64-xen-rdtsc-test > /dev/null 2>&1
302         case "$?" in
303             0) ;; # not virtual
304             1) # Could be some sort of virt, or could just be a bit slow.
305                 echo virt
306         esac
307     fi
308 fi
309
310 # Check for QEMU/KVM.
311 #
312 # Parallels exports KVMKVMKVM leaf, so skip this test if we've already
313 # seen that it's Parallels.  Xen uses QEMU as the device model, so
314 # skip this test if we know it is Xen.
315
316 if ! "$skip_qemu_kvm"; then
317     if [ "$cpuid" = "KVMKVMKVM" ]; then
318         echo kvm
319     elif [ "$cpuid" = "TCGTCGTCGTCG" ]; then
320         echo qemu
321         skip_lkvm=true
322     elif echo "$dmi" | grep -q 'Product Name: KVM'; then
323         echo kvm
324         skip_lkvm=true
325     elif echo "$dmi" | grep -q 'Manufacturer: QEMU'; then
326         # The test for KVM above failed, so now we know we're
327         # not using KVM acceleration.
328         echo qemu
329         skip_lkvm=true
330     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
331         if [ -d "${root}/proc/device-tree" ] &&
332             ls "${root}/proc/device-tree" | grep -q "fw-cfg"; then
333             # We don't have enough information to determine if we're
334             # using KVM acceleration or not.
335             echo qemu
336             skip_lkvm=true
337         fi
338     elif [ -d ${root}/proc/device-tree/hypervisor ] &&
339          grep -q "linux,kvm" /proc/device-tree/hypervisor/compatible; then
340         # We are running as a spapr KVM guest on ppc64
341         echo kvm
342         skip_lkvm=true
343     elif use_sysctl; then
344         # SmartOS KVM
345         product=$(sysctl -n hw.product)
346         if echo "$product" | grep -q 'SmartDC HVM'; then
347             echo kvm
348         fi
349     else
350         # This is known to fail for qemu with the explicit -cpu
351         # option, since /proc/cpuinfo will not contain the QEMU
352         # string. QEMU 2.10 added a new CPUID leaf, so this
353         # problem only triggered for older QEMU
354         if have_cpuinfo && grep -q 'QEMU' "${root}/proc/cpuinfo"; then
355             echo qemu
356         fi
357     fi
358 fi
359
360 if ! "$skip_lkvm"; then
361     if [ "$cpuid" = "LKVMLKVMLKVM" ]; then
362         echo lkvm
363     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
364         if [ -d "${root}/proc/device-tree" ] &&
365             grep -q "dummy-virt" "${root}/proc/device-tree/compatible"; then
366             echo lkvm
367         fi
368     fi
369 fi
370
371 # Check for Docker.
372 if [ -f "${root}/.dockerenv" ] || [ -f "${root}/.dockerinit" ] || \
373    grep -qF /docker/ "${root}/proc/self/cgroup" 2>/dev/null; then
374     echo docker
375 fi
376
377 # Check for Podman.
378 if [ -e "${root}/proc/1/environ" ] &&
379     cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container=podman'; then
380     echo podman
381 elif grep -qF /libpod- "${root}/proc/self/cgroup" 2>/dev/null; then
382     echo podman
383 fi
384
385 # Check ppc64 lpar, kvm or powerkvm
386
387 # example /proc/cpuinfo line indicating 'not baremetal'
388 # platform  : pSeries
389 #
390 # example /proc/ppc64/lparcfg systemtype line
391 # system_type=IBM pSeries (emulated by qemu)
392
393 if [ "$arch" = "ppc64" ] || [ "$arch" = "ppc64le" ] ; then
394     if have_cpuinfo && grep -q 'platform.**pSeries' "${root}/proc/cpuinfo"; then
395         if grep -q 'model.*emulated by qemu' "${root}/proc/cpuinfo"; then
396                 echo ibm_power-kvm
397         else
398             # Assume LPAR, now detect shared or dedicated
399             if grep -q 'shared_processor_mode=1' "${root}/proc/ppc64/lparcfg"; then
400                 echo ibm_power-lpar_shared
401             else
402                 echo ibm_power-lpar_dedicated
403             fi
404         # detect powerkvm?
405         fi
406     fi
407 fi
408
409 # Check for OpenBSD/VMM
410 if [ "$cpuid" = "OpenBSDVMM58" ]; then
411         echo vmm
412 fi
413
414 # Check for LDoms
415 if [ "${arch#sparc}" != "$arch" ] && [ -e "${root}/dev/mdesc" ]; then
416     echo ldoms
417     if [ -d "${root}/sys/class/vlds/ctrl" ] && \
418              [ -d "${root}/sys/class/vlds/sp" ]; then
419         echo ldoms-control
420     else
421         echo ldoms-guest
422     fi
423     MDPROP="${root}/usr/lib/ldoms/mdprop.py"
424     if [ -x "${MDPROP}" ]; then
425         if [ -n "$($MDPROP -v iodevice device-type=pciex)" ]; then
426             echo ldoms-root
427             echo ldoms-io
428         elif [ -n "$($MDPROP -v iov-device vf-id=0)" ]; then
429             echo ldoms-io
430         fi
431     fi
432 fi
433
434 # Check for AWS.
435 # AWS on Xen.
436 if echo "$dmi" | grep -Eq 'Version: [0-9]+\.[0-9]+\.amazon'; then
437     echo aws
438 # AWS on baremetal or KVM.
439 elif echo "$dmi" | grep -q 'Vendor: Amazon EC2'; then
440     echo aws
441 fi