]> git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/hypervisor.py
Add license information.
[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 return self.__info["virtype"]
82
83 @property
84 def virtual(self):
85 """
86 Returns true if the host is running in a virtual environment.
87 Otherwise: false.
88 """
89 return _fireinfo.is_virtualized() or \
90 "hypervisor" in self.system.cpu.flags or \
91 self.__is_hypervisor_virtualbox() or \
92 self.__is_hypervisor_vmware() or \
93 self.__is_hypervisor_qemu()
94
95 def __is_hypervisor_virtualbox(self):
96 """
97 Check for virtualbox hypervisor by comparing the bios vendor string
98 to "innotek GmbH".
99 """
100 return self.system.bios_vendor == "innotek GmbH"
101
102 def __is_hypervisor_vmware(self):
103 """
104 Check for the VMWare hypervisor by the VMWare Hypervisor port check.
105
106 http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458
107 """
108 return self.system.bios_vendor.startswith("VMWare-") and \
109 _fireinfo.vmware_hypervisor_port_check()
110
111 def __is_hypervisor_qemu(self):
112 """
113 Check for old qemu emulator.
114 """
115 return self.system.bios_vendor == "Bochs"
116
117
118 if __name__ == "__main__":
119 h = Hypervisor()
120
121 print "Vendor:", h.vendor
122 print "Type:", h.type
123 print "Virtual:", h.virtual