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

允我心安 提交于 2019-12-21 11:36:04

问题


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?


回答1:


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.




回答2:


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

Perl module and everything.




回答3:


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



来源:https://stackoverflow.com/questions/12874288/how-to-detect-if-the-script-is-running-on-a-virtual-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!