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