# # Module to probe Hardware info from the system # # Copyright (c) 2010 Red Hat, Inc. # # Authors: Pradeep Kilambi # # This software is licensed to you under the GNU General Public License, # version 2 (GPLv2). There is NO WARRANTY for this software, express or # implied, including the implied warranties of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 # along with this software; if not, see # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. # # Red Hat trademarks are not licensed under GPLv2. No permission is # granted to use or replicate Red Hat trademarks that are incorporated # in this software or its documentation. # import os import sys import dmidecode kvm = "kvm" qemu = "qemu" vmware = "vmware" virtualpc = "virtualpc" virtualbox = "virtualbox" openvz = "openvz" uml = "uml" xenhvm = "xen-hvm" xendom0 = "xen-dom0" xendomu = "xen-domU" # The logic was take from virt-what located at # http://people.redhat.com/~rjones/virt-what/ class GuestHardware: def __init__(self): self.cpuid = os.system("/usr/libexec/virt-what-cpuid-helper") self.dmi = { "bios": dmidecode.bios(), "processor": dmidecode.processor(), } def on_vmware(self): return (self.cpuid == "VMwareVMware") or (self._is_manufacturer("VMware")) def on_virtualpc(self): return self._is_manufacturer("Microsoft Corporation") def on_virtualbox(self): return self._is_manufacturer("innotek GmbH") def on_uml(self): return self._grep("/proc/cpuinfo", "UML") def on_xen_hvm(self): return (self.cpuid == "XenVMMXenVMM") def is_xen_dom0(self): xen = False if (os.path.exists("proc/xen/capablities")): xen = self_grep("proc/xen/capablities", control_d) return xen def is_xen_domU(self): xen = False if (os.path.exists("proc/xen/capablities")): xen = not self_grep("proc/xen/capablities", control_d) return xen # Need to make sure not xen before running this since # Xen uses QEMU for its device model def on_kvm(self): kvm = False if not (self.on_xen_hvm() or self.is_xen_dom0() or self.is_xen_domU()): if self._grep("/proc/cpuinfo", "QEMU"): kvm = self.cpuid == "KVMKVMKVM" return kvm # Need to make sure not xen before running this since # Xen uses QEMU for its device model def on_qemu(self): qemu = False if not (self.on_xen_hvm() or self.is_xen_dom0() or self.is_xen_domU()): if self._grep("/proc/cpuinfo", "QEMU"): qemu = not self.cpuid == "KVMKVMKVM" return qemu # /proc/vz - always exists if OpenVZ kernel is running (inside and outside # container) # /proc/bc - exists on node, but not inside container. def on_openvz(self): return (os.path.exists("/proc/vz")) and not (os.path.exists("/proc/bc")) def hypervisor_type(self): if self.on_vmware(): return vmware if self.on_virtualpc(): return virtualpc if self.on_virtualbox(): return virtualbox if self.on_openvz(): return openvz if self.on_uml(): return uml if self.on_xen_hvm(): return xenhvm if self.is_xen_domU(): return xendom0 if self.on_kvm(): return kvm if self.on_qemu(): return qemu return None def _is_manufacturer(self, mf): found = False for key, value in self.dmi["processor"].items(): if mf == value["data"]["Manufacturer"]["Vendor"]: found = True break return found def _grep(self, fname, data): found = False if (os.path.exists(fname)): for line in open(fname): if data in line: found = True break return found if __name__ == '__main__': g = GuestHardware() print "On VMWare: %i" % g.on_vmware() print "On VirtualPC: %i" % g.on_vmware() print "On Virtualbox: %i" % g.on_virtualbox() print "On Openvz: %i" % g.on_openvz() print "On UML: %i" % g.on_uml() print "On XenHVM: %i" % g.on_xen_hvm() print "Is XenDom0: %i" % g.is_xen_dom0() print "Is XenDomU: %i" % g.is_xen_domU() print "On Qemu: %i" % g.on_qemu() print "On KVM: %i" % g.on_kvm()