The FreeBSD hypervisor using VirtualBox

       976 words, 5 minutes

VirtualBox is a virtualization software that allows running several OSes on a single host machine. It was first a free VMware Workstation-like tools but has grown quite a bit now. You can now run virtual machines headless, like you do with Xen or KVM.

Here’s a little tour on setting up an hypervisor using VirtualBox on FreeBSD 9.

BTW: Why FreeBSD? Because it features ZFS filesystem version 5 and ZFS pool version 28.

Installing VirtualBox

By default, the VirtualBox package doesn’t have headless features on. So it has to be compiled from ports:

  # cd /usr/ports/emulators/virtualbox-ose
  # make config
  [ ] QT4             Build with QT4 Frontend
  [ ] DEBUG           Build with debugging symbols
  [ ] GUESTADDITIONS  Build with Guest Additions
  [*] DBUS            Build with D-Bus and HAL support
  [ ] PULSEAUDIO      Build with PulseAudio
  [ ] X11             Build with X11 support
  [*] VDE             Build with VDE support
  [*] VNC             Build with VNC support
  [*] WEBSERVICE      Build Webservice
  [ ] NLS             Native language support
  
  # make install clean-depends clean
  # cd /usr/ports/emulators/virtualbox-ose-kmod
  # make config
  [ ] DEBUG   Build with debugging symbols
  [*] VIMAGE  VIMAGE virtual networking support
  
  # make install clean-depends clean

Quite a few moments later, you have everything installed. There are modules that have to be loaded so that VirtualBox works:

  # kldload vboxdrv
  # kldload vboxnetadp
  # vi /etc/rc.conf
  (...)
  vboxdrv_load="YES"
  vboxnet_enable="YES"

The user that will run VirtualBox needs to be part of the “vboxusers” group:

  # pw groupmod vboxusers -m `whoami`

Everything is now ready to run the virtual machines.

Creating Virtual Machines

In the headless world, the virtual machines are created, from prompt, using the “VBoxManage” command. You can get the list of available VM using the “VBoxManage list ostypes” command.

Here’s a way to create an OpenBSD virtual machine:

  # VBoxManage createvm --name OpenBSD --ostype OpenBSD --register
  Virtual machine 'OpenBSD' is created and registered.
  UUID: 1a09731f-9bbb-410c-8c31-4004fda47446
  Settings file: '/home/jdoe/VirtualBox VMs/OpenBSD/OpenBSD.vbox'
  
  # VBoxManage modifyvm OpenBSD --memory 256 --boot1 dvd --nic1 bridged --bridgeadapter1 bge0
  
  # VBoxManage createhd --filename "OpenBSD.vdi" --size 10000
  0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
  Disk image created. UUID: 8ba53766-9848-4ba9-8cb9-c3556f86bc5b
  
  # VBoxManage storagectl OpenBSD --name "SATA Controller" --add sata --controller IntelAHCI --hostiocache on
  # VBoxManage storageattach OpenBSD --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "OpenBSD.vdi"
  
  # VBoxManage storagectl OpenBSD --name "IDE Controller" --add ide --controller PIIX4
  # VBoxManage storageattach OpenBSD --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium /mnt/NFS_dharma/openbsd-5.0-i386.iso 

To start the VM, just issue:

  # VBoxHeadless -s "OpenBSD" -n
  Oracle VM VirtualBox Headless Interface 4.0.14_OSE
  (C) 2008-2012 Oracle Corporation
  All rights reserved.
  
  12/04/2012 10:53:41 Listening for VNC connections on TCP port 5900
  Set framebuffer: buffer=8058eb000 w=800 h=600 bpp=32
  Set framebuffer: buffer=80a400000 w=640 h=480 bpp=32
  Set framebuffer: buffer=80b187000 w=720 h=400 bpp=32

Using a VNC client will bring us to the console.

The thing is, if you “Ctrl-C” or loose the remote console session (from where VBoxHeadless was run), the virtual machine will die.

To truly achieve headless configuration, you have to use the following commands:

  # VBoxManage startvm OpenBSD --type headless
  Waiting for the VM to power on...
  VM has been successfully started.

The problem is, AFAIK, VRDE doesn’t work on FreeBSD. This means that running the VM this way doesn’t allow access to the console ; this means no way to do the initial install on most instances.

Dealing with start/stop of virtual machines

There is a “vboxheadless” rc.d script installed with the VirtualBox package that allows to start/stop virtual machines automatically and at will. Here’s how I configured the system to deal with the previously created OpenBSD VM:

  # vi /etc/rc.conf
  (...)
  vboxheadless_enable="YES"
  vboxheadless_machines="OpenBSD WinXP"
  vboxheadless_user="jdoe"
  vboxheadless_OpenBSD_flags="-v off -n -m 5901"
  vboxheadless_WinXP_flags="-v off -n -m 5902"
  
  # /usr/local/etc/rc.d/vboxheadless start
  Starting Virtual Machines:
                    OpenBSD Started
                      WinXP Started
  
  # netstat -na | grep LISTEN
  tcp4       0      0 *.5902                 *.*                    LISTEN
  tcp4       0      0 *.5901                 *.*                    LISTEN
  (...)

The virtual machines are now created headless and their console is available using VNC on the defined port.

vboxwebsrv, the VirtualBox web service

One of the reason to compile VirtualBox from sources on FreeBSD is to enable the webservice. This will be used to remotely manage the VirtualBox machines.

Configure the rc.d script and start the Web service:

  # vi /etc/rc.conf
  (...)
  vboxwebsrv_enable="YES"
  vboxwebsrv_user="jdoe"
  vboxwebsrv_flags="-P /home/jdoe/vboxwebsrv.pid -F /home/jdoe/vboxwebsrv.log -R 5"
  
  # /usr/local/etc/rc.d/vboxwebsrv start

The Web service can now be used by any remote client to create/modify/delete/start/stop virtual machines.

phpVirtualBox, the Web interface

There is a way to install a graphical Web interface to manage the headless virtual machines: phpVirtualBox.

You will need a Web server and the PHP framework. On my instance, I got apache-2.2.21, php5-5.3.8 and phpvirtualbox-4.0.7 from the binary ports.

Configure Apache to run PHP and point its DocumentRoot to “/usr/local/www/phpvirtualbox”.

You are supposed to be able to configure some credentials on the phpVirtualBox layer. But I could never used them ; since there were l/p configured, I couldn’t log in (even using `VBoxManage setproperty websrvauthlibrary null`). So I just disabled it:

  # vi /usr/local/www/phpvirtualbox/config.php
  (...)
  var $username = '';
  var $password = '';
  (...)
  var $noAuth = true;
  (...)

Since then, just point your preferred Web browser to the VirtualHost and start configuring your virtual machines.

The main problem with phpVirtualBox on FreeBSD is the lack of support of VRDE (on the virtualbox-ose side). This means that there is no way to access to virtual machines console. In fact, there is a “screenshot” option that allows to see the console but there’s no way to interact with the console.

Final thoughts

Combining the BSD kernel with the ZFS features and the VirtualBox system is quite appealing. Unfortunately, the lack of VRDE supports makes VM management quite root, not to say unfriendly. It wouldn’t be that bad if VBoxManage could deal with VNC ; but it doesn’t.

This kind of solution looks like SmartOS, from Joyent. Except KVM is quite better integrated into their Solaris kernel than VirtualBox is on FreeBSD.