Skip to contentSkip to conversation

Converting installed windows (or ?) partition to virtual machine image

Qemu-devel [email protected] 3 participants 3 messages Oct 10, 2008 – Oct 11, 2008

lore
Share & tools
lore
#0 lore raw

I'd like to try to convert the installed Windows partition of a dual boot machine into a virtual machine image. It seems like qemu-img currently only supports copying an entire disk (/dev/sda for example) to a disk image. It would be nice if one could specify a single partition.

As a first hack/attempt to try this out, I'm trying the patch below (against 0.9.1) to only copy a certain number of sectors. Since the Windows partition is almost always the first partition on the disk, hopefully this will work for most folks. I guess if you've already installed grub you'll have issues unless you also include the /boot partition.

It would be nice though to think about what it would take to do this
"right". Seems like you could source from a single partition and
generate the proper MBR and partition table in the virtual machine image without too much trouble. A cursory glance seems to indicate that the vvfat format does something like this.

Thoughts?

- Orion

qemu-img.c 2008-10-09 16:46:47.000000000 -0600 renamed from qemu-img.c.part 2008-01-06 12:38:42.000000000 -0700 +1 -1
@@ -411,7 +411,7 @@
     const char *fmt, *out_fmt, *out_filename;
     BlockDriver *drv;
     BlockDriverState **bs, *out_bs;
-    int64_t total_sectors, nb_sectors, sector_num, bs_offset;
+    int64_t total_sectors, nb_sectors, sector_num, bs_offset,
requested_sectors = 0;
     uint64_t bs_sectors;
     uint8_t buf[IO_BUF_SIZE];
     const uint8_t *buf1;
@@ -421,7 +421,7 @@
     out_fmt = "raw";
     flags = 0;
     for(;;) {
-        c = getopt(argc, argv, "f:O:hce6");
+        c = getopt(argc, argv, "f:O:s:hce6");
         if (c == -1)
             break;
         switch(c) {
@@ -434,6 +434,9 @@
         case 'O':
             out_fmt = optarg;
             break;
+        case 's':
+            requested_sectors = atoll(optarg);
+            break;
         case 'c':
             flags |= BLOCK_FLAG_COMPRESS;
             break;
@@ -464,6 +467,9 @@
         total_sectors += bs_sectors;
     }

+ if (requested_sectors)
+ total_sectors = requested_sectors;
+

     drv = bdrv_find_format(out_fmt);
     if (!drv)
         error("Unknown file format '%s'", out_fmt);
#1 lore raw

Hi,

Quoted 11 lines · On Thu, 9 Oct 2008, [email protected] wrote:

I'd like to try to convert the installed Windows partition of a dual boot machine into a virtual machine image. It seems like qemu-img currently only supports copying an entire disk (/dev/sda for example) to a disk image. It would be nice if one could specify a single partition.

As a first hack/attempt to try this out, I'm trying the patch below (against 0.9.1) to only copy a certain number of sectors. Since the Windows partition is almost always the first partition on the disk, hopefully this will work for most folks. I guess if you've already installed grub you'll have issues unless you also include the /boot partition.

I think a better method would be to do something like this:

	(dd if=/dev/zero bs=32256 count=1 && dd if=/dev/sda1) > disk.img

and then adding one partition with "fdisk disk.img". The 32256 is what is commonly reserved before the first partition (this is where the mbr and boot sector viruses live).

Quoted 5 lines

It would be nice though to think about what it would take to do this
"right". Seems like you could source from a single partition and
generate the proper MBR and partition table in the virtual machine image without too much trouble. A cursory glance seems to indicate that the vvfat format does something like this.

vvfat does something completely different. It takes an existing directory structure and simulates a VFAT partition from that, no matter what the original file system was.

Ciao,
Dscho

#2 lore raw

2008/10/10 Johannes Schindelin <[email protected]>:

Quoted 10 lines · On Thu, 9 Oct 2008, [email protected] wrote:
Quoted 5 lines

It would be nice though to think about what it would take to do this
"right". Seems like you could source from a single partition and
generate the proper MBR and partition table in the virtual machine image without too much trouble. A cursory glance seems to indicate that the vvfat format does something like this.

vvfat does something completely different. It takes an existing directory structure and simulates a VFAT partition from that, no matter what the original file system was.

It simulates a filesystem and it simulates a whole disk with a valid partition table and one partition in it, so yes, it does what is wanted and it shouldn't be hard to replace the part that simulates the filesystem with an existing partition or file.

Cheers