Hello everyone!

I have been learning QEMU for the past day and so I’ve almost finished building my perfect VM from the qemu cli, and I was wondering if there is any method to say, copy a partition(+bootloader) of a physically installed system (in this case, Windows) over a hard disk image and run it with QEMU

Unfortunately I’ve had no luck searching online about it and I’m unsure how to proceed myself since I’ve never done any disk-cloning or anything like that

My best guess would be to dump the contents of the partitions into a shared folder with the VM, then use a live media to copy those contents over into the hard disk image and finally install a bootloader to work with the Windows boot manager, but I’m all ears for what anyone has to say about it.

Thanks for reading and please let me know what you know!

  • bizdelnick@lemmy.ml
    link
    fedilink
    arrow-up
    1
    ·
    10 hours ago

    Your idea is correct, but I don’t know how to do this in Wndows (while this is pretty simple in linux). However I want to warn you that if the partition that you are dumping is used by the OS, the resulting image will most likely be corrupted. Better use a linux live system and ensure that the partition is not mounted.

  • Max-P@lemmy.max-p.me
    link
    fedilink
    arrow-up
    34
    ·
    24 hours ago

    What you’re trying to do is called a P2V (Physical to Virtual). You want to directly copy the partition as going through a file share via Linux will definitely strip some metadata Windows wants on those files.

    First, make a disk image that’s big enough to hold the whole partition and 1-2 GB extra for the ESP:

    qemu-img create -f qcow2 YourDiskImageName.qcow2 300G
    

    Then you can make the image behave like a real disk using qemu-nbd:

    sudo modprobe nbd
    sudo qemu-nbd -c /dev/nbd0 YourDiskImageName.qcow2
    

    At this point, the disk image behaves like any other disk at /dev/nbd0.

    From there create a partition table, you can use cgdisk or parted or even the GUI GParted will work on it.

    And finally, copy the partition over with dd:

    sudo dd if=/dev/sdb3 of=/dev/nbd0p2 bs=4M status=progress
    

    You can also copy the ESP/boot partition as well so the bootloader works.

    Finally once you’re done with the disk image, unload it:

    sudo qemu-nbd -d /dev/nbd0
    
    • Coki91@dormi.zoneOP
      link
      fedilink
      English
      arrow-up
      6
      ·
      23 hours ago

      I wasn’t expecting a Step-by-Step, amazing, Thanks for your effort!

      Metadata is something I would’ve never thought of but besides that seems like my guess wasn’t that far off which im happy for

      • Max-P@lemmy.max-p.me
        link
        fedilink
        arrow-up
        6
        ·
        19 hours ago

        I also wanted to put an emphasis on how working with virtual disks is very much the same as real ones. Same well known utilities to copy partitions work perfectly fine. Same cgdisk/parted and dd dance as you otherwise would.

        Technically if you install the arch-install-scripts package on your host, you can even install ArchLinux into a VM exactly as if you were in archiso with the comfort of your desktop environment and browser. Straight up pacstrap it directly into the virtual disk.

        Even crazier is, NBD (Network Block Device) is generic so it’s not even limited to disk images. You can forward a whole ass drive from another computer over WiFi and do what you need on it, even pass it to a VM boot it up.

        With enough fuckery you could even wrap the partition in a fake partition table and boot the VM off the actual partition and make it bootable by both the host and the VM at the same time.

        • Coki91@dormi.zoneOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          17 hours ago

          Learning to use QEMU I started to grasp those possibilities actually, now getting these tips I do feel like I could dive into lots of “sandboxing” and generally be able to try anything with it, my only constrain now shall be disk space

  • nanook@friendica.eskimo.com
    link
    fedilink
    arrow-up
    2
    ·
    16 hours ago

    Yes. set your CD in the VM to a linux distro iso like Linux, set boot from the CD in the vm, then you can use all the tools on your ISO to do whatever you want to the vm.

  • enumerator4829@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    3
    ·
    24 hours ago

    Here be dragons. But basically:

    • Run a VM from contents of a physical disk: use ’dd’ to create disk image. If on linux, try to boot and fix all the errors, hopefully few.

    • Run VM as physical machine: other way around.

    You won’t find this in a tutorial. You need to understand concepts, read manuals, fit everything together, execute, fail and retry until it works.

    For Windows, I have no idea. Conceptually, I figure it’s similar.

    • Coki91@dormi.zoneOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      23 hours ago

      The Host is Linux (arch, btw) so just the guest would be Windows and im ready to squash errors too, so thanks for the heads up