WebSource


Home | Chi sono | Archives


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

July 16, 2010 10:16 pm

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:

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:

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

Posted by Autore

Categories: Linux, Opensource

Tags: , , , , , ,

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

  1. 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

    By Dennis on November 22, 2011 at 9:21 pm

    1. 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 😉

      By Giulio Turetta on November 26, 2011 at 2:39 pm

      1. 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

        By dmc on November 27, 2011 at 2:34 pm

  2. “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?

    By Emanuele on October 4, 2012 at 4:27 pm

    1. Hi Emanuele,
      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 😉

      By Giulio Turetta on October 14, 2012 at 12:21 pm

  3. 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

    By Mid on July 12, 2014 at 9:02 pm

    1. 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).

      By Giulio Turetta on July 27, 2014 at 5:01 pm

    2. Thank you Mid, I’ve updated the post about sparse file conversion.

      By Giulio Turetta on July 27, 2014 at 5:22 pm

Leave a Reply



Mobile Site | Full Site


Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.