Hyper-V Nested Virtualization: Run virtual machines on virtual machine  

    Hyper-V Nested Virtualization: Run virtual machines on virtual machine

    Hyper-V Nested Virtualization: Run virtual machines on virtual machine

    How to enable Nested Virtualization on Hyper-V VM to allow Hyper-V to run virtual machines
    Published by Category: Virtualization
    30 Aug 2019
    Designer Media Ltd

    information   Information
    Nested Virtualization means that user can enable Hyper-V on existing virtual machines, and run virtual machines and Windows Sandbox on them.

    To make Nested Virtualization to work, it and MAC Address Spoofing or NAT networking must be enabled separately for each first level host VM. The script in this tutorial will use MAC Spoofing.

    Wikipedia:

    MAC spoofing is a technique for changing a factory-assigned Media Access Control (MAC) address of a network interface on a networked device. The MAC address that is hard-coded on a network interface controller (NIC) cannot be changed. However, many drivers allow the MAC address to be changed. Additionally, there are tools which can make an operating system believe that the NIC has the MAC address of a user's choosing. The process of masking a MAC address is known as MAC spoofing. Essentially, MAC spoofing entails changing a computer's identity, for any reason, and it is relatively easy.

    To be honest, this is a feature that only interests real virtualization enthusiasts. One practical use is to run emulators or other software that requires regular restarts on a second level nested VM on first level VM, to avoid restarting host.

    I use Nested Virtualization to save current state of Windows Sandbox. Because Sandbox will be completely reset when shut down / turned off, running it on a virtual machine is the most practical way to save its state by using Hyper-V Production Checkpoints.

    If you want to enable Hyper-V on your Hyper-V virtual machines, this tutorial shows how to do it with a simple PowerShell script.


     Enable Nested Virtualizationt
    1) The script to enable Nested Virtualization:

    Code:
    ########################################################## 
    # 
    # Nested.ps1
    #
    # A PS Script to enable nested virtualization on a VM. 
    # 
    # You are free to edit & share this script as long as
    # source TenForums.com is mentioned.
    #
    # *** Twitter.com/TenForums *** Facebook.com/TenForums ***
    # 
    # Script by Kari 
    # - TenForums.com/members/kari.html
    # - Twitter.com/KariTheFinn
    # - YouTube.com/KariTheFinn
    # - https://Win10.guru
    #
    # 'Use-RunAs' function to check if script was launched
    # in normal user mode and elevating it if necessary by
    # Matt Painter (Microsoft TechNet Script Center)
    # https://gallery.technet.microsoft.com/scriptcenter/ 
    #
    ##########################################################
    
    ##########################################################
    # Checking if PS is running elevated. If not, elevating it
    ##########################################################
    
    function Use-RunAs 
    {    
        # Check if script is running as Administrator and if not elevate it
        # Use Check Switch to check if admin 
         
        param([Switch]$Check) 
         
        $IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()` 
            ).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") 
             
        if ($Check) { return $IsAdmin }   
          
        if ($MyInvocation.ScriptName -ne "") 
        {  
            if (-not $IsAdmin)  
              {  
                try 
                {  
                    $arg = "-file `"$($MyInvocation.ScriptName)`"" 
                    Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'  
                } 
                catch 
                { 
                    Write-Warning "Error - Failed to restart script elevated"  
                    break               
                } 
                exit 
            }  
        }  
    } 
    
    Use-RunAs 
    
    ##########################################################
    # Show short instructions to user
    ##########################################################   
    
    cls
    Write-Host                                                                       
    Write-Host ' This script will enable nested virtualization on a virtual machine. '
    Write-Host ' When nested virtualization is enabled, you can use Hyper-V on a VM'
    Write-Host ' to run virtual machines on it.'
    Write-Host
    Write-Host ' Nested virtualization also allows running Windows Sandbox container'
    Write-Host ' on a VM (Windows 10 build 18305 or later required).'
    Write-Host
    Write-Host ' Notice that Hyper-V and Sandbox are not available in W10 Home edition.'
    Write-Host '                                                                      '
    Write-Host
    Write-Host ' ' -NoNewline
    pause   
    
    cls
    Write-Host
    Write-Host ' Found following virtual machines:'
    #Write-Host 
    Get-VM | Format-Table -Property VMName
    #Write-Host
    Write-Host ' Please enter the name of the virtual machine on which you want' 
    Write-Host ' nested virtualization to be enabled.'
    Write-Host
    $VM = Read-Host -Prompt ' Virtual machine name '
    
    Set-VMProcessor -VMName $VM -ExposeVirtualizationExtensions $true
    Get-VMNetworkAdapter -VMName $VM | Set-VMNetworkAdapter -MacAddressSpoofing On
    cls
    Write-Host
    Write-Host ' Nested virtualization on virtual machine'$VM 'enabled. You can'
    Write-Host ' now enable and setup Hyper-V and / or Windows Sandbox on it.'
    Write-Host
    Write-Host ' More Windows 10 tips, tricks, videos & tutorials on'
    Write-Host
    Write-Host '              https://www.TenForums.com' -ForegroundColor Yellow 
    Write-Host
    Write-Host ' * Twitter.com/TenForums * Facebook.com/TenForums * ' -ForegroundColor Yellow
    
    Write-Host 
    Write-Host ' Script by Kari'
    Write-Host '  - TenForums.com/members/kari.html'
    Write-Host '  - Twitter.com/KariTheFinn'
    Write-Host '  - YouTube.com/KariTheFinn'
    write-Host '  - https://Win10.guru'
    Write-Host  
    
    Write-Host
    Write-Host ' ' -NoNewline
    pause

    You can download the script from my OneDrive:

    Download

    As always with downloaded content, be sure to unblock downloaded file before using it!

    2) Check that running local scripts in PowerShell is enabled (tutorial):
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-allow-ps-scripts.jpg

    3) Be sure the virtual machine on which you want to allow nested virtualization is turned off. Run the script by right clicking it and selecting Run with PowerShell:
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-run-script.jpg

    Alternatively, if using PowerShell ISE, open the script from File menu and press F5 to run it:
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-open-ps-ise.jpg

    4) If PowerShell warns you about Execution Policy change, press Y to accept it:
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-ps-policy-change.jpg

    5) Running the script, it will auto elevate (switch to admin mode if started in normal user mode), and lists all current Hyper-V virtual machines on your host:
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-vm-list.jpg

    6) Type or Copy & Paste the name of the virtual machine on which you want to enable nested virtualization, press Enter.
    Tip   Tip
    In PowerShell (and in Command Prompt), you cannot copy using context menu (mouse right click, select Copy). Also, by default Copy & Paste with keyboard shortcuts is disabled, although it can be enabled in PowerShell (and Command Prompt) Properties in Options tab.

    Instead, mark the string you want to copy with mouse, in this case name of a VM. To mark a string, press and hold down mouse left button on top of first character, and move to end of the string you want to copy. Release the mouse button, press Enter to copy.

    Pasting is done with right mouse button.
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-copy-paste-ps.jpg

    7) That's it. Nested virtualization on selected VM is now enabled:
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-nested-enabled.jpg

    8) Start the VM, setup Hyper-V on it (tutorial). In screenshot I am running Windows 7 on a Hyper-V VM on Windows 10 Hyper-V VM running on my host computer:
    Hyper-V Nested Virtualization: Run virtual machines on virtual machine-nested-virtualization.jpg

    Happy virtualization, geeks!

    Kari



  1. Posts : 68,954
    64-bit Windows 11 Pro for Workstations
       #1

    UPDATE:

    Starting with Windows 10 build 19645, support for Nested Virtualization on AMD processors is now available. Being an early release we recommend you read this blog post for details on what platforms currently work as well as how to enable the feature.
      My Computers


  2. Posts : 17,661
    Windows 10 Pro
    Thread Starter
       #2

    Brink said:
    UPDATE:

    Starting with Windows 10 build 19645, support for Nested Virtualization on AMD processors is now available. Being an early release we recommend you read this blog post for details on what platforms currently work as well as how to enable the feature.
    Thanks for the update, Brink!
      My Computer


  3. Posts : 68,954
    64-bit Windows 11 Pro for Workstations
       #3

      My Computers


 

Tutorial Categories

Hyper-V Nested Virtualization: Run virtual machines on virtual machine Tutorial Index Network & Sharing Instalation and Upgrade Browsers and Email General Tips Gaming Customization Apps and Features Virtualization BSOD System Security User Accounts Hardware and Drivers Updates and Activation Backup and Restore Performance and Maintenance Mixed Reality Phone


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:41.
Find Us




Windows 10 Forums