Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 970
Zagorax Avatar asked Oct 13 '12 14:10

Zagorax


3 Answers

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.

like image 63
slayedbylucifer Avatar answered Nov 08 '22 04:11

slayedbylucifer


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

Perl module and everything.

like image 38
Alan B Avatar answered Nov 08 '22 05:11

Alan B


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

like image 1
nneonneo Avatar answered Nov 08 '22 05:11

nneonneo