Migration of OpenVZ Container to KVM Guest
By Roman Pertl
This is a short tutorial how to migrate an OpenVZ container to a KVM Guest. Some ideas have been taken from other tutorials (1 and 2), the other half has been extracted from grml-debootstrap which can generate a KVM guest by using debootstrap.
# form where to where migrate
# don't forget the trailing / on the source!
export SOURCE=/srv/vz/private/xxxxxxx/
# an empty LVM volume!
export DEST=/dev/mapper/vg0-myfirstvm
# create some magic for grub/partion table
echo 4 66 | /usr/share/grml-debootstrap/bootgrub.mksh -A | sudo dd of=${DEST} conv=notrunc
sudo dd if=/dev/zero bs=1 conv=notrunc count=64 seek=446 of=${DEST}
# this is a partition with a partion table on on its own
sudo kpartx -av ${DEST}
# create partition
sudo parted -s "${DEST}" 'mkpart primary ext4 2M -1'
# create filesystem
sudo mkfs.ext4 "${DEST}p1"
# mount it
sudo mount "${DEST}p1" /mnt
# not it's time to rsync your files to the destination folder (/mnt)
sudo rsync -av --numeric-ids --stats --progress ${SOURCE}/ /mnt
# mount some needed thins inside the KVM guest
sudo mount -t proc none /mnt/proc
sudo mount -t sysfs none /mnt/sys
sudo mount --bind /dev /mnt/dev
# fix fstab
echo `sudo blkid -o export "${DEST}p1" |grep UUID ` / ext4 defaults,noatime 0 0 | sudo tee -a /mnt/etc/fstab
# install kernel + grub + acpi
# IMPORTANT: during install you are asked to install grub - DO NOT install on any disks/partitions!
sudo chroot /mnt aptitude install linux-image-amd64 linux-headers-amd64 busybox firmware-linux-free firmware-linux grub-pc acpid acpi-support-base
# the last command installed and started acpid, so we need to stop it again
sudo chroot /mnt service acpid stop
# configure grub
sudo chroot /mnt/ grub-mkimage -O i386-pc -p '(hd0,msdos1)/boot/grub' -o /tmp/core.img biosdisk part_msdos ext2
# ATTEENTION: wheezy uses an old gurb and you need to copy it to boot/grub directly!
sudo cp -rp /mnt/usr/lib/grub/i386-pc /mnt/boot/grub
sudo dd if=/mnt/tmp/core.img of=$DEST conv=notrunc seek=4
sudo chroot /mnt update-grub
# cleanup
sudo umount /mnt/proc
sudo umount /mnt/sys
sudo umount /mnt/dev
sudo umount /mnt
sudo kpartx -d "${DEST}"
# you may need to remount and rerun 'update-grub'
# you should check /boot/grub/gurb.cfg within the virtual server that the linux kernel command arguments contain the correct root parameter (with a UUID)!
# create the KVM VM with virt-install (see my previous post about KVM)
Update 2016/09/20: with some minor fixes and one missing command (copy grub/i386-pc directory)
Update 2016/10/04: I’ve found one more interesting post by ch: https://christian.hofstaedtler.name/blog/2012/07/openvz-to-kvm.html