]> pere.pagekite.me Git - homepage.git/blob - blog/data/2010-11-22-xen-kvm-migration.txt
Generated.
[homepage.git] / blog / data / 2010-11-22-xen-kvm-migration.txt
1 Title: Migrating Xen virtual machines using LVM to KVM using disk images
2 Tags: english, debian, debian edu
3 Date: 2010-11-22 11:20
4
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>
14
15 <p>I found
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>
22
23 <pre>
24 #!/bin/sh
25
26 # Based on
27 # http://searchnetworking.techtarget.com.au/articles/35011-Six-steps-for-migrating-Xen-virtual-machines-to-KVM
28
29 set -e
30 set -x
31
32 if [ -z "$1" ] ; then
33 echo "Usage: $0 &lt;hostname&gt;"
34 exit 1
35 else
36 host="$1"
37 fi
38
39 if [ ! -e /dev/vg_data/$host-disk ] ; then
40 echo "error: unable to find LVM volume for $host"
41 exit 1
42 fi
43
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 ) ))
48
49 img=$host.img
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
52
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
57
58 modprobe dm-mod
59 losetup /dev/loop0 $img
60 kpartx -a /dev/loop0
61
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
65
66 kpartx -d /dev/loop0
67 losetup -d /dev/loop0
68 </pre>
69
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>
72
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>