Here is how I eventually managed to do the conversion:
- On the openvz host, create an image (in my case, I need 20GB):
dd if=/dev/zero of=test.img bs=516096c count=40000 - Create a partition table in the image:
fdisk test.img
Type:o n p 1 2048 w - Mount the image using the offset corresponding to the partition table (in our case, 2048*512=1048576):
losetup -o1048576 /dev/loop0 test.img
mke2fs -b1024 /dev/loop0
tune2fs -j /dev/loop0
mount /dev/loop0 /mnt - Retrieve the UUID of the partition. Note it somewhere, you will need it several times:
blkid /dev/loop0 - Copy the container files to the image. I used rsync to exclude some data that were too big. Make sure to use
--numeric-idsto prevent bad mapping of UIDs:
rsync -av --exclude 'somedir' --numeric-ids /vz/root/1234/ /mnt - Install grub on the disk:
grub-install --root-directory /mnt /dev/loop0 - Chroot into the mounted partition:
chroot /mnt - Install a kernel and grub (using apt for example):
apt-get install linux-image-foobar #adapt to your kernel version
apt-get install grub-pc - Give a password to root:
passwd - Change inittab to add the ttys:
1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
6:23:respawn:/sbin/getty 38400 tty6 - Create an /etc/fstab file (change the UUID with yours):
proc /proc proc nodev,noexec,nosuid 0 0
UUID=7925e5b1-f2ad-4cdc-95f9-984d25378194 / ext4 errors=remount-ro 0 1 - Umount the image and convert it to a vmdk image:
umount /mnt
kvm-img convert -f raw test.img -O vmdk test.vmdk - Boot the machine on the virtual disk with VMWare or Virtualbox. You should get a grub prompt (adapt with your UUID). In grub2, you can use autocompletion for the Linux kernel and the initrd image:
grub> insmod ext2
grub> set root='(hd0,msdos1)'
grub> linux /boot/vmlinuz-foobar root=UUID=7925e5b1-f2ad-4cdc-95f9-984d25378194 ro
grub> initrd /boot/initrd-foobar
grub> boot - Log in to the machine and run
update-grub - Adapt your network settings






