Archive | July 2010

How-to convert an existing partition/disk in an image to virtualize it in linux qemu/kvm

Hi!
Yes, you can.
You can virtualize an existing windows/linux installation with qemu / kvm.
This isn’t a real news but here I write a very basic/fast tutorial.

Basically you have to choose between:

  • use your linux/windows hd by moving it to your kvm host server
  • convert your linux/windows to a kvm image (qcow2, row, ecc.) and run it on your kvm host server

Important note – 25/01/2011
Please note that when you virtualize a disk inside kvm, as we do in this tutorial, its OS find a new hardware configuration (fake/virtualized by kvm). This maybe a problem just because some operating systems (especially windows) doesn’t like that situation and panics with blue screen at boot & co©. To avoid this kind of problems make sure to uninstall specific drivers from your source disk OS before generate your image (especially windows!). Especially for windows (!!!) ensure to uninstall the IDE/SATA controller driver and replace it with general/universal driver. I have no problems like that to report for Linux os.

For the first choice the answer is simple: just use kvm’s -hda (or -drive) argument and point to your disk. Kvm and qemu can use a real hd without problems.

In order to convert an existing partition/disk to a qemu / kvm image (second choice) you have to follow these steps:

  • shutdown your linux/windows machine and reboot from cd with a live distro (use SystemRescueCd to follow this tutorial)
  • create a new folder to mount the image destination folder, it can’t be on our machine disk… we have to create our new kvm/qemu image from it!
    mkdir /mnt/image-dest
  • mount a remote samba partition with cifs in your destination folder (mount -t cifs //192.168.1.x/your-samba-share-name) or mount an usb drive, or a nfs partition, or your smartphone, that doesn’t matter! 🙂 You only need to transfer your windows/linux disk image to your qemu / kvm host.
  • umount your windows/linux partitions if one is mounted for any reason
  • create your disk raw image with dd from winows/linux disk; note that you have to copy ALL disk, not only a single partition. For example, if you have 3 partitions (/boot hda1 / hda2 and swap hda3) you have to use dd on your disk device, not on partitions. Wrong: dd if=/dev/hda1. Correct: dd if=/dev/hda. Please note that with dd you can kill your cat, burn your sister and immediately join Forza Italia. Use it with care!
    dd if=/dev/hda of=/mnt/image-dest/kvm-image.raw
  • fire up your kvm raw image: this may vary based on your source machine hardware configuration, if you have problems with missing partitions errors on your guest os boot phase just look at kvm / qemu -drive options (man kvm);
    kvm -drive file=/path/to/your/kvm-image.raw format=raw media=disk
  • optionally convert your raw image to other formats with kvm-img

TIP: Note that this process can be slow if your source disk is very large and may waste disk space on your destination host, especially if your source disk is large but only a small part of it’s used. In that case you can use the qemu-img tool to convert the physical disk to a kvm/qemu image: this tool allows you to use the -S/sparse option to convert (http://en.wikipedia.org/wiki/Sparse_file). The image of your source disk converted with the sparse option will be as big as the source used space.
Of course before convert with the sparse option you need a way to set to zero all the unused bytes in your source disk: for ext2, ext3 and ext4 partitions you can use the zerofree tool (https://packages.debian.org/wheezy/zerofree) or you can try another way (an easy way that works with all filesystems may be create a zero filled file on your source disk that fills its free space). Another way may be use a tool like parted to reduce partitions size and then copy only used disk space with a combo of dd’s ibs and count (untested).

Happy virtualization to you 🙂

UPDATE 2014-07-27: added a tip about big source disks or source disks with a lot of free space