Export a "LVM based" domU for use with KVM (or XEN-HVM)
Small lazy for converting a LVM based domU to a more portable raw file for migration to an other virtualization system or other XEN host
Specifics for this example
/dev/vg_raid10/<domU>-disk 8GB /dev/vg_raid10/<domU>-swap 2GB
Create a 10G raw image
$ qemu-img create export.raw 10G
Boot kvm with a live CD iso (e.g. Debian netinst) and partition export.raw as hda (8G=>83:Linux & 2G=>82:Linux swap). Be very careful in selecting the size for the linux partition (double check the exact logical volume size).
$ kvm -hda export.raw -cdrom <iso file> -m 512
After export.raw partitioning (and shutting down kvm) the raw file should look as follows when queried from the host
$ fdisk -lc ./export.raw You must set cylinders. You can do this from the extra functions menu. Disk ./export.raw: 0 MB, 0 bytes 255 heads, 63 sectors/track, 0 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x22455093 Device Boot Start End Blocks Id System ./export.raw1 1 1045 8393931 83 Linux ./export.raw2 1046 1305 2088450 82 Linux swap / Solaris
Install kpartx
$ sudo apt-get install kpartx
Prepare for mounting the partitions in the raw image on host.
$ sudo kpartx -v -a ./export.raw add map loop2p1 (254:16): 0 16787862 linear /dev/loop2 63 add map loop2p2 (254:17): 0 4176900 linear /dev/loop2 16787925
Create swap on the swap partition
$ sudo mkswap /dev/mapper/loop2p2 mkswap: /dev/mapper/loop2p2: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 2088444 KiB no label, UUID=ca2d322e-a245-43c0-9d6a-5ebf663818e8
Copy data
$ sudo dd if=/dev/<vg_name>/<domU>-disk of=/dev/mapper/loop2p1 bs=4k
Do a file system check (ext3 used in example)
$ sudo e2fsck -f /dev/mapper/loop2p1
Remove device mappings in host
$ sudo kpartx -d ./export.raw
Boot kvm with a debian net-install CD iso in rescue mode
$ kvm -hda export.raw -cdrom <iso file> -m 512 -boot d 1. Edit /etc/fstab to reflect the new device name (and partition numbers) xvda1(swap) -> sda2 + xvda2(disk) -> sda1 2. Clear mtab (cat /dev/null > /etc/mtab) 3. exit rescue mode and enter it again 4. start bash 5. apt-get update 6.1 apt-get install linux-image-2.6-amd64 # install non XEN kernel 6.2 apt-get purge linux-image-xen-amd64 # purge this and all other xen kernel packages 7.1 apt-get install grub-pc 7.2 Linux cmd line shall be "root=/dev/sda1 ro" 7.3 Install grub on /dev/sda (QEMU_HARDDISK) 8. Change getty console from hvc0 to tty1 in /etc/inittab 9. Exit rescue mode
Note: sda is for KVM use, if converting for XEN HVM use xvda (in other words just switch the partition numbers)
That's it, export.raw can now be used with KVM (or XEN HVM).