How to resize VM disk with RbVmomi in Ruby

761 views Asked by At

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 the numbers of CPUs and the size of memory, but when I try to change the 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
2

There are 2 answers

0
h3rmanj On BEST ANSWER

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
0
anquegi On

On their Github is a complete example. Here are the relevant parts:

{
          operation: :add,
          fileOperation: :create,
          device: VIM.VirtualDisk(
            key: 0,
            backing: VIM.VirtualDiskFlatVer2BackingInfo(
              fileName: '[datastore1]',
              diskMode: :persistent,
              thinProvisioned: true,
            ),
            controllerKey: 1000,
            unitNumber: 0,
            capacityInKB: 4000000,
          )
        }

The complete code is:

#!/usr/bin/env ruby
require 'trollop'
require 'rbvmomi'
require 'rbvmomi/trollop'

VIM = RbVmomi::VIM

opts = Trollop.options do
  banner <<-EOS
Create a VM.

Usage:
    create_vm-1.9.rb [options]

VIM connection options:
    EOS

    rbvmomi_connection_opts

    text <<-EOS

VM location options:
    EOS

    rbvmomi_datacenter_opt

    text <<-EOS

Other options:
  EOS
end

Trollop.die("must specify host") unless opts[:host]
vm_name = ARGV[0] or abort "must specify VM name"

vim = VIM.connect opts
dc = vim.serviceInstance.find_datacenter(opts[:datacenter]) or abort "datacenter not found"
vmFolder = dc.vmFolder
hosts = dc.hostFolder.children
rp = hosts.first.resourcePool

vm_cfg = {
  name: vm_name,
  guestId: 'otherGuest',
  files: { vmPathName: '[datastore1]' },
  numCPUs: 1,
  memoryMB: 128,
  deviceChange: [
    {
      operation: :add,
      device: VIM.VirtualLsiLogicController(
        key: 1000,
        busNumber: 0,
        sharedBus: :noSharing,
      )
    }, {
      operation: :add,
      fileOperation: :create,
      device: VIM.VirtualDisk(
        key: 0,
        backing: VIM.VirtualDiskFlatVer2BackingInfo(
          fileName: '[datastore1]',
          diskMode: :persistent,
          thinProvisioned: true,
        ),
        controllerKey: 1000,
        unitNumber: 0,
        capacityInKB: 4000000,
      )
    }, {
      operation: :add,
      device: VIM.VirtualE1000(
        key: 0,
        deviceInfo: {
          label: 'Network Adapter 1',
          summary: 'VM Network',
        },
        backing: VIM.VirtualEthernetCardNetworkBackingInfo(
          deviceName: 'VM Network',
        ),
        addressType: 'generated'
      )
    }
  ],
  extraConfig: [
    {
      key: 'bios.bootOrder',
      value: 'ethernet0'
    }
  ]
}

vmFolder.CreateVM_Task(:config => vm_cfg, :pool => rp).wait_for_completion