keepcalculator

06 Numbers and Powers

Migrate VirtualBox VMs to KVM: Full Guide

Card No. 06·84085 · Updated · Standard formula, shown below

Yes, you can migrate VirtualBox VMs to KVM, and the most reliable method is to convert the VirtualBox disk image to a KVM-compatible format (qcow2 or raw) using qemu-img, then create a new KVM VM that uses the converted disk. This guide walks you through the entire process, from preparation to post-migration checks, including common pitfalls and how to avoid them.

Why Migrate from VirtualBox to KVM?

KVM (Kernel-based Virtual Machine) is the native virtualization solution for Linux, offering better performance, tighter integration with the host kernel, and support for features like VirtIO drivers that can significantly boost I/O speed. VirtualBox, while excellent for desktop use and cross-platform support, often runs as a user-space process with an additional abstraction layer. If you are running a Linux server or a developer workstation, KVM can provide lower overhead and better scalability. Migrating also lets you consolidate your virtualization stack and use tools like libvirt, virsh, and virt-manager for management.

Before You Start: What You Need

Before touching any files, verify you have the following:

  • KVM installed on the target host: kvm, qemu-kvm, libvirt-bin (or equivalent), and virtinst for creating VMs from the command line.
  • qemu-img: This tool is part of the QEMU package and is used to convert disk images.
  • Enough disk space: The conversion process creates a new file, so you need at least the size of the original disk image free on the destination storage.
  • Shut down the VirtualBox VM: Do not convert a running VM’s disk; you will get a corrupted image. Use VBoxManage list runningvms to check.

Step-by-Step Migration Process

The core steps are straightforward, but each one has details that can trip you up.

  1. Locate the VirtualBox disk image: By default, VirtualBox stores VDI files in ~/VirtualBox VMs/<VM Name>/<VM Name>.vdi on Linux, or C:\Users\<User>\VirtualBox VMs\ on Windows. You can also find the exact path via VBoxManage showvminfo <VM Name>.
  2. Convert the disk image: Use qemu-img convert to change the format. For example, to convert a VDI to qcow2 (the default KVM format):
    qemu-img convert -f vdi -O qcow2 /path/to/disk.vdi /path/to/output.qcow2

    If your source is a VMDK (from VMware or VirtualBox with VMDK), use -f vmdk. The -O flag specifies the output format; you can also use raw if you prefer a flat file.

  3. Create a new KVM VM: Using virt-install (from virtinst), you can create a VM that uses the converted disk. Example:
    virt-install \
      --name my-vm \
      --memory 2048 \
      --vcpus 2 \
      --disk path=/path/to/output.qcow2,format=qcow2 \
      --import \
      --os-variant ubuntu22.04

    Replace the memory, vcpus, and os-variant with your VM’s specs. The --import flag tells virt-install to use the existing disk rather than installing a new OS.

  4. Boot the VM: Start the VM with virsh start my-vm or virt-manager. If the VM fails to boot, you may need to adjust the disk controller (see below).

Handling Disk Controllers and VirtIO Drivers

The most common migration issue is that the VM’s OS doesn’t have the drivers for the KVM virtual hardware. VirtualBox emulates an IDE or SATA controller by default, while KVM often uses VirtIO for better performance. If you boot the converted VM and get a blue screen (Windows) or kernel panic (Linux), the OS can’t find the disk because it lacks the VirtIO driver.

Solution for Linux VMs: Most modern Linux distributions include VirtIO drivers in the kernel, so they usually boot fine. If you have an older distro, you may need to add the virtio kernel module manually before converting.

Solution for Windows VMs: Before converting, download the VirtIO drivers (from the Fedora project, for example) and install them inside the VirtualBox VM. Alternatively, after conversion, you can attach the disk to a KVM VM with an IDE controller (set --disk bus=ide in virt-install) to boot, then install the VirtIO drivers and switch to VirtIO later.

Post-Migration Configuration and Optimizations

