Benyamin Limanto

Just Random Thought from myself

How To Run Linux Cloud Image Qcow2 in Hyper-V Windows with Cloud-Init

Total Views

0 views

I like to use qcow2 cloud image from any Linux distribution may it come from fedora, almalinux, or other Linux distribution that provide it. It give me a convenient of setting up clean box without need of installing from scratch step by step. I would only need to create cloud-init.iso that contain the machine hostname, user, password, ssh key, or static IP if needed, then build that cloud-init, then mount it same time with qcow2 linux image, then while boot everything is set for me, the rest done by ansible on Qemu/KVM. (I don’t cover ansible here, so expect there will be no ansible in this post)

So, could we do the same thing with Hyper-V? That was the question I had when I was stuck with a Windows laptop at work. My first solution was unnecessarily complicated: I installed Fedora in WSL2, then ran QEMU/KVM with nested virtualization. It worked, but I kept wondering why I needed virtualization inside virtualization. Using KVM was familiar, but from a technical standpoint, running the VM directly on Hyper-V seemed far cleaner and more interesting to explore.

In past, I did work with Hyper-V fleet while still in the University Lab, as I only have that option also in my Networking Lab. I’m thinking, the image should possible to interchanged between So, I do a research is it possible to transform qcow2 image to vhdx and recognized by Windows Hyper-V. After checking with qemu-img cli, it’s possible! We could transform qcow2 image into vhdx, then load it into Hyper-V then do same step like what we did in KVM.

There are two ways to get the qemu-img executable, 1 via WSL2 / native linux, 2 on windows via download from qemu installer or only get the qemu-imge from https://cloudbase.it/downloads/qemu-img-win-x64-2_3_0.zip . Either way is fine and works. The command to convert the qcow2 to vhdx is

qemu-img.exe convert -f qcow2 -O vhdx -o subformat=dynamic Fedora-Cloud-45.qcow2 fedora45.vhdx

After converting, the next question will be, will it work? Even with same file format, will it works? The answer it is. You need to create a cloud-init iso and mount it together. In Linux you could just use genisoimage. In Windows we should use C# code in the powershell cli.

# Put as gen-iso.ps1
$src = "."
$iso = "cloud-init.iso"

$code = @"
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
public class IsoWriter {
    public static void Write(object comStream, string path) {
        IStream s = (IStream)comStream;
        byte[] buf = new byte[65536];
        IntPtr p = Marshal.AllocHGlobal(4);
        using (FileStream fs = File.Create(path)) {
            while (true) {
                s.Read(buf, buf.Length, p);
                int n = Marshal.ReadInt32(p);
                if (n <= 0) break;
                fs.Write(buf, 0, n);
            }
        }
        Marshal.FreeHGlobal(p);
    }
}
"@
Add-Type -TypeDefinition $code

$fsi = New-Object -ComObject IMAPI2FS.MsftFileSystemImage
$fsi.FileSystemsToCreate = 3   # ISO9660 + Joliet
$fsi.VolumeName = "cidata"
$fsi.Root.AddTree($src, $false)
$result = $fsi.CreateResultImage()
[IsoWriter]::Write($result.ImageStream, $iso)

Next, create the meta-data and user-data files in a dedicated directory, following a structure similar to the example below.

[Ben@BEN-T14S cloud-iso] > ls

    Directory: C:\Users\Ben\Downloads\ISO\cloud-iso

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          06/12/2026    15:09          57344 cloud-init.iso
-a---          06/12/2026    15:31           1013 gen-iso.ps1
-a---          06/12/2026    15:07             53 meta-data
-a---          06/12/2026    15:07            229 user-data

The meta-data file can contain any machine-specific information you need, such as the instance ID and hostname. Adjust its contents to match your deployment requirements.

instance-id: fedoraqcow2
local-hostname: fedoraqcow2

The content of the user-data, please set as your need

#cloud-config - Default user is Fedora

users:
  - name: fedora
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    lock_passwd: false
    plain_text_passwd: fedoralinux
# uncomment this part if you want to ssh from outside
#    ssh_authorized_keys:
#      - ssh-ed25519 AAAA...yourkey
# password: fedoralinux
chpasswd: { expire: False }
ssh_pwauth: True

Then you could run the script to generate the cloud-init.iso

[Ben@BEN-T14S cloud-iso] > .\gen-iso.ps1
[Ben@BEN-T14S cloud-iso] > ls *.iso

    Directory: C:\Users\Ben\Downloads\ISO\cloud-iso

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          06/12/2026    15:09          57344 cloud-init.iso

Next is testing the image and which Hyper-V guest version we should use. For qcow2 to run, it should use Gen1 Hyper-V if you don’t want to set anything out of the box. We can use Gen2, but we need to set the booting priority it booted from network to hard drive.

The step of creating the VM are:

  1. Open Hyper-V Manager
  2. On right sidebar, press create > Create virtual machine
  3. Specify the name of the VM, and you could check store the virtual machine in different location if you need to (I choose the C:\VM\Fedora for my Fedora)
  4. Choose Gen1 or Gen2. (look at the explanation above)
  5. Assign the memory (2048 is enough for simple test case)
  6. Assign network to NAT if needed. Or you could use Bridge network if you need it.
  7. On Virtual hardisk, you need to choose the qcow2 we have. (mine is in C:\VM\Fedora\fedora45.vhdx)
  8. Click finish
  9. Choose the VM from the list of VM, then press settings
  10. In settings press SCSI Controller > DVD Drive > Add the cloud-init iso we use for provision the VM
  11. Start the VM, look it should boot from the VHDX
On the Step 10

It should look similar to the example below. The values highlighted in red correspond to the configuration settings defined in Steps 4, 6, and 10.

Detail of Settings page

After the VM starts, you can verify that cloud-init has configured the system correctly. The configured user account should be available for login, and passwordless sudo access should work as specified in the user-data configuration.

That’s it, we could run qcow2 directly in Hyper-V and no need to install step by step the VM. Just use the qcow2 image, then provision using cloud-init.iso, and it works perfectly like what we would like in KVM/Linux.


Comments

Leave a Reply. I will come back and maybe we can have some conversation 🙂

This site uses Akismet to reduce spam. Learn how your comment data is processed.