Background

I have a batch of nodes running on Xen, and the management overhead is high, so the plan is to consolidate on KVM. This post records how I migrated the VMs off XenServer, with the services still working afterwards.

Environment

  • XenServer 7.x node: 192.168.122.20
  • Gentoo KVM host: 192.168.122.1
  • nginx VM: 192.168.122.22

The goal is to move the nginx VM from XenServer to KVM and keep the service running as before.

XenServer VM Configuration

To make the VM easier to manage on KVM later, first add a serial console:

Edit /etc/default/grub and add the following to the GRUB_CMDLINE_LINUX line:

GRUB_CMDLINE_LINUX=".... net.ifnames=0 biosdevname=0 console=ttyS0,115200n8"

Generate a new GRUB configuration:

grub2-mkconfig -o /boot/grub2/grub.cfg

Migration

Export the Disk Image

List the disks attached to the VM:

xe vm-disk-list name-label=nginx

The disk to export is the one on the uuid (RO) line:

Disk 0 VDI:
uuid ( RO)             : 622bd53f-56fc-4e41-954c-c7e164a092bc
       name-label ( RW): nginx 0
    sr-name-label ( RO): Local storage
     virtual-size ( RO): 10737418240

Export the disk:

xe vdi-export uuid=622bd53f-56fc-4e41-954c-c7e164a092bc filename=nginx.vhd format=vhd

Copy the resulting VHD to the KVM node.

Boot It on KVM

Convert the disk format first:

qemu-img convert -p -f vpc nginx.vhd -O qcow2 nginx.qcow2

Boot the VM with virt-install:

sudo virt-install \
--name nginx --memory 4096 \
--vcpus 4 \
--disk /home/chris/workspace/qemu/disk/nginx.qcow2 \
--network network=default,model=virtio \
--cpu=host-passthrough \
--import \
--os-variant centos7 \
--graphics=vnc,password=000000,listen=0.0.0.0,port=5900

The VNC password is set to 000000. That’s only fine for a lab on an internal network; don’t copy it to a public-facing machine.

It wouldn’t boot into the system at all: the bootloader was broken. I attached an Arch Linux ISO and fixed it from the live environment.

Set the root password in the live environment, then start sshd:

passwd
systemctl start sshd

Check the assigned IP:

ip a

Connect over SSH and create the mount points:

mkdir -pv /mnt/centos

Mount the original system’s partitions:

mount /dev/mapper/centos-root /mnt/centos
mount /dev/vda1 /mnt/centos/boot

Wipe the original system’s /boot:

rm -rf /mnt/centos/boot/*

Then chroot in and reinstall the kernel and GRUB:

mount --bind /proc /mnt/centos/proc
mount --bind /dev /mnt/centos/dev
mount --bind /sys /mnt/centos/sys
mount --bind /run /mnt/centos/run
chroot /mnt/centos /bin/bash

Install GRUB and the kernel, then regenerate the configuration:

. /etc/profile
yum install grub2 -y
grub2-install /dev/vda
yum install kernel -y
grub2-mkconfig -o /boot/grub2/grub.cfg

Exit the chroot:

exit

Unmount the mount points and reboot:

umount -R /mnt/centos
reboot

Verification

After the reboot the system came up normally and the nginx service was running.

Potential Problems with Manual Migration

This manual approach is rough, and it doesn’t scale once you have many nodes.

If there are many disks, export the VDIs to NFS on the XenServer first and mount that NFS from the KVM node, so you don’t have to copy data back and forth.

The bootloader is another pain point: you have to boot a LiveCD and reinstall it. I never dug into exactly why.

Also, the virtual NIC model and MAC address both change. If the system hardcodes the interface name, MAC, or IP, you have to update them after the migration or the network won’t come up.