Page 1 of 1

[Solved] How to determine if this script is running under VM

Posted: 16. Aug 2008, 23:37
by devsk
I have a script which needs to do A depending if its invoked inside a VM and do B otherwise.

Is there a way to do this using virtualbox scripts/binaries? vmware has vmware-checkvm which does this.

Posted: 17. Aug 2008, 00:13
by Sasquatch
Could you give more information about this feature? If you want to run a script, and depending if it runs on your VM or your Guest it executes a certain command, you might want to do a certain hardware check or other check that you put on the Guest and Host and run it through an if statement.

Posted: 17. Aug 2008, 00:44
by devsk
There is a script which I run during init of linux. This script can run during normal native boot or run inside the VM using vritualbox. I just want to do different things in this script depending upon whether I am running inside VM or running native.

For example, xorg.conf is different for a native boot and a boot inside VM. I just want to be able to flip the xorg.conf to virtualbox specific xorg.conf if its inside VM.

Posted: 17. Aug 2008, 01:08
by TerryE
The VM has some is a different machine to the physical one so it is quite easy to detect variations in configuration. A good example is if you have GA installed then you can do an existence check on /dev/vboxadd, but even if you don't want to rely on GA being installed you can still probe /proc for things like grep /proc/cpuinfo pae or one of the flags that VBox always turns off (see the Vbox.log).

Anyway that's my suggestion for two cents.

Posted: 17. Aug 2008, 01:24
by chungy
I'm not sure if there is any special command to check 100% if it's under VirtualBox or not. The Guest Additions, for example, install as normal on a real system (just tested), so they definately don't do any checks.

How to determine if this script is running under VM

Posted: 17. Aug 2008, 01:43
by greenpossum
You could check the model of the hard disk, if your config has one, for example
cat /sys/block/sda/device/model
gives
VBOX HARDDISK

Posted: 17. Aug 2008, 02:32
by TerryE
Nice one, though that will fail if sda is not connected. You might want to do a test based on grepping sd*

Posted: 17. Aug 2008, 04:23
by devsk
based upon ideas from here, I think best check is probably for 'lspci|grep -i vga|grep -i virtualbox'

Can someone please tell me why does copy-paste with mouse not work from guest-to-host and vice versa? I just realized this when I was trying to copy paste that lspci command from guest to this text box....:-)

GA 1.6.4. is running in the guest.

Posted: 17. Aug 2008, 10:58
by TerryE
I think that your suggestion of using the output of the lspci enumerator is a good one, and I would just search for "VirtualBox Graphics Adapter" giving a test:

Code: Select all

if [ `lspci | grep -c "VirtualBox Graphics Adapter"` -eq "1" ]; then ...
As to your supplementary see Forum Posting Guide: one topic one Q. Has a scan: a common Q. :-)

Posted: 17. Aug 2008, 11:17
by devsk
TerryE wrote:I think that your suggestion of using the output of the lspci enumerator is a good one, and I would just search for "VirtualBox Graphics Adapter" giving a test:

Code: Select all

if [ `lspci | grep -c "VirtualBox Graphics Adapter"` -eq "1" ]; then ...
As to your supplementary see Forum Posting Guide: one topic one Q. Has a scan: a common Q. :-)
those muscles are going to ache...ease up a little, will ya.... :)

yeah, I will search. But if I was you and I knew the answer, I would write it anyway. We are all here to help each other and reduce pain, right?