Monday, May 2, 2016

Server-Based VirtualBox


Oracle’s free VirtualBox virtualization solution is intended for use on the desktop. Management of virtual machines is via a GUI that only works locally and cannot be used on the network. Although a command-line option is available, it offers very few of the comforts to which you’ve become accustomed. With the tools presented in this article, users can control VirtualBox VMs on the server remotely and conveniently.
Why VirtualBox?
VirtualBox offers a number of advantages compared with other virtualization solutions. For example, the program is available free for the major platforms (e.g., Windows, Linux, Mac OS X, and Solaris), and it supports a wide number of guest systems, including exotics such as DOS/WIN 3.1 and OS/2.
Installing VirtualBox is easy on any platform because it usually only involves running a setup program or installing a package on the system. VirtualBox does not place heavy demands on the hardware; it does not require Intel VTX/AMD-V or a special Linux kernel, although it still offers great performance, thanks to its own guest extensions. In the VirtualBox Remote Desktop Protocol (VRDP, Figure 1), VirtualBox also has a built-in graphical interface to all the virtual machines: an RDP client is sufficient to access the VMs on the network regardless of the guest operating system.
Figure 1: Thanks to VRDP support, you can access the virtual machine consoles in your browser with phpVirtualBox.
Free or Not Free?
Up to version 4.0 of VirtualBox, both a commercial and an open source edition (OSE) existed. The OSE often was available in the various Linux distro repositories, so users could easily install via their package repositories. As of version 4.0, Oracle has now merged these two versions in a binary, which you can download from the VirtualBox website. For Linux users, Oracle continues to providerepositories for various  Linux distributions. The non-free components, such as support for USB 2.0 devices, the VRDP server, and PXE boot for Intel network cards, were outsourced into the Extension Pack. Non-free here means Oracle has not released the source code. The Extension Pack is still available free of charge.
Headless Servers
For operations on devices without a graphical user interface, VirtualBox can launch virtual machines in Headless Mode. VMs launched in this way automatically have VRDP support, so that network access to the system running on the virtual machine is possible with an RDP client.
I installed VirtualBox on an Ubuntu LTS 10.04.4 (Lucid Lynx) server, 64-bit version. The host needs to be running a 64-bit kernel to create 64-bit VMs. The quickest way to install is to add the VirtualBox package source to your sources.list file
sudo sh -c 'echo "# VirtualBox repository Ubuntu 10.04 LTS Lucid \
   deb http://download. virtualbox.org/virtualbox/debian \
   lucid contrib" >> /etc/apt/sources.list'
and then add the GPG key to your keychain:
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 98AB5139
Now, update the package list and install the current 4.1 version of VirtualBox:
apt-get update && apt-get install virtualbox-4.1
For VRDP support, you still need the matching Extension Pack for your VirtualBox version. To be on the safe side, you will want to enter
vboxmanage -v
to check the version. Then download and install the Extension Pack. For my server, the procedure is shown in Listing 1. VirtualBox should now be running on your server.
A Walk Through VirtualBox
Because the Linux server doesn’t have a graphical user interface, controls are restricted to the command line. The focus here is the vboxmanage command; if you call it with the -h parameter set, you are treated to no fewer than 448 lines of commands and parameters. You just need a few of them to start creating, cloning, deleting, and modifying virtual machines. For more information, check out Chapter 8 of the VirtualBox online help.
Anyone who finds this process cumbersome can simply use a same-version VirtualBox installed on the desktop (independent of operating system) and point and click to create the VMs for the server. Of course, the configuration for, say, the network has to be adjusted to match the host later on, but you can then easily export the VM and its virtual hard disk in the main menu (File | Export Appliance) to create an .ova file. If you use the .ovf extension, the configuration file and virtual disk will be exported to their own files. Once the VM has been copied to the server, it can be imported again with the command:
vboxmanage import my-vm.ova
To start the virtual machine on the server, type:
vboxmanage startvm <UUID>/<Name>
If you are not quite sure of the name or the UUID for the VM, you can retrieve this information with the
vboxmanage list vms
command.
VirtualBox GUI on the Network
You will not always want to manage VirtualBox on the server via the command line. Alternatively, you can use the graphical front end phpVirtualBox by Ian Moore, which offers browser-based controls. phpVirtualBox maps the VirtualBox GUI in detail with the help of Ajax in the browser (Figure 2).
Figure 2: phpVirtualBox with its Ajax interface precisely emulates the known functionality of the native GUI.
This means virtually no restrictions compared with operations as usual on the native GUI. However, phpVirtualBox is not designed for setting up a hosted service in which multiple users have different permissions for the different VMs: The administrator on phpVirtualBox always has full control of all the VMs on the host.
phpVirtualBox
To communicate with the VirtualBox host, phpVirtualBox uses the Oracle SOAP interface VBoxWebSrv. In combination with a locally installed Apache2 web server with PHP 5 support, you then have a complete setup. The VirtualBox host and Apache could, in principle, be installed on two separate physical machines using phpVirtualBox. However, because Oracle’s VBoxWebSrv does not provide any kind of encryption, communications – and thus transmission of the password – from the web server to the VirtualBox host would be unencrypted.
VirtualBox doesn’t globally map VMs to the system, but always to the user who created the VM. Therefore, you first need to create an appropriate user and a group. In this example, the user will bevbox and the group vboxusers .
On Ubuntu, the following commands set this up:
sudo groupadd vboxusers
sudo useradd -m vbox -G vboxusers
The virtual machines are created later in the/home/vbox/VM directory. Finally, assign the new user a password:
sudo passwd vbox
Before you configure phpVirtualBox, you first need to install the Apache server, PHP, and some more packages, then set up VBoxWebSrv. To do so, create a file named /etc/default/VirtualBox with the editor of your choice and enter the user vbox in that file:
VBOXWEB_USER=vbox
Then, start VBoxWebSrv with the command
sudo /etc/init.d/vboxweb-service start
and type
sudo update-rc.d vboxweb-service defaults
to ensure that the service also starts automatically after a reboot. Next, install the packages required for running phpVirtualBox:
sudo apt-get install apache2-mpm-prefork apache2-utils apache2.2-bin \
   apache2.2-common apache2 apache2-mpm-prefork apache2-utils apache2.2-bin \
   apache2.2-common apache2-doc apache2-suexec libapache2-mod-php5 libapr1 \
   libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libapr1 libaprutil1 \
   libaprutil1-dbd-sqlite3 libaprutil1-ldap php5-common php5-common \
   php5-mysql php5-suhosin php-pear wget
