Relocate virtual machine disks to custom datastores

Relocate virtual machine disks to custom datastores

A recent post in the community (Re: Relocate VM Harddisk) resulted in the attached workflow. In short, the Library copy of the "Relocate virtual machine disks" workflow passes the "disk" value as null to the relocateSpec. This results in all disks being placed on the same datastore. What needs to be done (and is done in the attached flow) is each disk you wish to move must be matched up to a target datastore by way of a VcVirtualMachineRelocateSpecDiskLocator. These mappings are passed into an array and that array gets assigned to the "disk" input of the relocateSpec.

Here is the key code that makes things work:

var devices = vm.config.hardware.device;

if (datastore == null){

    // targetDatastore Must have a value. If no value is specified, default to the first returned disk from the vm.datastore array

    targetDatastore = vm.datastore[0];

}else{

    targetDatastore = datastore;

}

var dsArray = new Array(disk0DS,disk1DS,disk2DS,disk3DS,disk4DS,disk5DS,disk6DS,disk7DS);

var disk = new Array();

var diskUnitNumbers = new Array(); 

for (var i in devices) {

    if (devices[i] instanceof VcVirtualDisk) {

        if (devices[i].deviceInfo!=null){

            var unitNumber= devices[i].unitNumber;

            var dl = new VcVirtualMachineRelocateSpecDiskLocator();

            dl.diskId = devices[i].key;

            if (dsArray[unitNumber] != null){

                dl.datastore = dsArray[unitNumber].reference;

            }else{

                dl.datastore = targetDatastore.reference;

            }

            disk.push(dl);

        }

    }

}

The usual disclaimers apply: Use at your own risk!! No guarantee of support. Use on test VM to be sure it works as desired. Modify as needed to fit your own use-case.

UPDATE: I've just added "Relocate vm disks" workflow as an attachment here. This workflow ONLY moves disks to different datastores, not potentially the whole VM like the first one does... this allows the workflow to move disks even if the VM is powered on ... at least in my test is was successful Smiley Wink

Attachments
Comments

This has been working for me for quite some time now - although recently we have changed our second hard disk to be located on a different scsi controller, so hard disk 1 is on SCSI controller 0 (0:0) and hard disk 2 is on SCSI controller 1 (1:0) - relocating the disk no longer works now due to this. Is there a way to change the code so it is aware of the SCSI controllers? Cheers David.

Version history
Revision #:
1 of 1
Last update:
‎12-04-2013 01:50 PM
Updated by: