Python script to determine whether Linux runs on a virtual machine, pythonlinux

Source: Internet
Author: User

Python script to determine whether Linux runs on a virtual machine, pythonlinux

On the WebHostingTalk Forum, some foreign profiteers sell virtual machines as independent servers. In last July, a Chinese compatriot was cheated and posted a post on the WHT. The evidence is conclusive, even the service provider admitted that there were 355 replies. HostATree.com, an independent server/VPS provider, boldly sold OpenVZ VPS virtual machines as independent servers, at least one VMWare/KVM/Xen HVM should be created (more difficult to find it a virtual machine ), using OpenVZ containers is also a bully: yesterday, I received an email from a netizen asking me how to determine whether I bought an independent server or a virtual machine. VPSee briefly introduces the identification tips for common virtual technologies (including container technology) on the market.

Determine OpenVZ/Xen PV/UML

It is easiest to judge OpenVZ/Xen PV/UML. You can check the related directories and files in/proc directly. For example, the/proc/vz file will exist in OpenVZ VPS; the/proc/Xen/directory is available on the xen PV virtual machine, and there are some things in the directory. The/proc/cpuinfo file printed on UML will find the UML mark. I wrote a simple Python script for detection:
Copy codeThe 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 ()

Determine VMware/Xen HVM/KVM

If you are using a full virtual machine such as VMware, Xen HVM, or KVM, it will be harder to judge. The most accurate way is to read the CPUID for determination, under the Xen source code, there is a section of C-language code tools/misc/xen-detect.c that checks whether Xen is available. This section of code provides a good example. VPSee overwrites the code and replaces the function with macros, added recognition of VMware and KVM, which can be run after gcc Compilation:
Copy codeThe 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;
}

Determine VirtualBox/Virtual PC

What? This kind of home desktop virtual machine is installed by itself will not know ?! If you do not know, you can also run the dmidecode tool in Linux and then find the Manufacturer: innotek GmbH and Manufacturer: Microsoft Corporation keywords to correspond to VirtualBox 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.