Managing A VirtualBox Guest Using Upstart

Thursday, August 11, 2011

With a Windows 7 guest comfortably installed on my Ubuntu host, I turn my attention to making it automatically boot and shutdown with the host. Traditionally, we would use a SysV-style init script that uses VBoxManage/VBoxHeadless to start the guest and VBoxManage to stop it. Upstart seems to be the way to go now, so let’s make a configuration file for that. And instead of powering off the guest every time (which should be done using ACPI events), it would be easier and faster to simply save the running state and restore it during the next boot.

As before, the VM is named mustard, my username is inportb, and this file is called /etc/init/mustard.conf

description “mustard VM”
author “Jiang”

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [016]

console output

respawn
respawn limit 5 10

pre-stop script
su inportb -c “VBoxManage controlvm mustard savestate”
end script

exec su inportb -c “VBoxHeadless –startvm mustard”

As soon as the local filesystems are ready and the network is up, Upstart would run VBoxHeadless and monitor the process. Should the process die unexpectedly (such as if someone hits the guest’s shutdown button by mistake), Upstart would bring it right back up. Furthermore, if some problem keeps terminating the process soon after starting, Upstart would stop trying after 5 bounces in 10 seconds. The “correct” way to start and stop the guest, respectively, would be through the host…

sudo start mustard
sudo stop mustard

6 Comments

  1. Tom says:

    Mate thanks so much for this well written post. Has saved me a bunch of time, I now have my different machines autostarting on boot, very happy :)

    One word of advice for those that are copying/pasting the code above into a file – if you’re dropping it into a command line editor (eg vi in my case) some of the quotation marks did not copy right – took me a while to get it working as a result…

    Cheers

    Tom

  2. David says:

    Nice post. Worked like a charm, and thanks to Tom for pointing out the copy/paste issue with the quotation marks.

  3. Takagami says:

    I have tried this with no luck. Modified it with no luck. Switched all of my VM’s to be owned by root, with no luck. The VM’s will not start at boot, nor with the service/upstart commands. I am on Ubuntu 10.04.4 and I cannot for the life of me get any VM’s to load at boot. I have tried upstart, SysV, Scripts upon Scripts and nothing seems to work…

  4. Wimma says:

    Thanks for the script (and comment on quotation marks). I think I have entered it correctly, and can sudo start and sudo stop my VM, but it doesn’t autostart on boot – tries 5 times and quits (as per script) with error 1.
    Anyone used this on ubuntu server 12.04 headless?

  5. Jim says:

    Also, the dash is a double dash

Pingbacks & Trackbacks

  1. inportb » Setting Up A VirtualBox Guest On A Headless Linux Host - Pingback on 2011/08/11