VMware Aria Automation Orchestrator

 View Only
Expand all | Collapse all

Resizing VM disk through VCO

  • 1.  Resizing VM disk through VCO

    Posted Aug 08, 2011 11:46 AM

    Hi to all,

    I'm trying to resize a VM disk through the VCO, without success.

    Actually I wrote the following script:

    //*************************************************

    //vm is a VC:VirtualMachine parameter

    //newSize is a number parameter

    //return type is VC:Task

    if (!vm) return null;

    var devices = vm.config.hardware.device;
    var firstDisk = null;
    var diskSize = -1;
    var newSizeInKB = newSize * 1024 * 1024;

    //Finds the first VM disk

    for (var i in devices) {
        if (devices[i] instanceof VcVirtualDisk) {

             firstDisk = devices[i];
        }
    }


    diskSize = firstDisk.capacityInKB;
    if (diskSize < 0 || newSizeInKB <= diskSize) return null; //No disk found, or the newSize is too small

    var _key = firstDisk.key;
    var _controllerKey = firstDisk.controllerKey;
    var _unitNumber = firstDisk.unitNumber;
    var _backing = firstDisk.backing;
    var _newDiskSizeInKB = newSizeInKB;

    System.log("key:" + _key + "\nControllerKey:" + _controllerKey + "\nUnitNumber:" + _unitNumber + "\nBacking:" + _backing);

    var _newVirtualDevice = new VcVirtualDisk();
    _newVirtualDevice.controllerKey = _controllerKey;
    _newVirtualDevice.key = _key;
    _newVirtualDevice.backing = _backing;
    _newVirtualDevice.unitNumber = _unitNumber;
    _newVirtualDevice.capacityInKB = _newDiskSizeInKB;

    System.log("key:" + _newVirtualDevice.key + "\nControllerKey:" + _newVirtualDevice.controllerKey + "\nUnitNumber:" + _newVirtualDevice.unitNumber + "\nBacking:" + _newVirtualDevice.backing + "\nNew Size:" + _newVirtualDevice.capacityInKB);

    var _virtualDeviceConfigSpec = new VcVirtualDeviceConfigSpec();
    _virtualDeviceConfigSpec.device = _newVirtualDevice;
    _virtualDeviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
    _virtualDeviceConfigSpec.fileOperation = VcVirtualDeviceConfigSpecFileOperation.replace;


    var _virtualMachineConfigSpec = new VcVirtualMachineConfigSpec();
    _virtualMachineConfigSpec.deviceChange = new Array();
    _virtualMachineConfigSpec.deviceChange.push(_virtualDeviceConfigSpec);

    return vm.reconfigVM_Task(_virtualMachineConfigSpec);

    //*************************************************

    Executing the code I can see the task starting and immediately ending in vCenter without errors, but the vm disk size remains the same.

    Could you help me?



  • 2.  RE: Resizing VM disk through VCO
    Best Answer

    Broadcom Employee
    Posted Aug 08, 2011 12:37 PM

    Hi,

    It might seem strange but could you try change these lines:

    _virtualMachineConfigSpec.deviceChange = new Array();
    _virtualMachineConfigSpec.deviceChange.push(_virtualDeviceConfigSpec);

    with

    var changedDevices = new Array();

    changedDevices.push(_virtualDeviceConfigSpec);

    _virtualMachineConfigSpec.deviceChange = changedDevices;

    There is a problem with modifying directly properties of type Array.

    Regards,

    --Martin



  • 3.  RE: Resizing VM disk through VCO

    Posted Aug 08, 2011 01:40 PM

    Hi Martin!

    It worked like a charm.

    Thank you for your quick answer.

    (P.S. I also had to remove the line _virtualDeviceConfigSpec.fileOperation = VcVirtualDeviceConfigSpecFileOperation.replace;)



  • 4.  RE: Resizing VM disk through VCO

    Posted Mar 31, 2014 12:58 PM

    Hey,

    I'm getting error "invalid return" in this line if (!vm) return null;

    I had added out param as VC:Task.....



  • 5.  RE: Resizing VM disk through VCO

    Broadcom Employee
    Posted Mar 31, 2014 01:08 PM

    This entire thread is in the wrong community...



  • 6.  RE: Resizing VM disk through VCO

    Broadcom Employee
    Posted Nov 21, 2014 10:47 PM

    moved to the right Orchestrator community.



  • 7.  RE: Resizing VM disk through VCO

    Posted May 06, 2015 04:09 PM

    I know this is an old post but i get the same error. Did you ever find the fix? if so would you mind sharing?



  • 8.  RE: Resizing VM disk through VCO

    Posted Nov 29, 2018 05:22 AM

    Hello,

    I am trying the same thing to resize the existing disk. For me, the workflow runs with any error but the disk size does not increase (you have mentioned that you were facing same issue) As suggested in one of the answers by Martin and you , I changed few lines of script as well. But could get any solution.

    If you could provide some help ?

    Sakshi Singh



  • 9.  RE: Resizing VM disk through VCO

    Posted Sep 08, 2021 03:26 PM
    Is this still the case? can not change array property directly?


  • 10.  RE: Resizing VM disk through VCO

    Posted Dec 19, 2014 06:01 PM

    Hey Folks,

    I was trying to run the workflow, but I have got an error.

    Seems like the reconfig is not working. Follows the logs.

    If anyone has a hint, please, let me know.

    Running on VCO 5.1;

    [2014-12-19 15:52:49.797] [D] Found disk: DynamicWrapper (Instance) : [VcVirtualDisk]-[class com.vmware.vim.vi4.VirtualDisk] -- VALUE : com.vmware.vim.vi4.VirtualDisk@df621e50

    [2014-12-19 15:52:49.799] [D] diskAry: ,2000

    [2014-12-19 15:52:49.800] [D] diskArray: ,2000

    [2014-12-19 15:52:49.804] [D] diskkey: 2000

    [2014-12-19 15:52:49.808] [D] disk: DynamicWrapper (Instance) : [VcVirtualDisk]-[class com.vmware.vim.vi4.VirtualDisk] -- VALUE : com.vmware.vim.vi4.VirtualDisk@df621e50

    [2014-12-19 15:52:49.811] [D] oldSize: 20971520

    [2014-12-19 15:52:49.815] [D] dcs: DynamicWrapper (Instance) : [VcVirtualDeviceConfigSpec]-[class com.vmware.vim.vi4.VirtualDeviceConfigSpec] -- VALUE : com.vmware.vim.vi4.VirtualDeviceConfigSpec@e0318c5b

    [2014-12-19 15:52:49.817] [D] deviceSpec: DynamicWrapper (Instance) : [VcVirtualDeviceConfigSpec]-[class com.vmware.vim.vi4.VirtualDeviceConfigSpec] -- VALUE : com.vmware.vim.vi4.VirtualDeviceConfigSpec@e0318c5b

    [2014-12-19 15:52:49.820] [D] deviceSpec.operationDynamicWrapper (Instance) : [VcVirtualDeviceConfigSpecOperation]-[class com.vmware.vim.vi4.VirtualDeviceConfigSpecOperation] -- VALUE : edit

    [2014-12-19 15:52:49.825] [D] deviceSpec.device DynamicWrapper (Instance) : [VcVirtualDisk]-[class com.vmware.vim.vi4.VirtualDisk] -- VALUE : com.vmware.vim.vi4.VirtualDisk@e0021e50

    [2014-12-19 15:52:49.828] [D] new Size: 31457280

    [2014-12-19 15:52:49.831] [D] configSpec.deviceChange: DynamicWrapper (Instance) : [VcVirtualDeviceConfigSpec]-[class com.vmware.vim.vi4.VirtualDeviceConfigSpec] -- VALUE : com.vmware.vim.vi4.VirtualDeviceConfigSpec@e0318c5b

    [2014-12-19 15:52:53.864] [I] VIM Task is null (Dynamic Script Module name : vim3WaitTaskEnd#35)

    Thanks,

    Bruno Costa



  • 11.  RE: Resizing VM disk through VCO

    Posted Dec 19, 2014 07:29 PM

    Folks,

    For those who got the same error as I did, should check for VMO log.

    NotAuthenticated : The session is not authenticated., privilege id 'System.View'

    I restarted the Vcenter Orchestrator service, and the workflow worked 100%!

    Regards,

    Bruno Costa



  • 12.  RE: Resizing VM disk through VCO

    Posted Jun 11, 2025 03:36 PM

    This will resize SCSI Controller 0 Hard Disk 1 to the value of new_disk_size_gb

    devices = vm.config.hardware.device;
    
    for each (device in devices) {
        if (device instanceof VcVirtualDisk) {
            if (device.controllerKey == 1000 && device.unitNumber == 0) {
                device.capacityInKB = new_disk_size_gb * 1024 * 1024
                
                config_spec = new VcVirtualMachineConfigSpec()
                device_config_specs = new Array()
                device_config_spec = new VcVirtualDeviceConfigSpec()
                device_config_spec.device = device
                device_config_spec.operation = VcVirtualDeviceConfigSpecOperation.edit
                device_config_specs.push(device_config_spec)
                config_spec.deviceChange = device_config_specs
    
                vm.reconfigVM_Task(config_spec)
            }
        }
    }



  • 13.  RE: Resizing VM disk through VCO

    Broadcom Employee
    Posted Jan 02, 2026 06:04 AM

    I have written a solution to this in TypeScript using the Aria Build Tools

    https://github.com/vmware/build-tools-for-vmware-aria

    The first step is to take the new size and update the VcVirtualDisk CapacityInKB value

        public VcVirtualMachineIncreaseVcVirtualDiskSize(@notNull @notEmpty @required objVcVirtualMachine: VcVirtualMachine, @notNull @notEmpty @required objVcVirtualDisk: VcVirtualDisk, @notNull @notEmpty @required intDiskSizeGB: number): boolean {
            let intKilobytes: number = 1024 * 1024;
    
            let intNewSizeKilobytes: number = objVcVirtualDisk.capacityInKB + intDiskSizeGB * intKilobytes;
    
            objVcVirtualDisk.capacityInKB = intNewSizeKilobytes;
    
            let objVcVirtualDeviceConfigSpec: VcVirtualDeviceConfigSpec = this.VcVirtualDeviceConfigSpecCreate(objVcVirtualDisk, VcVirtualDeviceConfigSpecOperation.edit);
    
            let blnResult: boolean = this.VcVirtualMachineReconfigureDevice(objVcVirtualMachine, objVcVirtualDeviceConfigSpec);
    
            return blnResult;
        }

    The second step is to create a new VcVirtualDeviceConfigSpec

        private VcVirtualDeviceConfigSpecCreate(@notNull @notEmpty @required objVcVirtualDevice: VcVirtualDevice, @notNull @notEmpty @required enumVcVirtualDeviceConfigSpecOperation: VcVirtualDeviceConfigSpecOperation, @notNull @notEmpty @required enumVcVirtualDeviceConfigSpecFileOperation: VcVirtualDeviceConfigSpecFileOperation = null, @notNull @notEmpty @required arrVcVirtualMachineProfileSpec: VcVirtualMachineProfileSpec[] = []): VcVirtualDeviceConfigSpec {
            let objVcVirtualDeviceConfigSpec: VcVirtualDeviceConfigSpec = new VcVirtualDeviceConfigSpec();
            objVcVirtualDeviceConfigSpec.operation = enumVcVirtualDeviceConfigSpecOperation;
            objVcVirtualDeviceConfigSpec.fileOperation = enumVcVirtualDeviceConfigSpecFileOperation
            objVcVirtualDeviceConfigSpec.device = objVcVirtualDevice;
            objVcVirtualDeviceConfigSpec.profile = arrVcVirtualMachineProfileSpec;
    
            return objVcVirtualDeviceConfigSpec;
        }

    The third step is to create an array of VcVirtualDeviceConfigSpec with this single item

        private VcVirtualMachineReconfigureDevice(@notNull @notEmpty @required objVcVirtualMachine: VcVirtualMachine, @notNull @notEmpty @required objVcVirtualDeviceConfigSpec: VcVirtualDeviceConfigSpec): boolean {
    
            let arrVcVirtualDeviceConfigSpec: VcVirtualDeviceConfigSpec[] = [];
            arrVcVirtualDeviceConfigSpec.push(objVcVirtualDeviceConfigSpec);
    
            let blnResult: boolean = this.VcVirtualMachineReconfigureDevices(objVcVirtualMachine, arrVcVirtualDeviceConfigSpec);
    
            return blnResult;
        }

    The fourth step is to create a VcVirtualMachineConfigSpec 

        private VcVirtualMachineReconfigureDevices(@notNull @notEmpty @required objVcVirtualMachine: VcVirtualMachine, @notNull @notEmpty @required arrVcVirtualDeviceConfigSpec: VcVirtualDeviceConfigSpec[]): boolean {
    
            let objVcVirtualMachineConfigSpec: VcVirtualMachineConfigSpec = new VcVirtualMachineConfigSpec();
            objVcVirtualMachineConfigSpec.deviceChange = arrVcVirtualDeviceConfigSpec;
    
            let blnResult: boolean = this.VcVirtualMachineReconfigure(objVcVirtualMachine, objVcVirtualMachineConfigSpec);
    
            return blnResult;
        }

    The fifth step is to Reconfigure the VcVirtualMachine using the VcVirtualMachineConfigSpec

        private VcVirtualMachineReconfigure(@notNull @notEmpty @required objVcVirtualMachine: VcVirtualMachine, @notNull @notEmpty @required objVcVirtualMachineConfigSpec: VcVirtualMachineConfigSpec): boolean {
    
            try {
                Locking.lock(`${objVcVirtualMachine.name}-Modification`);
    
                let objVcTask: VcTask = objVcVirtualMachine.reconfigVM_Task(objVcVirtualMachineConfigSpec);
    
                this.WaitForVcTaskEnd(objVcTask, 1);
    
                Locking.unlock(`${objVcVirtualMachine.name}-Modification`);
    
                return true;
            }
            catch (objException) {
                Locking.unlock(`${objVcVirtualMachine.name}-Modification`);
    
                let objError: Error = objException as Error;
    
                this.objLogger.error(`Name: ${objError.name}`);
                this.objLogger.error(`Message: ${objError.message}`);
    
                if (objException instanceof VcConcurrentAccess) {
                    this.objLogger.error(`Thrown if the changeVersion does not match the server's changeVersion for the configuration.`);
                }
                else if (objException instanceof VcCpuHotPlugNotSupported) {
                    this.objLogger.error(`Thrown if the current configuration of the VM does not support hot - plugging of CPUs.`);
                }
                else if (objException instanceof VcDuplicateName) {
                    this.objLogger.error(`Thrown if the specified name already exists in the parent folder.`);
                }
                else if (objException instanceof VcFileFault) {
                    this.objLogger.error(`Thrown if there is a problem creating or accessing the virtual machine's files for this operation. Typically a more specific fault like NoDiskSpace or FileAlreadyExists is thrown.`);
                }
                else if (objException instanceof VcInsufficientResourcesFault) {
                    this.objLogger.error(`Thrown if this operation would violate a resource usage policy.`);
                }
                else if (objException instanceof VcInvalidDatastore) {
                    this.objLogger.error(`Thrown if the operation cannot be performed on the target datastores.`);
                }
                else if (objException instanceof VcInvalidName) {
                    this.objLogger.error(`Thrown if the specified name is invalid.`);
                }
                else if (objException instanceof VcInvalidPowerState) {
                    this.objLogger.error(`Thrown if the power state is poweredOn and the virtual hardware cannot support the configuration changes.`);
                }
                else if (objException instanceof VcInvalidState) {
                    this.objLogger.error(`Thrown if the operation cannot be performed in the current state of the virtual machine.For example, because the virtual machine's configuration is not available.`);
                }
                else if (objException instanceof VcMemoryHotPlugNotSupported) {
                    this.objLogger.error(`Thrown if the current configuration of the VM does not support hot - plugging of memory.`);
                }
                else if (objException instanceof VcRuntimeFault) {
                    this.objLogger.error(`Thrown if any type of runtime fault is thrown that is not covered by the other faults; for example, a communication error.`);
                }
                else if (objException instanceof VcTaskInProgress) {
                    this.objLogger.error(`Thrown if the virtual machine is busy.`);
                }
                else if (objException instanceof VcTooManyDevices) {
                    this.objLogger.error(`Thrown if the device specifications exceed the allowed limits.`);
                }
                else if (objException instanceof VcVmConfigFault) {
                    this.objLogger.error(`Thrown if the spec is invalid.Typically, a more specific subclass is thrown.`);
                }
                else if (objException instanceof VcVmWwnConflict) {
                    this.objLogger.error(`Thrown if the WWN of the virtual machine has been used by other virtual machines.`);
                }
    
                return false;
            }
        }

    The sixth step is to wait for the VcTask to complete and handle any error that occur.

        public WaitForVcTaskEnd(@notNull @notEmpty @required objVcTask: VcTask, @notNull @notEmpty @required intPollRate: number): any {
    
            while (objVcTask.info !== null) {
    
                let objVcTaskInfoState: VcTaskInfoState = objVcTask.info.state;
    
                if (objVcTaskInfoState.value === "success") {
                    break;
                }
                else if (objVcTaskInfoState.value === "error") {
                    if (objVcTask.info.error.localizedMessage === null) {
                        throw `Task '${objVcTask.info.name}' has encountered an unknown error`;
                    }
                    else {
                        throw `Task '${objVcTask.info.name}' error: '${objVcTask.info.error.localizedMessage}'`;
                    }
                }
                else if (objVcTaskInfoState.value === "running") {
                    if (objVcTask.info.progress === null) {
                        this.objLogger.info(`${objVcTask.info.name} - Queued or In Progress...`);
                    }
                    else {
                        this.objLogger.info(`${objVcTask.info.name} - Progress: ${objVcTask.info.progress} %`);
                    }
                }
    
                System.sleep(intPollRate * 1000);
            }
    
            this.objLogger.info(objVcTask.info.name + " - Completed");
    
            System.sleep(2 * 1000);
    
            if (objVcTask !== null && objVcTask.info !== null && objVcTask.info.result_AnyValue !== null) {
    
                let objVcSdkConnection: VcSdkConnection = objVcTask.sdkConnection;
    
                return VcPlugin.convertToVimManagedObject(objVcSdkConnection, objVcTask.info.result_AnyValue);
            }
            else {
                return null;
            }
        }



    ------------------------------
    Simon Sparks
    Automation & Orchestration Consultant
    North West England, UK, Europe
    Broadcom Employee
    ------------------------------