Convert VirtualBox VM to KVM
Jim O'Halloran • July 8, 2015
linux-and-open-sourceThis post outlines the procedure to convert a (Linux) vm from VirtualBox to KVM. Preferably VMs would be built on KVM directly, but this is the process used to migrate a VM built prior to standing up KVM infrastructure into KVM. Much was learned during this process which I wanted to capture before it fades from memory.
UPDATE 31/7/2021: These instructions were accurate at the time of writing in 2015, but I haven't had need of this process recently and can't vouch for their accuracy in 2021.
On the Virtualbox host:
- Delete all snapshots of the VM so that there is a single, canonical disk image file that can be converted.
- Shutdown the VM cleanly
- Run
mkdir ~/tmp && cd ~/tmp
to create a temporary folder to work in. - If you've converted the disk image previously use the "Virtual Media Manager" in Virtualbox, and find and delete the .raw file.
- Run
VBoxManage clonehd ../VirtualBox\ VMs/VMNameHere/VMNameHere-disk1_2.vmdk ./VMNameHere.raw --format raw
to export the virtualbox to a raw disk image. Make sure the source file name is correct and confirm that it has a current date. - Run
qemu-img convert -f raw VMNameHere.raw -O qcow2 VMNameHere.qcow
to convert the raw image to the QCOW2 format KVM/QEMU uses. - SCP the .qcow2 file to the KVM server. I dropped it into my home directory.
On the KVM server:
- Log in via ssh and su to root.
- Change to the KVM images folder:
cd /var/lib/libvirt/images
- Move the QCOW2 file into the KVM images folder and set it's
permissions:
mv /home/jim/VMNameHere.qcow2 . && chown qemu:qemu VMNameHere.qcow2 && chmod 644 VMNameHere.qcow2
- Using virt-manager from your workstation:
- Create a new vm with the following settings:
- Import Existing Disk Image
- Browse and find your QCOW2 file
- Select the appropriate OS (CentOS 7 is available under "Show Other Linux Versions")
- Set memory and CPU to be the same as the VirtualBox vm
- Name the VM appropriately (internal host name of the vm is the convention I use)
- Tick "Customise configuration before install"
- Expand "Advanced Options" and select the correct network type, and give the interface the same MAC address as the virtualbox VM.
- When customising hardware, add a Storage device
- Type CDROM
- Download a CentOS 7 ISO and place it in the
images
folder, or use a different ISO if the machine you're converting isn't Centos 7.
- Set the CROM to be the first boot device
- Boot the VM and select "Rescue an Installed Centos System" from the boot menu
- Once the VM has booted to the rescue mode shell run the following commands via
the VM's console:
chroot /mnt/sysimage
vi /boot/grub2/device.map
- Change any /dev/sda references to /dev/vda
grub2-mkconfig -o /etc/grub2.cfg
vi /etc/dracut.conf
- Look for the
#add_devices+=""
line and change it toadd_devices+="virtio_blk"
- Run
ls /lib/modules
and find the latest kernel version that's installed. The kernel version on the CD ISO may not match the latest in the vm, so we'll rebuild Dracut for a specific version instead. - Run
dracut kver <version found above> force
- Run
ls -al /boot
and check that there's an initramfs-*.img file with a current time stamp (i.e. Dracut has rebuilt initramfs correctly). exit
exit
- The vm should reboot. Press a key on the grub menu to hold the machine there and stop it from booting.
- Force Power off the VM.
- Change the boot order so that the "VirtIO Disk" is the first and only boot option, and set the VM to start automatically on host boot up.
- Start the VM, and ensure that the kernel version used previously is selected on the grub menu.
- The VM may reboot once during the initial boot up process, but should be good to go after that.
And you're done!