]> git.ipfire.org Git - oddments/fireinfo.git/blob - fireinfo/hypervisor.py
83f2a8538fff85d9b97a51a4ef01c0999502980c
[oddments/fireinfo.git] / fireinfo / hypervisor.py
1 #!/usr/bin/python
2
3 import _fireinfo
4 import system
5
6 class Hypervisor(object):
7 def __init__(self):
8 self.__info = _fireinfo.get_hypervisor()
9
10 @property
11 def system(self):
12 """
13 Return the current instance of the System class.
14
15 We need to do that as a property because otherwise
16 we get a recursion.
17 """
18 return system.System()
19
20 @property
21 def vendor(self):
22 """
23 Returns the name of the hypervisor vendor.
24 """
25 if not self.virtual:
26 return None
27
28 # Some of the hypervisors can be detected in a right way.
29 # We can return them at this place.
30 if self.__info["hypervisor"] in ("Xen", "VMWare", "KVM"):
31 return self.__info["hypervisor"]
32
33 # Citrix Xen says it is Microsoft Hv.
34 if self.__info["hypervisor"] == "Microsoft" and \
35 self.system.bios_vendor == "Xen":
36 return "Xen"
37
38 if not self.__info["hypervisor"]:
39 # On VMWare systems, the bios vendor string contains "VMWare".
40 if self.__is_hypervisor_vmware():
41 return "VMWare"
42
43 # VirtualBox got "innotek GmbH" as bios vendor.
44 elif self.__is_hypervisor_virtualbox():
45 return "VirtualBox"
46
47 return "unknown"
48
49 @property
50 def type(self):
51 """
52 Returns if the host is running in full virt mode or
53 if it is running in a paravirtualized environment.
54 """
55 if not self.virtual:
56 return None
57
58 return self.__info["virtype"]
59
60 @property
61 def virtual(self):
62 """
63 Returns true if the host is running in a virtual environment.
64 Otherwise: false.
65 """
66 return _fireinfo.is_virtualized() or \
67 "hypervisor" in self.system.cpu.flags or \
68 self.__is_hypervisor_virtualbox() or \
69 self.__is_hypervisor_vmware()
70
71 def __is_hypervisor_virtualbox(self):
72 """
73 Check for virtualbox hypervisor by comparing the bios vendor string
74 to "innotek GmbH".
75 """
76 return self.system.bios_vendor == "innotek GmbH"
77
78 def __is_hypervisor_vmware(self):
79 """
80 Check for the VMWare hypervisor by the VMWare Hypervisor port check.
81
82 http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458
83 """
84 return self.system.bios_vendor.startswith("VMWare-") and \
85 _fireinfo.vmware_hypervisor_port_check()
86
87
88 if __name__ == "__main__":
89 h = Hypervisor()
90
91 print "Vendor:", h.vendor
92 print "Type:", h.type
93 print "Virtual:", h.virtual