Tuesday, 10 June 2014

VirtualBox and phpvirtualbox

I am using successfully Virtualbox now on more than 2 servers with phpvirtualbox interface. It is very convenient with few things you should have in mind:

1. Prepare your virtual machines on another host with Virtualbox and then export them and import them back. Because the console on phpvirtualbox didn't work out good at least for me. When you prepare the virtual machines use Bridge adapter. If you get an error before starting you can change the bridged adapter (example the adapter was wifi in my case you can change it to eth0).
2. Reserve the RAM that you need - resources are consumed regardless if the RAM is used or not.
3. After importing the virtual machines note on the network settings if it is denying the connections and correct it to Allow them.

Now to the server after installing Ubuntu Server follow this steps:

sudo apt-get update
sudo apt-get install virtualbox

Now we need to get the virtualbox version:
sudo VBoxManage --version

and get the appropriate extension pack from here:

After downloading the extension pack install it
VBoxManage extpack install

If you mess up the package:

VBoxManage extpack uninstall "Oracle VM VirtualBox Extension Pack"

The init script for the vbox web service is missing so we need to initialize the vboxwebsrv process. You can get the starting script from here:

sudo update-rc.d vboxweb-service.sh defaults 99 01

Don't forget to edit this file:

Add the users:
sudo useradd -d /home/vbox -m -g vboxusers -s /bin/bash vbox
sudo passwd vbox

Install Ngnix:
sudo apt-get install nginx php5-common php5-mysql php5-fpm php-pear unzip

I modified the

Nginx seems to be very picky when it comes to configuration - however it is very fast and stable. Modify the configuration to this:

server {
        listen 80; ## listen for ipv4; this line is default and implied
  #      listen [::]:80; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                deny all;

        # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;

        # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
root /usr/share/nginx/www;
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;

Let's reload the web server:
sudo /etc/init.d/nginx reload


Extract it in:

sudo cp config.php-example config.php
sudo nano config.php

Set the variables:

/* Username / Password for system user that runs VirtualBox */
var $username = 'vbox';
var $password = 'yourpassword';
var $consoleHost = 'your ip here';

Change this as well:
var $location = "http://localhost:18083";

Or you will get this:

Exception Object
    [message:protected] => Could not connect to host (
    [string:Exception:private] =>
    [code:protected] => 64
    [file:protected] => /usr/share/nginx/www/lib/ajax.php
    [line:protected] => 128
    [trace:Exception:private] => Array

    [previous:Exception:private] =>

I changed here the ip to localhost because otherwise the soap can not be connected because of ipv6.

sudo sysctl net.ipv6.bindv6only net.ipv6.bindv6only=0

How to start all VMs at boot here is a script that does the trick. Have in mind it is very important that the names of the VMs are without white spaces otherwise the VMs won't start - use it to your advantage.

#This init script autostarts necessary vms at boot
#and saves running vms on shutdown

# Sed explanation: sed -e 's/^.//' -e 's/.$//'
#   1.  -e means to allow multiple arguments in a single sed command
#   2.  's/^.//' means to substitute (s) / at the beginning of the line (^), any character (.) / [substitute with nothing] /
#   3.  's/.$//' means to substitute (s) / any character (.), at the end of the line / [substitute with nothing] /

RUNNINGVMS=$(sudo -H -u $VBOXUSER vboxmanage list runningvms | cut -d " " -f1 | sed -e 's/^.//' -e 's/.$//')
STOPPEDVMS=$(sudo -H -u $VBOXUSER vboxmanage list vms | cut -d " " -f1 | sed -e 's/^.//' -e 's/.$//')

case "$1" in
        for i in $STOPPEDVMS
                        echo "Starting" $i "VM"
                        sudo -H -u $VBOXUSER vboxmanage startvm $i --type headless
                        sleep 5
        for i in $RUNNINGVMS
                        echo "Saving state of" $i "VM"
                        sudo -H -u  $VBOXUSER vboxmanage controlvm $i savestate
    echo "Usage: /etc/init.d/startvm {start|stop}"
    exit 1

exit 0

This will start the script at boot:

update-rc.d [name of the script] defaults 99 01

How to add a raw disk (can be applied also to flash drives and external drives). Now log in as the vbox user and execute. In this example I add the whole disk (/dev/sdb) in raw format:

sudo usermod -a -G disk vbox
sudo chmod 777 /dev/sdb
VBoxManage internalcommands createrawvmdk -filename /home/vbox/sdb.vmdk -rawdisk /dev/sdb

If there is a partition on the disk add this to the end of the command specify the correct number of partitions:

-partitions 1 -relative

List the VMs to get the ids: 

VBoxManage list vms

Starting a vm:

/usr/bin/VBoxHeadless -startvm badc09ac-8ef4-4be9-b45b-73eb8d590b1f

Helps if for no apparent reason the home points to root. Setting vbox user home:
su vbox

VBoxManage setproperty machinefolder /home/vbox