Sunday, February 12, 2017

Linked clones in Hyper-V


I've allways been a fan of VMware Workstation, but I've been spending a bit of time with Hyper-V on Windows 10 now too to see how the cookies are on the other side. One thing I thought I missed was creating what is called linked clones in VMware Workstation.



Linked clones allow a user to create multiple VM's that are referenced off of only one VM. The changes compared to the first VM are stored in a so called delta file under the covers. This way, you can build a large lab while needing a lot less space. You will however need access to the original VM files.


It turns out Hyper-V has the same functionality, but the steps to create a linked clone are a bit more hidden, and the operations are a bit more manual. The steps are as follows:
  • You need a template VM to reference your linked clones from. No different from VMware Workstation. The difference though, is that with VMware Workstation, you can keep working with this VM. With Hyper-V, this template is going to be left as an image on your harddrive, but you can't use it.
  • Sysprep the template VM. Theoretically you should do that in VMware Workstation too, but if you have some quick test you want to do and don't require domain functionality this could be omitted.
  • Turn off the VM, and mark the vhdx file of the VM as read-only. You can even remove that VM out of Hyper-V, so you don't accidentally turn it back on again
  • In Hyper-V, go to Action->New->Hard Disk. Select "Differencing Disk" as harddisk type, set the destination location and select the vhdx from the VM you created earlier.
  • Finally, create a new VM and select the newly created differenced disk instead of a new harddisk.
A lot of steps for what is relatively simple wizard in VMware Workstation. I think we can do better than that ;-) Powershell to the rescue! In the case below I've been lazy, and created a function that has paths and memory and such hardcoded inside, but the idea is what counts:


  • You create a differencing disk with New-VHD
  • You create the VM
  • You add the harddisk

$Template = "C:\VM\2012 Template.vhdx"


function New-CloneVM
{
    [CmdletBinding()]
    [Alias()]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $Template,
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        $NewVM
    )

    $destdisk = “C:\VM\$NewVM.vhdx”
    
    New-VHD –ParentPath $Template -Path $destdisk  –Differencing
    New-VM  $NewVM -MemoryStartupBytes 2048MB -SwitchName VLAN2 -Generation 2 -BootDevice CD
    Add-VMHardDiskDrive -VMName $NewVM -Path $destdisk -ControllerType SCSI
}


New-CloneVM -template $Template -NewVM Test1



If you quickly want to create a bunch of VM's for your testlab, Powershell and Hyper-V are a good alternative to VMware workstation, and in fact, since VMware Workstation doesn't support Powershell, Hyper-V might even be better.. I know, I know. blasphemy.

No comments:

Post a Comment