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

Discussions related to using VirtualBox on Linux hosts.
Post Reply
devsk
Posts: 47
Joined: 6. Aug 2008, 22:15

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

Post 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.
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Post 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.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
devsk
Posts: 47
Joined: 6. Aug 2008, 22:15

Post 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.
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post 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.
Read the Forum Posting Guide
Google your Q site:VirtualBox.org or search for the answer before posting.
chungy
Volunteer
Posts: 132
Joined: 26. Jan 2008, 10:27

Post 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.
greenpossum
Volunteer
Posts: 98
Joined: 11. Jul 2008, 08:10

How to determine if this script is running under VM

Post 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
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post by TerryE »

Nice one, though that will fail if sda is not connected. You might want to do a test based on grepping sd*
Read the Forum Posting Guide
Google your Q site:VirtualBox.org or search for the answer before posting.
devsk
Posts: 47
Joined: 6. Aug 2008, 22:15

Post 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.
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post 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. :-)
Read the Forum Posting Guide
Google your Q site:VirtualBox.org or search for the answer before posting.
devsk
Posts: 47
Joined: 6. Aug 2008, 22:15

Post 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?
Post Reply