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