diff options
Diffstat (limited to 'src/hypervisor_vendor.c')
-rw-r--r-- | src/hypervisor_vendor.c | 96 |
1 files changed, 80 insertions, 16 deletions
diff --git a/src/hypervisor_vendor.c b/src/hypervisor_vendor.c index bfa78c21..eade6c66 100644 --- a/src/hypervisor_vendor.c +++ b/src/hypervisor_vendor.c @@ -1,7 +1,29 @@ /* * Identify hypervisor vendor * - * based on code from util-linux lscpu + * This is based on code from lscpu and virt-what. Unfortunately, neither + * of those is sufficient. lscpu doesn't detect many VM's, + * and virt-what is a shell script that has to be run as root. + * + * **** License **** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * # + * A copy of the GNU General Public License is available as + * `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution + * or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'. + * You can also obtain it by writing to the Free Software Foundation, + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * Portions created by Vyatta are Copyright (C) 2011 Vyatta, Inc. + * All Rights Reserved. */ #include <stdio.h> @@ -11,6 +33,8 @@ #define PROC_XEN "/proc/xen" #define PROC_XENCAP PROC_XEN "/capabilities" #define PROC_PCIDEVS "/proc/bus/pci/devices" +#define SYS_HYPERVISOR "/sys/hypervisor/type" +#define SYS_DMI_VENDOR "/sys/class/dmi/id/sys_vendor" /* * This CPUID leaf returns the information about the hypervisor. @@ -41,7 +65,12 @@ cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, : "1" (op), "c"(0)); } -static const char *get_hypervisor(void) + +/* Use CPUID instruction to find hypervisor vendor. + * This is the preferred method, but doesn't work with older + * hypervisors. + */ +static const char *get_hypervisor_cpuid(void) { unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0; char hyper_vendor_id[13]; @@ -58,7 +87,7 @@ static const char *get_hypervisor(void) return NULL; else if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12)) - return "Xen"; + return "Xen hvm"; else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9)) return "KVM"; else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12)) @@ -69,6 +98,34 @@ static const char *get_hypervisor(void) return NULL; } +/* Use DMI vendor information */ +static const char *get_hypervisor_dmi(void) +{ + FILE *f = fopen(SYS_DMI_VENDOR, "r"); + char vendor_id[128]; + + if (!f) + return NULL; + + if (fgets(vendor_id, sizeof(vendor_id), f) == NULL) { + fclose(f); + return NULL; + } + fclose(f); + + if (!strncmp(vendor_id, "VMware", 6)) + return "VMware"; + /* Note: Hyper-V has same DMI, but different CPUID */ + else if (!strncmp(vendor_id, "Microsoft Corporation", 21)) + return "VirtualPC"; + else if (!strncmp(vendor_id, "innotek GmbH", 12)) + return "VirtualBox"; + else if (!strncmp(vendor_id, "Parallels", 9)) + return "Parallels"; + else + return NULL; +} + static int has_pci_device(int vendor, int device) { @@ -98,27 +155,34 @@ found: int main(int argc, char **argv) { - const char *hvm = get_hypervisor(); + FILE *f; + const char *hvm; + char buf[256]; + hvm = get_hypervisor_cpuid(); + if (hvm) + printf("%s\n", hvm); + + hvm = get_hypervisor_dmi(); if (hvm) printf("%s\n", hvm); /* Grotty code to look for old Xen */ - else if (access(PROC_XEN, F_OK) == 0) { - FILE *fd = fopen(PROC_XENCAP, "r"); + else if ((f = fopen(PROC_XENCAP, "r")) != NULL) { int dom0 = 0; - if (fd) { - char buf[256]; - - if (fscanf(fd, "%s", buf) == 1 && - !strcmp(buf, "control_d")) - dom0 = 1; - fclose(fd); - } - printf("Xen %s\n", dom0 ? "none" : "para"); + if (fscanf(f, "%s", buf) == 1 && + !strcmp(buf, "control_d")) + dom0 = 1; + printf("Xen %s\n", dom0 ? "dom0" : "domU"); + fclose(f); + } + else if ((f = fopen(SYS_HYPERVISOR, "r")) != NULL) { + if (fgets(buf, sizeof(buf), f) != NULL + && !strncmp(buf, "xen", 3)) + printf("Xen\n"); + fclose(f); } - else if (has_pci_device(0x5853, 0x0001)) { /* Xen full-virt on non-x86_64 */ printf("Xen full\n"); |