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 sorce 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
Note that this process can be slow if your source disk is very large, in that case I think you can 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
Tags: convert disk in image for kvm qemu, kvm, kvm image, linux partition to kvm image, qemu, virtualization, windows partition to kvm image

22 November 2011 at 9:21 pm |
Hi, Thanks for the tutorial.
I found my way to your post because I have a problem which is the reverse of what you’ve done: given a qemu raw image file, how do I transfer this to a disk? Essentially I need to copy a bootable VM image to a desktop disk, let’s say, and start up the desktop.
Any ideas? Thanks.
– Dennis
26 November 2011 at 2:39 pm |
Hi Dennis,
if you have a raw image you can copy it on your disk with dd.
If your image is not raw you need to convert it before: kvm-img it’s a good tool to convert vm images.
To copy your image you need to attach your disk and find its device file (look on output of dmesg after disk attach).
Be sure that you find the _right_ device file for you destination disk: if you use another device (for example another disk) you will lose all data that it contains.
Also be sure to use the disk device (ex. /dev/sda) and not a partition device (ex. /dev/sda1).
Finally you can copy your raw data on disk by dd if=my-vm-image-file.raw of=/dev/my-disk-device.
After copy your system may not boot from hard-disk because of wrong partition description on boot loader: for example in your VM your virtual disk may be attached to the primary ide controller and so your bootloader (ex. grub/lilo) may point to hda(0,0) but your real disk may be attached to a secondary ide controller hda(1,0).
To fix these problems use the bootloader console (grub has it) or a live-cd distro to chrood on system. As example, you can fix grub configuration with update-grub.
Tell me if you need any help
27 November 2011 at 2:34 pm
Giulio, thanks again, this time for the follow up. I was able to handle the raw image as you explained. The root cause of my problem lay with dd, in my misunderstanding of block sizes (in my cases, there was no need to pass the bs or ibs/obs parameters). Fortunately the relevant disk partitions of the VM are all in one device, so I did not have to cope with the situation you mentioned.
So far so good! It’s a small step that boosts my confidence and my appreciation for qemu.
– Dennis