3 # Copyright (C) 2008-2015 Red Hat Inc.
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.
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.
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.
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.
23 # Please see also the manual page virt-what(1).
24 # This script should be run as root.
26 # The following resources were useful in writing this script:
27 # . http://dmo.ca/blog/detecting-virtualization-on-linux/
29 # Do not allow unset variables, and set defaults.
38 test -e "${root}/proc/cpuinfo"
42 echo "virt-what: $1" >&2
47 echo "virt-what [options]"
49 echo " --help Display this help"
50 echo " --version Display version and exit"
54 # Handle the command line arguments, if any.
55 while test $# -gt 0; do
59 # Deliberately undocumented: used for 'make check'.
60 root=$(echo "$1" | sed 's/.*=//')
62 test -z "$root" && fail "--test-root option requires a value"
64 -v|--version) echo "$VERSION"; exit 0 ;;
66 *) fail "unrecognized option '$1'";;
69 test $# -gt 0 && fail "extra operand '$1'"
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.
75 exec_prefix=@exec_prefix@
76 PATH="${root}@libexecdir@:${root}/sbin:${root}/usr/sbin:${PATH}"
78 # Check we're running as root.
79 EFFUID=$(id -u) || fail "failed to get current user id"
81 if [ "x$root" = "x" ] && [ "$EFFUID" -ne 0 ]; then
82 fail "this script must be run as root"
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"
91 # Many fullvirt hypervisors give an indication through CPUID. Use the
92 # helper program to get this information.
94 cpuid=$(virt-what-cpuid-helper)
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).
101 dmi=$(LANG=C dmidecode 2>&1)
104 # Note for the purpose of testing, we only call uname with -p option.
106 arch=$(uname -p | sed -e 's/i.86/i386/' | sed -e 's/arm.*/arm/')
109 # cpuid check added by Chetan Loke.
111 if [ "$cpuid" = "VMwareVMware" ]; then
113 elif echo "$dmi" | grep -q 'Manufacturer: VMware'; then
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
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
131 # Check for VirtualBox.
132 # Added by Laurent Léonard.
133 if echo "$dmi" | grep -q 'Manufacturer: innotek GmbH'; then
137 # Check for OpenVZ / Virtuozzo.
138 # Added by Evgeniy Sokolov.
139 # /proc/vz - always exists if OpenVZ kernel is running (inside and outside
141 # /proc/bc - exists on node, but not inside container.
143 if [ -d "${root}/proc/vz" -a ! -d "${root}/proc/bc" ]; then
147 # Check for LXC containers
148 # http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
149 # Added by Marc Fournier
151 if [ -e "${root}/proc/1/environ" ] &&
152 cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container='; then
156 # Check for Linux-VServer
157 if test -e "${root}/proc/self/status" \
158 && cat "${root}/proc/self/status" | grep -q "VxID: [0-9]*"; then
160 if grep -q "VxID: 0$" "${root}/proc/self/status"; then
161 echo linux_vserver-host
163 echo linux_vserver-guest
168 # Added by Laurent Léonard.
169 if have_cpuinfo && grep -q 'UML' "${root}/proc/cpuinfo"; then
173 # Check for IBM PowerVM Lx86 Linux/x86 emulator.
174 if have_cpuinfo && grep -q '^vendor_id.*PowerVM Lx86' "${root}/proc/cpuinfo"
179 # Check for Hitachi Virtualization Manager (HVM) Virtage logical partitioning.
180 if echo "$dmi" | grep -q 'Manufacturer.*HITACHI' &&
181 echo "$dmi" | grep -q 'Product.* LPAR'; then
185 # Check for IBM SystemZ.
186 if have_cpuinfo && grep -q '^vendor_id.*IBM/S390' "${root}/proc/cpuinfo"; then
188 if [ -f "${root}/proc/sysinfo" ]; then
189 if grep -q 'VM.*Control Program.*z/VM' "${root}/proc/sysinfo"; then
191 elif grep -q '^LPAR' "${root}/proc/sysinfo"; then
192 echo ibm_systemz-lpar
194 # This is unlikely to be correct.
195 echo ibm_systemz-direct
200 # Check for Parallels.
201 if echo "$dmi" | grep -q 'Vendor: Parallels'; then
208 if [ "$cpuid" = "XenVMMXenVMM" ]; then
209 echo xen; echo xen-hvm
211 elif [ -d "${root}/proc/xen" ]; then
213 if grep -q "control_d" "${root}/proc/xen/capabilities" 2>/dev/null; then
220 elif [ -f "${root}/sys/hypervisor/type" ] &&
221 grep -q "xen" "${root}/sys/hypervisor/type"; then
222 # Ordinary kernel with pv_ops. There does not seem to be
223 # enough information at present to tell whether this is dom0
226 elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
227 if [ -d "${root}/proc/device-tree/hypervisor" ] &&
228 grep -q "xen" "${root}/proc/device-tree/hypervisor/compatible"; then
233 elif [ "$arch" = "ia64" ]; then
234 if [ -d "${root}/sys/bus/xen" -a ! -d "${root}/sys/bus/xen-backend" ]; then
235 # PV-on-HVM drivers installed in a Xen guest.
239 # There is no virt leaf on IA64 HVM. This is a last-ditch
240 # attempt to detect something is virtualized by using a
242 virt-what-ia64-xen-rdtsc-test > /dev/null 2>&1
245 1) # Could be some sort of virt, or could just be a bit slow.
251 # Check for QEMU/KVM.
253 # Parallels exports KVMKVMKVM leaf, so skip this test if we've already
254 # seen that it's Parallels. Xen uses QEMU as the device model, so
255 # skip this test if we know it is Xen.
257 if ! "$skip_qemu_kvm"; then
258 if [ "$cpuid" = "KVMKVMKVM" ]; then
260 elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
261 if [ -d "${root}/proc/device-tree" ] &&
262 ls "${root}/proc/device-tree" | grep -q "fw-cfg"; then
263 # We don't have enough information to determine if we're
264 # using KVM acceleration or not.
267 elif echo "$dmi" | grep -q 'Manufacturer: QEMU'; then
268 # We don't have enough information to determine if we're
269 # using KVM acceleration or not.
274 # XXX This is known to fail for qemu with the explicit -cpu
275 # option, since /proc/cpuinfo will not contain the QEMU
276 # string. The long term fix for this would be to export
277 # another CPUID leaf for non-accelerated qemu.
278 if grep -q 'QEMU' "${root}/proc/cpuinfo"; then
284 if ! "$skip_lkvm"; then
285 if [ "$cpuid" = "LKVMLKVMLKVM" ]; then
287 elif [ "$arch" = "arm" ] || [ "$arch" = "aarch64" ]; then
288 if [ -d "${root}/proc/device-tree" ] &&
289 grep -q "dummy-virt" "${root}/proc/device-tree/compatible"; then
296 if [ -f "${root}/.dockerinit" ]; then