Ubuntu Server - Scripting Virtual Box Machines to Suspend and Restart on Server Reboot

Posted by : Unknown | Saturday, March 12, 2011 | Published in

I run a headless VirtualBox instance of Server 2k3 on my Ubuntu 10.04 Server and I wanted a way to be able to cleanly startup and shutdown the host machine without having to manually control the virtual one. The following is what I've come up with after cobbling together various things found on Google. Particular thanks to Brendan Kidwell on Glump.net for his info on setting up VirtualBox to behave as a service. This guide basically updates and simplifies his how-to.

Throughout this guide USER indicates the user on your system that is part of the virtual box management group and is able to run the 'VBoxManage' commands. The guide assumes that you already have a working virtual machine and so doesn't cover setup or configuration. The following can be applied to any guest, not just a Windows system.

The first thing to do was to create a script for starting your virtual machine.

This example is called server03start.sh:

#Script to start Server03 VBox machine in vrdp mode
su USER -c "VBoxManage startvm Server03 -type vrdp"

Once you have the script you need to make it executable:


The next step is to copy your script to /etc/init.d/ and edit /etc/rc.local to run the script on system boot. Your rc.local file should look like this:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

exit 0

Your system will now start the virtual machine at boot.

Now we need to configure cleanly suspending the machine on system shutdown. Fortunately VirtualBox provides this facility for us.
If you look through /etc/init.d/vboxdrv you will find a section that reads

# enter the following variables in /etc/default/virtualbox:
# SHUTDOWN_USERS="foo bar"
# check for running VMs of user foo and user bar
# SHUTDOWN=poweroff
# SHUTDOWN=acpibutton
# SHUTDOWN=savestate
# select one of these shutdown methods for running VMs

Do "sudo nano /etc/default/virtualbox" to create the required file and then enter the following:


I chose "savestate" as I decided that pausing the virtual machine during reboot was quicker and more efficient than fully shutting it down.

So, there we have it. Your system will now cleanly and quietly deal with your virtual machine during system reboot.

(0) Comments