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