Converting virtual machines from VirtualBox to KVM on ZFS

Context: I was running Virtual Box on my desktop for a mix of Windows testing VMs (for Internet Explorer) and GNU/Linux test servers for development. However, I also kind of like the idea of running some services from home, since not everything needs to be in a datacenter. My home Internet is reliable enough and connected to the local Montreal mesh network. I also like the idea of mirroring virtual machines that I host in a data center locally, and vice-versa, meaning that using ZFS snapshots that are backed-up nightly, I could re-spawn a VM in another location pretty quickly.

Long story short, I had to convert my VirtualBox VDI files to KVM. Disclaimer: This is by no means a complete guide, just personal notes that may or may not help you.

1- Convert the files from VDI to RAW, normally one would use:

$ cd /path/to/your/vboxfiles
$ VBoxManage clonehd machine.vdi machine.img --format RAW

On my VDI files, it was stuck at 0%. Is it meant only for static (non-dynamically allocated) files? No idea.

This worked for me:

$ cd /path/to/your/vboxfiles
$ vboxmanage internalcommands converttoraw machine.vdi machine.img

Practical command if you interrupt an operation and VirtualBox complains that the file is locked:

$ vboxmanage showhdinfo machine.vdi

You can unlock it with this command:

$ vboxmanage modifyhd --type normal machine.vdi

2- Create a ZFS volume with the same size:

$ ls -l machine.img
-rw------- 1 user user 8589934592 nov 25 10:47 machine.img

# zfs create -s -V 8589934592b mypool/machine

3- Copy the raw image into that partition:

# dd if=machine.img of=/dev/zvol/mypool/machine

4- Create the new virtual machine in KVM, using /dev/zvol/mypool/machine as its main disk.