Published: | |
Updated: |
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.
Install VirtualBox in the usual way. Create your virtual machine. In particular I recommend these settings, but YMMV:
setting | value |
---|---|
Display → Remote Display → Enable Server | true |
Display → Remote Display → Server Port | 3389 |
Network → Adapter 1 → Attached to | NAT |
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.
name | protocol | host IP | host port | guest IP | guest port |
---|---|---|---|---|---|
MS SQLServer | TCP | 1433 | 1433 | ||
VNC | TCP | 5904 | 5900 | ||
RDP | TCP | 3390 | 3389 | ||
SSH | TCP | 8022 | 22 |
To test all these mapped ports, connect to them on "localhost" when the virtual machine is running:
rdesktop -g 1024x768 localhost # connect to VirtualBox RDP server
rdesktop -g 1024x768 localhost:3390 # connect to Windows RDP server
ssh -p 8022 localhost # connect to ssh
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.
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:
sudo touch /etc/default/virtualbox
sudo nano /etc/default/virtualbox
/etc/default/virtualbox
SHUTDOWN_USERS="user1 user2" # space-delimited list of users who might have runnings vms
SHUTDOWN=savestate # if any are found, suspend them to disk
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 script's status
, start
, and start
commands:
/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
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
sudo update-rc.d vbox-NAME defaults 90 # Ubuntu
# ... or ...
sudo chkconfig vbox-NAME on # Fedora
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
Reader-contributed comments on Glump.net are owned by their original authors, who reserve all rights.
Comments rules:
Comments (7)
Your Service Script works on Ubunto14.04 but the VM would not start on reboot or Coldboot.
What may i do ?
If your VM starts when explicitly told to (i.e. 'service vbox-name start') but not after a reboot, you may want to simply leave VM_HOSTNAME blank. Also, I needed to change line 127 from '-type vrdp' to '-type headless.' I'm using Debian Squeeze and VirtualBox 3.2.10_OSE.
Works for me too in a Debian 7.3 in a HA environment, but I had to swap the exit codes on line 180 to get the correct exit codes on status (runnig=0, not running=1)
Thx!
The vbox-service-template works on my Debian 7.1 box also very well! Thank you!
Hi,
Thanks for your script!
A quick question....
If you have multiple VMs you want to start in sequence, what is the best way to proceed?