96c18b498eef45f3c00dd23b61feab9974685996
[virt-what.git] / virt-what.in
1 #!/bin/bash -
2 # @configure_input@
3 # Copyright (C) 2008-2010 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 -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         -v|--version) echo $VERSION; exit 0 ;;
54         --) shift; break ;;
55         *) fail "internal error" ;;
56     esac
57 done
58
59 # Check we're running as root.
60
61 uid=`id -u`
62 if [ "$uid" != 0 ]; then
63     fail "this script must be run as root"
64 fi
65
66 # Add /sbin and /usr/sbin to the path so we can find system
67 # binaries like dmicode.
68 # Add /usr/libexec to the path so we can find the helper binary.
69 prefix=@prefix@
70 exec_prefix=@exec_prefix@
71 PATH=@libexecdir@:/sbin:/usr/sbin:$PATH
72
73 # Many fullvirt hypervisors give an indication through CPUID.  Use the
74 # helper program to get this information.
75
76 cpuid=`virt-what-cpuid-helper`
77
78 # Check for various products in the BIOS information.
79
80 dmi=`dmidecode 2>&1`
81
82 # Check for VMware.
83 # cpuid check added by Chetan Loke.
84
85 if [ "$cpuid" = "VMwareVMware" ]; then
86     echo vmware
87 elif echo "$dmi" | grep -q 'Manufacturer: VMware'; then
88     echo vmware
89 fi
90
91 # Check for VirtualPC.
92
93 if echo "$dmi" | grep -q 'Manufacturer: Microsoft Corporation'; then
94     echo virtualpc
95 fi
96
97 # Check for VirtualBox.
98 # Added by Laurent Léonard.
99 if echo "$dmi" | grep -q 'Manufacturer: innotek GmbH'; then
100     echo virtualbox
101 fi
102
103 # Check for OpenVZ / Virtuozzo.
104 # Added by Evgeniy Sokolov.
105 # /proc/vz - always exists if OpenVZ kernel is running (inside and outside
106 # container)
107 # /proc/bc - exists on node, but not inside container.
108
109 if [ -d /proc/vz -a ! -d /proc/bc ]; then
110     echo openvz
111 fi
112
113 # Check for UML.
114 # Added by Laurent Léonard.
115 if grep -q 'UML' /proc/cpuinfo; then
116     echo uml
117 fi
118
119 # Check for Xen.
120
121 if [ "$cpuid" = "XenVMMXenVMM" ]; then
122     echo xen; echo xen-hvm
123     is_xen=1
124 elif [ -f /proc/xen/capabilities ]; then
125     echo xen
126     if grep -q "control_d" /proc/xen/capabilities; then
127         echo xen-dom0
128     else
129         echo xen-domU
130     fi
131     is_xen=1
132 elif [ -d /proc/xen ]; then
133     # This directory can be present when Xen paravirt drivers are
134     # installed, even on baremetal.  Don't confuse people by
135     # printing anything.
136     :
137 fi
138
139 # Check for QEMU/KVM.
140
141 if [ ! "$is_xen" ]; then
142     # Disable this test if we know this is Xen already, because Xen
143     # uses QEMU for its device model.
144
145     if grep -q 'QEMU' /proc/cpuinfo; then
146         if [ "$cpuid" = "KVMKVMKVM" ]; then
147             echo kvm
148         else
149             echo qemu
150         fi
151     fi
152 fi