Are you decided to switch from Oracle VirtualBox to Kernel-based Virtual Machine? Great! This step by step guide explains how to migrate Virtualbox VMs into KVM VMs in Linux.
You might have running some important guest machines on VirtualBox. Instead of creating new KVM guests with same configuration, you can easily convert the existing Virtualbox machines to KVM as described here.
Table of Contents
Migrate Virtualbox VMs into KVM VMs in Linux
First, power off all running virtual machines in VirtualBox.
For the purpose of this guide, I will be using CentOS and FreeBSD virtual machines that is hosted in VirtualBox.
As you may already know, the default disk image format of a KVM virtual machine is .qcow2
and Virtualbox VM is .vdi
.
We can convert a vdi disk image file to qcow2 in two ways. We can directly convert the vdi
image to qcow2
format.
If it doesn't work for any reason, we can first convert the vdi
format to raw format and then convert the raw format to qcow2
.
1. Convert vdi image format to qcow2 image format
Let me show you the disk image type of my CentOS VM created using VirtualBox.
$ ls CentOS\ 8\ Server/
Sample output:
CentOS 8 Server.vbox CentOS 8 Server.vbox-prev CentOS 8 Server.vdi Logs
As you may have noticed, CentOS VM disk image's file type is .vdi. For those wondering, VDI is the native format of VirtualBox. VirtualBox uses this format by default when we create a new virtual machine.
And here is the file type of the FreeBSD disk image:
$ ls FreeBSD\ 12/
FreeBSD-12.0-RELEASE-amd64.vhd freebsd12.qcow2 FreeBSD 12.vbox FreeBSD 12.vbox-prev Logs
Here, freebsd disk image file type is .vhd. VHD is the native format of Microsoft Virtual PC.
I created both virtual machines in VirtualBox. But Virtualbox used .vdi
format for CentOS and .vhd
format for FreeBSD.
Both VDI and VHD formats are supported very well by VirtualBox.And both image types can be easily converted to qcow2 format.
Now let us get back to the topic - how to convert the Virtualbox machine disk images to the KVM supported disk image type using "qemu-img"
command?
Well, it is easy! The qemu-img
command is used to convert virtual machine disk images to various different formats, including qcow2
, raw
, vdi
, vhd
, and vmdk
.
To convert a vdi
image to qcow2
image using qemu-img
command, run:
$ qemu-img convert CentOS\ 8\ Server/CentOS\ 8\ Server.vdi -O qcow2 centos8.qcow2
The above command will convert the given vdi
file to qcow2
format.
Here, we used -O qcow2
to explicitly specify the output format. Of course, you don't have to explicitly specify the output format and simply specify the input and output format like below:
$ qemu-img convert CentOS\ 8\ Server/CentOS\ 8\ Server.vdi centos8.qcow2
But, it will create a larger size image.
Similarly, you can convert the vhd
image file to qcow2
image using the following command:
$ qemu-img convert FreeBSD\ 12/FreeBSD-12.0-RELEASE-amd64.vhd -O qcow2 freebsd12.qcow2
This is how we migrate Virtualbox Virtual machines to KVM virtual machines.
Now head over to the "Create a KVM virtual machine from Qcow2 image" section given below to know how to create new KVM virtual machine using the Qcow2 images.
Just in case the above method doesn't work (which is very unlikely), you can use the following method to convert vdi
images to qcow2
images.
2. Convert vdi to raw image format and then to qcow2 image format
First, find the list list of available virtualbox disk images and their location using command:
$ vboxmanage list hdds
Or,
$ VBoxManage list hdds
Sample output:
UUID: ecfb6d5c-aa10-4ffc-b40c-b871f0404da8 Parent UUID: base State: created Type: normal (base) Location: /home/sk/VirtualBox VMs/CentOS 8 Server/CentOS 8 Server.vdi Storage format: VDI Capacity: 20480 MBytes Encryption: disabled UUID: 34a5709f-188c-4040-98f9-6093628c3d88 Parent UUID: base State: created Type: normal (base) Location: /home/sk/VirtualBox VMs/Ubuntu 20.04 Server/Ubuntu 20.04 Server.vdi Storage format: VDI Capacity: 20480 MBytes Encryption: disabled
As you can see, I have two virtualbox VMs.
Now I am going to convert CentOS 8 machines' disk image to a raw disk format using vboxmanage
command:
$ vboxmanage clonehd --format RAW /home/sk/VirtualBox\ VMs/CentOS\ 8\ Server/CentOS\ 8\ Server.vdi CentOS_8_Server.img
Or,
$ VBoxManage clonehd --format RAW /home/sk/VirtualBox\ VMs/CentOS\ 8\ Server/CentOS\ 8\ Server.vdi CentOS_8_Server.img
Sample output:
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% Clone medium created in format 'RAW'. UUID: afff3db8-b460-4f68-9c02-0f5d0d766c8e
The RAW image is too big to use. In my case, RAW image is ten times bigger than Qcow2 image.
$ du -h CentOS_8_Server.img 21G CentOS_8_Server.img
So let us convert the RAW image format into KVM disk format i.e. compressed qcow2 using qemu-img command:
$ qemu-img convert -f raw CentOS_8_Server.img -O qcow2 CentOS_8_Server.qcow2
Done! We have converted Virtualbox disk image format VDI into KVM image format qcow2.
Check the size of the Qcow2 image:
$ du -h CentOS_8_Server.qcow2 2.1G CentOS_8_Server.qcow2
See? Qcow2 is much smaller than RAW image.
3. Create a KVM virtual machine from Qcow2 image
You can now create a new KVM instance by importing the virtual disk image file from command line or using any graphical KVM management applications like Virt-manager or Cockpit web console.
Refer the following guide for more details:
Troubleshooting
Sometimes you might have deleted the virtual disk without properly releasing it from Virtualbox media manager.
When you try to create new RAM image from the same VDI file, you will get an error something like - "hard disk with UUID already exists"
.
To fix this issue, you must release the virtual disk from the Virtualbox and try again to convert the image. Refer the following guide to know how to do this:
Conclusion
In this guide, we have seen how to migrate Virtualbox VMs into KVM VMs in Linux. We also looked at how to create a new KVM instance by importing the Qcow2 image file.
Related read:
5 comments
Hi,
Thanks a lot
Very useful article
I’ve already been able to get this far. I’ve also been able to import the qcow2 into Proxmox VE (KVM) without issues IF the VirtualBox VM was created using BIOS instead of UEFI.
Would you please cover any extra steps for a UEFI VM going into KVM, even if it’s not Proxmox?
Thanks
I added this ot my to-do list. I will try it in my test machine and post a guide.
So, how big is the RAW image format in comparison to VDI and qcow2 usually? Does it have other disadvantages?
RAW image is ten times bigger than Qcow2 image. Except the size, it doesn’t have any disadvantages as far as I know.