/** * 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 * * @access private * @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 LinfoTimerStart('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', LinfoCommon::getContents('/proc/modules', ''), $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $modules[] = $match[1]; } } // VMware guest. Tested on debian under vmware fusion for mac... if (LinfoCommon::anyInArray(array('vmw_balloon', 'vmwgfx', 'vmw_vmci'), $modules)) { return array('type' => 'guest', 'method' => 'VMWare'); } // VMware Host! tested on rhel6 running vmware..workstation? if (LinfoCommon::anyInArray(array('vmnet', 'vmci', 'vmmon'), $modules)) { return array('type' => 'host', 'method' => 'VMWare'); } // Looks like it might be xen... if (LinfoCommon::anyInArray(array('xenfs', 'xen_gntdev', 'xen_evtchn', 'xen_blkfront', 'xen_netfront'), $modules) || is_dir('/proc/xen')) { // Guest or host? if (LinfoCommon::anyInArray(array('xen-netback', 'xen_blkback'), $modules) || strpos('control_d', LinfoCommon::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 (in_array('kvm', $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 (LinfoCommon::anyInArray(array('virtio', 'virtio_balloon', 'virtio_pci', 'virtio_blk', 'virtio_net'), $modules)) { return array('type' => 'guest', 'method' => 'Qemu/KVM'); } // idk return false; }
/** * @test */ public function anyInArray() { $this->assertTrue(LinfoCommon::anyInArray(array(1, 2, 3, 4, 5), array(5, 6, 7))); $this->assertFalse(LinfoCommon::anyInArray(array(8, 9, 10), array(11, 12, 3))); }