Thorstein Løkensgard Thorstein Løkensgard - 1 year ago 70
Ruby Question

Resize disk size of vm with RbvMomi in Ruby

I'm working on a project where I'm cloning a vm with rbvmomi and after the clone is finished I want to ReConfig the Vm.

The cloning is working great, but I have some problems when I want to change the disk size of the Vm. I can change numbers of cpus and the size of memory, but when I trie to change disk size i get the error:

RbVmomi::Fault (InvalidDeviceSpec: Invalid configuration for device '0'.):

Code:

if json.has_key?('CPU_COUNT') && json.has_key?('RAM')
vm_cfg = {
:numCPUs => json['CPU_COUNT'],
:memoryMB => json['RAM'],
:deviceChange => [
:operation => :edit, #also tried with :add
:fileOperation => :create,
:device => RbVmomi::VIM.VirtualDisk(
:key => 0,
:backing => RbVmomi::VIM.VirtualDiskFlatVer2BackingInfo(
:fileName => '[datastore]',
:diskMode => :persistent,
:thinProvisioned => true
),
:controllerKey => 1000,
:unitNumber => 0,
:capacityInKB => json['DISK_SIZE'] * 1024 * 1024
)
]
}

dc.find_vm(vmID).ReconfigVM_Task(:spec => vm_cfg).wait_for_completion

Answer Source

Since you are trying to reconfigure an existing VirtualMachine, you could do something like this:

#Get the disk from the VM (assuming you only have one disk)
disk = vm.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk).first

#Set new capacity
disk.capacityInKB = new_capacity_in_kb

#Add the disk to the devicechange, specifying operation edit
vm_cfg = {
    :deviceChange => [
        {
            :device => disk,
            :operation => :edit
        }
    ]
}

#Start the ReconfigVM_Task with the disk edit on the VM
vm.ReconfigVM_Task(:spec => vm_cfg).wait_for_completion
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download