You may find yourself needing to simulate a small network or create a lab environment to run tests in. One way to do this is to run Hyper-V inside of an existing virtual machine. In this quick tip I’ll show you how to enable the features required to do so.
In order to run Hyper-V inside of an existing VM you must enable Nested Virtualization. Inside of your physical host fire up an elevated PowerShell session. Make sure the virtual machine is powered off and run the following cmdlet:
Set-VMProcessor -VMName -ExposeVirtualizationExtensions $true
In order to set up networking you must configure the settings to either spoof the the MAC address that is attached to your VM’s virtual switch or use NAT. Run the following command on the first level virtual machine to turn on MAC address spoofing
Get-VMNetworkAdapter -VMName | Set-VMNetworkAdapter -MacAddressSpoofing On
Alternatively, you can configure the VMs to communicate using NAT (Network Address Translation). This method should only be used if it’s not possible to spoof your MAC address.
First we create a vSwitch on the first level VM (the “middle” vm).
New-VMSwitch -Name VmNAT -SwitchType Internal
New-NetNat –Name LocalNAT –InternalIPInterfaceAddressPrefix “192.168.100.0/24”
Next give your network adapter an IP address
Get-NetAdapter "vEthernet (VmNat)" | New-NetIPAddress -IPAddress 192.168.100.1 -AddressFamily IPv4 -PrefixLength 24
Now we need to assign each VM an IP address and a gateway. The net adapter we created in the last step should be used for our gateway. We also assign a DNS server using netsh.
Get-NetAdapter "Ethernet" | New-NetIPAddress -IPAddress 192.168.100.2 -DefaultGateway 192.168.100.1 -AddressFamily IPv4 -PrefixLength 24
Netsh interface ip add dnsserver “Ethernet” address=YourDNSserver
Note: Keep in mind when you enable Nested Virtualization your first and second level VMs will not be able to use the Dynamic Memory feature.