How do I detect Xen in a Python script?
Asked Answered
D

7

5

I need to determine when my Python script is running in a Xen virtual machine. The VM will be running Linux.

I can't find anything obvious in the platform module. The closest I can get is the appearance of 'xen' in platform.platform()

>>> platform.platform()
'Linux-2.6.18-194.el5xen-x86_64-with-redhat-5.5-Final'

What is the best way to determine this?

Thanks.

Dye answered 2/12, 2010 at 19:4 Comment(0)
D
7

FYI, if its a paravirtual VM, there should be a /proc/xen/capabilities file. If its contents is "control_d" then, you are running under dom0 else , you are running on a domU.
DONT rely on the kernel version. If the VM is compiled with a custom kernel or a different kernel version or even a modern day PV-ops kernel (which has no "xen" string in it, unlike REDHAT's kernel), then your code wont work.

On the other hand, there are other nifty tricks. cpuid instruction is one such example. I dont know how to do it in python, but if you set eax to 1 and call cpuid, bit 31 of ECX would have the answer. If its set, you are running on a hypervisor. Else, you are not. But this works only for 64bit platforms.

Duty answered 4/12, 2010 at 1:35 Comment(1)
The /proc/xen/capabilities file is what I'm looking for. If it's not there, it's not Xen. If it's there but empty, it's a domU. If it's there and contains 'control_d', then it's dom0. Thanks!Dye
P
1

Your can call xen-detect command, which is written in C.

Protonema answered 18/4, 2012 at 6:1 Comment(0)
P
1

virt-what: http://people.redhat.com/~rjones/virt-what/

virt-what is a shell script which can be used to detect if the program is running in a virtual machine.

virt-what supports a very large number of different hypervisor types, including common open source hypervisors (KVM, Xen, QEMU, VirtualBox), mainframe systems like IBM Systemz, LPAR, z/VM, hardware partitioning schemes like Hitachi Virtage, proprietary hypervisors like VMWare, Microsoft Hyper-V and much more.

Putscher answered 6/5, 2012 at 22:53 Comment(0)
L
0

Can you depend on platform.platform()? I don't know. If you can and it works everytime:

>>> output = 'Linux-2.6.18-194.el5xen-x86_64-with-redhat-5.5-Final'
>>> if 'xen' in output:
      print 'Xen found'

Xen found

There is more than one method of doing this. Which one you want to follow depends on you. Have a look at this question here on SO, which answers this question only. Now your task is implementing this in Python, which might involve calling some external process and checking the output. Is it possible? Yes.

Lynelllynelle answered 2/12, 2010 at 19:25 Comment(0)
A
0

Some systems don't make difference in "normal" kernel and kernel for Xen DomU, such as Fedora. It's not always reliable to use kernel's name to detect whether the system is running on top of Xen.

One possible method is to check the kernel booting message and grep xen:

dmesg | grep xen

Alexandrina answered 12/12, 2010 at 15:29 Comment(0)
A
0

For paravirtualized VM, use this:

ps auwx | egrep -c '\[xenbus\]$'

If return value is 1, it is a xen paravirtualized guest. Otherwise, it is not.

Amplify answered 30/1, 2012 at 17:6 Comment(0)
R
-1
import re, platform

def is_xen():
    return bool(re.search('xen', platform.platform()))
Rancor answered 2/12, 2010 at 19:8 Comment(3)
if 'xen' in platform.platform() should suffice.Lynelllynelle
But re is fun :D However, yes, in, will suffice.Rancor
re is no fun for me! But if this is the best way to do it, then I'll gladly use it. Thanks.Dye

© 2022 - 2024 — McMap. All rights reserved.