Virtualbox and raw host disk access

The VirtualBox forums lists numerous reports of people not being able to get VirtualBox to work with raw disk partitions. Initially I bumped into the same problems, but got it working in the end.

The setup used in this article is an Ubuntu 12.04 host system with VirtualBox 4.1.18 installed. From disk /dev/sda the partitions 3 and 4 will be made available to a virtual machine. All virtual machines are run as user virtualbox. All console samples show clearly the user that runs the command.

On Ubuntu, raw devices have the following default access rights:

Terminal window
root@host:~# ls -l /dev/sda*
brw-rw—- 1 root disk 8, 0 Aug 20 15:11 /dev/sda
brw-rw—- 1 root disk 8, 1 Aug 10 17:37 /dev/sda1
brw-rw—- 1 root disk 8, 2 Aug 10 17:37 /dev/sda2
brw-rw—- 1 root disk 8, 3 Aug 21 09:43 /dev/sda3
brw-rw—- 1 root disk 8, 4 Aug 21 09:43 /dev/sda4

To give the virtualbox user proper access to the disks, the disk group will be extended:

Terminal window
root@host:~# usermod –groups disk -a virtualbox

Before going any further, please make sure that any processes currently running as user virtualbox are restarted!

This is needed for these processes to pick up the changed permissions. Next to the VirtualBox subsystems, don’t forget to restart the active shell sessions. By adding the virtualbox user to the group disk, the change is also persisted over system reboots.

With the proper access in place, we first create the VMDK file pointing to the raw device:

Terminal window
virtualbox@host:~$ VBoxManage internalcommands createrawvmdk -filename /home/virtualbox/virtualmachines/machine1/machine1-sda.vmdk -rawdisk /dev/sda -partitions 3,4 -relative
RAW host disk access VMDK file /home/virtualbox/virtualmachines/machine1/machine1-sda.vmdk created successfully.

Assuming that the VM machine1 is already created and contains a storage controller, the raw disk is now attached to the virtual machine:

Terminal window
virtualbox@host:~$ VBoxManage storageattach machine1 –storagectl “SATA Controller” –port 0 –type hdd –medium /home/virtualbox/virtualmachines/machine1/machine1-sda.vmdk

On successful completion, this command returns no output. If you want to validate that the disk is attached correctly, print the virtual machine information:

Terminal window
virtualbox@host:~$ VBoxManage showvminfo machine1
SATA Controller (0, 0): /home/virtualbox/virtualmachines/machine1/machine1-sda.vmdk (UUID: afc0a…)

That’s it!