converting_a_partition_image_to_a_disk_image( 转)

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 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 :)

 

from: http://blog.sviluppoweb.eu/2010/07/16/how-to-convert-an-existing-partitiondisk-in-an-image-to-virtualize-it-in-linux-qemukvm/

 

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This will document the steps necessary to convert a singe-partition image (a la Xen) to a full disk image suitable for use with KVM.

Essentially, we need to make a disk with the partitions laid out in such a way that the QEMU virtual device can find them. For this first iteration, we are only going to try to create a device with a root and swap partition.

First, create an empty disk (raw format)

qemu-img create -f raw 10G starworker.img

Next, perform a fresh install of the OS (in a VM) that you wish to migrate, taking care to make the root partition at least as big as the partition image.

Next we will need to mount the root partition of the VM offline. To do this, we will need to use fdisk on the disk image to find the start of the root partition.

fdisk -lu starworker.img

The start column contains the offset, but the units are not bytes. The fdisk output will contain the units.

Now mount the both the new and old filesystems and copy the files.

mount -o loop starworker_part.img /mnt/looppart/
mount -o loop,offset=$(( $START * $UNITS )) starworker.img /mnt/loopdisk/
cp -a /mnt/looppart/* /mnt/loopdisk

When copying the files, make sure that you don't overwrite the /boot/ directory on this disk image. This probably won't be an issue, because the partition image probably doesn't contain a /boot/ or if it does, it would be empty. (That's why we're going through this process in the first place!)

Now, you should be ready to boot the new image. It should be the same as the old image, only now we have a disk image with a bootloader, kernel, and initrd.

 

from: http://www.mfenn.com/converting_a_partition_image_to_a_disk_image


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

how to mount a kvm image file

To the best of my knowledge there is no way to loop mount a qcow or qcow2 disk image. There is a workaround with a raw disk image:

1) Convert your qcow2 to raw

Code:

root@name:/home# qemu-img convert -f qcow2 /home/your_current_image.qcow2 -O raw /home/your_new_image.raw2) Find your physical hard disk block size:

Code:

root@name:/home# sfdisk -l Disk /dev/sda: 19457 cylinders, 255 heads, 63 sectors/track Warning: extended partition does not start at a cylinder boundary. DOS and Linux will interpret the contents differently. Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sda1 0+ 19456 19457- 156288321 5 Extended /dev/sda2 0 - 0 0 0 Empty /dev/sda3 0 - 0 0 0 Empty /dev/sda4 0 - 0 0 0 Empty /dev/sda5 0+ 243 244- 1959867 82 Linux swap /dev/sda6 * 244+ 1217 974- 7823623+ 83 Linux /dev/sda7 1218+ 19456 18239- 146504736 83 LinuxNOTE: This disk shows ", blocks of 1024 bytes,"

3) Issue the mount command with calculated offset:

Code:

root@name:/home# mount -o loop,offset=$((63*1024)) /home/your_new_image.raw /mnt/your_mount_pointor do the math manually: 63 * 1024 = 64512

Code:

root@name:/home# mount -o loop,offset=64512 /home/your_new_image.raw /mnt/your_mount_point

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

posted @ 2012-10-08 23:11  CCJPP  阅读(551)  评论(0)    收藏  举报