天天看点

通过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