linuxperlvirtualizationdetection

How to detect if the script is running on a virtual machine?


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?


Solution

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