]> git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/hypervisor.py
9286bd48ab863f92775dac26d400926bd0701886
[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 # Check for Microsoft.
71 elif self.__is_hypervisor_microsoft():
72 return "Microsoft"
73
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
85 if self.__info["virtype"]:
86 return self.__info["virtype"]
87
88 if self.vendor in ("Qemu", "KVM", "VirtualBox", "VMWare"):
89 return "full"
90
91 return "unknown"
92
93 @property
94 def virtual(self):
95 """
96 Returns true if the host is running in a virtual environment.
97 Otherwise: false.
98 """
99 return _fireinfo.is_virtualized() or \
100 "hypervisor" in self.system.cpu.flags or \
101 self.__is_hypervisor_virtualbox() or \
102 self.__is_hypervisor_vmware() or \
103 self.__is_hypervisor_qemu() or \
104 self.__is_hypervisor_microsoft()
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 """
119 if self.system.vendor:
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.
124
125 def __is_hypervisor_qemu(self):
126 """
127 Check for old qemu emulator.
128 """
129 return self.system.bios_vendor == "Bochs"
130
131 def __is_hypervisor_microsoft(self):
132 """
133 Check for Microsoft hypervisor.
134 """
135 return "Microsoft" in self.system.vendor
136
137
138 if __name__ == "__main__":
139 h = Hypervisor()
140
141 print "Vendor:", h.vendor
142 print "Type:", h.type
143 print "Virtual:", h.virtual