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