Have you decided to switch from Oracle VirtualBox to Kernel-based Virtual Machine? Great! This 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 migrate the existing Virtualbox machines to KVM as described here.
Migrate Virtualbox VMs Into KVM VMs In Linux
First, power off all VMs hosted with KVM and VirtualBox.
The default disk image format of a Virtualbox VM is VDI.
We can find the list of virtualbox disk images and their location using command:
$ vboxmanage list hdds
$ VBoxManage list hdds
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
$ VBoxManage clonehd --format RAW /home/sk/VirtualBox\ VMs/CentOS\ 8\ Server/CentOS\ 8\ Server.vdi CentOS_8_Server.img
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.
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:
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:
Hope this helps.