Migrate openVZ Container to KVM/XEN VM

KVM Logo

I've been doing some virtual machine related stuff this week. I have a VM node which hosts openVZ containers, I need to move them to a new node with different virtualization, QEMU/KVM in this case.
The main different between KVM and openVZ is the level of virtualization, while openVZ is an OS level virtualization which mean the host's kernel shared to guest containers inside it, you can't use your own kernel. KVM is way different, It's a full virtualization, the whole OS and its kernel is virtualized so you can even install an OS with its own kernel in it, like installing Windows VM inside a linux host.

  • Converting OpenVZ to KVM box

Let's start the moving process.

  1. The first step before start moving the box is installing KVM box with exact same OS as openVZ container, preferably same point revision too.

  2. SSH into both VMs.

  3. In KVM box, or openVZ box, install mingetty. I'm using CentOS 6.2 so I use yum to install mingetty

yum install mingetty
  1. Edit /etc/inittab in the VM installed with mingetty
nano /etc/inittab

and add these lines

# Run gettys in standard run levels 1:2345:respawn:/sbin/mingetty tty1 2:2345:respawn:/sbin/mingetty tty2 3:2345:respawn:/sbin/mingetty tty3 4:2345:respawn:/sbin/mingetty tty4 5:2345:respawn:/sbin/mingetty tty5 6:2345:respawn:/sbin/mingetty tty6

This will enable the machine to actually get a logon prompt.

  1. Install rsync on both VMs
yum install rsync
  1. From the openVZ box, make a text file containing files to exclude. This will preserve settings in KVM box to make sure the VM is bootable.
nano /root/exclude.txt

add these lines to exclude.txt, you may customize it to your needs

/boot /proc /sys /tmp /dev /var/lock /etc/fstab /etc/mtab /etc/resolv.conf /etc/conf.d/net /etc/network/interfaces /etc/networks /etc/sysconfig/network* /etc/sysconfig/hwconf /etc/sysconfig/ip6tables-config /etc/sysconfig/kernel /etc/hostname /etc/HOSTNAME /etc/hosts /etc/modprobe* /etc/modules /net /lib/modules /etc/rc.conf /usr/share/nova-agent* /usr/sbin/nova-agent* /etc/init.d/nova-agent* /etc/ips /etc/ipaddrpool /etc/ips.dnsmaster /etc/resolv.conf /etc/sysconfig/network-scripts/ifcfg-eth0
  1. Now we can start to sync the OpenVZ box to the KVM box. In openVZ box, type:
rsync --exclude-from="/root/exclude.txt" --delete --numeric-ids -avpogtStlHz -e "ssh -p <ssh port>" / root@<target ip>:/

That's all, to make sure everything's working, reboot the KVM box.

I'm not using image/snapshot method, so I think this method will also work for KVM to OpenVZ migration, or maybe XEN/VMWare and vice versa.