简体   繁体   English

寻找一种检测我是否在虚拟机(Linux)中运行的好方法

[英]Looking for a nice way to detect if I am running in a Virtual Machine (Linux)

In VMs OS-provided real-time scheduling tends not to be reliable. 在VM中,OS提供的实时调度往往不可靠。 For my application I'd like to be able to detect whether I am running on a VM or not (Linux-only). 对于我的应用程序,我希望能够检测我是否在VM上运行(仅Linux)。

So I am looking for a nice way to detect (in C) whether I am in a virtualized environment. 因此,我正在寻找一种检测(在C语言中)是否处于虚拟化环境中的好方法。 Depending on the VM used there seem to be various DMI and CPUID strings in use. 根据使用的VM,似乎使用了各种DMI和CPUID字符串。 I am primarily interested in a generic way though. 我主要对通用方法感兴趣。

Anyone got any ideas? 有人有任何想法吗?

Facter和Imvirt都会检测到一些虚拟化

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 ? 那么,为什么不写一个测试,检查是否存在

You can also look for VMware in the scsi devices: 您也可以在scsi设备中寻找VMware:

cat /proc/scsi/scsi | grep VMware

will probably succeed only on VMs 可能只会在虚拟机上成功


example output on VM: VM上的示例输出:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: VMware   Model: Virtual disk     Rev: 1.0
  Type:   Direct-Access                    ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
  Vendor: VMware   Model: Virtual disk     Rev: 1.0
  Type:   Direct-Access                    ANSI SCSI revision: 02

example output on real machine: 实际机器上的示例输出:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: TSSTcorp Model: CDRW/DVD TSL462D Rev: DE01
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi6 Channel: 00 Id: 08 Lun: 00
  Vendor: DP       Model: BACKPLANE        Rev: 1.05
  Type:   Enclosure                        ANSI  SCSI revision: 05
Host: scsi6 Channel: 02 Id: 00 Lun: 00
  Vendor: DELL     Model: PERC 5/i         Rev: 1.03
  Type:   Direct-Access                    ANSI  SCSI revision: 05

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. 虚拟化产品的部分目标是使vm实例相信它在真实硬件上运行。 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. 这样,您仍然相对远离检测虚拟机的烦恼。

Look for specific devices that only show up while you're in a VM. 查找仅在VM中显示的特定设备。 For instance, a display device marked "Parallels" or "VMWare" might be a good indication that you're in a VM. 例如,标记为“ Parallels”或“ VMWare”的显示设备可能很好地表明您在VM中。

Of course this only works for VMs that you know about and thus isn't very generic. 当然,这仅适用于您了解的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) 这是一个代码示例: http : //www.codeproject.com/KB/system/VmDetect.aspx,http : //mark.michaelis.net/Blog/HowToDetectVirtualMachineExecution.aspx (但这是从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. 在某些杂志中,我读到可以用硬件设置检测虚拟机,因为VM使用的仿真硬件数量有限。

ifconfig to get the MAC address and then look up the vendor code (google: mac address lookup). ifconfig获取MAC地址,然后查找供应商代码(google:mac地址查找)。 Helps if you know in advance what virtualization platform is used. 如果您提前知道使用了哪种虚拟化平台,将对您有所帮助。

though not definitive, you can also check your interface names... ifconfig would spit out "venet0" rather than "eth0" 尽管不是确定的,但是您也可以检查您的接口名称... ifconfig会吐出“ venet0”而不是“ eth0”

also, 'df' will give away some tells: vmware - /dev/vzfs citrix/xen - /dev/xvda1 同样,“ df”也会泄露一些信息:vmware-/ dev / vzfs citrix / xen-/ dev / xvda1

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

相关问题 如何从主机上的Eclipse调试器远程调试在Debian虚拟机上运行的某些代码? - How can I debug remotely some code running on a Debian Virtual Machine from Eclipse debugger on host machine? 在Ubuntu上的虚拟机上学习Linux内核编程? - Learning Linux Kernel programming on a virtual machine on Ubuntu? 寻找一种强制在linux中进行简短读取的方法 - Looking for a way to force a short read in linux C-检查系统是否为虚拟机的简便方法? - C - Convenient way to check if system is a virtual machine? 检测Linux上正在运行的进程的内存泄漏 - Detect memory leak for a running process on Linux 在编译时和/或运行时检测正在运行的Linux发行版? - Detect the running Linux distribution at compile time and/or runtime? 我正在寻找内容寻址数据结构 - I am looking for a content addressing data structure 在虚拟机上运行的 C 中动态分配的数组更改内容 - Dynamically allocated array changing contents unwantedly in C running on virtual machine 寻找一种在运行时检测valgrind / memcheck而不包括valgrind头的方法 - Looking for a way to detect valgrind/memcheck at runtime without including valgrind headers C-终止的好方法 - C - Nice way to terminate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM