Last active
August 29, 2015 13:56
-
-
Save terrywang/9031215 to your computer and use it in GitHub Desktop.
Check Linux VM hardware assisted virtualization type {Xen HVM,KVM,VMware}. Based on the C program written by http://www.vpsee.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Use CPUID opcode from EAX register to determine Linux VM Virtualization type {Xen HVM,KVM,VMware} | |
* Based on the c program written by http://www.vpsee.com ;-) | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#define HYPERVISOR_INFO 0x40000000 | |
#define CPUID(idx, eax, ebx, ecx, edx) \ | |
asm volatile ( \ | |
"test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid" \ | |
: "=b" (*ebx), "=a" (*eax), "=c" (*ecx), "=d" (*edx) \ | |
: "0" (idx) ); | |
int main(void) | |
{ | |
unsigned int eax, ebx, ecx, edx; | |
char string[13]; | |
CPUID (HYPERVISOR_INFO, &eax, &ebx, &ecx, &edx); | |
*(unsigned int *) (string + 0) = ebx; | |
*(unsigned int *) (string + 4) = ecx; | |
*(unsigned int *) (string + 8) = edx; | |
string[12] = 0; | |
if (strncmp (string, "XenVMMXenVMM", 12) == 0) | |
{ | |
printf ("Xen HVM\n"); | |
} | |
else if (strncmp (string, "VMwareVMware", 12) == 0) | |
{ | |
printf ("VMware\n"); | |
} | |
else if (strncmp (string, "KVMKVMKVM", 12) == 0) | |
{ | |
printf ("KVM\n"); | |
} | |
else | |
printf ("Bare Metal\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment