[[howto:virtualization:setup-virtualbox-as-a-service-in-linux]]

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

howto:virtualization:setup-virtualbox-as-a-service-in-linux [2018-08-05 04:25] (current)
Line 1: Line 1:
 +~~META:
 +creator = Brendan Kidwell
 +&date created = 2008-10-29
 +&date updated = 2012-07-18
 +~~
 +{{tag>​linux service virtualbox}}
 +~~DISCUSSION~~
 +====== 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 <wrap file>​init.d</​wrap>​ scripts to start and stop services.
 +
 +**Alternate version available:​**
 +
 +[Spanish] [[http://​cgacimartin.wordpress.com/​2009/​01/​09/​crear-servicio-para-virtualbox/​|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 [[https://​www.virtualbox.org/​manual/​ch09.html#​autostart|built-in method yet]], but it should work better than my instructions on this web page.//
 +
 +===== Prepare Your VirtualBox Virtual Machine =====
 +
 +Install [[http://​www.virtualbox.org/​|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 ​ |
 +
 +===== 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 [[http://​pigtail.net/​LRP/​printsrv/​cygwin-sshd.html|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** <wrap file>/​etc/​ssh/​ssh_host_*</​wrap>​ to your **guest'​s** Cygwin <wrap file>/​etc/</​wrap>​ 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 <wrap nav>​Settings</​wrap>​ for your chosen VM, then <wrap nav>​Network -> Adapter (n) (the one using NAT) -> Advanced → Port Forwarding</​wrap>​. 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:
 +
 +<code bash>
 +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
 +</​code>​
 +
 +===== 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 <wrap nav>​Bridged Networking</​wrap>​ --- 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 <wrap file>/​etc/​hosts</​wrap>​ 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 <wrap file>/​etc/​init.d/</​wrap>​ to start and suspend the guest on demand.
 +
 +First, if you look at <wrap file>/​etc/​init.d/​vboxdrv</​wrap>,​ 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:
 +
 +<code bash>
 +sudo touch /​etc/​default/​virtualbox
 +sudo nano /​etc/​default/​virtualbox
 +</​code>​
 +
 +<file bash /​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
 +</​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.
 +
 +<​del>​Old script used to be included here...</​del>​
 +
 +Grab a copy of my [[https://​github.com/​bkidwell/​vbox-service-template|vbox-service-template]] init.d script and follow the instructions there to customize it for your VM and install it in <wrap file>/​etc/​init.d</​wrap>​.
 +
 +===== Test the init.d Script =====
 +
 +Test the script'​s ''​status'',​ ''​start'',​ and ''​start''​ commands:
 +
 +<​code>​
 +/​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
 +</​code>​
 +===== Install the init.d Script =====
 +
 +Following the instructions on the <wrap file>​vbox-service-tempalte</​wrap>​ 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
 +
 +<code bash>
 +sudo update-rc.d vbox-NAME defaults 90 # Ubuntu
 +# ... or ...
 +sudo chkconfig vbox-NAME on         # Fedora
 +</​code>​
 +
 +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.