How to make an image of an existing physical hard drive and attach it to a VM in virsh-manager and boot from it

Context

I replaced the hard drive of my desktop, and I wanted to have a full clone of the disk in case I forgot to save anything and stuff like that. As I wanted to use the disk in another device, I made a Disk Image and then attached it to a VM to boot from it and get the same state I previously had. Here are my notes for that.

NOTE: This is a note for myself as I tend to forget these steps and commands and spent several hours trying to make this work.

Create the disk image

To create the image use dd

sudo dd if=/dev/sdX of=/path/to/image.img bs=4M status=progress

or using qemu-img

qemu-img create -f qcow2 -o size=10G /path/to/image.qcow2 # set the right size

I used dd

Create the VM in virsh-manager

Then create a new VM in virsh-manager with UEFI instead of BIOS (only if your original machine used UEFI. If it booted using BIOS, then the default will be enough).

If the option is not shown, change the defaults for the new machines in vish-manager -> Preferences -> New VM -> x86 Firmware.

NOTE: As stated above, if your original device used BIOS, then ignore this part.

Screenshot of the settings screen of virsh manager

Convert the .img image to .qcow2 to use with qemu and virsh-manager

If you created the disk image as .img, convert it to qcow2 with the following command:

qemu-img convert -f raw -O qcow2 originak_image.img converted_image.qcow2

NOTE: If you get a permissions problem, try sudo or set the right permissions of the image using chmod or chown

The problems I had

I had several thing not really working:

  1. In my case the VM was not booting at all, and the first issue was the BIOS → UEFI part (see above).

  2. Then it was loading the boot menu but not booting. When trying to boot Linux Mint (the OS inside the disk image), it went to black. I had to go to advanced options and select the recovery boot. There I could get into root, and edit the /etc/fstab by commenting out the other hard drives I had in the original machine that were no longer available in this machine.

I have done this more than once, and more than once have I forgotten how this goes. Thus, it is now here.