1 Title: Migrating Xen virtual machines using LVM to KVM using disk images
2 Tags: english, debian, debian edu
5 <p>Most of the computers in use by the
6 <a href="http://www.skolelinux.org/">Debian Edu/Skolelinux project</a>
7 are virtual machines. And they have been Xen machines running on a
8 fairly old IBM eserver xseries 345 machine, and we wanted to migrate
9 them to KVM on a newer Dell PowerEdge 2950 host machine. This was a
10 bit harder that it could have been, because we set up the Xen virtual
11 machines to get the virtual partitions from LVM, which as far as I
12 know is not supported by KVM. So to migrate, we had to convert
13 several LVM logical volumes to partitions on a virtual disk file.</p>
16 <a href="http://searchnetworking.techtarget.com.au/articles/35011-Six-steps-for-migrating-Xen-virtual-machines-to-KVM">a
17 nice recipe</a> to do this, and wrote the following script to do the
18 migration. It uses qemu-img from the qemu package to make the disk
19 image, parted to partition it, losetup and kpartx to present the disk
20 image partions as devices, and dd to copy the data. I NFS mounted the
21 new servers storage area on the old server to do the migration.</p>
27 # http://searchnetworking.techtarget.com.au/articles/35011-Six-steps-for-migrating-Xen-virtual-machines-to-KVM
33 echo "Usage: $0 <hostname>"
39 if [ ! -e /dev/vg_data/$host-disk ] ; then
40 echo "error: unable to find LVM volume for $host"
44 # Partitions need to be a bit bigger than the LVM LVs. not sure why.
45 disksize=$( lvs --units m | grep $host-disk | awk '{sum = sum + $4} END { print int(sum * 1.05) }')
46 swapsize=$( lvs --units m | grep $host-swap | awk '{sum = sum + $4} END { print int(sum * 1.05) }')
47 totalsize=$(( ( $disksize + $swapsize ) ))
50 #dd if=/dev/zero of=$img bs=1M count=$(( $disksize + $swapsize ))
51 qemu-img create $img ${totalsize}MMaking room on the Debian Edu/Sqeeze DVD
53 parted $img mklabel msdos
54 parted $img mkpart primary linux-swap 0 $disksize
55 parted $img mkpart primary ext2 $disksize $totalsize
56 parted $img set 1 boot on
59 losetup /dev/loop0 $img
62 dd if=/dev/vg_data/$host-disk of=/dev/mapper/loop0p1 bs=1M
63 fsck.ext3 -f /dev/mapper/loop0p1 || true
64 mkswap /dev/mapper/loop0p2
70 <p>The script is perhaps so simple that it is not copyrightable, but
71 if it is, it is licenced using GPL v2 or later at your discretion.</p>
73 <p>After doing this, I booted a Debian CD in rescue mode in KVM with
74 the new disk image attached, installed grub-pc and linux-image-686 and
75 set up grub to boot from the disk image. After this, the KVM machines
76 seem to work just fine.</p>