private void addCdDrive()

in src/com/vmware/vim25/mox/VirtualMachineDeviceManager.java [258:317]


  private void addCdDrive(String isoPath, String hostDevice, boolean startConnected) throws InvalidProperty, RuntimeFault, RemoteException, InterruptedException
  {
    VirtualMachinePowerState powerState = vm.getRuntime().getPowerState();
    if(powerState != VirtualMachinePowerState.poweredOff)
    {
      throw new RuntimeException("VM is not yet powered off for adding a CD drive.");
    }
    
    VirtualCdrom cdrom = new VirtualCdrom();
    cdrom.connectable = new VirtualDeviceConnectInfo();
    cdrom.connectable.allowGuestControl = true;
    cdrom.connectable.startConnected = startConnected;

    if (hostDevice != null) 
    {
      validateCdromHostDevice(hostDevice);
      VirtualCdromAtapiBackingInfo backing = new VirtualCdromAtapiBackingInfo();
      backing.deviceName = hostDevice;
      cdrom.backing = backing;
    }
    else if (isoPath != null) 
    {
      VirtualCdromIsoBackingInfo backing = new VirtualCdromIsoBackingInfo();
      backing.fileName = isoPath;
      cdrom.backing = backing;
    } 
    else 
    {
      // We don't allow adding a CD drive without hooking it up to something. 
      // In an ideal world, you may want an ISO backing without having to specify a valid ISO 
      // at this time. Create a remote passthrough backing and just set it as not connected.
      VirtualCdromRemotePassthroughBackingInfo backing = new VirtualCdromRemotePassthroughBackingInfo();
      backing.exclusive = true;
      backing.deviceName = "";
      cdrom.backing = backing;
    }

    cdrom.key = -1;

    VirtualDeviceConfigSpec cdSpec = new VirtualDeviceConfigSpec();
    cdSpec.operation = VirtualDeviceConfigSpecOperation.add;
    cdSpec.device = cdrom;

    VirtualMachineConfigSpec config = new VirtualMachineConfigSpec();
    config.deviceChange = new VirtualDeviceConfigSpec[] { cdSpec };

    VirtualIDEController controller = getFirstAvailableController(VirtualIDEController.class);

    if (controller != null) 
    {
      config.deviceChange[0].device.controllerKey = controller.key;
    }
    else 
    {
      throw new RuntimeException("No free IDE controller for addtional CD Drive.");
    }
    
    Task task = vm.reconfigVM_Task(config);
    task.waitForTask();
  }