user175104
user175104

Reputation: 3696

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?

Upvotes: 2

Views: 3626

Answers (8)

Freddo
Freddo

Reputation: 1

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.

Upvotes: 0

Narf
Narf

Reputation: 11

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

Upvotes: 1

user406905
user406905

Reputation: 1418

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

Upvotes: 0

Pavlo Svirin
Pavlo Svirin

Reputation: 508

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.

Upvotes: 0

caf
caf

Reputation: 239361

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?

Upvotes: 2

Matt
Matt

Reputation: 10564

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.

Upvotes: 1

ptman
ptman

Reputation: 917

facter and imvirt will both detect some virtualizations

Upvotes: 3

Epsilon Prime
Epsilon Prime

Reputation: 4586

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.

Upvotes: 0

Related Questions