How-to convert an existing partition/disk in an image to virtualize it in linux qemu/kvm
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!
- 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
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.
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 😉
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.
“Especially for windows (!!!) ensure to uninstall the IDE/SATA controller driver and replace it with general/universal driver.”
Where can I get proper general/universal IDE driver? how can I be sure the sistem will boot again after this?
Is it sufficent to uninstall current drivers?
I’m not sure but I think the general/universal IDE driver is (usually) the driver that windows use after install, at first system boot, before you install other drivers.
It should work without problems if you didn’t need to install a particular IDE driver during the windows os install to recognize your controller.
Anyway there is no warranty about that. System may not boot after reboot but you can easily make an image with dd before try. If your system won’t start anymore you can restore your image and rollback.
In order to use the general/universal driver you have to update your IDE controller driver(s) with the generic driver from microsoft (don’t uninstall your driver, simply replace it by an update).
Happy hacking 😉
Hi, thanks for your post. I need to convert my physical OS rhel 6 in to kvm, but my disk is 1TB(it is taking only 2.5G of space), and my current OS fedora 18 has 500GB, then I cant do dd to backup it, any idea how to convert that partition?
I mounted it on mnt:
/dev/mapper/vg_server1-lv_root 912G 2.5G 863G 1% /mnt
not sure how can convert this physcial into the kvm
any help will be appreciated
Hi Mid, 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 1TB disk converted with the sparse option will occupy ~2.5GB of disk space.
Of course you need a way to set to zero all the unused bytes in your physical disk before the conversion: for ext2, ext3 and ext4 partitions you can use the zerofree tool (https://packages.debian.org/wheezy/zerofree) or you can try another way (for example you can create a zero filled file on your 1TB disk partition that fills your free space).
Thank you Mid, I’ve updated the post about sparse file conversion.