How to use an LVM partition (or any other raw device) as VirtualBox disk

root's picture

1. Create the target LV:

core:osx# lvcreate -L450G -n MacPool450Glv bkupvg
  Logical volume "MacPool450Glv" created
core:osx# ls -la /dev/bkupvg/MacPool450Glv 
lrwxrwxrwx 1 root root 32 2012-09-16 20:51 /dev/bkupvg/MacPool450Glv -> /dev/mapper/bkupvg-MacPool450Glv

2. Use VBoxManage tool to link a vmdk filename to the desired LV:

core:osx# VBoxManage internalcommands createrawvmdk -filename /home/vbox/osx/MacPool450Glv.vmdk -rawdisk /dev/mapper/bkupvg-MacPool450Glv
RAW host disk access VMDK file /home/vbox/osx/MacPool450Glv.vmdk created successfully.

If you are curios, the file is normal text and it looks like this inside:
core:osx# cat /home/vbox/osx/MacPool450Glv.vmdk
# Disk DescriptorFile

# Extent description
RW 943718400 FLAT "/dev/mapper/bkupvg-MacPool450Glv" 0

# The disk Data Base 

ddb.virtualHWVersion = "4"

3. Use this newly crated disk inside a virtual machine but slightly different:

create another disk controller and enable "Use host I/O cache" otherwise you will experience very (very very) bad performance of the entire virtual machine. Example below:

Extra note:

If you want to resize the logical volume, you will have to shut down the machine, remove the disk from virtualbox, delete the vmdk file, resize the logical volume in LVM and redo this tutorial from step 2.

As bonus, will show you below how to convert an lvm partition (or raw device) into vdi image:

1. Create a raw image with dd:
dd if=/dev/mapper/sourcelv of=/path/to/image.raw
2. Convert the raw image to vdi:
VBoxManage convertdd image.raw image.vdi --format VDI


root@core:~# dd bs=16000 if=/dev/mapper/bkupvg-pluto200g1 of=/images/pluto200g1
13421772+1 records in
13421772+1 records out
214748364800 bytes (215 GB) copied, 3608.66 s, 59.5 MB/s

root@core:/images# VBoxManage convertdd pluto200g1.raw pluto200g1.vdi --format VDI
Converting from raw image file="pluto200g1.raw" to file="pluto200g1.vdi"...
Creating dynamic image with size 214748364800 bytes (204800MB)...

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself!
Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.