Once the VM boots, you should update the guest tools and drivers:

  • Remove VirtualBox Guest Additions: These are unnecessary and can cause conflicts. Uninstall them from the guest OS.
  • Install KVM guest agents: For Linux, install qemu-guest-agent to enable features like proper shutdown and clipboard sharing. For Windows, install the VirtIO guest tools.
  • Enable VirtIO for all devices: If you initially used IDE for the disk, you can later hot-swap to VirtIO (requires a reboot) for better performance. Also consider VirtIO for network and balloon devices.
  • Adjust the display: KVM uses VGA or QXL for graphics. Install the appropriate drivers (e.g., xserver-xorg-video-qxl on Linux) for better resolution.

Common Problems and How to Fix Them

Here are typical issues you might encounter:

  • VM doesn’t boot after conversion: Check the disk bus (use IDE as a fallback), and ensure the OS has the right drivers. Also verify the disk image isn’t corrupted by running qemu-img check on the converted file.
  • Network not working: KVM’s default network is NAT via a bridge. Ensure the VM’s network interface is set to virtio and that the guest has the correct drivers. For Linux, you might need to reconfigure the network interface name (e.g., eth0 to ens3).
  • Performance is worse than VirtualBox: This is usually due to missing VirtIO drivers. Install them and ensure the VM is using the KVM acceleration (check virsh capabilities for kvm).
  • Disk image is larger than expected: If you converted a dynamic VDI to a qcow2, it should be similar in size. If you used raw, it will be the full virtual size. You can shrink a qcow2 with qemu-img convert again, but only if the guest OS has zeroed free space.

Alternatives and Other Migration Paths

While qemu-img convert is the standard method, there are other approaches:

  • Clone the disk while the VM is running: Tools like virt-p2v (for physical-to-virtual) can also convert a running VirtualBox VM, but this is riskier. Always prefer a clean shutdown.
  • Use virt-v2v: This tool can directly convert a VirtualBox VM to KVM, including adjusting drivers and configuration. It is designed for this purpose and can handle Windows and Linux guests. Example: virt-v2v -i vbox -o libvirt.
  • Export to OVA/OVF: VirtualBox can export a VM as OVA, which contains a VMDK disk. You can then import that VMDK into KVM using qemu-img convert or virt-v2v.

Costs and Considerations

Both VirtualBox and KVM are open-source and free, so there are no licensing costs. However, migration has indirect costs: time spent on conversion, potential downtime during the switch, and the need to reconfigure the guest environment. KVM’s performance benefits can reduce server costs if you are running many VMs, as it uses fewer resources per VM. For a single desktop VM, the difference may be negligible.

Safety and Backup

Before converting, always back up the original VirtualBox disk image. The conversion process itself is non-destructive (it creates a new file), but you might make mistakes in the new VM configuration. Keep the original until you have verified the KVM VM works correctly. Also, ensure the target host has sufficient disk space to avoid a failed conversion that leaves a partial file.

Frequently Asked Questions

Can I migrate a running VirtualBox VM to KVM without shutting it down?

Technically, you can use qemu-img convert with the -S option to copy a snapshot, but this is risky and not recommended. The safest method is to shut down the VM, convert the disk, and then boot it in KVM. For zero-downtime migration, you would need to set up a live migration, which is complex and not supported between VirtualBox and KVM directly.

Will my Windows VM work after conversion?

Yes, but you will likely need to install VirtIO drivers. The easiest way is to install them inside the VirtualBox VM before conversion. If you forget, you can boot the converted VM with an IDE disk controller (using --disk bus=ide) and then install the drivers, then switch to VirtIO.

What is the difference between qcow2 and raw for KVM?

qcow2 is a copy-on-write format that supports snapshots, compression, and encryption. It is the default for KVM and is generally recommended. Raw is a flat, uncompressed image that offers slightly better performance but takes up more space and lacks advanced features. For most users, qcow2 is the better choice.

Can I convert a VDI file directly to a KVM disk without using qemu-img?

Yes, you can use virt-v2v which automates the process and also handles driver changes. However, qemu-img is more straightforward if you just need a disk conversion and are comfortable with manual configuration.

Will my snapshots from VirtualBox be preserved?

No, qemu-img convert only converts the current state of the disk. If you have snapshots in VirtualBox, you need to either merge them into the current state (via VBoxManage snapshot discard) or use a tool like virt-v2v which can handle snapshots in some cases. The easiest path is to discard all snapshots before conversion.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →