How To Setup Nested Virtualization For Azure VM Or VHD?

How to setup Nested Virtualization for Azure VM/VHD ?

Scenario: An Azure nested virtualization environment is needed to fix an OS issue on a broken Azure VM or created a custom image in Azure.

Prerequisite:

3.png

Process

  1. After Azure VM is deployed, RDP into the Azure VM, open PowerShell as administrator and run command below to install the HyperV and DHCP server roles.
  2. Install-WindowsFeature -Name DHCP,Hyper-V –IncludeManagementTools
Output:

  1. Once roles are installed without error, restart Azure VM using
  • Shutdown -R

  1. When Azure VM comes back up, RDP into it, open PowerShell as an administrator and run commands below to configure the HyperV network,
  • $switchName = "InternalNAT"
  • New-VMSwitch -Name $switchName -SwitchType Internal
  • New-NetNat –Name $switchName –InternalIPInterfaceAddressPrefix “192.168.0.0/24”
  • $ifIndex = (Get-NetAdapter | ? {$_.name -like "*$switchName)"}).ifIndex
  • New-NetIPAddress -IPAddress 192.168.0.1 -InterfaceIndex $ifIndex -PrefixLength 24

  1. Commands above will create a HyperV internal switch, set nat rule and gateway for that switch. Make sure all commands run without errors, see sample output
1.png

  1. If all the commands above are successful, run commands below in the same PowerShell window to configure the DHCP Service.
  • Add-DhcpServerV4Scope -Name "DHCP-$switchName" -StartRange 192.168.0.50 -EndRange 192.168.0.100 -SubnetMask 255.255.255.0
  • Set-DhcpServerV4OptionValue -Router 192.168.0.1 -DnsServer 168.63.129.16
  • Restart-service dhcpserver
  1. Commands above will create DHCP a scope for HyperV nat, assign gateway IP, DNS IP for that scope on the DHCP service and restart dhcp service
  2. Make sure all commands run without errors, See sample output
2.png
  1. If all the commands above are successful, now create a VM in the nested virtualization environment using the InternalNAT switch.
  2. This will give a VM in the Azure VM, that is setup for DHCP and has internet connection.
  3. The correct configuration for a nested environment to manage Azure VMs.