Search code examples
bashvirtualbox

Execute Command after VM Booted


I've gotten sick of how many steps it takes me to get started in the morning. Yes it only takes me a few minutes to start up my whole environment, but I'd really rather just run a single command on boot-up and be ready to go immediately.

I'm writing an app on Rails connected to SqlServer. To develop for it I have a local version of the DB I use on a VM. My manual process goes like this:

Run VirtualBox.
Start the VM.
     When the VM is done booting:
          Open terminal
          Run `rails s`
          When rails is done starting:
                 open browser
                 navigate to localhost:3000 and start developing
Run Sublime

I'd love to do this in one script:

VirtualBox Windows7 &
sublime &
google-chrome &

But I can't figure out how to run this command only once the VM is done booting:

gnome-terminal --working-directory=git/my_project --tab -e 'rails s' --tab -e 'git status'

Also, it'd be nice (but not necessary) to have chrome start after rails s has succeeded.

Is this even possible?

I'm not opposed to polling, but it feels like this is something VirtualBox should be able to do a bit more naturally.

EDIT

From Comment:

I'm using Host-Only network with two Bridged Interfaces (one for wireless and one for wired) available. (It allows me to use the VM whether or not I'm connected to a network, and lets me freely switch between wired and wireless without noticing the difference).


Solution

  • Here is how I would do:

    1. In the VM, create a script which will find the default gateway, & keep pinging to it. & add it to user's startup. (needs parsing of ipconfig /all which can be done with vbscript/python.)
    2. In host, look at the network interface between host & VM. Find the default gateway on host (parse route -n output in bash script). Since both use same physical interface, the gateway would be same (assuming NAT & ONE physical interface). Use tcpdump, to wait for the ping packets to the gateway.

    "Default gateway" was chosen because that was something host & VM can find out independent of each other. Other alternative was to hard-code host's address.

    After the host tcpdump on host exits, it means that the VM is alive & booted upto windows desktop.