Pages

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:
http://www.virtualbox.org/svn/vbox/trunk/src/VBox/Installer/linux/vboxweb-service.sh

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

Don't forget to edit this file:
etc/default/virtualbox
VBOXWEB_USER=vbox

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
/etc/nginx/sites-available/default 

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;
                allow 127.0.0.1;
                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 127.0.0.1:9000
        #
        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

Download:
http://sourceforge.net/projects/phpvirtualbox/

Extract it in:
/usr/share/nginx/www

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 (http://127.0.0.1:18083/)
    [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.

#!/bin/bash
#
#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] /

VBOXUSER=vbox
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
  start)
        for i in $STOPPEDVMS
                do
                        echo "Starting" $i "VM"
                        sudo -H -u $VBOXUSER vboxmanage startvm $i --type headless
                        sleep 5
                done
    ;;
  stop)
        for i in $RUNNINGVMS
                do
                        echo "Saving state of" $i "VM"
                        sudo -H -u  $VBOXUSER vboxmanage controlvm $i savestate
                done
    ;;
  *)
    echo "Usage: /etc/init.d/startvm {start|stop}"
    exit 1
    ;;
esac

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

References:
http://www.vionblog.com/virtualbox-4-3-phpvirtualbox-debian-wheezy/
http://trac.nginx.org/nginx/ticket/345
https://forums.virtualbox.org/viewtopic.php?f=7&p=181318
http://scarygliders.net/2011/11/04/virtualbox-and-iscsi-nas-how-to-linux-windows/