我正在做一个项目,我正在使用 RbVmomi 克隆一个 VM,在克隆完成后我想重新配置 VM。
克隆工作得很好,但是当我想更改 VM 的磁盘大小时我遇到了一些问题。我可以更改 CPU 数量和内存大小,但是当我尝试更改磁盘大小时出现错误:
RbVmomi::Fault (InvalidDeviceSpec: Invalid configuration for device '0'.):
代码:
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
最佳答案
由于您正在尝试重新配置现有的 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
关于ruby-on-rails - 如何在 Ruby 中使用 RbVmomi 调整 VM 磁盘大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42090844/