Migrating Windows Server 2012 R2 from Hyper-V to KVM: Fixing UEFI Boot Issues After qcow2 Conversion


1 views

When transitioning from Hyper-V Gen2 VMs to KVM with UEFI boot, we frequently encounter Windows boot failures due to differences in virtual hardware abstraction layers. The automatic repair loop indicates the OS can't locate or validate critical boot components despite the disk being intact.

From your diskpart output, we observe three key partitions:


Volume 1    C NTFS     Partition    126GB   Healthy   # OS partition
Volume 2    E Recovery Partition    300MB   Healthy   # WinRE tools
Volume 3               Partition     99MB   Healthy   # EFI System Partition

The hidden 99MB partition (likely FAT32 formatted) contains the EFI bootloader that OVMF firmware expects to find.

1. Verify EFI Partition Contents

From recovery command prompt:


diskpart
select volume 3
assign letter=S
exit

dir S:\EFI\Microsoft\Boot\bootmgfw.efi

This confirms if the UEFI bootloader exists in the expected location.

2. Rebuild BCD Store

First mount the OS partition:


diskpart
select volume 1
assign letter=W
exit

Then reconstruct boot configuration:


bcdboot W:\Windows /s S: /f UEFI
bootrec /rebuildbcd

3. KVM Configuration Adjustments

Modify your VM XML definition to ensure proper UEFI detection:



  hvm
  /usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd
  /var/lib/libvirt/qemu/nvram/win2012r2_VARS.fd


  
  
  
    
    
    
  

For future conversions, consider this qemu-img approach:


# Convert directly from Hyper-V to KVM
qemu-img convert -p -f vhdx -O qcow2 source.vhdx target.qcow2

# Validate partition table
virt-filesystems --format=qcow2 -a target.qcow2 --all

If still encountering issues, capture OVMF debug output:


sudo qemu-system-x86_64 \
 -drive file=/path/to/disk.qcow2,format=qcow2 \
 -drive if=pflash,format=raw,readonly=on,file=/usr/share/OVMF/OVMF_CODE.fd \
 -drive if=pflash,format=raw,file=my_vars.fd \
 -serial stdio \
 -global isa-debugcon.iobase=0x402 \
 -debugcon file:debug.log

This provides detailed firmware-level boot sequence information.

Once booted successfully:


# Check Hyper-V integration services removal
Get-WindowsFeature | Where-Object {$_.Name -like "*Hyper*"} | Uninstall-WindowsFeature

# Verify KVM drivers
Get-Service | Where-Object {$_.DisplayName -like "*virtio*"}


When moving a Generation 2 Hyper-V VM (UEFI-based) to KVM, several technical hurdles emerge. The Automatic Repair loop typically indicates either:

  • Bootloader corruption during conversion
  • UEFI firmware mismatch
  • Storage controller incompatibility
  • BCD store configuration issues

First, let's validate your KVM XML configuration for critical UEFI parameters:


<domain type='kvm'>
  <os>
    <type arch='x86_64' machine='pc-q35-2.9'>hvm</type>
    <loader readonly='yes' type='pflash'>/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd</loader>
    <nvram>/var/lib/libvirt/qemu/nvram/win2012r2_VARS.fd</nvram>
  </os>
  <features>
    <acpi/>
    <apic/>
    <hyperv>
      <relaxed state='on'/>
      <vapic state='on'/>
      <spinlocks state='on' retries='8191'/>
    </hyperv>
  </features>
</domain>

The IDE disk bus in your configuration might cause issues. For UEFI VMs, consider using virtio with proper drivers:


virsh edit your_vm_name
# Change disk controller to:
<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2' cache='none'/>
  <source file='/path/to/disk.qcow2'/>
  <target dev='vda' bus='virtio'/>
</disk>

When standard bootrec commands fail, try this comprehensive repair sequence from the recovery console:


bootrec /scanos
bootrec /fixmbr
bootrec /fixboot
bootrec /rebuildbcd
bcdedit /set {bootmgr} path \EFI\Microsoft\Boot\bootmgfw.efi
bcdedit /set {default} osdevice partition=C:
bcdedit /set {default} device partition=C:

If direct conversion proves problematic, consider this workflow:

  1. On Hyper-V host: Export-VM -Name "YourVM" -Path "C:\Export"
  2. Convert using qemu-img: qemu-img convert -f vhdx -O qcow2 source.vhdx target.qcow2
  3. Before first boot, mount the qcow2 and validate partition structure:
    
    sudo modprobe nbd max_part=8
    sudo qemu-nbd -c /dev/nbd0 -f qcow2 target.qcow2
    sudo fdisk -l /dev/nbd0
    

Before attempting to boot:

  • Confirm UEFI NVRAM is properly initialized
  • Validate that OVMF firmware supports Windows secure boot
  • Check for any Hyper-V specific dependencies in the registry (may require offline registry editing)