Skip to content

Instantly share code, notes, and snippets.

@terrywang
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrywang/9031215 to your computer and use it in GitHub Desktop.
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
/*
* 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