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