Setup VirtualBox as a Service in Linux
I run VirtualBox on my office desktop to provide not only a Windows desktop environment for sticky Windows-requiring apps, but also to provide MS SQL Server for my web development stack of JRun, ColdFusion, and MS SQL Server. As such, it would be useful to have an init script that starts and stops VirtualBox automatically when the host system boots up and shuts down. That way, I don’t have to think about manually starting VirtualBox before I can interact with my web development environment, and this is particularly useful if I have to work remotely after calling in to ask my office mate to start my computer.
This guide shows you how to setup a virtual machine hosted by VirtualBox on a Linux host. The guest OS can be anything. The guest will be configured to start or resume when the host boots and hibernate (suspend execution, save to disk, and terminate) when the host shuts down. The guest can be started and stopped from the command prompt as well. I have only tested this setup on Linux Mint 13 (based on Ubuntu 12.04), but it should work fine on any Linux that uses init.d scripts to start and stop services.
Alternate version available:
[Spanish] Crear servicio para VirtualBox — based on this page, by Carlos Gacimartín. Thanks Carlos!
10 Oct 2012: According to the VirtualBox manual, these instructions have been superseded by new built-in functionality as of version 4.2. I haven’t tried the built-in method yet, but it should work better than my instructions on this web page.
Prepare Your VirtualBox Virtual Machine
Install VirtualBox in the usual way. Create your virtual machine. In particular I recommend these settings, but YMMV:
NAT Network Connectivity from Host
The reason I use NAT networking is because I feel it’s easier to manage than any other networking. My network admin doesn’t see any new mysterious MAC and IP addresses appearing on the network, and I know exactly what ports on the virtual machine are exposed to the outside world. If you prefer, you can use bridged networking instead.
Save your virtual machine, start it (in the GUI) and install your guest OS and VirtualBox Guest Additions. It’s important that you install every kind of remote access you’ll need, since the virtual machine’s screen won’t actually reside on your real desktop when you run the virtual machine as a service. I’ve found that VirtualBox’s RDP server has some issues with catching screen updates, so I like to enable Windows’ Remote Desktop server and forward it from an alternate port in VirtualBox’s NAT.
(Aside: Most people wouldn’t think of it for a Windows guest, but I find the SSH server provided by Cygwin quite useful, especially for no-nonsense SFTP file transfers. If you choose to use it, you can copy your host’s/etc/ssh/ssh_host_* to your guest’s Cygwin /etc/ folder and that will prevent SSH clients from complaining about different keys being used on different ports on your host IP address.)
To configure port forwarding in VirtualBox’s NAT, go to Settings for your chosen VM, then Network → Adapter (n) (the one using NAT) → Advanced → Port Forwarding. Here are examples from my setup.
To test all these mapped ports, connect to them on “localhost” when the virtual machine is running:
Other Kinds of Network Connectivity from Host
You have some alternatives for setting up networking.
If you are on a non-mobile computer that is always connected to the same network and it can grab as many IPs from your local gateway as it wants to, you can use Bridged Networking — the host OS and all the guest machines in this configuration look like peers on the local network.
You can also get by with NAT and Host-Only Networking — two network interfaces on the guest machine. VirtualBox will setup a private network in the 192.168.56.x subnet for the Host-Only virtual network, and your host OS and the guests can use that network to talk to each other. The guest can talk to the outside world using the NAT interface.
Be sure edit /etc/hosts on each host so they know what to call each other — or setup static hostnames in your local home/office router or use some other method if you need mnemonic names for your hosts instead of remembering IP addresses.
Create the init.d Script
Now that you’ve got your virtual machine setup and configured and tested, you’re ready to create and configure scripts in /etc/init.d/ to start and suspend the guest on demand.
First, if you look at /etc/init.d/vboxdrv, the service manager script for the VirtualBox kernel driver, you’ll see that it looks for a couple of options to tell it what to do with running virtual machines when vboxdrv stops. Create or edit the config file:
Now, the above settings only cover the system shutdown process, not startup. VirtualBox itself doesn’t provide any mechanism for starting virtual machines automatically. To do that, create a new service manager script of your own.
Old script used to be included here…
Grab a copy of my vbox-service-template init.d script and follow the instructions there to customize it for your VM and install it in /etc/init.d.
Test the init.d Script
Test the script’s status, start, and start commands:
sudo touch /etc/default/virtualbox sudo nano /etc/default/virtualbox
SHUTDOWN_USERS="user1 user2" # space-delimited list of users who might have runnings vmsSHUTDOWN=savestate # if any are found, suspend them to disk
rdesktop -g 1024x768 localhost # connect to VirtualBox RDP server rdesktop -g 1024x768 localhost:3390 # connect to Windows RDP serverssh -p 8022 localhost # connect to ssh
/etc/init.d/vbox-NAME status # Is the VM running?/etc/init.d/vbox-NAME start # Start the VM rdesktop localhost # Connect to the VM/etc/inid.d/vbox-NAME stop # Stop the VM
Install the init.d Script
Following the instructions on the vbox-service-tempalte page, Use the update-rc.d or chkconfig command to install the script if you want it to start your VM when you boot your computer
Now whenever you enter normal multiuser run mode (at boot time, for example) your VM will start, and when you exit multiuser mode (during shutdown, for example), your VM will be suspended to disk if it was running.
sudo update-rc.d vbox-NAME defaults 90 # Ubuntu# ... or ...sudo chkconfig vbox-NAME on # Fedora