]> git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/hypervisor.py
Fix for VMWare hypervisor detection (crashed on Alix).
[oddments/fireinfo.git] / fireinfo / hypervisor.py
1 #!/usr/bin/python
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 ###############################################################################
21
22 import _fireinfo
23 import system
24
25 class Hypervisor(object):
26 def __init__(self):
27 self.__info = _fireinfo.get_hypervisor()
28
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
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
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"):
50 return self.__info["hypervisor"]
51
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
57 if not self.__info["hypervisor"]:
58 # On VMWare systems, the bios vendor string contains "VMWare".
59 if self.__is_hypervisor_vmware():
60 return "VMWare"
61
62 # VirtualBox got "innotek GmbH" as bios vendor.
63 elif self.__is_hypervisor_virtualbox():
64 return "VirtualBox"
65
66 # Check for qemu.
67 elif self.__is_hypervisor_qemu():
68 return "Qemu"
69
70 return "unknown"
71
72 @property
73 def type(self):
74 """
75 Returns if the host is running in full virt mode or
76 if it is running in a paravirtualized environment.
77 """
78 if not self.virtual:
79 return None
80
81 if self.__info["virtype"]:
82 return self.__info["virtype"]
83
84 if self.hypervisor in ("Qemu", "KVM", "VirtualBox", "VMWare"):
85 return "full"
86
87 return "unknown"
88
89 @property
90 def virtual(self):
91 """
92 Returns true if the host is running in a virtual environment.
93 Otherwise: false.
94 """
95 return _fireinfo.is_virtualized() or \
96 "hypervisor" in self.system.cpu.flags or \
97 self.__is_hypervisor_virtualbox() or \
98 self.__is_hypervisor_vmware() or \
99 self.__is_hypervisor_qemu()
100
101 def __is_hypervisor_virtualbox(self):
102 """
103 Check for virtualbox hypervisor by comparing the bios vendor string
104 to "innotek GmbH".
105 """
106 return self.system.bios_vendor == "innotek GmbH"
107
108 def __is_hypervisor_vmware(self):
109 """
110 Check for the VMWare hypervisor by the VMWare Hypervisor port check.
111
112 http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458
113 """
114 if self.system.bios_vendor:
115 return self.system.bios_vendor.startswith("VMWare-") and \
116 _fireinfo.vmware_hypervisor_port_check()
117
118 def __is_hypervisor_qemu(self):
119 """
120 Check for old qemu emulator.
121 """
122 return self.system.bios_vendor == "Bochs"
123
124
125 if __name__ == "__main__":
126 h = Hypervisor()
127
128 print "Vendor:", h.vendor
129 print "Type:", h.type
130 print "Virtual:", h.virtual