Convert a Hyper-V Gen2 VM to Gen1

More often than not there wouldn't need to be a need to convert a Gen2 VM down to a Gen1 VM. The exceptions to this would be downgrading a version of Hyper-V from Windows Server 2012 to 2008 R2; the chances of this are quite remote to very unlikely these days.
The other reason could be that you can't or rather not use Azure Site Recovery service or Azure Migrate: Server Migration and prefer to manually migrate your on premise Gen2 VM's from Hype-V into Azure yourself. In either case, I have the steps needed to assist with your pursuit.

It's worth noting that Azure only supports the following:

  • Fixed VHD not VHDX and/or dynamically expanding disks
  • MBR (msdos) partition tables, not GPT/UEFI for the OS disk
  • Essentially Gen1 Hyper-V guests
  1. To start, backup/clone the virtual disk and convert the existing VHDX to a VHD:

Convert-VHD –Path .\test.vhdx –DestinationPath test.vhd -VHDType Fixed

Create a Gen1 VM with a smaller, whole number fixed size whole VHD, ie: 30GB, and also attach the newly converted VHD to the new Gen1 VM.

Alternatively, in the existing Gen2 VM, shrink the OS partition to a desired size, shrink the VHDX to just accommodate the resized partition with the command below, then create a new VHD copy as a fixed size virtual disk using the command above:


Resize-VHD -Path .\test.vhdx -ToMinimumSize

Note: The above command only works with VHDX virtual disks.

  1. Booted into Gparted and on the smaller newly created VHD, created a MBR (msdos) partition table

  2. Copy both the boot (Recovery) and OS partitions to new MBR VHD (resize within Gparted if needed before copying), shutdown the VM, detaching the converted VHD and leaving the smaller newly created VHD attached

  3. Boot into Windows Server ISO and repair the bootloader (Recovery) partition:

Select "Repair your computer" > "Use recovery tools" > "Start Command prompt"

 
diskpart
list disk
select disk 0
list partition
select partition 1 (or partition of the 'Windows' Partition)
active
exit
  1. Copy the 'bootmgr' file from the root of the Windows Server DVD to the boot (Windows) partition, assuming drive letter is C:\, ie:
 
copy (DVD Drive Letter):BootMgr c:
  1. Reboot from Windows Server DVD and repair the bootloader;

Select "Repair your computer" > "Use recovery tools" > "Start Command prompt"

 
bootrec /fixmbr
bootrec /fixboot
bootrec /rebuildbcd

Repair the BCD file (Located under C:\Boot):

 
cd /d <drive letter>:\EFI\Microsoft\Boot\
bootrec /FixBoot
bcdboot c:\Windows /l en-us
  1. Eject the Windows Server DVD, reboot and allow Windows to run chkdsk
  2. Prepare the VM to be hosted by Azure, including Windows Server 2008 SP2, install all the latest Updates, and install the latest Azure VM Agent

Note: If you change the product SKU after following Step 9, ie: Evaluation to Retail, repeat Step 8 again.

  1. Shutdown VM, upload the OS VHD to a blob storage in the same region and resource group which you'd need to create the VM in Azure - Use AzCopy as upload as a Page Blob:

NB: Use v10 of AzCopy or Azure Storage Explorer to upload the fixed sized VHD as a Managed Disk.


azcopy copy 'C:\Hyper-V\test1\test.vhd' 'https://bla.blob.core.windows.net/vhds/test.vhd?SASToken' --blob-type PageBlob
  1. Create the VM; you can convert the uploaded VHD into a Managed Disk and then create a VM. Alternatively, via create the VM from the uploaded VHD via PowerShell. Change the variables to suit your needs:
 
$vmName = "test1"
$rgName = "Servers"
$subnetName = "default"
$location = "East US"
$vnetName = "vnet"
$vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgName
$nicName = $vmName
$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location -SubnetId $vnet.Subnets[0].Id
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize "Standard_B2s"
$vm = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
$osDiskUri = "https://bla.blob.core.windows.net/vhds/test1.vhd"
$osDiskName = $vmName + "_OsDisk"
$vm = Set-AzVMOSDisk -VM $vm -Name $osDiskName -VhdUri $osDiskUri -CreateOption Attach -Windows
New-AzVM -ResourceGroupName $rgName -Location $location -VM $vm

To determine the Subnet array number:

 
$rgName = "Servers"#$subnetName = "default"$location = "East US"$vnetName = "vnet"$vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgName$vnet.Subnets[1].Id

Change the array number, starting from 0 being the first subnet onwards.

  1. Ensure the VM boots. Proceed to use the Unmanaged Disk or convert, otherwise read the steps below to create a Managed Disk with a preferred naming convention.

Tip: To retain the neat OsDisk naming convention, migrate the VHD from the Storage account from Unmanaged to a Managed Disk. This will allow you to provide the VHD a preferred name, amongst defining the size and type (Standard, SSD, etc…).

  1. Finally, assign an static IP, include in the backup and enable disk encryption.

Note: Try resetting the TCP/IP stack if the VM NIC doesn’t initialize, run netsh int ip reset in an elevated command prompt.

Please refer to the sites below for more information: