Beispiel #1
0
def remove_virtualcdrom_with_iso(iso_file_name=None, target_vm=None):
    '''
    remove a VirtualCdrom device with the specified iso media file name
    :param file_name:
    :param target_vm:
    '''
    logger.info("Looking for VirtualCdrom with iso image %s" % (iso_file_name))
    the_device = None
    for device in target_vm.config.hardware.device:
        if isinstance(device, Vim.Vm.Device.VirtualCdrom):
            logger.info(device.deviceInfo.summary)
            if iso_file_name in device.deviceInfo.summary:
                logger.info("Found VirtualCdrom with iso image %s" %
                            (iso_file_name))
                the_device = device
                break
    if the_device is None:
        logger.info("Failed finding VirtualCdrom with iso image %s" %
                    (iso_file_name))
        return
    logger.info("Deleting VirtualCdrom with iso image %s" % (iso_file_name))
    spec = Vim.VirtualDeviceConfigSpec(operation='remove', device=the_device)
    device_change = []
    device_change.append(spec)
    vm_config_spec = Vim.Vm.ConfigSpec(deviceChange=device_change)
    task = target_vm.ReconfigVM_Task(vm_config_spec)
    wait_for_task(task, "RemovingVirtualCdrom")
    logger.info("Successfully deleted VirtualCdrom with iso image %s" %
                (iso_file_name))
    return
Beispiel #2
0
def add_virtualcdrom_with_iso(file_name=None, datastore=None, target_vm=None):
    try:
        vm_name = target_vm.summary.config.name
        logger.info(
            "Adding VirtualCdrom with iso image %s under datastore %s to vm %s"
            % (file_name, datastore, vm_name))
        cdrom = Vim.Vm.Device.VirtualCdrom()
        dev_backing_info = Vim.Vm.Device.VirtualCdrom.IsoBackingInfo()
        dev_backing_info.fileName = '[{}] {}'.format(datastore, file_name)
        cdrom.SetBacking(dev_backing_info)
        connect_info = Vim.Vm.Device.VirtualDevice.ConnectInfo()
        connect_info.SetStartConnected(True)
        connect_info.SetAllowGuestControl(True)
        connect_info.SetConnected(True)
        cdrom.SetConnectable(connect_info)

        ctrls = []
        for device in target_vm.config.hardware.device:
            if isinstance(device, Vim.Vm.Device.VirtualIDEController):
                ctrls.append(device)
                break
        if len(ctrls) == 0:
            logger.warn(
                "Failed finding VirtualIDEController to mount the virtualcdrom. Trying AHCI controller."
            )
            for device in target_vm.config.hardware.device:
                if isinstance(device, Vim.Vm.Device.VirtualAHCIController):
                    ctrls.append(device)
                    break
        if len(ctrls) == 0:
            logger.error(
                "Failed finding any controller device for the virtualcdrom.")
            return
        cdrom.SetControllerKey(ctrls[0].GetKey())
        conf_spec = Vim.VirtualDeviceConfigSpec(operation='add', device=cdrom)
        device_change = []
        device_change.append(conf_spec)
        vm_conf_spec = Vim.Vm.ConfigSpec(deviceChange=device_change)
        task = target_vm.ReconfigVM_Task(vm_conf_spec)
        wait_for_task(task, "AddingVirtualCdrom")
        logger.info(
            "Successfully added VirtualCdrom with iso image %s under datastore %s to vm %s"
            % (file_name, datastore, vm_name))

    except Exception as ex:
        logger.error(ex.message)
        raise
    return