Posts Tagged linked clones

Listing all linked clones on your vCenter Server

To list linked clones on your vCenter 4.0 Server, grouped (in most cases) by the base disks that they are sharing:

function Get-LinkedClone {
   #The following line is a fast replacement for:  $vms = get-vm args[0] | get-view
   if( $args[0] -eq $null ) {
      $vms = Get-View -ViewType VirtualMachine -Property Name,Summary,Config.Hardware.Device
   } else {
      $vms = Get-View -ViewType VirtualMachine -Property Name,Summary,Config.Hardware.Device -Filter @{Name = $args[0]}
   }

   $linkedClones = @()
   foreach ($vm in $vms) {
      $unshared = $vm.Summary.Storage.Unshared
      $committed = $vm.Summary.Storage.Committed
      $ftInfo = $vm.Summary.Config.FtInfo

      if ( ($unshared -ne $committed) -and (($ftInfo -eq $null) -or ($ftInfo.InstanceUuids.Length -le 1)) ){
         # then $vm is a linked clone. 

         # Find $vm's base disks.
         $baseDisks = @()
         foreach ($d in $vm.Config.Hardware.Device) {
            $backing = $d.backing
            if ($backing -is [VMware.Vim.VirtualDeviceFileBackingInfo] -and $backing.parent -ne $null) {
               do {
                  $backing = $backing.parent
               } until ($backing.parent -eq $null)
               $baseDisks += $backing.fileName

            }
         }

         $linkedClone = new-object PSObject
         $linkedClone | add-member -type NoteProperty -Name Name -Value $vm.name
         $linkedClone | add-member -type NoteProperty -Name BaseDisks -Value $baseDisks
         $linkedClones += $linkedClone
      }
      #else { do nothing for VMs that are not linked clones }
   }

   $linkedClones | sort BaseDisks, Name
}

Read the rest of this entry »

Tags: , ,

Creating many linked clones, a few at a time

Even if your ESX hosts can run a large number of VMs, they may not be able to power them all on simultaneously. Boot storming your virtual infrastructure can cause problems — among other things, if your VMs’ guest OS boots too slowly, errors can occur in the guest (e.g. Windows services can fail).

When creating linked clones, you can set cloneSpec.powerOn to true in order for linked clones to be powered on immediately upon creation, but if you issue too large a number of linked clone tasks at the same time using CloneVM_Task, you may experience such issues. You should therefore throttle the rapid creation of linked clones.

To limit the concurrency of linked clone creation, you can use CloneVM_Task to create a small batch of clones and then wait on those tasks to finish before continuing. You can use the wait-task cmdlet to wait until a set of tasks is complete. The following example creates 20 clones, 5 at a time, from the VM named “source_vm”. The linked clones are named “linked_clone_1” to “linked_clone_20”. You can write something similar if you are just interested in powering on existing VMs in batches.

 
$vm = Get-VM "source_vm" | Get-View
$clonePrefix = "linked_clone_"
$numClones = 20
$concurrentClones = 5

$cloneFolder = $vm.parent

$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$cloneSpec.Snapshot = $vm.Snapshot.CurrentSnapshot

$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
$cloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::createNewChildDiskBacking

#To power on each clone immediately after it is created:
#$cloneSpec.powerOn = $true

$i = 1
while ($i -le $numClones) {
   $taskViewArray = @()
   foreach ($j in 1..$concurrentClones) {
      $taskViewArray += $vm.CloneVM_Task( $cloneFolder, $clonePrefix+$i, $cloneSpec )
      $i++
   }
   $taskArray = $taskViewArray | Get-VIObjectByVIView
   Wait-Task $taskArray
}

Tags: , ,

Creating linked clones with the Virtualization EcoShell

Eric Sloof has adapted Hal Rottenberg’s linked clone script into a script action for the Virtualization EcoShell: Creating linked clones with the Virtualization EcoShell

Tags: , ,

Using the vSphere CloneVM API to create linked clones, clones from snapshots, and every other kind of clone

There are many options for cloning VMs that are not available through vSphere Client. The vSphere API CloneVM is available to users of vCenter Server and allows you to specify:

  • Whether the clone should be a fully-cloned VM or a linked clone (usually created very quickly, because it shares the disk of the source VM instead of copying it)
  • Whether to clone from a snapshot point (an earlier, saved state of the VM) or from the current state of the VM
  • Whether all the delta disks in the VM should be collapsed in into a single disk in the clone or should be copied as separate disks (if a VM has snapshots, and therefore a delta disk tree)
  • Whether the clone’s root/parent disk will be thick or thin provisioned (if creating a full clone)
  • Whether the clone should be powered on after being created
  • Whether the clone should be marked as a template
  • The folder, datastore, cluster, resource pool, and host where the clone should be created

For a non-PowerShell-based explanation, see the “Linked Virtual Machines” Technical Note from VMware.

The CloneVM API appears as a method of a VirtualMachine and takes three parameters:

$vm.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )

Read the rest of this entry »

Tags: , ,

Creating a linked clone from a snapshot point in PowerCLI

The following PowerShell code uses VMware vSphere’s CloneVM API to create a linked clone from a source VM’s current snapshot point. The clone will be located on the same host, datastore, and folder as the source VM. It requires a snapshot to exist on the source VM, and it requires vCenter Server.

connect-viserver "vCenter_hostname"
$sourceVM = Get-VM "source_vm_name" | Get-View
$cloneName = "linked_clone_name"
$cloneFolder = $sourceVM.parent

$cloneSpec = new-object Vmware.Vim.VirtualMachineCloneSpec
$cloneSpec.Snapshot = $sourceVM.Snapshot.CurrentSnapshot

$cloneSpec.Location = new-object Vmware.Vim.VirtualMachineRelocateSpec
$cloneSpec.Location.DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::createNewChildDiskBacking

$sourceVM.CloneVM_Task( $cloneFolder, $cloneName, $cloneSpec )

-Keshav Attrey

Tags: , ,