Python script to determine if Linux is running on a virtual machine

Source: Internet
Author: User
Tags vps
In the Webhostingtalk forum, some foreign businessmen will be virtual machine as a standalone server to sell, last July, when a Chinese fellow was deceived, and in WHT posted denounced, evidence, even the service providers themselves also admitted that the replies up to 355 articles. This standalone server/vps provider hostatree.com incredibly bold OpenVZ VPS such a look on the virtual machine as a standalone server sold, dizzy, at least to get a vmware/kvm/xen HVM it (more difficult to find is a virtual machine), with OpenVZ This kind of container is also too bullying: Yesterday, just received a netizen e-mail asked how to determine whether they bought a standalone server or virtual machine problems. Here Vpsee a brief introduction to the market on the common virtual technology (including container technology) discrimination tips.

Judging Openvz/xen pv/uml

Judge Openvz/xen Pv/uml is the easiest, directly check the relevant directories and files under/proc can be known, such as OpenVZ VPS will have/proc/vz this file, Xen PV virtual machine will have/proc/xen/this directory, and the directory has a The UML logo is found on the UML print/proc/cpuinfo. Wrote a simple Python script to detect:

The code is as follows:


#!/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/virtuozzo
If Os.path.exists ("/proc/vz"):
If not os.path.exists ("/PROC/BC"):
Print "OpenVZ container"
Else
Print "OpenVZ node"

# Check Xen
If 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 the use of Vmware/xen HVM/KVM such a full virtual is more difficult to judge some, the most accurate way is to read the CPUID to determine, the Xen source code below is a detection is Xen C code TOOLS/MISC/XEN-DETECT.C, this code provides A good example of this, Vpsee rewritten the code, replaced the function with a macro, added the recognition of VMware and KVM, and then compiled it with GCC to run:

The code is as follows:


/*
* 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\n");
} else if (strncmp (String, "Vmwarevmware", 12) = = 0) {
printf ("vmware\n");
} else if (strncmp (String, "KVMKVMKVM", 12) = = 0) {
printf ("kvm\n");
} else
printf ("Bare hardware\n");

return 0;
}


Judging virtualbox/virtual PC

What the? This kind of Home desktop virtual machine installed itself will not know?! If you do not know, there is a way to run the Dmidecode tool under Linux and then find Manufacturer:innotek GmbH, the manufacturer:microsoft Corporation keyword can correspond to Virtualb Ox and Virtual PC.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.