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

Source: Internet
Author: User
Tags vps python script

In the Webhostingtalk forum some foreign profiteers will be the virtual machine as a stand-alone server to sell, last July when there was a Chinese compatriots deceived, and posted on the WHT, the evidence, even the service providers themselves also admit that the replies reached 355. This independent server/VPS provider hostatree.com incredibly bold to OpenVZ VPS such a look at the virtual machine as a stand-alone server to sell, Halo, at least to get a vmware/kvm/xen HVM bar (more difficult to find is a virtual machine), with OpenVZ This kind of container is too bully: Yesterday happened to receive an e-mail from a netizen asked how to determine whether they bought a stand-alone server or virtual machine problem. Here Vpsee a simple introduction of the common virtual technology (including container technology) in the market to identify small skills.

Judge Openvz/xen Pv/uml

Judge Openvz/xen Pv/uml is the easiest, directly check the related directories and files under/proc can know, 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 something; on UML the print/proc/cpuinfo will find the UML flags. Wrote a simple Python script to detect:

Copy Code code 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 ()


Judge Vmware/xen HVM/KVM

If the use of Vmware/xen HVM/KVM such a full virtual more difficult to judge some, the most accurate way is to read the CPUID to judge, Xen source code below a section of detection is the Xen code TOOLS/MISC/XEN-DETECT.C, the Code provides A good example, Vpsee rewrites the code, replaces the function with macros, increases the recognition of VMware and KVM, and then compiles it with GCC to run:

Copy Code code 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;
}


Judge Virtualbox/virtual PC

What the? This kind of Home desktop virtual machine installed on their own will not know?! If you don't know, there are ways to run the Dmidecode tool under Linux and then look for Manufacturer:innotek GmbH, manufacturer:microsoft Corporation keyword can correspond to Virtualb Ox and Virtual PC.

Related Article

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.