9

I don't think the question needs any explanation. I'm writing a software that can boot virtual machines to simulate a network. I would like to disable this feature if the software is already running on a virtual machine.

I already read this post: How to detect if my application is running in a virtual machine? But I need to do it on Linux and that post covers only Windows.

I'm tagging it as a Perl question as I've to do it in Perl, but probably there are some file in proc or somewhere else to check and it's the same for all languages. I don't know actually.

I'm interested in detecting mainly VirtualBox and KVM, as those are the VMs I'm using.

So, any suggestion?

2
  • which virtual machine software are you using? The instructions probably differ for different VMs.
    – nneonneo
    Oct 13, 2012 at 15:02
  • Why do you ask? An ideal VM should not be detectable, since by definition it should behave like a real machine. Oct 13, 2012 at 15:30

4 Answers 4

15

I will talk specific to VMware and virtual Box Virtual Machines running Linux as guest Operating system. If you run below command, you will come to know that the underlying hardware is VMware/VirtualBox which certifies that it is a Virtual Machine.

For VMware guest:

# dmidecode  | grep -i product
        Product Name: VMware Virtual Platform

For Virtual Box guest:

# dmidecode  | grep -i product
    Product Name: VirtualBox

"dmidecode" is a linux system command. You can have perl run dmidecode in the beginning of your script and extract the value. If it is a virtual machine then the script should exit without any further execution.

I do not have any other hypervisor at my disposal to get you what above command return on them.

Hope this helps.

3

A quick Google yields dmo.ca/ blog/ How to detect virtualization

Perl module and everything.

1

Have you tried Sys::Detect::Virtualization, available on CPAN?

0

On a modern Linux system with systemd installed, you can simply run systemd-detect-virt. If it detects it is running in a virtual environment (both VMs and containers) it will print a string indicating the detected environment and exit with 0, otherwise, it will print "none" and exit with a non-zero value. In PERL you could do something like this:

#! /usr/bin/perl -w
if ( system("systemd-detect-virt >& /dev/null") == 0){
    print "Running in a virtual machine\n";
}
else {
    print "No virtualization technologies detected\n";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.