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