VMware Cloud Community
DavidMcKnight
Contributor
Contributor

Datastore vMotion Linked Clone VMs

I have situation where I have a bunch of VMs that I created via Powershell that are linked clones.  So I am not using View to manage them.  My problem is, I want to do some maintenance on the storage server they reside.  If I try to datastore migrate them to another storage server vCenter makes a copy of the base replica (that each VM shares) for each VM.  I could probable shut down the VMs then copy the files, but...

Can anyone recommend some code I could use that would allow me migrate powered on, linked clone VMs to another datastore without having to copy the base replica?

Thanks,

0 Kudos
3 Replies
LucD
Leadership
Leadership

Not sure if you even can svMotion linked clones at all, see also the note in KB1028754.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DavidMcKnight
Contributor
Contributor

I've read that, but again that implies I'm using View which I am not.  And yes, you can svMotion a linked clone VM from within vCenter but it will put a full copy of the base replica in the folder with the VM on the destination datastore, completely negating the benefits of having the VM be a linked clone in the first place.

Maybe the better question would be, can someone suggest what the equivalent code would be to pull off what View is doing to rebalance a linked clone datastore?

0 Kudos
DavidMcKnight
Contributor
Contributor

Once I posted my original question I kept googling and came across these two pages:

https://www.vmware.com/support/developer/vc-sdk/linked_vms_note.pdf

Storage vmotion to different datastores for different vm hard disks | VMware and Powershell

After reading these I was able to cobble together this code:

$vmxds= (Get-Datastore 'Destination_Datastore' | get-view).moref.value

$myvmtomove=get-vm 'LinkedClone_To_svMotion'

$disks=$myvmtomove|Get-harddisk|% {$_.id.split('/')[1]} 

$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.datastore = New-Object VMware.Vim.ManagedObjectReference

$spec.datastore.type = "Datastore"

$spec.datastore.Value = $vmxds

$spec.disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator[] (1)

$spec.disk[0] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator

$spec.disk[0].diskId = $disks

$spec.disk[0].DiskMoveType = [Vmware.Vim.VirtualMachineRelocateDiskMoveOptions]::moveChildMostDiskBacking

$spec.disk[0].datastore = New-Object VMware.Vim.ManagedObjectReference

$spec.disk[0].datastore.type = "Datastore"

$spec.disk[0].datastore.Value =  $vmxds

$rv = (Get-View -Id $myvmtomove.id).RelocateVM_Task($spec, "defaultPriority")

do {

  start-sleep 1

  $rv = Get-Task Running | where {$_.name -eq 'RelocateVM_Task'}

   } until ($rv -eq $null)

This seems to work perfect if your linked clone does not have any snapshots.  If you do have a snapshot it moves the snapshot vmdk and the rest VMs config files, but leaves behind the vmdk that is made when the liked clone is created.  And if you revert the snapshot, the snapshot's vmdk is recreated on the original datastore.

So, any advise on dealing with linked clone with snapshots would be appreciated.

Thanks,

0 Kudos