ruby 用于将磁盘添加到vSphere VM的代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于将磁盘添加到vSphere VM的代码相关的知识,希望对你有一定的参考价值。

    def add_disk(vmname, size, datastore)
      begin
        @connection.serviceInstance.CurrentTime
      rescue
        initialize
      end

      vm = find_vm(vmname) || find_vm_heavy(vmname)[vmname]

      vmdk_datastore = find_datastore(datastore)
      vmdk_file_name = "#{vmname}/#{vmname}_#{find_vmdks(vmname, datastore).length + 1}.vmdk"

      controller = find_disk_controller(vm)

      vmdk_spec = RbVmomi::VIM::FileBackedVirtualDiskSpec(
        capacityKb: size.to_i * 1024 * 1024,
        adapterType: 'lsiLogic',
        diskType: 'thin'
      )

      vmdk_backing = RbVmomi::VIM::VirtualDiskFlatVer2BackingInfo(
        datastore: vmdk_datastore,
        diskMode: 'persistent',
        fileName: "[#{vmdk_datastore.name}] #{vmdk_file_name}"
      )

      device = RbVmomi::VIM::VirtualDisk(
        backing: vmdk_backing,
        capacityInKB: size.to_i * 1024 * 1024,
        controllerKey: controller.key,
        key: -1,
        unitNumber: find_disk_unit_number(vm, controller)
      )

      device_config_spec = RbVmomi::VIM::VirtualDeviceConfigSpec(
        device: device,
        operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation('add')
      )

      vm_config_spec = RbVmomi::VIM::VirtualMachineConfigSpec(
        deviceChange: [device_config_spec]
      )

      @connection.serviceContent.virtualDiskManager.CreateVirtualDisk_Task(
        datacenter: @connection.serviceInstance.find_datacenter,
        name: "[#{vmdk_datastore.name}] #{vmdk_file_name}",
        spec: vmdk_spec
      ).wait_for_completion

      vm.ReconfigVM_Task(spec: vm_config_spec).wait_for_completion

      true
    end

以上是关于ruby 用于将磁盘添加到vSphere VM的代码的主要内容,如果未能解决你的问题,请参考以下文章

ruby 将CPU添加到Vagrant VM

磁盘调配策略--vsphere

转:vSphere 配置用于安装 Oracle RAC

如何在VMWare vCenter环境下实现共享存储

Azure Vm 备份 - 从仅 OS 磁盘更改为所有磁盘

使用 Perl SDK 将额外数据存储上的 VMFS5 磁盘添加到 VM