简体   繁体   中英

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. 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?

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:

cat /proc/scsi/scsi | grep VMware

will probably succeed only on VMs


example output on 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. 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. 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.

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 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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