Detect KVM property when running on POWER (RHBZ#1455676).
[virt-what.git] / virt-what.in
1 #!/bin/sh -
2 # @configure_input@
3 # Copyright (C) 2008-2015 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
78 # Check we're running as root.
79 EFFUID=$(id -u) || fail "failed to get current user id"
80
81 if [ "x$root" = "x" ] && [ "$EFFUID" -ne 0 ]; then
82     fail "this script must be run as root"
83 fi
84
85 # Try to locate the CPU-ID helper program
86 CPUID_HELPER=$(which virt-what-cpuid-helper 2>/dev/null)
87 if [ -z "$CPUID_HELPER" ] ; then
88     fail "virt-what-cpuid-helper program not found in \$PATH"
89 fi
90
91 # Many fullvirt hypervisors give an indication through CPUID.  Use the
92 # helper program to get this information.
93
94 cpuid=$(virt-what-cpuid-helper)
95
96 # Check for various products in the BIOS information.
97 # Note that dmidecode doesn't exist on all architectures.  On the ones
98 # it does not, then this will return an error, which is ignored (error
99 # message redirected into the $dmi variable).
100
101 dmi=$(LANG=C dmidecode 2>&1)
102
103 # Architecture.
104 # Note for the purpose of testing, we only call uname with -p option.
105
106 arch=$(uname -p | sed -e 's/i.86/i386/' | sed -e 's/arm.*/arm/')
107
108 # Check for VMware.
109 # cpuid check added by Chetan Loke.
110
111 if [ "$cpuid" = "VMwareVMware" ]; then
112     echo vmware
113 elif echo "$dmi" | grep -q 'Manufacturer: VMware'; then
114     echo vmware
115 fi
116
117 # Check for Hyper-V.
118 # http://blogs.msdn.com/b/sqlosteam/archive/2010/10/30/is-this-real-the-metaphysics-of-hardware-virtualization.aspx
119 if [ "$cpuid" = "Microsoft Hv" ]; then
120     echo hyperv
121 fi
122
123 # Check for VirtualPC.
124 # The negative check for cpuid is to distinguish this from Hyper-V
125 # which also has the same manufacturer string in the SM-BIOS data.
126 if [ "$cpuid" != "Microsoft Hv" ] &&
127     echo "$dmi" | grep -q 'Manufacturer: Microsoft Corporation'; then
128     echo virtualpc
129 fi
130
131 # Check for VirtualBox.
132 # Added by Laurent Léonard.
133 if echo "$dmi" | grep -q 'Manufacturer: innotek GmbH'; then
134     echo virtualbox
135 fi
136
137 # Check for bhyve.
138 if [ "$cpuid" = "bhyve bhyve " ]; then
139   echo bhyve
140 elif echo "$dmi" | grep -q "Vendor: BHYVE"; then
141   echo bhyve
142 fi
143
144 # Check for OpenVZ / Virtuozzo.
145 # Added by Evgeniy Sokolov.
146 # /proc/vz - always exists if OpenVZ kernel is running (inside and outside
147 # container)
148 # /proc/bc - exists on node, but not inside container.
149
150 if [ -d "${root}/proc/vz" -a ! -d "${root}/proc/bc" ]; then
151     echo openvz
152 fi
153
154 # Check for LXC containers
155 # http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
156 # Added by Marc Fournier
157
158 if [ -e "${root}/proc/1/environ" ] &&
159     cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container='; then
160     echo lxc
161 fi
162
163 # Check for Linux-VServer
164 if test -e "${root}/proc/self/status" \
165    && cat "${root}/proc/self/status" | grep -q "VxID: [0-9]*"; then
166     echo linux_vserver
167     if grep -q "VxID: 0$" "${root}/proc/self/status"; then
168         echo linux_vserver-host
169     else
170         echo linux_vserver-guest
171     fi
172 fi
173
174 # Check for UML.
175 # Added by Laurent Léonard.
176 if have_cpuinfo && grep -q 'UML' "${root}/proc/cpuinfo"; then
177     echo uml
178 fi
179
180 # Check for IBM PowerVM Lx86 Linux/x86 emulator.
181 if have_cpuinfo && grep -q '^vendor_id.*PowerVM Lx86' "${root}/proc/cpuinfo"
182 then
183     echo powervm_lx86
184 fi
185
186 # Check for Hitachi Virtualization Manager (HVM) Virtage logical partitioning.
187 if echo "$dmi" | grep -q 'Manufacturer.*HITACHI' &&
188    echo "$dmi" | grep -q 'Product.* LPAR'; then
189     echo virtage
190 fi
191
192 # Check for IBM SystemZ.
193 if have_cpuinfo && grep -q '^vendor_id.*IBM/S390' "${root}/proc/cpuinfo"; then
194     echo ibm_systemz
195     if [ -f "${root}/proc/sysinfo" ]; then
196         if grep -q 'VM.*Control Program.*z/VM' "${root}/proc/sysinfo"; then
197             echo ibm_systemz-zvm
198         elif grep -q '^LPAR' "${root}/proc/sysinfo"; then
199             echo ibm_systemz-lpar
200         else
201             # This is unlikely to be correct.
202             echo ibm_systemz-direct
203         fi
204     fi
205 fi
206
207 # Check for Parallels.
208 if echo "$dmi" | grep -q 'Vendor: Parallels'; then
209     echo parallels
210     skip_qemu_kvm=true
211 fi
212
213 # Check for oVirt/RHEV.
214 if echo "$dmi" | grep -q 'Manufacturer: oVirt'; then
215     echo ovirt
216 fi
217 if echo "$dmi" | grep -q 'Product Name: RHEV Hypervisor'; then
218     echo rhev
219 fi
220
221 # Check for Xen.
222
223 if [ "$cpuid" = "XenVMMXenVMM" ]; then
224     echo xen; echo xen-hvm
225     skip_qemu_kvm=true
226 elif [ -d "${root}/proc/xen" ]; then
227     echo xen
228     if grep -q "control_d" "${root}/proc/xen/capabilities" 2>/dev/null; then
229         echo xen-dom0
230     else
231         echo xen-domU
232     fi
233     skip_qemu_kvm=true
234     skip_lkvm=true
235 elif [ -f "${root}/sys/hypervisor/type" ] &&
236     grep -q "xen" "${root}/sys/hypervisor/type"; then
237     # Ordinary kernel with pv_ops.  There does not seem to be
238     # enough information at present to tell whether this is dom0
239     # or domU.  XXX
240     echo xen
241 elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
242     if [ -d "${root}/proc/device-tree/hypervisor" ] &&
243         grep -q "xen" "${root}/proc/device-tree/hypervisor/compatible"; then
244         echo xen
245         skip_qemu_kvm=true
246         skip_lkvm=true
247     fi
248 elif [ "$arch" = "ia64" ]; then
249     if [ -d "${root}/sys/bus/xen" -a ! -d "${root}/sys/bus/xen-backend" ]; then
250         # PV-on-HVM drivers installed in a Xen guest.
251         echo xen
252         echo xen-hvm
253     else
254         # There is no virt leaf on IA64 HVM.  This is a last-ditch
255         # attempt to detect something is virtualized by using a
256         # timing attack.
257         virt-what-ia64-xen-rdtsc-test > /dev/null 2>&1
258         case "$?" in
259             0) ;; # not virtual
260             1) # Could be some sort of virt, or could just be a bit slow.
261                 echo virt
262         esac
263     fi
264 fi
265
266 # Check for QEMU/KVM.
267 #
268 # Parallels exports KVMKVMKVM leaf, so skip this test if we've already
269 # seen that it's Parallels.  Xen uses QEMU as the device model, so
270 # skip this test if we know it is Xen.
271
272 if ! "$skip_qemu_kvm"; then
273     if [ "$cpuid" = "KVMKVMKVM" ]; then
274         echo kvm
275     elif echo "$dmi" | grep -q 'Product Name: KVM'; then
276         echo kvm
277         skip_lkvm=true
278     elif echo "$dmi" | grep -q 'Manufacturer: QEMU'; then
279         # The test for KVM above failed, so now we know we're
280         # not using KVM acceleration.
281         echo qemu
282         skip_lkvm=true
283     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
284         if [ -d "${root}/proc/device-tree" ] &&
285             ls "${root}/proc/device-tree" | grep -q "fw-cfg"; then
286             # We don't have enough information to determine if we're
287             # using KVM acceleration or not.
288             echo qemu
289             skip_lkvm=true
290         fi
291     elif [ -d /proc/device-tree/hypervisor ] &&
292          grep -q "linux,kvm" /proc/device-tree/hypervisor/compatible; then
293         # We are running as a spapr KVM guest on ppc64
294         echo kvm
295         skip_lkvm=true
296     else
297         # XXX This is known to fail for qemu with the explicit -cpu
298         # option, since /proc/cpuinfo will not contain the QEMU
299         # string.  The long term fix for this would be to export
300         # another CPUID leaf for non-accelerated qemu.
301         if grep -q 'QEMU' "${root}/proc/cpuinfo"; then
302             echo qemu
303         fi
304     fi
305 fi
306
307 if ! "$skip_lkvm"; then
308     if [ "$cpuid" = "LKVMLKVMLKVM" ]; then
309         echo lkvm
310     elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
311         if [ -d "${root}/proc/device-tree" ] &&
312             grep -q "dummy-virt" "${root}/proc/device-tree/compatible"; then
313             echo lkvm
314         fi
315     fi
316 fi
317
318 # Check for Docker.
319 if [ -f "${root}/.dockerinit" ]; then
320     echo docker
321 fi
322
323 # Check ppc64 lpar, kvm or powerkvm
324
325 # example /proc/cpuinfo line indicating 'not baremetal'
326 # platform  : pSeries
327 #
328 # example /proc/ppc64/lparcfg systemtype line
329 # system_type=IBM pSeries (emulated by qemu)
330
331 if [ "$arch" = "ppc64" ]; then
332     if have_cpuinfo && grep -q 'platform.**pSeries' "${root}/proc/cpuinfo"; then
333         if grep -q 'model.*emulated by qemu' "${root}/proc/cpuinfo"; then
334                 echo ibm_power-kvm
335         else
336             # Assume LPAR, now detect shared or dedicated
337             if grep -q 'shared_processor_mode=1' "${root}/proc/ppc64/lparcfg"; then
338                 echo ibm_power-lpar_shared
339             else
340                 echo ibm_power-lpar_dedicated
341             fi
342         # detect powerkvm?
343         fi
344     fi
345 fi