VMware Cloud Community
segadson
Contributor
Contributor

Finding the Next Free Spot on a VM to add a disk

I have a VM with this 2 different types of Disk Configuration scenerios Hard Disk 1 (0: 0), Hard Disk 2 (0: 1), Hard Disk 3 (0: 2), Hard Disk 4 (0: 3), Hard Disk 5 (1: 0)

Scenerio 1:

Hard Disk 1 (0: 0), Hard Disk 2 (0: 1), Hard Disk 3 (0: 2), Hard Disk 4 (0: 3), Hard Disk 5 (1: 0)

Scenerio 2:

Hard Disk 1 (0: 0), Hard Disk 2 (0: 1), Hard Disk 3 (0: 2), Hard Disk 4 (1: 0)

I have a workflow scriptable taks that has the input of SCSI Controller Hardware types (since it could be more than one in Scenerio 1 it has two (Bus 0 and bus 1) and in scenerio 2 it only has 1) and the input of all of the virtual disks.  I was wondering where my logic is going wrong because I am having trouble breaking out of loops.  What I am trying to do is find the first open slot on a VM to add a disk.  so for scenerio 1 the first open slot will be Hard Disk 6 (1:1) and for scenerio two Hard Disk 5 (0:3) but I am having trouble breaking out of my loops when as it will just itterate to the next controller

var scsiControllerSpots = [0,1,2];

var scsiControllerDisksArray = [];

var openSlots = [0,1,2,3];

for (var i = 0; i < scsiControllerSpots.length; i++) {

    for (var j = 0; j < scsiControllers.length; j++) {

        if (scsiControllerSpots[i] == scsiControllers[j].busNumber) {

  //System.log("Evaluate this SCSI Controller with Bus Number " + scsiControllers[j].busNumber);

  var SCSIcontrollerKey = scsiControllers[j].key

  //System.log(SCSIcontrollerKey);

  for (var k = 0; k < virtualDisks.length; k++) {

  if (virtualDisks[k].controllerKey == SCSIcontrollerKey){

  scsiControllerDisksArray.push(virtualDisks[k].unitNumber);

  //System.log("this disk belongs to SCSI Controller with Bus Nubmer " + scsiControllers[j].busNumber);

  //System.log(virtualDisks[k].unitNumber);

  }

        } if (scsiControllerDisksArray.length < 4){

  var openslot = scsiControllerDisksArray.length;

  System.log(openslot);

  //System.log(scsiControllerDisksArray.pop());

  break;

  } else {

  System.log("go to the next scsi slot");

  }

    }

  }

}

Reply
0 Kudos
2 Replies
admin
Immortal
Immortal

Have a look at this script:

var scsiControllerKey = -1;

var devices = vm.config.hardware.device;

var isScsiBusNumberInvalid = true;

if(scsiBusNumber==null || scsiBusNumber<0) scsiBusNumber=0;

//Find controller key for bus number

if (devices != null)  {

    for ( device in devices )  {

        if ( devices[device] instanceof VcVirtualBusLogicController || devices[device] instanceof VcVirtualLsiLogicController

        || devices[device] instanceof VcParaVirtualSCSIController || devices[device] instanceof VcVirtualLsiLogicSASController )  {

            if(scsiBusNumber==devices[device].busNumber){

                scsiControllerKey = devices[device].key;

                isScsiBusNumberInvalid = false;

                System.log( "SCSI controller found. (Key: " + scsiControllerKey + ")" );

                break;

            }

        }

    }

    if (isScsiBusNumberInvalid) throw("Error: Invalid SCSI busnumber");

    diskIndex = -1;  

    for ( device in devices )  {

        if ( (devices[device] instanceof VcVirtualDisk) && (devices[device].controllerKey == scsiControllerKey))  {

            if(devices[device].unitNumber > diskIndex)diskIndex = devices[device].unitNumber;          

        }

    }

    diskIndex++;

}

System.log("Next available disk index number: " +diskIndex);

input parameters for this workflow are : vm is the VC:VirtualMachine and scsiController is a number

output parameter is : diskIndex is a number

Once you have determined the diskNumber, you can pass that in to the add disk workflow. Hope that helps?

Oli

Reply
0 Kudos
segadson
Contributor
Contributor

Yes! Thank you this actually helped me alot

Reply
0 Kudos