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