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