Ruby on rails 如何在Ruby中使用RbVmomi调整VM磁盘的大小

Ruby on rails 如何在Ruby中使用RbVmomi调整VM磁盘的大小,ruby-on-rails,ruby,vmware,vsphere,rbvmomi,Ruby On Rails,Ruby,Vmware,Vsphere,Rbvmomi,我正在做一个项目,我正在用RbVmomi克隆一个VM,克隆完成后,我想重新配置VM 克隆工作很好,但是当我想更改虚拟机的磁盘大小时,我遇到了一些问题。我可以更改CPU的数量和内存大小,但当我尝试更改磁盘大小时,会出现错误: RbVmomi::Fault (InvalidDeviceSpec: Invalid configuration for device '0'.): 代码: 在他们的Github上有一个。以下是相关部分: { operation: :add,

我正在做一个项目,我正在用RbVmomi克隆一个VM,克隆完成后,我想重新配置VM

克隆工作很好,但是当我想更改虚拟机的磁盘大小时,我遇到了一些问题。我可以更改CPU的数量和内存大小,但当我尝试更改磁盘大小时,会出现错误:

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

在他们的Github上有一个。以下是相关部分:

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

#!/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
#/usr/bin/env ruby
需要“trollop”
需要“rbvmomi”
需要“rbvmomi/trollop”
VIM=RbVmomi::VIM
opts=Trollop.options do

banner由于您正在尝试重新配置现有的VirtualMachine,您可以执行以下操作:

#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
#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