3

Is there any portable way or library to check if Python script is running on a virtualized OS and also which virtualization platform it's running on.

This Detect virtualized OS from an application? questions discusses a c version.

Community
  • 1
  • 1
user
  • 5,335
  • 7
  • 47
  • 63
  • 7
    Not if the Virtual environment is really good - that is after all the idea! – Steve Barnes Nov 19 '13 at 06:13
  • What are you hoping to accomplish? There may be another way to solve the problem. – mipadi Nov 19 '13 at 06:21
  • 3
    @SteveBarnes actually more times then not, its important to know if you are on a virtualized system or not, and which virtual environment (you may need to install virtio drivers, or guest additions). Hiding that fact is useless. Most virtualization systems will leave a hint, be it in the way it advertises the CPU, or in the DMI data (system vendor name, chassis, etc). There is no module that I know of (I may be wrong). There are not that many virtualization technologies out there. Each has its own way of advertising itself. I think you will have to write a module yourself though :). – Gabriel Samfira Nov 19 '13 at 07:10

2 Answers2

5

I think you call linux command virt-what in python. The descriptio of virt-what is here: http://people.redhat.com/~rjones/virt-what/

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
  • Assuming that's on your box. Neat though, I didn't know about this program! – Michael Nov 19 '13 at 08:07
  • Is it available on all Linux platforms or just RHEL? – user Nov 19 '13 at 08:17
  • 1
    @user virt-what is already packaged in Fedora (13+), Red Hat Enterprise Linux (5.7+ and 6.1+), Debian, Ubuntu, ArchLinux and Gentoo, and it can be compiled from source on just about any Linux. – BlackMamba Nov 19 '13 at 08:18
0

To my knowledge, there is no nice, portable way to figure this out from Python. Most of the time, the way people try to figure out if they're being virtualized or not is looking for clues left by the VM -- either some instruction isn't quite right, or some behavior is off.

However, all might not be lost if you're willing to go off box. When you're in a VM, you will almost never have perfect native performance. Thus, if you make enough measurements against a server you trust, then it might be possible to detect if you're in a VM. This is especially the case if you're running on a machine with multiple machines. Check your own time, how much time you're getting scheduled, and how much wall time has past (based on an external measurement because you can't trust the local machine). You'll probably have better luck if you can watch how much time has passed on the local machine rather than just inside one process.

Michael
  • 2,181
  • 14
  • 14