如何在 Python 脚本中检测 Xen?
我需要确定我的 Python 脚本何时在 Xen 虚拟机中运行。 VM 将运行 Linux。
我在平台模块中找不到任何明显的东西。我能得到的最接近的是 platform.platform() 中“xen”的外观
>>> platform.platform()
'Linux-2.6.18-194.el5xen-x86_64-with-redhat-5.5-Final'
确定这一点的最佳方法是什么?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
仅供参考,如果它是半虚拟机,则应该有一个 /proc/xen/capability 文件。如果其内容是“control_d”,那么您正在 dom0 下运行,否则,您正在 domU 上运行。
不要依赖内核版本。如果虚拟机是使用自定义内核或不同的内核版本甚至现代的 PV-ops 内核(其中没有“xen”字符串,与 REDHAT 的内核不同)编译的,那么您的代码将无法工作。
另一方面,还有其他一些巧妙的技巧。 cpuid 指令就是这样的一个例子。我不知道如何在Python中做到这一点,但如果你将eax设置为1并调用cpuid,ECX的第31位就会有答案。如果已设置,则您正在虚拟机管理程序上运行。否则,你就不是。但这仅适用于 64 位平台。
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.
您可以调用 xen-detect 命令,该命令是用 C 编写的。
Your can call xen-detect command, which is written in C.
virt-what: http://people.redhat.com/~rjones/virt-what/
virt-what: http://people.redhat.com/~rjones/virt-what/
你可以依赖
platform.platform()
吗?我不知道。如果可以的话,并且每次都有效:有不止一种方法可以做到这一点。您想遵循哪一个取决于您。请在此处查看此问题,它仅回答此问题。现在您的任务是在 Python 中实现这一点,这可能涉及调用一些外部进程并检查输出。是否可以?是的。
Can you depend on
platform.platform()
? I don't know. If you can and it works everytime: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.
有些系统的“正常”内核和 Xen DomU 内核没有区别,例如 Fedora。使用内核名称来检测系统是否运行在 Xen 之上并不总是可靠的。
一种可能的方法是检查内核启动消息并 grep xen:
dmesg | grep xen
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
对于半虚拟化 VM,请使用以下命令:
如果返回值为 1,则它是 xen 半虚拟化来宾。否则,就不是。
For paravirtualized VM, use this:
If return value is 1, it is a xen paravirtualized guest. Otherwise, it is not.