6e9aef11c2df77790b1007115a8f754c2e371c83
[virt-what.git] / virt-what.in
1 #!/bin/sh -
2 # @configure_input@
3 # Copyright (C) 2008-2011 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
34 VERSION="@VERSION@"
35
36 have_cpuinfo() {
37     test -e "${root}/proc/cpuinfo"
38 }
39
40 fail() {
41     echo "virt-what: $1" >&2
42     exit 1
43 }
44
45 usage() {
46     echo "virt-what [options]"
47     echo "Options:"
48     echo "  --help          Display this help"
49     echo "  --version       Display version and exit"
50     exit 0
51 }
52
53 # Handle the command line arguments, if any.
54
55 TEMP=$(getopt -o v --long help --long version --long test-root: -n 'virt-what' -- "$@")
56 if [ $? != 0 ]; then exit 1; fi
57 eval set -- "$TEMP"
58
59 while true; do
60     case "$1" in
61         --help) usage ;;
62         --test-root)
63             # Deliberately undocumented: used for 'make check'.
64             root="$2"
65             shift 2
66             ;;
67         -v|--version) echo "$VERSION"; exit 0 ;;
68         --) shift; break ;;
69         *) fail "internal error ($1)" ;;
70     esac
71 done
72
73 # Add /sbin and /usr/sbin to the path so we can find system
74 # binaries like dmidecode.
75 # Add /usr/libexec to the path so we can find the helper binary.
76 prefix=@prefix@
77 exec_prefix=@exec_prefix@
78 PATH="${root}@libexecdir@:${root}/sbin:${root}/usr/sbin:${PATH}"
79
80 # Check we're running as root.
81 EFFUID=$(id -u) || fail "failed to get current user id"
82
83 if [ "x$root" = "x" ] && [ "$EFFUID" -ne 0 ]; then
84     fail "this script must be run as root"
85 fi
86
87 # Try to locate the CPU-ID helper program
88 CPUID_HELPER=$(which virt-what-cpuid-helper 2>/dev/null)
89 if [ -z "$CPUID_HELPER" ] ; then
90     fail "virt-what-cpuid-helper program not found in \$PATH"
91 fi
92
93 # Many fullvirt hypervisors give an indication through CPUID.  Use the
94 # helper program to get this information.
95
96 cpuid=$(virt-what-cpuid-helper)
97
98 # Check for various products in the BIOS information.
99 # Note that dmidecode doesn't exist on non-PC architectures.  On these,
100 # this will return an error which is ignored (error message redirected
101 # into $dmi variable).
102
103 dmi=$(LANG=C dmidecode 2>&1)
104
105 # Architecture.
106 # Note for the purpose of testing, we only call uname with -p option.
107
108 arch=$(uname -p)
109
110 # Check for VMware.
111 # cpuid check added by Chetan Loke.
112
113 if [ "$cpuid" = "VMwareVMware" ]; then
114     echo vmware
115 elif echo "$dmi" | grep -q 'Manufacturer: VMware'; then
116     echo vmware
117 fi
118
119 # Check for Hyper-V.
120 # http://blogs.msdn.com/b/sqlosteam/archive/2010/10/30/is-this-real-the-metaphysics-of-hardware-virtualization.aspx
121 if [ "$cpuid" = "Microsoft Hv" ]; then
122     echo hyperv
123 fi
124
125 # Check for VirtualPC.
126 # The negative check for cpuid is to distinguish this from Hyper-V
127 # which also has the same manufacturer string in the SM-BIOS data.
128 if [ "$cpuid" != "Microsoft Hv" ] &&
129     echo "$dmi" | grep -q 'Manufacturer: Microsoft Corporation'; then
130     echo virtualpc
131 fi
132
133 # Check for VirtualBox.
134 # Added by Laurent Léonard.
135 if echo "$dmi" | grep -q 'Manufacturer: innotek GmbH'; then
136     echo virtualbox
137 fi
138
139 # Check for OpenVZ / Virtuozzo.
140 # Added by Evgeniy Sokolov.
141 # /proc/vz - always exists if OpenVZ kernel is running (inside and outside
142 # container)
143 # /proc/bc - exists on node, but not inside container.
144
145 if [ -d "${root}/proc/vz" -a ! -d "${root}/proc/bc" ]; then
146     echo openvz
147 fi
148
149 # Check for LXC containers
150 # http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface
151 # Added by Marc Fournier
152
153 if [ -e "${root}/proc/1/environ" ] &&
154     cat "${root}/proc/1/environ" | tr '\000' '\n' | grep -Eiq '^container='; then
155     echo lxc
156 fi
157
158 # Check for Linux-VServer
159 if test -e "${root}/proc/self/status" \
160    && cat "${root}/proc/self/status" | grep -q "VxID: [0-9]*"; then
161     echo linux_vserver
162     if grep -q "VxID: 0$" "${root}/proc/self/status"; then
163         echo linux_vserver-host
164     else
165         echo linux_vserver-guest
166     fi
167 fi
168
169 # Check for UML.
170 # Added by Laurent Léonard.
171 if have_cpuinfo && grep -q 'UML' "${root}/proc/cpuinfo"; then
172     echo uml
173 fi
174
175 # Check for IBM PowerVM Lx86 Linux/x86 emulator.
176 if have_cpuinfo && grep -q '^vendor_id.*PowerVM Lx86' "${root}/proc/cpuinfo"
177 then
178     echo powervm_lx86
179 fi
180
181 # Check for Hitachi Virtualization Manager (HVM) Virtage logical partitioning.
182 if echo "$dmi" | grep -q 'Manufacturer.*HITACHI' &&
183    echo "$dmi" | grep -q 'Product.* LPAR'; then
184     echo virtage
185 fi
186
187 # Check for IBM SystemZ.
188 if have_cpuinfo && grep -q '^vendor_id.*IBM/S390' "${root}/proc/cpuinfo"; then
189     echo ibm_systemz
190     if [ -f "${root}/proc/sysinfo" ]; then
191         if grep -q 'VM.*Control Program.*z/VM' "${root}/proc/sysinfo"; then
192             echo ibm_systemz-zvm
193         elif grep -q '^LPAR' "${root}/proc/sysinfo"; then
194             echo ibm_systemz-lpar
195         else
196             # This is unlikely to be correct.
197             echo ibm_systemz-direct
198         fi
199     fi
200 fi
201
202 # Check for Parallels.
203 if echo "$dmi" | grep -q 'Vendor: Parallels'; then
204     echo parallels
205     skip_qemu_kvm=true
206 fi
207
208 # Check for Xen.
209
210 if [ "$cpuid" = "XenVMMXenVMM" ]; then
211     echo xen; echo xen-hvm
212     skip_qemu_kvm=true
213 elif [ -d "${root}/proc/xen" ]; then
214     echo xen
215     if grep -q "control_d" "${root}/proc/xen/capabilities" 2>/dev/null; then
216         echo xen-dom0
217     else
218         echo xen-domU
219     fi
220     skip_qemu_kvm=true
221 elif [ -f "${root}/sys/hypervisor/type" ] &&
222     grep -q "xen" "${root}/sys/hypervisor/type"; then
223     # Ordinary kernel with pv_ops.  There does not seem to be
224     # enough information at present to tell whether this is dom0
225     # or domU.  XXX
226     echo xen
227 elif [ "$arch" = "ia64" ]; then
228     if [ -d "${root}/sys/bus/xen" -a ! -d "${root}/sys/bus/xen-backend" ]; then
229         # PV-on-HVM drivers installed in a Xen guest.
230         echo xen
231         echo xen-hvm
232     else
233         # There is no virt leaf on IA64 HVM.  This is a last-ditch
234         # attempt to detect something is virtualized by using a
235         # timing attack.
236         virt-what-ia64-xen-rdtsc-test > /dev/null 2>&1
237         case "$?" in
238             0) ;; # not virtual
239             1) # Could be some sort of virt, or could just be a bit slow.
240                 echo virt
241         esac
242     fi
243 fi
244
245 # Check for QEMU/KVM.
246 #
247 # Parallels exports KVMKVMKVM leaf, so skip this test if we've already
248 # seen that it's Parallels.  Xen uses QEMU as the device model, so
249 # skip this test if we know it is Xen.
250
251 if ! "$skip_qemu_kvm"; then
252     if [ "$cpuid" = "KVMKVMKVM" ]; then
253         echo kvm
254     else
255         # XXX This is known to fail for qemu with the explicit -cpu
256         # option, since /proc/cpuinfo will not contain the QEMU
257         # string.  The long term fix for this would be to export
258         # another CPUID leaf for non-accelerated qemu.
259         if grep -q 'QEMU' "${root}/proc/cpuinfo"; then
260             echo qemu
261         fi
262     fi
263 fi
264
265 # Check for Docker.
266 if [ -f "${root}/.dockerinit" ]; then
267         echo docker
268 fi