天天看點

通過pyvmomi給ESXi現有VM添加一個SCSIcontroller

def add_scsi_controller_to_vm(content, vm_name):
    vm = pchelper.get_obj(content, [vim.VirtualMachine], vm_name)
    if not vm:
        print("Can't find VirtualMachine: %s" % vm_name)
        sys.exit(1)

    spec = vim.vm.ConfigSpec()
    dev_changes = []
    deviceConfig = vim.vm.device.VirtualDeviceSpec()

    deviceConfig.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
    deviceConfig.device = vim.vm.device.ParaVirtualSCSIController()
    deviceConfig.device.sharedBus = vim.vm.device.VirtualSCSIController.Sharing.noSharing

    dev_changes.append(deviceConfig)
    spec.deviceChange = dev_changes
    WaitForTask(vm.ReconfigVM_Task(spec=spec))
    print("SCSI controller add to %s" % vm.config.name)
           

之前遇到了無法add SCSI的問題,錯誤原因:

  1. 不能加deviceConfig.fileOperation = "create"
  2. 用錯了SCSIController struce type,應該用:ParaVirtualSCSIController(vim.vm.device.ParaVirtualSCSIController),而不是VirtualSCSIPassthrough(vim.vm.device.VirtualSCSIPassthrough)

通過web手動添加SCSIcontroller的時候可以知道,ESXi的VM隻能添加ParaVirtualSCSIController