4

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.

Martin Del Vecchio
  • 3,558
  • 2
  • 27
  • 36

7 Answers7

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.

Tautology
  • 385
  • 2
  • 9
  • 2
    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! – Martin Del Vecchio Dec 14 '10 at 21:24
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.

Savvas Radevic
  • 543
  • 1
  • 8
  • 23
0

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

feisky
  • 1,863
  • 3
  • 14
  • 16
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.

Community
  • 1
  • 1
user225312
  • 126,773
  • 69
  • 172
  • 181
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

ericzma
  • 763
  • 3
  • 9
  • 23
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.

yl35
  • 111
  • 6
-1
import re, platform

def is_xen():
    return bool(re.search('xen', platform.platform()))
Blue Peppers
  • 3,718
  • 3
  • 22
  • 24