1

I want to convert a Windows 7 C:\ Drive from my previous laptop into a qcow2 image which I wan to use to create a KVM virtual machine from on a new (linux based) computer.

I guess the simplest way would be some sort of dd plus a qemu-img convertoperation.

1.) Boot the old W7 laptop from a live system and run: dd if=/dev/sda1 of=/path/to/W7.img

(would I need to add any dd options to make that bootable? any other options [for blocksize i.e.]) recommended?)

2.) On the new machine say something like
qemu-img convert -f raw -O qcow2 /path/to/W7.img /path/to/W7.qcow2

that should give me a pretty exact copy with the same size of the C:\ drive I guess.

Now the original C:drive is quite large and half empty, so I'd like to have a dynamically expanding qcow2 image as an end result and likewise reduce it's size.

Is there any option I can add to the qemu-img convert operation which would help to achieve that?

vrms
  • 501

2 Answers2

1

There is a tool already available to do this conversion, called virt-p2v.

To get started, install libguestfs-tools on your Ubuntu system which will serve as the hypervisor for your new KVM virtual machine. This will install virt-p2v, virt-v2v and virt-p2v-make-disk, all of which you will need.

sudo apt install libguestfs-tools

Now insert a USB stick to your Linux system, make note of its /dev/sd* device name, and run virt-p2v-make-disk to create a bootable virt-p2v environment, from which you will later boot the Windows machine. (Or see the man page for other options for creating the bootable image.)

sudo virt-p2v-make-disk -o /dev/sd*

Now boot your Windows system from this USB stick. It will ask you to provide credentials to SSH into the Linux machine. You will either need to enable root on the Ubuntu machine, or set up passwordless sudo for your account.

After you set up ssh, virt-p2v will ask you some questions about the virtual machine you want to create, and then it will begin copying data from your physical machine to the Ubuntu server.

  • sorry, for slow response. I was stuck in "can't load external javascript hell" .... now, sounds like a pretty sophisticated tool. but I am missing the part that makes the size of the qcow2 image smaller then the actually 102 GB of the physical drive. Is that built in? – vrms Apr 18 '18 at 01:22
  • regarding qemu-img convert I came across the -c option which may 'compress' the new file and (maybe also create a (sizewise) dynamic qcow2 image. – vrms Apr 18 '18 at 01:23
  • @vrms The image you get should already be sparse, but if it isn't, you can use virt-sparsify to fix that. As for actually shrinking it, that's probably something you should do with Windows tools before p2ving it. – Michael Hampton Apr 18 '18 at 01:39
  • you mean shrinking the existing physical C:\ drive rahter then trying to shrink an image file I guess. Sounds much more risky that way to me but maybe there is not other way? – vrms Apr 18 '18 at 08:47
  • Or, at least, shrinking the partition(s). The virtual disk can then be shrunk to match later. – Michael Hampton Apr 18 '18 at 09:59
0

Another way is to connect the laptop drive (I used a M2 storage to sata adapter - careful there are M2 pci adapters too that wont work. Or through network.)

Create a VM based on a raw partition. Both Virt-manager and VBox can do this. It is very safe and stable, have been using Windows from a raw partition for a while inside Ubuntu with no issues, but you could also copy the partition or use KDE Part-mgr to back it up to file. Windows likes having its partition table present, so use the whole drive /dev/sdX.

Then attach Clonezilla ISO to the VM, as well as a target storage location for the Qcow2, boot the ISO and it will clone the drive to a file. I believe it can do the reverse as well.

EDIT: I just confirmed using dd to copy a whole Windows drive and then qemu-img to convert it to qcow2 does work. https://moozing.wordpress.com/2015/06/24/converting-a-physical-machine-to-a-virtual-machine/

Both that and the Clonezilla p2v worked. Its important to get the EFI working in the VM manager. Heres some info on how to do that with VMM/KVM: https://unix.stackexchange.com/questions/612813/virt-manager-change-firmware-after-installation

And heres some links to clarify the midly unclear restore step with CLoneZilla (The step that has notes: "Do not go any further into the folders. Press tab twice and enter to select Done"): https://support.onlogic.com/documentation/linux-clonezilla/
https://clonezilla.org/clonezilla-live-doc.php

alchemy
  • 762