]> git.ipfire.org Git - oddments/fireinfo.git/blame - fireinfo/hypervisor.py
Fix some hypervisor detection if the system returned no BIOS/system vendor.
[oddments/fireinfo.git] / fireinfo / hypervisor.py
CommitLineData
715ba5ac 1#!/usr/bin/python
3b5ed4e1
MT
2###############################################################################
3# #
4# Fireinfo #
5# Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org) #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
715ba5ac
MT
21
22import _fireinfo
32aeec73 23import system
715ba5ac
MT
24
25class Hypervisor(object):
26 def __init__(self):
27 self.__info = _fireinfo.get_hypervisor()
28
32aeec73
MT
29 @property
30 def system(self):
31 """
32 Return the current instance of the System class.
33
34 We need to do that as a property because otherwise
35 we get a recursion.
36 """
37 return system.System()
38
715ba5ac
MT
39 @property
40 def vendor(self):
41 """
42 Returns the name of the hypervisor vendor.
43 """
44 if not self.virtual:
45 return None
46
c0d2bae3
MT
47 # Some of the hypervisors can be detected in a right way.
48 # We can return them at this place.
49 if self.__info["hypervisor"] in ("Xen", "VMWare", "KVM"):
715ba5ac
MT
50 return self.__info["hypervisor"]
51
c0d2bae3
MT
52 # Citrix Xen says it is Microsoft Hv.
53 if self.__info["hypervisor"] == "Microsoft" and \
54 self.system.bios_vendor == "Xen":
55 return "Xen"
56
d9fc4e89
MT
57 if not self.__info["hypervisor"]:
58 # On VMWare systems, the bios vendor string contains "VMWare".
2dc5f0d0 59 if self.__is_hypervisor_vmware():
d9fc4e89
MT
60 return "VMWare"
61
62 # VirtualBox got "innotek GmbH" as bios vendor.
2dc5f0d0 63 elif self.__is_hypervisor_virtualbox():
d9fc4e89 64 return "VirtualBox"
715ba5ac 65
f2bc7d30
MT
66 # Check for qemu.
67 elif self.__is_hypervisor_qemu():
68 return "Qemu"
69
a79b7395
MT
70 # Check for Microsoft.
71 elif self.__is_hypervisor_microsoft():
72 return "Microsoft"
73
715ba5ac
MT
74 return "unknown"
75
76 @property
77 def type(self):
78 """
79 Returns if the host is running in full virt mode or
80 if it is running in a paravirtualized environment.
81 """
82 if not self.virtual:
83 return None
84
0cf29180
MT
85 if self.__info["virtype"]:
86 return self.__info["virtype"]
87
a7409949 88 if self.vendor in ("Qemu", "KVM", "VirtualBox", "VMWare"):
0cf29180
MT
89 return "full"
90
91 return "unknown"
715ba5ac
MT
92
93 @property
94 def virtual(self):
95 """
96 Returns true if the host is running in a virtual environment.
97 Otherwise: false.
98 """
f54ce566 99 return _fireinfo.is_virtualized() or \
5060bde8 100 "hypervisor" in self.system.cpu.flags or \
2dc5f0d0 101 self.__is_hypervisor_virtualbox() or \
f2bc7d30 102 self.__is_hypervisor_vmware() or \
a79b7395
MT
103 self.__is_hypervisor_qemu() or \
104 self.__is_hypervisor_microsoft()
2dc5f0d0
MT
105
106 def __is_hypervisor_virtualbox(self):
107 """
108 Check for virtualbox hypervisor by comparing the bios vendor string
109 to "innotek GmbH".
110 """
111 return self.system.bios_vendor == "innotek GmbH"
112
113 def __is_hypervisor_vmware(self):
114 """
115 Check for the VMWare hypervisor by the VMWare Hypervisor port check.
116
117 http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458
118 """
95efe20b 119 if self.system.vendor:
a59811e2
MT
120 return self.system.vendor.startswith("VMware")
121
122 # XXX We should use _fireinfo.vmware_hypervisor_port_check() here, too.
123 # This currently segfaults (and I have no clue why) on VMware player.
715ba5ac 124
f2bc7d30
MT
125 def __is_hypervisor_qemu(self):
126 """
127 Check for old qemu emulator.
128 """
aa77c970
MT
129 if self.system.bios_vendor:
130 return self.system.bios_vendor == "Bochs"
131
132 return False
f2bc7d30 133
a79b7395
MT
134 def __is_hypervisor_microsoft(self):
135 """
136 Check for Microsoft hypervisor.
137 """
aa77c970
MT
138 if self.system.vendor:
139 return "Microsoft" in self.system.vendor
140
141 return False
a79b7395 142
715ba5ac
MT
143
144if __name__ == "__main__":
145 h = Hypervisor()
146
147 print "Vendor:", h.vendor
148 print "Type:", h.type
149 print "Virtual:", h.virtual