Showing posts with label VirtualBox. Show all posts
Showing posts with label VirtualBox. Show all posts

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 http://192.168.1.250 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.

Monday, August 30, 2010

Note about permissions in VirtualBox physical hard disk usage in Linux host

From VirtualBox manual: "Starting with version 1.4, as an alternative to using virtual disk images, VirtualBox can also present either entire physical hard disks or selected partitions thereof as virtual disks to virtual machines...
...this type of access is called "raw hard disk access"; it allows a guest OS to access its virtual hard disk without going through the host OS file system."
Do read the manual (part 9.7.1 at now) and do it at your own risk! Here we will talk only about permission problem you can get in Linux host. That, you will need read/write access for the entire disk (or selected partitions). For example:

$ VBoxManage internalcommands createrawvmdk -filename $HOME/.VirtualBox/HardDisks/sda2.vmdk -rawdisk /dev/sda -partitions 2

set up an image for access /dev/sda2 in Linux host. User needs read/write access to this partition. Otherwise, we will get some messages like this while adding sda2.vmdk to a virtual machine:

NS_ERROR_FAILURE (0x80004005)
Component: 
HardDisk
Interface: 
IHardDisk {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
Callee: 
IVirtualBox {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

Check the physical disks properties:

$ ls -l /dev/sda*

brw-rw---- 1 root disk 8, 0 2010-08-30 21:53 /dev/sda
brw-rw---- 1 root disk 8, 1 2010-08-30 12:22 /dev/sda1
brw-rw---- 1 root disk 8, 2 2010-08-30 21:31 /dev/sda2
brw-rw---- 1 root disk 8, 5 2010-08-30 12:22 /dev/sda5

We can see one solution to grant permissions is adding username to the disk group, for example, by editing /etc/group file. And don't forget to re-login.