Thursday, September 02, 2010

Setup a FreeNAS virtual machine in Linux by VirtualBox

We will consider two VirtualBox features — raw hard disk access and VBoxHeadless, to create and run a FreeNAS server "in background" (as a Linux service). It takes us 3 steps.

Step 1. Create and setup (install) a FreeNAS virtual machine with a small hard disk in VirtualBox, network interface is "bridged". The easiest way is using VirtualBox graphical interface. We are not going into details. Just be aware to use so-called "embedded" FreeNAS if you have much of RAM (FreeNAS 0.7.1 requires minimum 192MB). Otherwise install "full version" of FreeNAS and use swap, because the host (Linux) system needs RAM, too.

Step 2. Add raw disk to virtual machine [1].

Step 3. Use VBoxHeadless to run FreeNAS virtual machine without GUI:

$ VBoxHeadless --startvm FreeNAS --vrdp=off >> $HOME/VBoxHeadless.out 2>&1

Read more about VBoxHeadless in VirtualBox documentation [2]. In the command above FreeNAS is the name of created virtual machine, we turn vrdp server off, as FreeNAS has a very powerful Web interface at by default [3].
You may want to run the virtual machine automatically during boot process. Be aware to run it after vboxdrv service. For this purpose the most convenient solution is /etc/init.d/rc.local. This script run last, but run as root, thus sudo command is used:

$ cat /etc/rc.local
#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

sudo -H -u phan VBoxHeadless --startvm FreeNAS --vrdp=off >> $HOME/VBoxHeadless.out 2>&1

exit 0

Change phan to your username. The -H option is important, it tell sudo to set HOME environment variable to the home directory of the target user, where VBoxHeadless will find the virtual machine with "FreeNAS" name.

P.S. don't forget to turn off FreeNAS virtual machine before shutdown Linux.

No comments: