2010-04-08 freebsd, ghc, virtualbox

GHC on FreeBSD on VirtualBox

This blog entry is about installing FreeBSD 7.2 as a guest OS under VirtualBox, and then GHC 6.10.4 in order to compile CGI binaries to be run on nearlyfreespeech.net.

A few days ago, I decided to host my homepage on nearlyfreespeech.net. Before signing up I had to make sure I could get CGI programs written in Haskell to run on their servers. Nearlyfreespeech.net uses FreeBSD 6 and 7.2. They say you can use Haskell to write CGI program but I'm not sure what it is supposed to mean. GHC 6.10.4 is available on their FreeBSD 7.2 servers and the 6 ones have GHC 6.8.3. Even with GHC installed, it is not very practical to compile things there; for instance the CGI package is not available.

So I used the same idea that Michael Snoyman did. The idea is to install FreeBSD (7.2 in this case) as a guest OS in VirtualBox to compile CGI programs and then upload them to nearlyfreespeech.net. In the rest of this post, I give the steps I followed.

The first step is to get VirtualBox. I simply installed the open source edition (in Arch Linux and Debian-based distros, this is the virtualbox-ose package). There are some additional steps, like adding the desired user to the vboxusers group and loading the vboxdrv module. VirtualBox provides different ways to get networking in the guest OS; I just used the simplest one: NAT network.

Once VirtualBox is installed, just launch the GUI to create a virtual disk and a VM. I have made a 6GB disk. Before starting the VM, it is possible to virtually insert a disc in its tray. Use the 'settings' button to do so and select the iso image of the FreeBSD 7.2 dvd (I guess it should be possible to use just the cd1, both are available at freebsd.org).

When the VM starts, it will boot on the disc and the install of FreeBSD can begin. I've made a 1GB swap and a 5GB filesystem partitions. I've have chosen the minimal install then selected the portupgrade package in the ports-mgmt category. When asked, I've enabled networking via DHCP and SSH access. The SSH access is important as it makes possible to log in the running guest OS without any need for a GUI. As it happens, I run the VM on another laptop. If I use the GUI with X forwarding, the refreshing of the terminal makes the whole thing damn slow.

During the install, you will be able to add a regular user. Do it and add it to the wheel group. If you don't, you can add it later with (as root):

pw user mod <user> -G wheel

This is necessary so you can SSH into FreeBSD as a regular user (SSH access by root is disalowed by default) then use su - to get root privileges.

To actually expose the guest 22 port (SSH) as the host 2222 port, add the lines

<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/GuestPort"
  value="22"/>
<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/HostPort"
  value="2222"/>
<ExtraDataItem name="VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/Protocol"
  value="TCP"/>

in .VirtualBox/Machines/freebsd72/freebsd72.xml (freebsd72 is the name of my VM). The e1000 should match the used network adapter (pcnet is often cited on the sites I visited).

Now, I can start the VM without any GUI with

VBoxManage startvm freebsd72 --type headless

connect to the running FreeBSD with

ssh -l <user> -p 2222 localhost

and stop it with

VBoxManage controlvm freebsd72 poweroff

As said above, if root access is needed, just log as regular user <user> then use su - to get root privileges.

The next step is to get the latest ports (otherwise we would compile and install GHC 6.8) with the following command

csup -L 2 -h cvsup.<site>.FreeBSD.org /usr/share/examples/cvsup/ports-supfile

where <site> can be for instance fr or nl. This will take some time. Detailed information about using ports can be found at Using the Ports Collection.

The last step is to install GHC. To do so, cd /usr/ports/lang/ghc and type make then make install. For the Haskell CGI package , the port is in /usr/ports/www/hs-cgi.

The hello world CGI example on the Haskell wiki compiles without problem. A copy of it on nearlyfreespeech.net executes without problem too.

submit to reddit