The FreeBSD hypervisor using VirtualBox

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/ptijo/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.
Running OpenBSD in VirtualBox Headless

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="ptijo" 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="ptijo" vboxwebsrv_flags="-P /home/ptijo/vboxwebsrv.pid -F /home/ptijo/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.

Sources

10 Comments

  • steve Sun, 22 Apr 2012 11:09:35 +0000 Reply

    why not prebuild version iso cd with some minimalistic gui app for easy setup mainintain and work with it ! How tos are only for showing of at my opinion and for reinventing for reinvention ! It only shows how hard and work overkill is involved in open source not easy to use and ! Its open source why then not ??? Should be easy to use unless someone wants to be hard ! There is enough of howtos !!!

    • Joel Carnat Wed, 25 Apr 2012 18:36:16 +0000 Reply Author

      howtos are to learn how to do it when there is no pre-build system or when you want to learn.

  • Иван Tue, 15 May 2012 08:40:01 +0000 Reply

    Thanks!

  • Graeme Thu, 17 May 2012 11:38:41 +0000 Reply

    Many thanks for this page. I have been fighting with VirtualBox (headless) on FreeBSD 9.0 for 2 days now! I just couldn’t get RDP to work, and a port scan showed that the default port 3389 is never openned. I came to the conclusion that VirtualBox’s RDP simply doesn’t work under FreeBSD – your article seems to confirm this.

    PS:
    I do agree with you 100% that ZFS rocks on FreeBSD 9.0! I wouldn’t trust by data on anything else. 🙂

  • Thord Matre Wed, 13 Jun 2012 13:54:53 +0000 Reply

    Hey Joel.

    I love these Howto’s. Some of us like to build things from scratch. We learn and understand how things hang together.

    It keeps me interested.

  • Ray Chakrit Sat, 21 Jul 2012 10:09:39 +0000 Reply

    This howto is very helpful and save people a lot of time.

  • bhb Mon, 17 Sep 2012 23:16:52 +0000 Reply

    very cool howto! It works great for me

  • Tiago Fri, 12 Apr 2013 23:57:49 +0000 Reply

    thank you! you helped me a lot! congratulations on the great work!

  • patpro Sun, 23 Jun 2013 09:09:31 +0000 Reply

    Bonjour,

    Looks like VirtualBox changed a lot on FreeBSD with the 4.2.x, your how to is very interesting, but doesn’t work any more 🙁
    Basically, it’s dead from “To start the VM, just issue” and beyond.

  • girgen Fri, 07 Nov 2014 01:28:49 +0000 Reply

    VNC is supported nowadays, so that part is old information.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

%d bloggers like this: