How to determine whether Linux is running on a virtual machine

  

Just determine OpenVZ/Xen PV/UML

It is easiest to judge OpenVZ/Xen PV/UML. Check the relevant directories and files under /proc directly. You know, for example, the /proc/vz file on OpenVZ VPS; the /proc/xen/directory on the Xen PV virtual machine, and some things in the directory; /proc/cpuinfo on UML will find the UML flag. I wrote a simple Python script to detect: #!/usr/bin/python# check if a linux system running on a virtual machine (openvz/xen pv/uml)

import sys, os

def main():if os.getuid() != 0:print "must be run as root"sys.exit(0)

# check OpenVZ/Virtuozzoif os.path.exists( "/proc/vz"):if not os.path.exists("/proc/bc"):print "openvz container"else:print "openvz node"

# check Xenif os .path.exists("/proc/xen/capabilities"):if (os.path.getsize("/proc/xen/capabilities") > 0):print "xen dom0"else:print " Xen domU"

# check User Mode Linux (UML)f = open("/proc/cpuinfo", "r"); t = f.read(); f.close()if ( T.find("UML") > 0):print "uml"

if __name__=="__main__":main()

Judging VMware/Xen HVM/KVM

If you are using a virtual virtual such as VMware/Xen HVM/KVM, it is more difficult to judge. The most accurate way is to read the CPUID to judge. The Xen source code has a section below to check if Xen's C code is tools/misc/xen-detect.c. This code provides a good example. VPSee rewrites the code, replaces the function with a macro, and adds a pair. The identification of VMware and KVM, compiled with gcc, can be run: /** check if a linux system running on a virtual machine (vmware/xen hvm/kvm)*/#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\ ");} else if (strncmp(string, "VMwareVMware", 12) == 0) {printf("vmware\ ");} else if (strncmp(string, " KVMKVMKVM", 12) == 0) {printf("kvm\ ");} elseprintf("bare hardware\ ");

return 0;}

Judging VirtualBox /Virtual PC

What? This kind of home desktop virtual machine will not know what to install? ! If you don't know, there is a way to run the dmidecode tool under Linux and look for Manufacturer: innotek GmbH, Manufacturer: Microsoft Corporation keywords to correspond to VirtualBox and Virtual PC.

Copyright © Windows knowledge All Rights Reserved