Getting Started with Hyper-V

As part of studying for the MS 70-410 exam, I recently began playing with Hyper-V. At first, I scoffed at the idea that anyone except VMWare could build a reliable hypervisor. Now that I’ve actually seen Hyper-V, I’m impressed!

Here’s something neat: Hyper-V is actually a Type-1 ‘bare-metal’ hypervisor. The hypervisor is actually loaded before the management OS. After installing the role, the windows server OS that you see and interact with is actually a virtualized ‘parent partition’ on the hypervisor.

Overview

  1. Install the Role
  2. Configure a Virtual Switch
  3. Create Your First VM
  4. Turn your First VM Into a Template
  5. Create Multiple Subsequent VMs

Install The Role

This part’s easy.

Install-WindowsFeature Hyper-V -IncludeManagementTools

Configure a Virtual Switch

There are three types of virtual switches:

  1. Internal. This switch is connected to the parent partition and can be connected to VM’s.
  2. Private. This switch is not connected to the parent partition. It can only be connected to VM’s.
  3. External. This switch acts like a physical switch that’s connected to the same uplink that the parent partition is connected to. It’s equivalent to having both the parent partition and the VM’s connected to the same switch. This type of switch is the most common, as it allows your VM’s to access the internet.

We are going to create an ‘external’ switch so that our VM’s can access the internet.

New-VMSwitch -name "internet" -switchtype external

Create Your First VM

  1. Get a copy of the Windows Server 2012 R2 or Windows 8.1 media in ISO format. Copy it to C:\VM\ISO or an alternate location of your choosing.
  2. Create the first VM’s VHD with the following command:
    New-VHD C:\VM\VHD\Windows-Server-2012-R2-Template.vhdx -sizeBytes 40GB -Dynamic
  3. Create the first VM with the following command:
    New-VM -Name Server2012Template -VHDPath C:\VM\VHD\Windows-Server-2012-R2-Template.vhdx -Generation 2 -MemoryStartupBytes 1024MB -SwitchName "internet"
    #enable dynamic memory
    Set-VMMemory -VMName Server2012Template -DynamicMemoryEnabled $true
  4. Configure a DVD drive and mount your OS install media.
    Add-VMDVDDrive -VMName Server2012Template
    Set-VMDVDDrive -Path C:\VM\ISO\Server-2012-R2.iso
  5. Start your VM, then connect to it and follow through with the install.
    Start-VM Server2012Template

Turn your First VM Into a Template

Great! But what if we need more VM’s? Hyper-V lets you sysprep a machine and use ‘differencing’ virtual disks to easily create new VM’s. A differencing disk is a VHDX image that’s based on a read-only parent image. Changes are written to the differencing disk instead of the parent. This allows for a lot of deduplication for creating VM’s based off of an initial VHDX file. Very cool stuff.

  1. Install all Microsoft Updates on the template VM.
  2. Install any common applications you want on the template — 7zip, office, RSAT, etc.
  3. When your template is configured, run the following command from inside the template VM’s OS:
    C:\windows\system32\sysprep\sysprep.exe
  4. A window will appear titled “System Preparation Tool 3.14”. Use the following parameters:
    System Cleanup Action: Enter System Out-Of-Box Experience
    Generalize: check this box
    Shutdown Options: Shutdown
  5. Click OK and allow the VM to shut itself down.
  6. Find the VM’s VHDX file in windows explorer, then right-click it and choose ‘properties’, then check the ‘read only’ box.
  7. It is now safe to delete the template VM if desired, but do not delete the template VHDX file.

Alright! You now have a template VMDX that can be used for the basis of new VM’s.

Create Multiple Subsequent VMs

Here’s the process:

  1. Create a new VHDX based on the parent template.
    New-VHDX -ParentPath C:\VM\VHD\Server-2012-R2-Template.vhdx -path C:\VM\VHD\MyNewServer1.vhdx -differencing
  2. Create a new VM.
    New-VM -Name MyNewServer1 -VHDPath C:\VM\VHD\MyNewServer1.vhdx -memoryStartupBytes 1024MB -SwitchName Internet -Generation 2
    Set-VMMemory -VMName MyNewServer1 -DynamicMemoryEnabled $true
    Start-VM MyNewServer1

Bam! That’s it. Your new VM will be based on the template. Check out the disk size on the parent partition via windows explorer. You’ll be amazed at how little space the new VM will use, even after a decent amount of use.

Happy Hunting.