<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hyper-v Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/hyper-v/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/hyper-v/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Wed, 08 Apr 2020 10:55:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>hyper-v Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/hyper-v/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>HL Tools &#8211; Part 2 &#8211; Create a Nested Hypervisor</title>
		<link>https://www.lucd.info/2013/07/12/hl-tools-part-2-create-a-nested-hypervisor/</link>
					<comments>https://www.lucd.info/2013/07/12/hl-tools-part-2-create-a-nested-hypervisor/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Fri, 12 Jul 2013 21:07:26 +0000</pubDate>
				<category><![CDATA[ESXi]]></category>
		<category><![CDATA[homelab]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[hyper-v]]></category>
		<category><![CDATA[nested]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=4513</guid>

					<description><![CDATA[During last year&#8217;s VMworld #NotSupported sessions one of the hot topics was William [&#8230;]]]></description>
										<content:encoded><![CDATA[<p style="text-align: left;">During last year&#8217;s VMworld <a href="https://www.virtuallyghetto.com/2012/08/notsupported-sessions-at-vmworld-2012.html" target="_blank" rel="noopener noreferrer">#NotSupported sessions</a> one of the hot topics was <a href="https://twitter.com/lamw" target="_blank" rel="noopener noreferrer">William Lam</a>&#8216;s <a href="https://www.virtuallyghetto.com/2012/09/vinception-notsupported-slides-posted.html" target="_blank" rel="noopener noreferrer">vInception</a> talk. &#8220;<em>Nested ESXi</em>&#8221; has since then become indispensable in the homelab of everyone tinkering with virtualisation !</p>
<p style="text-align: left;">As a follow-up to that session, <a href="https://twitter.com/lamw" target="_blank" rel="noopener noreferrer">William</a> posted several blog posts on the subject. You can find them all in a <a href="https://www.virtuallyghetto.com/2012/10/nested-virtualization-resources.html" target="_blank" rel="noopener noreferrer">handy overview</a>.</p>
<p style="text-align: left; padding-left: 90px;"><a href="https://www.lucd.info/2013/07/12/hl-tools-part-2-create-a-nested-hypervisor/nested-esxi-sierpinski/" rel="attachment wp-att-4520"><img fetchpriority="high" decoding="async" class="alignnone  wp-image-4520" alt="nested-esxi-sierpinski" src="https://lucd.info/wp-content/uploads/2013/07/nested-esxi-sierpinski.png" width="329" height="291" srcset="https://www.lucd.info/wp-content/uploads/2013/07/nested-esxi-sierpinski.png 522w, https://www.lucd.info/wp-content/uploads/2013/07/nested-esxi-sierpinski-300x265.png 300w" sizes="(max-width: 329px) 100vw, 329px" /></a></p>
<p style="text-align: left;">But as much as I like his clear instructions on how to set up nested ESXi, I wanted to automate the process <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> In my homelab I create, and remove, ESXi VMs on a regular basis. So with the &#8220;<em>If you do it more than once, automate it</em>&#8221; in mind, I decided to create a function for the process.</p>
<p style="text-align: left;"><span id="more-4513"></span></p>
<h2 style="text-align: left;">The Script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function New-NestedHypervisor {
&lt;#  
.SYNOPSIS  Create a nested hypervisor   
.DESCRIPTION The function will create a VM that is
  configured to run a nested hypervisor
.NOTES  Author:  Luc Dekens  
.PARAMETER Name
  The name of the VM that will be created
.PARAMETER VMHost
  The ESXi server where the VM shall be created
.PARAMETER DiskGB
  The size of the virtual disk assigned to the VM
.PARAMETER NumCpu
  The number of vCPU allocated to the VM
.PARAMETER MemoryGB
  The amount of memory to be allocated to the VM
.PARAMETER Portgroup
  An array of VirtualPortgroupBase objects. For each 
  entry a NIC will be assigned to the VM, and it will
  be connected to the portgroup
.PARAMETER CD
  A switch to define if a CD/DVD shall be created on
  the VM
.PARAMETER ISOPath
  If a CD is allocated to the VM, this variable allows to specify
  an ISO file that will be connected.
.PARAMETER ESXi
  A switch indicating if the VM shall be configured for a nested
  ESXi hypervisor
.PARAMETER HyperV
  A switch indicating if the VM shall be configured for a nested
  Hyper-V hypervisor
.PARAMETER Force
  If a VM with the same Name already exists then setting this
  switch to $True will remove it before creating the new VM.
.EXAMPLE
  PS&gt; New-NestedHypervisor @splattedParameters
#&gt;

  [CmdletBinding(DefaultParameterSetName = &quot;ESXi&quot;)]
  Param(
    [Parameter(Mandatory=$true)]
    [string]$Name,
    [PSObject]$VMHost,
    [int]$DiskGB,
    [int]$NumCpu,
    [int]$MemoryGB,
    [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.VirtualPortGroupImpl[]]$Portgroup,
    [switch]$CD,
    [string]$ISOPath,
    [Parameter(Mandatory=$true,ParameterSetName = &quot;ESXi&quot;)]
    [switch]$ESXi,
    [Parameter(Mandatory=$true,ParameterSetName = &quot;Hyper-V&quot;)]
    [switch]$HyperV,
    [switch]$Force
  )

  process {
    # Simple VMHost OBN
    if($VMHost -is [System.String]){
      try {
        $VMHost = Get-VMHost -Name $VMHost
      }
      catch {
        Write-Error &quot;Invalid hostname $VMHost&quot;
        return
      }
    }

    # Check this is on a ESXi 5.1 ESXi
    if($VMHost.ApiVersion -ne &quot;5.1&quot;){
      Write-Error &quot;Host $($VMHost).Name doesn't seem to be an ESXi 5.1&quot;
      return
    }
    
    # Check if nested hypervisor is allowed
    if(!$VMHost.ExtensionData.Capability.NestedHVSupported){
      Write-Error &quot;Host $($VMHost).Name doesn't seem to support HVT&quot;
      return
    }

    # Check if the VM already exists. If yes, remove it
    $hv = Get-VM -Name $Name -ErrorAction SilentlyContinue
    if($hv){
      if($Force){
        if($hv.PowerState -eq &quot;PoweredOn&quot;){
          Stop-VM -VM $hv -Confirm:$false
        }
        Remove-VM -VM $hv -DeletePermanently -Confirm:$false
      }
      else{
        Write-Error &quot;There is already a VM called $Name present&quot;
        return
      }
    }

    # Create the VM
    $osId = &quot;vmkernel5Guest&quot;
    if($HyperV){
      $osId = &quot;windows8Server64Guest&quot;
    }
    $hvParams = @{
      Name = $Name
      VMHost = $VMHost
      GuestId = $osId
      Version = &quot;v9&quot;
      DiskGB = $DiskGB
      NumCpu = $NumCpu
      MemoryGB = $MemoryGB
      Portgroup = $Portgroup
      CD = $CD
    }
    $hv = New-VM @hvParams

    # Boot order (CD) &amp; nested enabled 
    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    if($CD){
      $spec.BootOptions = New-Object VMware.Vim.VirtualMachineBootOptions
      $spec.BootOptions.bootOrder += New-Object VMware.Vim.VirtualMachineBootOptionsBootableCdromDevice
    }
    $spec.nestedHVEnabled = $true
    if($HyperV){
      $vmxEntry = New-Object VMware.Vim.OptionValue
      $vmxEntry.Key = &quot;hypervisor.cpuid.v0&quot;
      $vmxEntry.Value = &quot;FALSE&quot;
      $spec.ExtraConfig += $vmxEntry
    }

    try {
      $hv.ExtensionData.ReconfigVM($spec)
    }
    catch {
      &quot;Reconfig failed&quot;
      return
    }

    # Mount the ISO
    if($CD -and $ISOPath){
      Get-CDDrive -VM $hv | 
      Set-CDDrive -IsoPath $ISOPath -StartConnected $true -Confirm:$false |
      Out-Null
    }
 
    # Return the newly created VM
    Get-VM -Name $Name
  }
}</pre><p></p>
<h4 style="text-align: left;">Annotations</h4>
<p><strong>Line 60-68</strong>: A rather simple implementation of the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI51R2/html/about_obn.html" target="_blank" rel="noopener noreferrer">Object By Name</a> concept for the VMHost parameter.</p>
<p><strong>Line 71-74</strong>: A test to verify that the function is running against an <strong>ESXi 5.1</strong>. Note that the function doesn&#8217;t test on &#8220;<em>5.1 or higher</em>&#8220;, the reason is that the concept in a future ESXi build may change.</p>
<p><strong>Line 77-80</strong>: The function verifies that the ESXi server passed in VMHost supports <a href="http://www.vmware.com/files/pdf/VMware_paravirtualization.pdf" target="_blank" rel="noopener noreferrer">Virtual Hardware-Assisted Virtualization</a> (<strong>VHV</strong>).</p>
<p><strong>Line 83-95</strong>: When a VM with the same <strong>Name</strong> already exists, the function will return unless the <strong>Force</strong> parameter is used.</p>
<p><strong>Line 98</strong>: Contrary to the procedure to be used with the Web Client, we can immediately select the ESXi <strong>guestId</strong>.</p>
<p><strong>Line 102-111</strong>: The &#8220;<em>splatted</em>&#8221; <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI51R2/html/New-VM.html" target="_blank" rel="noopener noreferrer">New-VM</a> parameters</p>
<p><strong>Line 116-119</strong>: If the <strong>CD</strong> switch was set to $True, the function will set the VM to boot from the CD/DVD.</p>
<p><strong>Line 121-126</strong>: In case the <strong>HyperV</strong> switch was set to $True, the function needs to add the &#8220;<em>hypervisor.cpuid.vo = FALSE</em>&#8221; entry to the VMX file.</p>
<p><strong>Line 137-141</strong>: If the <strong>CD</strong> switch was set to $True and an <strong>ISOPath</strong> was given, the function will connect the CD/DVD drive to the ISO.</p>
<h2>Sample Usage</h2>
<p>With the function you can now automate the creation of a number of nested hypervisors on your homelab server.</p>
<p>In the following example I create 3 VMs that will run <strong>nested ESXi</strong> hypervisors.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$esx = Get-VMHost -Name MyLab
$pgName = &quot;Internal&quot;
$pgObj = Get-VirtualPortGroup -Name $pgName
$pg = 1..4 | %{$pgObj}

$nestedParams = @{
  Name = $null
  VMHost = $esx
  DiskGB = 8
  NumCpu = 2
  MemoryGB = 8
  Portgroup = $pg
  CD = $true
  ISOPath = &quot;[datastore1] ISO/VMware-VMvisor-Installer-5.1.0-799733.x86_64.iso&quot;
  ESXi = $true
}

1..3 | %{
  $nestedParams.Name = &quot;ESX$($_)&quot;
  $newesx = New-NestedHypervisor @nestedParams
  Start-VM -VM $newesx -Confirm:$false
}</pre><p></p>
<p>In a bit more than 1 minute my 3 nested ESXi VMs have been created, and are ready to be installed. Hardly enough time to get me a decent Belgian beer from the fridge <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><a href="https://www.lucd.info/2013/07/12/hl-tools-part-2-create-a-nested-hypervisor/hv-nested-events/" rel="attachment wp-att-4517"><img decoding="async" class="alignnone  wp-image-4517" alt="hv-nested-events" src="https://lucd.info/wp-content/uploads/2013/07/hv-nested-events.png" width="478" height="211" srcset="https://www.lucd.info/wp-content/uploads/2013/07/hv-nested-events.png 796w, https://www.lucd.info/wp-content/uploads/2013/07/hv-nested-events-300x132.png 300w" sizes="(max-width: 478px) 100vw, 478px" /></a></p>
<p>And the same can of course be done for the creation of a nested <strong>Hyper-V</strong> hypervisor.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$esx = Get-VMHost -Name MyLab
$pgName = &quot;Internal&quot;
$pgObj = Get-VirtualPortGroup -Name $pgName
$pg = 1..4 | %{$pgObj}

$nestedParams = @{
  Name = &quot;HV1&quot;
  VMHost = $esx
  DiskGB = 8
  NumCpu = 2
  MemoryGB = 8
  Portgroup = $pg
  CD = $false
  HyperV = $true
}

$hv1 = New-NestedHypervisor @nestedParams</pre><p></p>
<p style="text-align: left;">Enjoy !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2013/07/12/hl-tools-part-2-create-a-nested-hypervisor/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
