简体   繁体   English

如何在Python脚本中检测Xen?

[英]How do I detect Xen in a Python script?

I need to determine when my Python script is running in a Xen virtual machine. 我需要确定我的Python脚本何时在Xen虚拟机中运行。 The VM will be running Linux. VM将运行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()中“ xen”的外观

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

What is the best way to determine this? 确定此的最佳方法是什么?

Thanks. 谢谢。

FYI, if its a paravirtual VM, there should be a /proc/xen/capabilities file. 仅供参考,如果它是半虚拟机,则应该有一个/ proc / xen / capabilities文件。 If its contents is "control_d" then, you are running under dom0 else , you are running on a domU. 如果其内容为“ control_d”,则说明您正在dom0 else下运行,而您则在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. 如果VM是使用自定义内核或其他内核版本甚至是现代的PV-ops内核(与REDHAT的内核不同,其中没有“ xen”字符串)编译的,则您的代码将无法工作。

On the other hand, there are other nifty tricks. 另一方面,还有其他一些妙招。 cpuid instruction is one such example. cpuid指令就是这样一个例子。 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. 我不知道如何在python中执行此操作,但是如果将eax设置为1并调用cpuid,则ECX的第31位将具有答案。 If its set, you are running on a hypervisor. 如果已设置,则说明您正在虚拟机监控程序上运行。 Else, you are not. 否则,您不是。 But this works only for 64bit platforms. 但这仅适用于64位平台。

virt-what: http://people.redhat.com/~rjones/virt-what/ 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是一个Shell脚本,可用于检测程序是否在虚拟机中运行。

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. virt-what支持大量不同的管理程序类型,包括常见的开源管理程序(KVM,Xen,QEMU,VirtualBox),大型机系统(如IBM Systemz,LPAR,z / VM),硬件分区方案(如Hitachi Virtage),专有的管理程序(如) VMWare,Microsoft Hyper-V等。

您可以调用用C编写的xen-detect命令。

Can you depend on platform.platform() ? 您可以依赖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. 看看这个问题, 这里的SO,只回答了这个问题。 Now your task is implementing this in Python, which might involve calling some external process and checking the output. 现在您的任务是在Python中实现此操作,这可能涉及调用一些外部进程并检查输出。 Is it possible? 可能吗? Yes. 是。

Some systems don't make difference in "normal" kernel and kernel for Xen DomU, such as Fedora. 某些系统在Fedora等Xen DomU的“常规”内核和内核中没有区别。 It's not always reliable to use kernel's name to detect whether the system is running on top of Xen. 使用内核名称来检测系统是否在Xen之上运行并不总是可靠的。

One possible method is to check the kernel booting message and grep xen: 一种可能的方法是检查内核启动消息和grep xen:

dmesg | dmesg | grep xen grep xen

For paravirtualized VM, use this: 对于半虚拟化的VM,请使用以下命令:

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

If return value is 1, it is a xen paravirtualized guest. 如果返回值为1,则它是xen半虚拟化来宾。 Otherwise, it is not. 否则,事实并非如此。

import re, platform

def is_xen():
    return bool(re.search('xen', platform.platform()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM