TurnKey Linux Virtual Appliance Library

Converting a virtual disk image: VDI to VMDK to a raw loopback file you can mount

By default, VirtualBox creates virtual disk images in a special format called VDI, which is unique to VirtualBox. Disk images are stored in $HOME/.VirtualBox/HardDisks.

You'll need to convert VDI into another format if you want to run a VirtualBox VM on another virtualization platform, such as VMWare or KVM.

The VMDK virtual disk format is a good choice because even though it originated with VMWare it is supported by other virtualization platforms including VirtualBox and KVM.

VirtualBox enables the conversion using the low-level "VBoxManage clonehd" command:

VBoxManage list hdds # prints a list of disk image UUIDs
VBoxManage clonehd <UUID> -o converted.vmdk format VMDK
cd ~/.VirtualBox/HardDisks/
ls -la converted.vmdk

Once you have converted to VMDK you can use qemu-img, a tool bundled with qemu (KVM's virtualization backend) to further convert VMDK to other formats.

A particularly useful format to convert to is 'raw' which you can then mount as a loopback device:

apt-get install qemu
qemu-img convert -f vmdk converted.vmdk -O raw converted.raw
mount -o loop converted.raw /mnt

Ever had to convert between disk image formats? Post a comment!

You can get future posts delivered by email or good old-fashioned RSS.
TurnKey also has a presence on Google+, Twitter and Facebook.

Comments

qcow2 for avoiding the size of a raw file

Just a note, but the biggest disadvantage is that raw will exctract a file that is the same size as the compressed drive onto your system. Usually the native file system has some compression but this is still a problem if you have 1000 gig disk images. 

A work-around is to use kvm-nbd or qemu-nbd which are part of the kvm package. This creates a network block device attached to a qcow2 image which you can mount instead of loop.  This will sve you the trouble of having a 1 TB file sitting on your drive.

 

Thanks!

Liraz Siri's picture

qemu-nbd looks very useful

Usually the filesystems we play with during appliance development are pretty small, so the size of a raw loopback file is manageable. For larger systems it can be a problem.

I haven't tried the nbd tool yet, but it sounds like a very useful trick. Might have other creative uses as well. Thanks for sharing!

PS: Sorry for the late response. We're in the middle of a development cycle and I'm spending most of my time offline.

It sometimes is handy to mount RAW images via loopback

 We can directly create RAW images with newer versions of VBoxManage clonehd <uuid>|<filename> <outputfile> [--format VDI|VDMK|VHD|RAW|<other>] ...

Though, I'm not sure what that <other> could be.

 

Yet another way

This way it should also work to directly convert VDI to (uncompressed) RAW:

VBoxManage internalcommands converttoraw file.vdi file.raw

Directly mounting a partition from inside a .vdi file: qemu-nbd

This post notes how to use qemu-nbd to directly get at the partitions in a .vdi file - no need to convert to vmdk first.

http://bethesignal.org/blog/2011/01/05/how-to-mount-virtualbox-vdi-image/

contingency plans for virtualbox

I am curious about <a href="http://blog.thehigheredcio.com/2011/08/25/vdi-and-business-continuity/">business continuity for virtual desktops</a> and what a VritualBox approach might be for comparison to other solutions.

Mount Dynamic VDI on CentOS

I run CentOS 5.5. My nbd kernel module does not support partitions, at least I don't see /dev/nbd0p1 etc., but do get a /dev/nbd0. Here are my commands:

# modprobe nbd

# qemu-nbd -c /dev/nbd0 <some-dynamic>.vdi

# mount -o loop,offset=32256 /dev/nbd0 /mnt/nbd0p1

That's it! The offset was found by looking at the starting sector in the fdisk output. It lists 63 in my case which is 63 * 512 = 32256 bytes.

mount -o loop,offset=32256 /dev/nbd0 /mnt/nbd0p1

another way to access the partitions in a raw disk is kpartx (Fedora)

# kpartx -a /dev/nbd0

one can now mount, use and eject the filesystems in nautilus

# kpartx -d /dev/nbd0

Simple solution

Having VirtualBox GUI installed, you need at least one VM to manage disks, then you can "Copy" VDI disk to VMDK.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <strike> <caption>

More information about formatting options

Leave this field empty. It's part of a security mechanism.
(Dear spammers: moderators are notified of all new posts. Spam is deleted immediately)