/** * getVirtualization. Potentially not very accurate especially since you can virtualize hypervisors, * kernel module names change frequently, you can load (some of) these modules if you aren't a host/guest, etc. * * @return array('type' => 'guest', 'method' => kvm or vmware or xen or openvz) or array('type' => 'host', 'methods' = ['intel', 'amd']) */ public function getVirtualization() { // Time? if (!empty($this->settings['timer'])) { $t = new Timer('Determining virtualization type'); } // OpenVZ host? if (is_file('/proc/vz/version')) { return array('type' => 'host', 'method' => 'OpenVZ'); } elseif (is_file('/proc/vz/veinfo')) { return array('type' => 'guest', 'method' => 'OpenVZ'); } // Try getting kernel modules $modules = array(); if (preg_match_all('/^(\\S+)/m', Common::getContents('/proc/modules', ''), $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $modules[] = $match[1]; } } // Sometimes /proc/modules is missing what is in this dir on VMs foreach (@glob('/sys/bus/pci/drivers/*') as $name) { $modules[] = basename($name); } // VMware guest. Tested on debian under vmware fusion for mac... if (Common::anyInArray(array('vmw_balloon', 'vmwgfx', 'vmw_vmci'), $modules)) { return array('type' => 'guest', 'method' => 'VMWare'); } // VMware Host! tested on rhel6 running vmware..workstation? if (Common::anyInArray(array('vmnet', 'vmci', 'vmmon'), $modules)) { return array('type' => 'host', 'method' => 'VMWare'); } // Looks like it might be xen... if (Common::anyInArray(array('xenfs', 'xen_gntdev', 'xen_evtchn', 'xen_blkfront', 'xen_netfront'), $modules) || is_dir('/proc/xen')) { // Guest or host? if (Common::anyInArray(array('xen-netback', 'xen_blkback'), $modules) || strpos('control_d', Common::getContents('/proc/xen/capabilities', '')) !== false) { return array('type' => 'host', 'method' => 'Xen'); } else { return array('type' => 'guest', 'method' => 'Xen'); } } // VirtualBox Host! Tested on lucid running vbox.. if (in_array('vboxdrv', $modules)) { return array('type' => 'host', 'method' => 'VirtualBox'); } // VirtualBox Guest! Tested on wheezy under mac vbox if (in_array('vboxguest', $modules)) { return array('type' => 'guest', 'method' => 'VirtualBox'); } // Looks like it might be KVM HOST! if (Common::anyInArray(array('kvm_intel', 'kvm_amd'), $modules)) { return array('type' => 'host', 'method' => 'KVM'); } // Looks like it might be a KVM or QEMU guest! This is a bit lame since Xen can also use virtio but its less likely (?) if (Common::anyInArray(array('virtio', 'virtio_balloon', 'virtio_pci', 'virtio-pci', 'virtio_blk', 'virtio_net'), $modules)) { return array('type' => 'guest', 'method' => 'Qemu/KVM'); } // idk return false; }
/** * @test */ public function anyInArray() { $this->assertTrue(Common::anyInArray(array(1, 2, 3, 4, 5), array(5, 6, 7))); $this->assertFalse(Common::anyInArray(array(8, 9, 10), array(11, 12, 3))); }