寻找一种好方法来检测我是否在虚拟机(Linux)中运行
在虚拟机中,操作系统提供的实时调度往往不可靠。对于我的应用程序,我希望能够检测我是否在虚拟机上运行(仅限 Linux)。
因此,我正在寻找一种很好的方法来检测(用 C 语言)我是否处于虚拟化环境中。根据所使用的 VM,似乎使用了各种 DMI 和 CPUID 字符串。不过,我主要对通用方式感兴趣。
有人有什么想法吗?
In VMs OS-provided real-time scheduling tends not to be reliable. For my application I'd like to be able to detect whether I am running on a VM or not (Linux-only).
So I am looking for a nice way to detect (in C) whether I am in a virtualized environment. Depending on the VM used there seem to be various DMI and CPUID strings in use. I am primarily interested in a generic way though.
Anyone got any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
facter 和 imvirt 都会检测一些虚拟化
facter and imvirt will both detect some virtualizations
看来您真正想要回答的问题是“实时调度工作不可靠吗?”。那么为什么不编写一个测试来检查那个呢?
It seems that the real question you want answered is "Is real-time scheduling working unreliably?". So why not write a test that checks for that?
我认为你必须启发式地这样做。虚拟化产品的部分目标是让虚拟机实例相信它正在真实的硬件上运行。每个虚拟化产品都会模拟特定的硬件,因此我的解决方案是创建一个库,您可以询问“我在虚拟机上吗”,并在幕后维护一些虚拟机存在证据的搜索。这样,您仍然可以相对隔离于检测虚拟机的具体细节。
I think you're going to have to do this heuristically. Part of the goal of virtualization products is to make the vm instance believe it's running on real hardware. Each virtualization product is going to simulate specific hardware, so my solution would be to make a library that you can ask "am I on a vm" and just maintain under the hood some search for evidence of vm presence. This way you still remain relatively isolated from the nitty gritty of detecting the vm.
您还可以在 scsi 设备中查找 VMware:
可能仅在 VM 上成功
VM 上的示例输出:
真实计算机上的示例输出:
You can also look for VMware in the scsi devices:
will probably succeed only on VMs
example output on VM:
example output on real machine:
查找仅在您位于虚拟机中时才显示的特定设备。例如,标记为“Parallels”或“VMWare”的显示设备可能很好地表明您位于虚拟机中。
当然,这仅适用于您了解的虚拟机,因此不是很通用。
Look for specific devices that only show up while you're in a VM. For instance, a display device marked "Parallels" or "VMWare" might be a good indication that you're in a VM.
Of course this only works for VMs that you know about and thus isn't very generic.
下面是一个代码示例: http://www.codeproject.com/KB/system/ VmDetect.aspx 、http://mark.michaelis.net/Blog/HowToDetectVirtualMachineExecution。 aspx (但这是从 2005 年开始的)
并且在一些杂志中我读到可以使用硬件集检测虚拟机,因为 VM 使用有限的模拟硬件集。
Here is a code example: http://www.codeproject.com/KB/system/VmDetect.aspx , http://mark.michaelis.net/Blog/HowToDetectVirtualMachineExecution.aspx (but this is from year 2005)
And in some magazine I've read that virtual machine can be detected with the hardware set because VM use the limited set of emulated hardware.
虽然不是明确的,但您也可以检查您的接口名称... ifconfig 也会吐出“venet0”而不是“eth0”
,“df”也会透露一些信息:
vmware - /dev/vzfs
citrix/xen - /dev/xvda1
though not definitive, you can also check your interface names... ifconfig would spit out "venet0" rather than "eth0"
also, 'df' will give away some tells:
vmware - /dev/vzfs
citrix/xen - /dev/xvda1
ifconfig 获取 MAC 地址,然后查找供应商代码(google:mac 地址查找)。如果您提前知道使用的虚拟化平台会有所帮助。
ifconfig to get the MAC address and then look up the vendor code (google: mac address lookup). Helps if you know in advance what virtualization platform is used.