Now, change directory to /var/www and download the latest stable version from the phpVirtualBox project website be typing the following at the command line:
wget http://phpvirtualbox.googlecode.com/files/phpvirtualbox-4.1.17.zip
After unpacking with
sudo unzip phpvirtualbox-*.zip
you will find a new directory named phpvirtualbox-4.1-7 in /var/www . For simplicity’s sake, change the directory name to phpvirtualbox to avoid unnecessary typing later on.
Finally, you need to add the username and password to the configuration file for phpVirtualBox. Fortunately, the developer provides a suitable template that you simply copy:
sudo cp /var/www/phpvirtualbox/config.php-example /var/www/phpvirtualbox/config.php
Now add the following lines to the /var/www/phpvirtualbox/config.php file:
var $username = 'vbox';
var $password = 'User-password-vbox';
In your web browser, you can now surf to http://<Hostname>/phpvirtualbox to access phpVirtualBox. For the first login, the username is admin and the password admin . Next, in File | Global Settings | User , assign administrative rights to the user vbox and define a password for logging into the web interface (Figure 3).
Figure 3: Setting up user vbox.
It is a good idea to delete the admin user immediately – or at least change the password to something secure. In the phpVirtualBox web GUI, you can now fully control VirtualBox over the network, just as in the native client.
Although it is easy to work with phpVirtualBox, unfortunately the VirtualBox API does not support automatically starting and stopping VMs at system boot or shutdown time, so you must again delve into the toolbox to automate the VirtualBox host’s operations as much as possible.
VBoxTool for Automation
VBoxTool comprises a series of wrapper scripts for the VirtualBox CLI tool VBoxManage; for example, you can automatically start and stop individual or all virtual machines when the host is started or shut down (Figure 4).
Figure 4: VBoxTool simplifies the management of virtual machines on a VirtualBox host. With a single command, you can view the status of VMs and start, stop, or backup via rsync.
With VBoxTool, you can also retrieve information about the status of all or just the current virtual machines or handle bulk operations at the command line with a single command.
All it takes to start, stop, or switch all virtual machines to save mode at the same time, or to back up the VMs automatically with the help of rsync , are the following commands:
vboxtool start
vboxtool stop
vboxtool save
vboxtool backup
When running the backup command, VBoxTool also takes care of automatically switching the machine to secure mode and restarting after creating the image.
Installation
Installing VBoxTool is very easy: First, download the ZIP archive from the VBoxTool project page, then unzip the contents of the ZIP archive in your home directory on the server system:
unzip vboxtool-0.4.zip
Then, copy the main script to /usr/local/bin :
sudo cp ~/script/vboxtool /usr/local/bin
Now you need to make the script executable,
sudo chmod +x /usr/local/bin/vboxtool
and make the init script executable,
sudo cp script/vboxtoolinit /etc/init.d
sudo chmod +x /etc/init.d/vboxtoolinit
which then needs to be enabled for the appropriate runlevels:
sudo update-rc.d vboxtoolinit defaults 99 10
The VBoxTool configuration is based on just two files, For the first file, you need to create a directory in /etc :
sudo mkdir /etc/vboxtool
With your editor of choice, now create a file named vboxtool.conf that only contains the username of the account on which VBoxTool runs. In this example, the user is vbox :
vbox_user='vbox'
Optionally, you can specify a backup directory to which the virtual machines are automatically backed up. If you specify a backup directory, you can back up virtual machines over the network – for example, to a NAS or a share on a server. However, you must make sure the user also has the appropriate write permissions in the specified directory.
backup_folder=/home/user/vboxbackup
If you do not explicitly specify the backup folder, the backups are created in the respective folders of the VMs.
For VBoxTool to know which VMs it is responsible for from now on, you need to create another file:
/etc/vboxtool/machines.conf
In the file, enter a separate line for each of the virtual machines on your host, using the following format:
<VM-Name>,<VRDP-Port>
Notice that you are not allowed to put spaces before or after the comma . On my test server,/etc/vboxtool/machines.conf looks like this:
zarafa,3389
WindowsXP,3390
The machines listed here are now automatically started by VBoxTool when the host system boots and are stopped again at shutdown.
The configuration stage is done. From now on, the commands listed in Table 1 are available for you to control your virtual machines.
Conclusions
VirtualBox is a genuine alternative to the established, and usually much more complex, virtualization environments of VMware and other server-side virtualization specialists. Although VirtualBox is primarily intended for use on the desktop, the hypervisor can be managed in headless mode – that is, without a graphical user interface.
If you are ready for some creative configuration, you can compensate for the lack of network connectivity in the VirtualBox GUI by designing your solution around a pair of helpful open source tools: VBoxManage and phpVirtualBox.
Info
The Author
Thomas Zeller has focused on IT security and open source for 15 years. Zeller is also the author/co-author of the books OpenVPN kompakt  and Mindmapping mit FreeMind (in German). In real life, Thomas Zeller is the managing director of a medium-sized IT system integration company.

No comments:

Post a Comment