Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Using RAW Devices In VirtualBox VMs


Usually, VirtualBox creates its virtual machines in disk images (.vdi, .vmdk, etc.). This tutorial explains how you can use RAW devices from the host (partitions, LVM volumes, etc.) and create a VirtualBox VM in it. I will also explain how to mount a RAW device in an existing VM.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

Because I’ve installed VirtualBox on a headless server, I’m using phpvirtualbox as the VirtualBox GUI here. If you use the original VirtualBox GUI, that is perfectly fine.

My VirtualBox host uses LVM, so I will concentrate on using LVM volumes as RAW devices in this tutorial. The procedure is the same for “normal” partitions.

Make sure that you are logged in as root (type in

sudo su

to become root), because we must run all the steps from this tutorial as root user.

 

2 Add The VirtualBox User To The disk Group

Before we start, we must make sure that the user under which we run VirtualBox is a member of the disk group. If you use phpvirtualbox, the username is probably vbox. If you use the normal VirtualBox GUI, the username is the name under which you are logged into your Linux desktop.

usermod -a -G disk vbox

To make sure that this change takes effect, it is a good idea to reboot now:

reboot

 

3 Prepare A RAW Device For Use With VirtualBox

As I mentioned before, I’m using LVM volumes here. Let’s create a 20G volume called vm10 (the virtual machine I want to create will have the name vm10) in the volume group vg0:

lvcreate -L20G -n vm10 vg0

Next we must create a .vmdk file that tells VirtualBox to use the RAW device /dev/vg0/vm10. It’s probably best to create it in the home directory of the user under which VirtualBox is running (e.g. /home/vbox if you use phpvirtualbox) – the file must also be owned by that user:

cd /home/vbox/
VBoxManage internalcommands createrawvmdk -filename vm10.vmdk -rawdisk /dev/vg0/vm10
chown vbox:vbox vm10.vmdk

 

4 Create The VM In VirtualBox

Next go to your VirtualBox GUI (original VirtulBox GUI or phpvirtualbox) and use the Create New Virtual Machine wizard to create a new virtual machine. On the Virtual Hard Disk screen, select Use existing hard disk and click on the Choose a virtual hard disk file icon:

1

Select the .vmdk file (vm10.vmdk in this example) that we have created in chapter three and click on OK:

2

Click on Next >>

3

… and on Create:

4

Now create the virtual machine as you would normally do in VirtualBox.

You are now using a RAW device instead of a disk image for the virtual machine.

5 Mount A RAW Device Inside An Existing VM

Now let’s assume we want to add a RAW device as an addtitional partition to an existing VM. I want to add the LVM volume /dev/vg0/vm10storage. Let’s create it as follows (with a size of 5GB – this is just an example):

lvcreate -L5G -n vm10storage vg0

Like in chapter three, we need to create a new .vmdk file for this partition before we can use it with VirtualBox:

cd /home/vbox/
VBoxManage internalcommands createrawvmdk -filename vm10storage.vmdk -rawdisk /dev/vg0/vm10storage
chown vbox:vbox vm10storage.vmdk

Now go to the VirtualBox GUI and stop the VM to which you want to add the partition (e.g. Stop > ACPI Shutdown):

5

After the VM has stopped, go to Storage:

6

Click on the Add Hard Disk icon next to SATA Controller:

7

Click on Choose existing disk:

8

Select the appropriate .vmdk file (vm10storage.vmdk in this case) and click on OK:

9

Click on OK again:

10

Now start the VM. After it has started, log into it. The following commands have to be run inside the VM!

You should find the new drive (/dev/sdb in this case) in the output of

fdisk -l

root@vm10:~# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003974c

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    41940991    20719617    5  Extended
/dev/sda5          501760    41940991    20719616   8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders, total 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn’t contain a valid partition table

Disk /dev/mapper/vm10-root: 20.7 GB, 20661141504 bytes
255 heads, 63 sectors/track, 2511 cylinders, total 40353792 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/vm10-root doesn’t contain a valid partition table

Disk /dev/mapper/vm10-swap_1: 532 MB, 532676608 bytes
255 heads, 63 sectors/track, 64 cylinders, total 1040384 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/vm10-swap_1 doesn’t contain a valid partition table
root@vm10:~#

You can now use this drive as you would normally do, e.g. partition it…

fdisk /dev/sdb

root@vm10:~# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x862fb2cf.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): <– n
Partition type:
p   primary (0 primary, 0 extended, 4 free)
e   extended
Select (default p):
 <– p
Partition number (1-4, default 1): <– 1
First sector (2048-10485759, default 2048): <– ENTER
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
 <– ENTER
Using default value 10485759

Command (m for help): <– t
Selected partition 1
Hex code (type L to list codes):
 <– 83

Command (m for help): <– w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@vm10:~#

… create a file system on it (e.g. ext4)…

mkfs.ext4 /dev/sdb1

… and mount it:

mount /dev/sdb1 /mnt

 

  • VirtualBox: http://www.virtualbox.org/
  • phpvirtualbox: http://code.google.com/p/phpvirtualbox/

Comments

comments