示例#1
0
    def delete(self, vmid, dev_name):
        dom = VMModel.get_vm(vmid, self.conn)
        xmlstr = dom.XMLDesc(0)
        root = objectify.fromstring(xmlstr)

        try:
            hostdev = root.devices.hostdev
        except AttributeError:
            raise NotFoundError('KCHVMHDEV0001E',
                                {'vmid': vmid, 'dev_name': dev_name})

        pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                    for e in hostdev if e.attrib['type'] == 'pci']

        for e in hostdev:
            if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                xmlstr = etree.tostring(e)
                dom.detachDeviceFlags(
                    xmlstr, get_vm_config_flag(dom, mode='all'))
                if e.attrib['type'] == 'pci':
                    self._delete_affected_pci_devices(dom, dev_name, pci_devs)
                break
        else:
            raise NotFoundError('KCHVMHDEV0001E',
                                {'vmid': vmid, 'dev_name': dev_name})
示例#2
0
    def delete(self, vm_name, dev_name):
        try:
            bus_type = self.lookup(vm_name, dev_name)['bus']
            dom = VMModel.get_vm(vm_name, self.conn)
        except NotFoundError:
            raise

        if (bus_type not in HOTPLUG_TYPE and
                DOM_STATE_MAP[dom.info()[0]] != 'shutoff'):
            raise InvalidOperation('KCHVMSTOR0011E')

        try:
            disk = get_device_node(dom, dev_name)
            path = get_vm_disk_info(dom, dev_name)['path']
            if path is None or len(path) < 1:
                path = self.lookup(vm_name, dev_name)['path']
            # This has to be done before it's detached. If it wasn't
            #   in the obj store, its ref count would have been updated
            #   by get_disk_used_by()
            if path is not None:
                used_by = get_disk_used_by(self.objstore, self.conn, path)
            else:
                wok_log.error("Unable to decrement volume used_by on"
                              " delete because no path could be found.")
            dom.detachDeviceFlags(etree.tostring(disk),
                                  get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed("KCHVMSTOR0010E", {'error': e.message})

        if used_by is not None and vm_name in used_by:
            used_by.remove(vm_name)
            set_disk_used_by(self.objstore, path, used_by)
        else:
            wok_log.error("Unable to update %s:%s used_by on delete."
                          % (vm_name, dev_name))
示例#3
0
    def delete(self, vm_name, dev_name):
        try:
            bus_type = self.lookup(vm_name, dev_name)['bus']
            dom = VMModel.get_vm(vm_name, self.conn)
        except NotFoundError:
            raise

        if (bus_type not in HOTPLUG_TYPE and
                DOM_STATE_MAP[dom.info()[0]] != 'shutoff'):
            raise InvalidOperation('KCHVMSTOR0011E')

        try:
            disk = get_device_node(dom, dev_name)
            path = get_vm_disk_info(dom, dev_name)['path']
            if path is None or len(path) < 1:
                path = self.lookup(vm_name, dev_name)['path']
            # This has to be done before it's detached. If it wasn't
            #   in the obj store, its ref count would have been updated
            #   by get_disk_used_by()
            if path is not None:
                used_by = get_disk_used_by(self.objstore, self.conn, path)
            else:
                wok_log.error("Unable to decrement volume used_by on"
                              " delete because no path could be found.")
            dom.detachDeviceFlags(etree.tostring(disk),
                                  get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed("KCHVMSTOR0010E", {'error': e.message})

        if used_by is not None and vm_name in used_by:
            used_by.remove(vm_name)
            set_disk_used_by(self.objstore, path, used_by)
        else:
            wok_log.error("Unable to update %s:%s used_by on delete."
                          % (vm_name, dev_name))
示例#4
0
    def delete(self, vmid, dev_name):
        dom = VMModel.get_vm(vmid, self.conn)
        xmlstr = dom.XMLDesc(0)
        root = objectify.fromstring(xmlstr)

        try:
            hostdev = root.devices.hostdev
        except AttributeError:
            raise NotFoundError('KCHVMHDEV0001E', {
                'vmid': vmid,
                'dev_name': dev_name
            })

        pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                    for e in hostdev if e.attrib['type'] == 'pci']

        for e in hostdev:
            if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                xmlstr = etree.tostring(e)
                dom.detachDeviceFlags(xmlstr,
                                      get_vm_config_flag(dom, mode='all'))
                if e.attrib['type'] == 'pci':
                    self._delete_affected_pci_devices(dom, dev_name, pci_devs)
                break
        else:
            raise NotFoundError('KCHVMHDEV0001E', {
                'vmid': vmid,
                'dev_name': dev_name
            })
示例#5
0
    def delete(self, vmid, dev_name):
        dom = VMModel.get_vm(vmid, self.conn)
        xmlstr = dom.XMLDesc(0)
        root = objectify.fromstring(xmlstr)

        try:
            hostdev = root.devices.hostdev
        except AttributeError:
            raise NotFoundError('KCHVMHDEV0001E',
                                {'vmid': vmid, 'dev_name': dev_name})

        pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                    for e in hostdev if e.attrib['type'] == 'pci']

        dev_model = DeviceModel(conn=self.conn)
        dev_info = dev_model.lookup(dev_name)
        is_3D_device = dev_model.is_device_3D_controller(dev_info)
        if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
            raise InvalidOperation('KCHVMHDEV0006E',
                                   {'name': dev_info['name']})

        for e in hostdev:
            if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                xmlstr = etree.tostring(e)
                dom.detachDeviceFlags(
                    xmlstr, get_vm_config_flag(dom, mode='all'))
                if e.attrib['type'] == 'pci':
                    self._delete_affected_pci_devices(dom, dev_name, pci_devs)
                if is_3D_device:
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)
                break
        else:
            raise NotFoundError('KCHVMHDEV0001E',
                                {'vmid': vmid, 'dev_name': dev_name})
示例#6
0
    def _detach_device(self, cb, params):
        cb('Detaching device')
        self._cb = cb
        vmid = params['vmid']
        dev_name = params['dev_name']
        dom = params['dom']
        hostdev = params['hostdev']
        lock = params['lock']

        with lock:
            pci_devs = {
                DeviceModel.deduce_dev_name(e, self.conn): e
                for e in hostdev
                if e.attrib['type'] == 'pci'
            }

            dev_info = self.dev_model.lookup(dev_name)
            is_3D_device = self.dev_model.is_device_3D_controller(dev_info)
            if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != 'shutoff':
                raise InvalidOperation(
                    'KCHVMHDEV0006E', {'name': dev_info['name']})

            if not pci_devs.get(dev_name):
                raise NotFoundError(
                    'KCHVMHDEV0001E', {'vmid': vmid, 'dev_name': dev_name}
                )

            dev_name_elem = pci_devs[dev_name]
            self._managed = dev_name_elem.get('managed', 'no') == 'yes'

            # check for multifunction and detach all functions together
            try:
                multi = self.unplug_multifunction_pci(
                    dom, hostdev, dev_name_elem)
            except libvirt.libvirtError:
                multi = False

            # successfully detached all functions: finish operation
            if multi:
                if is_3D_device:
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)

                if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
                    cb('OK', True)
                return

            # detach individually
            xmlstr = etree.tostring(dev_name_elem)
            dom.detachDeviceFlags(xmlstr, get_vm_config_flag(dom, mode='all'))
            if dev_name_elem.attrib['type'] == 'pci':
                self._delete_affected_pci_devices(dom, dev_name, pci_devs)
            if is_3D_device:
                devsmodel = VMHostDevsModel(conn=self.conn)
                devsmodel.update_mmio_guest(vmid, False)

        if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
            cb('OK', True)
示例#7
0
    def _detach_device(self, cb, params):
        cb('Detaching device')
        self._cb = cb
        vmid = params['vmid']
        dev_name = params['dev_name']
        dom = params['dom']
        hostdev = params['hostdev']
        lock = params['lock']

        with lock:
            pci_devs = {
                DeviceModel.deduce_dev_name(e, self.conn): e
                for e in hostdev
                if e.attrib['type'] == 'pci'
            }

            dev_info = self.dev_model.lookup(dev_name)
            is_3D_device = self.dev_model.is_device_3D_controller(dev_info)
            if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != 'shutoff':
                raise InvalidOperation(
                    'KCHVMHDEV0006E', {'name': dev_info['name']})

            if not pci_devs.get(dev_name):
                raise NotFoundError(
                    'KCHVMHDEV0001E', {'vmid': vmid, 'dev_name': dev_name}
                )

            dev_name_elem = pci_devs[dev_name]
            self._managed = dev_name_elem.get('managed', 'no') == 'yes'

            # check for multifunction and detach all functions together
            try:
                multi = self.unplug_multifunction_pci(
                    dom, hostdev, dev_name_elem)
            except libvirt.libvirtError:
                multi = False

            # successfully detached all functions: finish operation
            if multi:
                if is_3D_device:
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)

                if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
                    cb('OK', True)
                return

            # detach individually
            xmlstr = etree.tostring(dev_name_elem)
            dom.detachDeviceFlags(xmlstr, get_vm_config_flag(dom, mode='all'))
            if dev_name_elem.attrib['type'] == 'pci':
                self._delete_affected_pci_devices(dom, dev_name, pci_devs)
            if is_3D_device:
                devsmodel = VMHostDevsModel(conn=self.conn)
                devsmodel.update_mmio_guest(vmid, False)

        if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
            cb('OK', True)
示例#8
0
    def _detach_device(self, cb, params):
        cb('Detaching device')
        self._cb = cb
        vmid = params['vmid']
        dev_name = params['dev_name']
        dom = params['dom']
        hostdev = params['hostdev']
        lock = params['lock']

        with lock:
            pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                        for e in hostdev if e.attrib['type'] == 'pci']

            dev_info = self.dev_model.lookup(dev_name)
            is_3D_device = self.dev_model.is_device_3D_controller(dev_info)
            if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
                raise InvalidOperation('KCHVMHDEV0006E',
                                       {'name': dev_info['name']})

            # check for multifunction and detach all functions together
            try:
                multi = self._unplug_multifunction_pci(dom, hostdev, dev_name)
            except libvirt.libvirtError:
                multi = False

            # successfully detached all functions: finish operation
            if multi:
                if is_3D_device:
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)

                if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
                    cb('OK', True)
                return

            # detach each function individually
            for e in hostdev:
                if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                    xmlstr = etree.tostring(e)
                    dom.detachDeviceFlags(
                        xmlstr, get_vm_config_flag(dom, mode='all'))
                    if e.attrib['type'] == 'pci':
                        self._delete_affected_pci_devices(dom, dev_name,
                                                          pci_devs)
                    if is_3D_device:
                        devsmodel = VMHostDevsModel(conn=self.conn)
                        devsmodel.update_mmio_guest(vmid, False)
                    break
            else:
                msg = WokMessage('KCHVMHDEV0001E',
                                 {'vmid': vmid, 'dev_name': dev_name})
                cb(msg.get_text(), False)
                raise NotFoundError('KCHVMHDEV0001E',
                                    {'vmid': vmid, 'dev_name': dev_name})

        if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
            cb('OK', True)
示例#9
0
    def _attach_multifunction_devices(self, dom, pci_infos, driver, vmid):
        slot = 0
        is_multifunction = len(pci_infos) > 1
        device_flags = get_vm_config_flag(dom, mode='all')
        with RollbackContext() as rollback:
            # multifuction: try to attach all functions together within one
            # xml file. It requires libvirt support.
            if is_multifunction:
                # search for the first available slot in guest xml
                slot = self._available_slot(dom)
                xmlstr = self._get_pci_devices_xml(pci_infos, slot, driver)

                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    # If operation fails, we try the other way, where each
                    # function is attached individually
                    pass
                else:
                    rollback.prependDefer(
                        dom.detachDeviceFlags, xmlstr, device_flags
                    )
                    rollback.commitAll()
                    if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
                        self._cb('OK', True)
                    return

            # attach each function individually (multi or single function)
            for pci_info in pci_infos:
                pci_info['detach_driver'] = driver
                xmlstr = self._get_pci_device_xml(
                    pci_info, slot, is_multifunction)
                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    msg = WokMessage(
                        'KCHVMHDEV0007E', {
                            'device': pci_info['name'], 'vm': vmid}
                    )
                    self._cb(msg.get_text(), False)
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        pci_info['name'],
                        vmid,
                        xmlstr,
                    )
                    raise

                rollback.prependDefer(
                    dom.detachDeviceFlags, xmlstr, device_flags)

            rollback.commitAll()
示例#10
0
    def _attach_multifunction_devices(self, dom, pci_infos, driver, vmid):
        slot = 0
        is_multifunction = len(pci_infos) > 1
        device_flags = get_vm_config_flag(dom, mode='all')
        with RollbackContext() as rollback:
            # multifuction: try to attach all functions together within one
            # xml file. It requires libvirt support.
            if is_multifunction:
                # search for the first available slot in guest xml
                slot = self._available_slot(dom)
                xmlstr = self._get_pci_devices_xml(pci_infos, slot, driver)

                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    # If operation fails, we try the other way, where each
                    # function is attached individually
                    pass
                else:
                    rollback.prependDefer(
                        dom.detachDeviceFlags, xmlstr, device_flags
                    )
                    rollback.commitAll()
                    if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
                        self._cb('OK', True)
                    return

            # attach each function individually (multi or single function)
            for pci_info in pci_infos:
                pci_info['detach_driver'] = driver
                xmlstr = self._get_pci_device_xml(
                    pci_info, slot, is_multifunction)
                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    msg = WokMessage(
                        'KCHVMHDEV0007E', {
                            'device': pci_info['name'], 'vm': vmid}
                    )
                    self._cb(msg.get_text(), False)
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        pci_info['name'],
                        vmid,
                        xmlstr,
                    )
                    raise

                rollback.prependDefer(
                    dom.detachDeviceFlags, xmlstr, device_flags)

            rollback.commitAll()
示例#11
0
    def _detach_device(self, cb, params):
        cb('Detaching device')
        self._cb = cb
        vmid = params['vmid']
        dev_name = params['dev_name']
        dom = params['dom']
        hostdev = params['hostdev']
        lock = params['lock']

        with lock:
            pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                        for e in hostdev if e.attrib['type'] == 'pci']

            dev_info = self.dev_model.lookup(dev_name)
            is_3D_device = self.dev_model.is_device_3D_controller(dev_info)
            if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
                raise InvalidOperation('KCHVMHDEV0006E',
                                       {'name': dev_info['name']})

            if self._hotunplug_multifunction_pci(dom, hostdev, dev_name):
                if is_3D_device:
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)

                if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
                    cb('OK', True)
                return

            for e in hostdev:
                if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                    xmlstr = etree.tostring(e)
                    dom.detachDeviceFlags(xmlstr,
                                          get_vm_config_flag(dom, mode='all'))
                    if e.attrib['type'] == 'pci':
                        self._delete_affected_pci_devices(
                            dom, dev_name, pci_devs)
                    if is_3D_device:
                        devsmodel = VMHostDevsModel(conn=self.conn)
                        devsmodel.update_mmio_guest(vmid, False)
                    break
            else:
                msg = WokMessage('KCHVMHDEV0001E', {
                    'vmid': vmid,
                    'dev_name': dev_name
                })
                cb(msg.get_text(), False)
                raise NotFoundError('KCHVMHDEV0001E', {
                    'vmid': vmid,
                    'dev_name': dev_name
                })

        if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
            cb('OK', True)
示例#12
0
    def unplug_multifunction_pci(self, dom, hostdevs, dev_elem):
        if not self.is_hostdev_multifunction(dev_elem):
            return False

        devices = self.get_devices_same_addr(hostdevs, dev_elem)

        if len(devices) <= 1:
            return False

        devices_xml = '<devices>%s</devices>' % ''.join(devices)
        dom.detachDeviceFlags(devices_xml, get_vm_config_flag(dom, mode='all'))

        return True
示例#13
0
    def unplug_multifunction_pci(self, dom, hostdevs, dev_elem):
        if not self.is_hostdev_multifunction(dev_elem):
            return False

        devices = self.get_devices_same_addr(hostdevs, dev_elem)

        if len(devices) <= 1:
            return False

        devices_xml = '<devices>%s</devices>' % ''.join(devices)
        dom.detachDeviceFlags(devices_xml, get_vm_config_flag(dom, mode='all'))

        return True
示例#14
0
    def _hotunplug_multifunction_pci(self, dom, hostdev, dev_name):
        domain, bus, slot, _ = dev_name.split('_')[1:]
        # get all devices attached to the guest in the same domain+bus+slot
        # that the one we are going to detach because they must be detached
        # together
        devices = self._get_devices_same_addr(hostdev, int(domain, 16),
                                              int(bus, 16), int(slot, 16))
        if len(devices) <= 1:
            return False

        devices_xml = '<devices>%s</devices>' % ''.join(devices)
        dom.detachDeviceFlags(devices_xml, get_vm_config_flag(dom, mode='all'))

        return True
示例#15
0
    def _delete_affected_pci_devices(self, dom, dev_name, pci_devs):
        try:
            self.dev_model.lookup(dev_name)
        except NotFoundError:
            return

        affected_names = set(
            DevicesModel(conn=self.conn).get_list(
                _passthrough_affected_by=dev_name))

        for pci_name, e in pci_devs:
            if pci_name in affected_names:
                xmlstr = etree.tostring(e)
                dom.detachDeviceFlags(xmlstr,
                                      get_vm_config_flag(dom, mode='all'))
示例#16
0
    def _delete_affected_pci_devices(self, dom, dev_name, pci_devs):
        try:
            self.dev_model.lookup(dev_name)
        except NotFoundError:
            return

        affected_names = set(
            DevicesModel(
                conn=self.conn).get_list(_passthrough_affected_by=dev_name))

        for pci_name, e in pci_devs.iteritems():
            if pci_name in affected_names:
                xmlstr = etree.tostring(e)
                dom.detachDeviceFlags(
                    xmlstr, get_vm_config_flag(dom, mode='all'))
示例#17
0
    def _hotunplug_multifunction_pci(self, dom, hostdev, dev_name):
        domain, bus, slot, _ = dev_name.split('_')[1:]
        # get all devices attached to the guest in the same domain+bus+slot
        # that the one we are going to detach because they must be detached
        # together
        devices = self._get_devices_same_addr(hostdev,
                                              int(domain, 16),
                                              int(bus, 16),
                                              int(slot, 16))
        if len(devices) <= 1:
            return False

        devices_xml = '<devices>%s</devices>' % ''.join(devices)
        dom.detachDeviceFlags(devices_xml,
                              get_vm_config_flag(dom, mode='all'))

        return True
示例#18
0
    def update(self, vm_name, dev_name, params):
        old_disk_used_by = None
        new_disk_used_by = None

        dom = VMModel.get_vm(vm_name, self.conn)

        dev_info = self.lookup(vm_name, dev_name)
        if dev_info['type'] != 'cdrom':
            raise InvalidOperation("KCHVMSTOR0006E")

        params['path'] = params.get('path', '')
        old_disk_path = dev_info['path']
        new_disk_path = params['path']
        if new_disk_path != old_disk_path:
            # An empty path means a CD-ROM was empty or ejected:
            if old_disk_path is not '':
                old_disk_used_by = get_disk_used_by(
                    self.objstore, self.conn, old_disk_path)
            if new_disk_path is not '':
                new_disk_used_by = get_disk_used_by(
                    self.objstore, self.conn, new_disk_path)

        dev_info.update(params)
        dev, xml = get_disk_xml(dev_info)

        try:
            dom.updateDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed("KCHVMSTOR0009E", {'error': e.message})

        try:
            if old_disk_used_by is not None and \
               vm_name in old_disk_used_by:
                old_disk_used_by.remove(vm_name)
                set_disk_used_by(self.objstore, old_disk_path,
                                 old_disk_used_by)
            if new_disk_used_by is not None:
                new_disk_used_by.append(vm_name)
                set_disk_used_by(self.objstore, new_disk_path,
                                 new_disk_used_by)
        except Exception as e:
            wok_log.error("Unable to update dev used_by on update due to"
                          " %s:" % e.message)
        return dev
示例#19
0
    def _attach_scsi_device(self, cb, params):
        cb('Attaching SCSI device...')
        self._cb = cb
        vmid = params['vmid']
        dev_info = params['dev_info']
        lock = params['lock']

        try:
            self._passthrough_device_validate(dev_info['name'])

        except InvalidParameter as e:
            cb(str(e), False)
            raise

        with lock:
            dom = VMModel.get_vm(vmid, self.conn)

            with RollbackContext() as rollback:
                xmlstr = self._get_scsi_device_xml(dev_info)
                device_flags = get_vm_config_flag(dom, mode='all')
                try:
                    cb('Attaching device to VM')
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    msg = WokMessage(
                        'KCHVMHDEV0007E', {
                            'device': dev_info['name'], 'vm': vmid}
                    )
                    cb(msg.get_text(), False)
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        dev_info['name'],
                        vmid,
                        xmlstr,
                    )
                    raise

                rollback.prependDefer(
                    dom.detachDeviceFlags, xmlstr, device_flags)
                rollback.commitAll()

        if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
            cb('OK', True)
示例#20
0
    def update(self, vm_name, dev_name, params):
        old_disk_used_by = None
        new_disk_used_by = None

        dom = VMModel.get_vm(vm_name, self.conn)

        dev_info = self.lookup(vm_name, dev_name)
        if dev_info['type'] != 'cdrom':
            raise InvalidOperation("KCHVMSTOR0006E")

        params['path'] = params.get('path', '')
        old_disk_path = dev_info['path']
        new_disk_path = params['path']
        if new_disk_path != old_disk_path:
            # An empty path means a CD-ROM was empty or ejected:
            if old_disk_path is not '':
                old_disk_used_by = get_disk_used_by(
                    self.objstore, self.conn, old_disk_path)
            if new_disk_path is not '':
                new_disk_used_by = get_disk_used_by(
                    self.objstore, self.conn, new_disk_path)

        dev_info.update(params)
        dev, xml = get_disk_xml(dev_info)

        try:
            dom.updateDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed("KCHVMSTOR0009E", {'error': e.message})

        try:
            if old_disk_used_by is not None and \
               vm_name in old_disk_used_by:
                old_disk_used_by.remove(vm_name)
                set_disk_used_by(self.objstore, old_disk_path,
                                 old_disk_used_by)
            if new_disk_used_by is not None:
                new_disk_used_by.append(vm_name)
                set_disk_used_by(self.objstore, new_disk_path,
                                 new_disk_used_by)
        except Exception as e:
            wok_log.error("Unable to update dev used_by on update due to"
                          " %s:" % e.message)
        return dev
示例#21
0
    def _attach_scsi_device(self, cb, params):
        cb('Attaching SCSI device...')
        self._cb = cb
        vmid = params['vmid']
        dev_info = params['dev_info']
        lock = params['lock']

        try:
            self._passthrough_device_validate(dev_info['name'])

        except InvalidParameter as e:
            cb(str(e), False)
            raise

        with lock:
            dom = VMModel.get_vm(vmid, self.conn)

            with RollbackContext() as rollback:
                xmlstr = self._get_scsi_device_xml(dev_info)
                device_flags = get_vm_config_flag(dom, mode='all')
                try:
                    cb('Attaching device to VM')
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    msg = WokMessage(
                        'KCHVMHDEV0007E', {
                            'device': dev_info['name'], 'vm': vmid}
                    )
                    cb(msg.get_text(), False)
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        dev_info['name'],
                        vmid,
                        xmlstr,
                    )
                    raise

                rollback.prependDefer(
                    dom.detachDeviceFlags, xmlstr, device_flags)
                rollback.commitAll()

        if DOM_STATE_MAP[dom.info()[0]] == 'shutoff':
            cb('OK', True)
示例#22
0
    def _detach_device(self, cb, params):
        cb('Detaching device.')
        vmid = params['vmid']
        dev_name = params['dev_name']
        dom = params['dom']
        hostdev = params['hostdev']

        pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                    for e in hostdev if e.attrib['type'] == 'pci']

        dev_model = DeviceModel(conn=self.conn)
        dev_info = dev_model.lookup(dev_name)
        is_3D_device = dev_model.is_device_3D_controller(dev_info)
        if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
            raise InvalidOperation('KCHVMHDEV0006E',
                                   {'name': dev_info['name']})

        if self._hotunplug_multifunction_pci(dom, hostdev, dev_name):
            if is_3D_device:
                cb('Updating MMIO from VM...')
                devsmodel = VMHostDevsModel(conn=self.conn)
                devsmodel.update_mmio_guest(vmid, False)
            cb('OK', True)
            return

        for e in hostdev:
            if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                xmlstr = etree.tostring(e)
                cb('Detaching device from VM...')
                dom.detachDeviceFlags(
                    xmlstr, get_vm_config_flag(dom, mode='all'))
                if e.attrib['type'] == 'pci':
                    cb('Deleting affected PCI devices...')
                    self._delete_affected_pci_devices(dom, dev_name, pci_devs)
                if is_3D_device:
                    cb('Updating MMIO from VM...')
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)
                break
        else:
            raise NotFoundError('KCHVMHDEV0001E',
                                {'vmid': vmid, 'dev_name': dev_name})

        cb('OK', True)
示例#23
0
    def update(self, vm_name, dev_name, params):
        old_disk_used_by = None
        new_disk_used_by = None

        dom = VMModel.get_vm(vm_name, self.conn)

        dev_info = self.lookup(vm_name, dev_name)
        if dev_info['type'] != 'cdrom':
            raise InvalidOperation('KCHVMSTOR0006E')

        params['path'] = params.get('path', '')
        old_disk_path = dev_info['path']
        new_disk_path = params['path']
        if new_disk_path != old_disk_path:
            # An empty path means a CD-ROM was empty or ejected:
            if old_disk_path != '':
                old_disk_used_by = get_disk_used_by(self.conn, old_disk_path)
            if new_disk_path != '':
                new_disk_used_by = get_disk_used_by(self.conn, new_disk_path)

        dev_info.update(params)
        dev, xml = get_disk_xml(dev_info)

        try:
            # FIXME: when updating from local file to remote file (http)
            # libvirt adds a new device with same name instead of replacing
            # the existing one
            dom.updateDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed('KCHVMSTOR0009E', {'error': str(e)})

        try:
            if old_disk_used_by is not None and vm_name in old_disk_used_by:
                old_disk_used_by.remove(vm_name)
            if new_disk_used_by is not None:
                new_disk_used_by.append(vm_name)
        except Exception as e:
            wok_log.error(
                'Unable to update dev used_by on update due to' ' %s:' % str(e)
            )
        return dev
示例#24
0
    def _attach_usb_device(self, cb, params):
        cb('Attaching USB device...')
        vmid = params['vmid']
        dev_info = params['dev_info']
        dom = VMModel.get_vm(vmid, self.conn)

        with RollbackContext() as rollback:
            cb('Reading source device XML')
            xmlstr = self._get_usb_device_xml(dev_info)
            device_flags = get_vm_config_flag(dom, mode='all')
            try:
                cb('Attaching device to VM')
                dom.attachDeviceFlags(xmlstr, device_flags)
            except libvirt.libvirtError:
                wok_log.error('Failed to attach host device %s to VM %s: \n%s',
                              dev_info['name'], vmid, xmlstr)
                raise
            rollback.prependDefer(dom.detachDeviceFlags, xmlstr, device_flags)
            rollback.commitAll()

        cb('OK', True)
示例#25
0
    def _attach_usb_device(self, cb, params):
        cb('Attaching USB device...')
        vmid = params['vmid']
        dev_info = params['dev_info']
        dom = VMModel.get_vm(vmid, self.conn)

        with RollbackContext() as rollback:
            cb('Reading source device XML')
            xmlstr = self._get_usb_device_xml(dev_info)
            device_flags = get_vm_config_flag(dom, mode='all')
            try:
                cb('Attaching device to VM')
                dom.attachDeviceFlags(xmlstr, device_flags)
            except libvirt.libvirtError:
                wok_log.error('Failed to attach host device %s to VM %s: \n%s',
                              dev_info['name'], vmid, xmlstr)
                raise
            rollback.prependDefer(dom.detachDeviceFlags, xmlstr, device_flags)
            rollback.commitAll()

        cb('OK', True)
示例#26
0
    def update(self, vm_name, dev_name, params):
        old_disk_used_by = None
        new_disk_used_by = None

        dom = VMModel.get_vm(vm_name, self.conn)

        dev_info = self.lookup(vm_name, dev_name)
        if dev_info['type'] != 'cdrom':
            raise InvalidOperation('KCHVMSTOR0006E')

        params['path'] = params.get('path', '')
        old_disk_path = dev_info['path']
        new_disk_path = params['path']
        if new_disk_path != old_disk_path:
            # An empty path means a CD-ROM was empty or ejected:
            if old_disk_path != '':
                old_disk_used_by = get_disk_used_by(self.conn, old_disk_path)
            if new_disk_path != '':
                new_disk_used_by = get_disk_used_by(self.conn, new_disk_path)

        dev_info.update(params)
        dev, xml = get_disk_xml(dev_info)

        try:
            # FIXME: when updating from local file to remote file (http)
            # libvirt adds a new device with same name instead of replacing
            # the existing one
            dom.updateDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed('KCHVMSTOR0009E', {'error': str(e)})

        try:
            if old_disk_used_by is not None and vm_name in old_disk_used_by:
                old_disk_used_by.remove(vm_name)
            if new_disk_used_by is not None:
                new_disk_used_by.append(vm_name)
        except Exception as e:
            wok_log.error('Unable to update dev used_by on update due to'
                          ' %s:' % str(e))
        return dev
示例#27
0
    def delete(self, vmid, dev_name):
        dom = VMModel.get_vm(vmid, self.conn)
        xmlstr = dom.XMLDesc(0)
        root = objectify.fromstring(xmlstr)

        try:
            hostdev = root.devices.hostdev
        except AttributeError:
            raise NotFoundError('KCHVMHDEV0001E', {
                'vmid': vmid,
                'dev_name': dev_name
            })

        pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                    for e in hostdev if e.attrib['type'] == 'pci']

        dev_model = DeviceModel(conn=self.conn)
        dev_info = dev_model.lookup(dev_name)
        is_3D_device = dev_model.is_device_3D_controller(dev_info)
        if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
            raise InvalidOperation('KCHVMHDEV0006E',
                                   {'name': dev_info['name']})

        for e in hostdev:
            if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                xmlstr = etree.tostring(e)
                dom.detachDeviceFlags(xmlstr,
                                      get_vm_config_flag(dom, mode='all'))
                if e.attrib['type'] == 'pci':
                    self._delete_affected_pci_devices(dom, dev_name, pci_devs)
                if is_3D_device:
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)
                break
        else:
            raise NotFoundError('KCHVMHDEV0001E', {
                'vmid': vmid,
                'dev_name': dev_name
            })
示例#28
0
    def _detach_device(self, cb, params):
        cb('Detaching device.')
        vmid = params['vmid']
        dev_name = params['dev_name']
        dom = params['dom']
        hostdev = params['hostdev']

        pci_devs = [(DeviceModel.deduce_dev_name(e, self.conn), e)
                    for e in hostdev if e.attrib['type'] == 'pci']

        dev_model = DeviceModel(conn=self.conn)
        dev_info = dev_model.lookup(dev_name)
        is_3D_device = dev_model.is_device_3D_controller(dev_info)
        if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
            raise InvalidOperation('KCHVMHDEV0006E',
                                   {'name': dev_info['name']})

        for e in hostdev:
            if DeviceModel.deduce_dev_name(e, self.conn) == dev_name:
                xmlstr = etree.tostring(e)
                cb('Detaching device from VM...')
                dom.detachDeviceFlags(xmlstr,
                                      get_vm_config_flag(dom, mode='all'))
                if e.attrib['type'] == 'pci':
                    cb('Deleting affected PCI devices...')
                    self._delete_affected_pci_devices(dom, dev_name, pci_devs)
                if is_3D_device:
                    cb('Updating MMIO from VM...')
                    devsmodel = VMHostDevsModel(conn=self.conn)
                    devsmodel.update_mmio_guest(vmid, False)
                break
        else:
            raise NotFoundError('KCHVMHDEV0001E', {
                'vmid': vmid,
                'dev_name': dev_name
            })

        cb('OK', True)
示例#29
0
    def _attach_pci_device(self, vmid, dev_info):
        self._validate_pci_passthrough_env()

        dom = VMModel.get_vm(vmid, self.conn)
        # Due to libvirt limitation, we don't support live assigne device to
        # vfio driver.
        driver = ('vfio' if DOM_STATE_MAP[dom.info()[0]] == "shutoff" and
                  self.caps.kernel_vfio else 'kvm')

        # on powerkvm systems it must be vfio driver.
        distro, _, _ = platform.linux_distribution()
        if distro == 'IBM_PowerKVM':
            driver = 'vfio'

        # Attach all PCI devices in the same IOMMU group
        dev_model = DeviceModel(conn=self.conn)
        devs_model = DevicesModel(conn=self.conn)
        affected_names = devs_model.get_list(
            _passthrough_affected_by=dev_info['name'])
        passthrough_names = devs_model.get_list(
            _cap='pci', _passthrough='true')
        group_names = list(set(affected_names) & set(passthrough_names))
        pci_infos = [dev_model.lookup(dev_name) for dev_name in group_names]
        pci_infos.append(dev_info)

        is_multifunction = len(pci_infos) > 1 and \
            DOM_STATE_MAP[dom.info()[0]] == "shutoff"
        pci_infos = sorted(pci_infos, key=itemgetter('name'))

        # all devices in the group that is going to be attached to the vm
        # must be detached from the host first
        with RollbackContext() as rollback:
            for pci_info in pci_infos:
                try:
                    dev = self.conn.get().nodeDeviceLookupByName(
                        pci_info['name'])
                    dev.dettach()
                except Exception:
                    raise OperationFailed('KCHVMHDEV0005E',
                                          {'name': pci_info['name']})
                else:
                    rollback.prependDefer(dev.reAttach)

            rollback.commitAll()

        device_flags = get_vm_config_flag(dom, mode='all')

        slot = 0
        if is_multifunction:
            slot = self._available_slot(dom)
        with RollbackContext() as rollback:
            for pci_info in pci_infos:
                pci_info['detach_driver'] = driver
                xmlstr = self._get_pci_device_xml(pci_info,
                                                  slot,
                                                  is_multifunction)
                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)
                except libvirt.libvirtError:
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        pci_info['name'], vmid, xmlstr)
                    raise
                rollback.prependDefer(dom.detachDeviceFlags,
                                      xmlstr, device_flags)
            rollback.commitAll()

        return dev_info['name']
示例#30
0
    def create(self, vm_name, params):
        # Path will never be blank due to API.json verification.
        # There is no need to cover this case here.
        if not ('vol' in params) ^ ('path' in params):

            if not is_s390x():
                raise InvalidParameter('KCHVMSTOR0017E')

            if 'dir_path' not in params:
                raise InvalidParameter('KCHVMSTOR0019E')

        dom = VMModel.get_vm(vm_name, self.conn)
        params['bus'] = _get_device_bus(params['type'], dom)

        if is_s390x() and params['type'] == 'disk' and 'dir_path' in params:
            if 'format' not in params:
                raise InvalidParameter('KCHVMSTOR0020E')
            size = params['size']
            name = params['name']
            dir_path = params.get('dir_path')
            params['path'] = dir_path + '/' + name
            if os.path.exists(params['path']):
                raise InvalidParameter(
                    'KCHVMSTOR0021E', {'disk_path': params['path']})
            create_disk_image(
                format_type=params['format'], path=params['path'], capacity=size
            )
        else:
            params['format'] = 'raw'

        dev_list = [
            dev for dev, bus in get_vm_disks(dom).items() if bus == params['bus']
        ]
        dev_list.sort()
        if len(dev_list) == 0:
            params['index'] = 0
        else:
            char = dev_list.pop()[2]
            params['index'] = string.ascii_lowercase.index(char) + 1

        if (
            params['bus'] not in HOTPLUG_TYPE and
            DOM_STATE_MAP[dom.info()[0]] != 'shutoff'
        ):
            raise InvalidOperation('KCHVMSTOR0011E')

        if params.get('vol'):
            vol_info = self._get_vol_info(params)

            params['path'] = vol_info['path']
            params['disk'] = vol_info['type']

        params.update(self._get_available_bus_address(params['bus'], vm_name))

        # Add device to VM
        dev, xml = get_disk_xml(params)
        try:
            dom = VMModel.get_vm(vm_name, self.conn)
            dom.attachDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed('KCHVMSTOR0008E', {'error': str(e)})

        # Don't put a try-block here. Let the exception be raised. If we
        #   allow disks used_by to be out of sync, data corruption could
        #   occur if a disk is added to two guests unknowingly.
        if params.get('vol'):
            used_by = vol_info['used_by']
            used_by.append(vm_name)

        return dev
示例#31
0
    def create(self, vm_name, params):
        # Path will never be blank due to API.json verification.
        # There is no need to cover this case here.
        if not ('vol' in params) ^ ('path' in params):

            if not is_s390x():
                raise InvalidParameter('KCHVMSTOR0017E')

            if 'dir_path' not in params:
                raise InvalidParameter('KCHVMSTOR0019E')

        dom = VMModel.get_vm(vm_name, self.conn)
        params['bus'] = _get_device_bus(params['type'], dom)

        if is_s390x() and params['type'] == 'disk' and 'dir_path' in params:
            if 'format' not in params:
                raise InvalidParameter('KCHVMSTOR0020E')
            size = params['size']
            name = params['name']
            dir_path = params.get('dir_path')
            params['path'] = dir_path + '/' + name
            if os.path.exists(params['path']):
                raise InvalidParameter('KCHVMSTOR0021E',
                                       {'disk_path': params['path']})
            create_disk_image(format_type=params['format'],
                              path=params['path'],
                              capacity=size)
        else:
            params['format'] = 'raw'

        dev_list = [
            dev for dev, bus in get_vm_disks(dom).items()
            if bus == params['bus']
        ]
        dev_list.sort()
        if len(dev_list) == 0:
            params['index'] = 0
        else:
            char = dev_list.pop()[2]
            params['index'] = string.ascii_lowercase.index(char) + 1

        if (params['bus'] not in HOTPLUG_TYPE
                and DOM_STATE_MAP[dom.info()[0]] != 'shutoff'):
            raise InvalidOperation('KCHVMSTOR0011E')

        if params.get('vol'):
            vol_info = self._get_vol_info(params)

            params['path'] = vol_info['path']
            params['disk'] = vol_info['type']

        params.update(self._get_available_bus_address(params['bus'], vm_name))

        # Add device to VM
        dev, xml = get_disk_xml(params)
        try:
            dom = VMModel.get_vm(vm_name, self.conn)
            dom.attachDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed('KCHVMSTOR0008E', {'error': str(e)})

        # Don't put a try-block here. Let the exception be raised. If we
        #   allow disks used_by to be out of sync, data corruption could
        #   occur if a disk is added to two guests unknowingly.
        if params.get('vol'):
            used_by = vol_info['used_by']
            used_by.append(vm_name)

        return dev
示例#32
0
    def _attach_pci_device(self, vmid, dev_info):
        self._validate_pci_passthrough_env()

        dom = VMModel.get_vm(vmid, self.conn)
        # Due to libvirt limitation, we don't support live assigne device to
        # vfio driver.
        driver = ('vfio' if DOM_STATE_MAP[dom.info()[0]] == "shutoff"
                  and self.caps.kernel_vfio else 'kvm')

        # on powerkvm systems it must be vfio driver.
        distro, _, _ = platform.linux_distribution()
        if distro == 'IBM_PowerKVM':
            driver = 'vfio'

        # Attach all PCI devices in the same IOMMU group
        dev_model = DeviceModel(conn=self.conn)
        devs_model = DevicesModel(conn=self.conn)
        affected_names = devs_model.get_list(
            _passthrough_affected_by=dev_info['name'])
        passthrough_names = devs_model.get_list(_cap='pci',
                                                _passthrough='true')
        group_names = list(set(affected_names) & set(passthrough_names))
        pci_infos = [dev_model.lookup(dev_name) for dev_name in group_names]
        pci_infos.append(dev_info)

        is_multifunction = len(pci_infos) > 1 and \
                DOM_STATE_MAP[dom.info()[0]] == "shutoff"
        pci_infos = sorted(pci_infos, key=itemgetter('name'))

        # all devices in the group that is going to be attached to the vm
        # must be detached from the host first
        with RollbackContext() as rollback:
            for pci_info in pci_infos:
                try:
                    dev = self.conn.get().nodeDeviceLookupByName(
                        pci_info['name'])
                    dev.dettach()
                except Exception:
                    raise OperationFailed('KCHVMHDEV0005E',
                                          {'name': pci_info['name']})
                else:
                    rollback.prependDefer(dev.reAttach)

            rollback.commitAll()

        device_flags = get_vm_config_flag(dom, mode='all')

        slot = 0
        if is_multifunction:
            slot = self._available_slot(dom)
        with RollbackContext() as rollback:
            for pci_info in pci_infos:
                pci_info['detach_driver'] = driver
                xmlstr = self._get_pci_device_xml(pci_info, slot,
                                                  is_multifunction)
                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)
                except libvirt.libvirtError:
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        pci_info['name'], vmid, xmlstr)
                    raise
                rollback.prependDefer(dom.detachDeviceFlags, xmlstr,
                                      device_flags)
            rollback.commitAll()

        return dev_info['name']
示例#33
0
 def _attach_usb_device(self, vmid, dev_info):
     xmlstr = self._get_usb_device_xml(dev_info)
     dom = VMModel.get_vm(vmid, self.conn)
     dom.attachDeviceFlags(xmlstr, get_vm_config_flag(dom, mode='all'))
     return dev_info['name']
示例#34
0
    def _attach_pci_device(self, cb, params):
        cb('Attaching PCI device')
        self._cb = cb
        vmid = params['vmid']
        dev_info = params['dev_info']
        lock = params['lock']

        try:
            self._passthrough_device_validate(dev_info['name'])

        except InvalidParameter as e:
            cb(e.message, False)
            raise

        with lock:
            try:
                self._validate_pci_passthrough_env()

            except InvalidOperation as e:
                cb(e.message, False)
                raise

            dom = VMModel.get_vm(vmid, self.conn)
            driver = 'vfio' if self.caps.kernel_vfio else 'kvm'

            # 'vfio' systems requires a usb controller in order to support pci
            # hotplug on Power.
            if driver == 'vfio' and platform.machine().startswith('ppc') and \
               DOM_STATE_MAP[dom.info()[0]] != "shutoff" and \
               not self.have_usb_controller(vmid):
                msg = WokMessage('KCHVMHDEV0008E', {'vmid': vmid})
                cb(msg.get_text(), False)
                raise InvalidOperation("KCHVMHDEV0008E", {'vmid': vmid})

            # Attach all PCI devices in the same IOMMU group
            affected_names = self.devs_model.get_list(
                _passthrough_affected_by=dev_info['name'])
            passthrough_names = self.devs_model.get_list(
                _cap='pci', _passthrough='true')
            group_names = list(set(affected_names) & set(passthrough_names))
            pci_infos = [self.dev_model.lookup(dev_name) for dev_name in
                         group_names]
            pci_infos.append(dev_info)

            is_multifunction = len(pci_infos) > 1
            pci_infos = sorted(pci_infos, key=itemgetter('name'))

            # does not allow hot-plug of 3D graphic cards
            is_3D_device = self.dev_model.is_device_3D_controller(dev_info)
            if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
                msg = WokMessage('KCHVMHDEV0006E', {'name': dev_info['name']})
                cb(msg.get_text(), False)
                raise InvalidOperation('KCHVMHDEV0006E',
                                       {'name': dev_info['name']})

            # all devices in the group that is going to be attached to the vm
            # must be detached from the host first
            with RollbackContext() as rollback:
                for pci_info in pci_infos:
                    try:
                        dev = self.conn.get().nodeDeviceLookupByName(
                            pci_info['name'])
                        dev.dettach()
                    except Exception:
                        msg = WokMessage('KCHVMHDEV0005E',
                                         {'name': pci_info['name']})
                        cb(msg.get_text(), False)
                        raise OperationFailed('KCHVMHDEV0005E',
                                              {'name': pci_info['name']})
                    else:
                        rollback.prependDefer(dev.reAttach)

                rollback.commitAll()

            device_flags = get_vm_config_flag(dom, mode='all')

            # when attaching a 3D graphic device it might be necessary to
            # increase the window size memory in order to be able to attach
            # more than one device to the same guest
            if is_3D_device:
                self.update_mmio_guest(vmid, True)

            slot = 0
            if is_multifunction:
                # search for the first available slot in guest xml
                slot = self._available_slot(dom)

            with RollbackContext() as rollback:
                # multifuction: try to attach all functions together within one
                # xml file. It requires libvirt support.
                if is_multifunction:
                    xmlstr = self._get_pci_devices_xml(pci_infos, slot, driver)

                    try:
                        dom.attachDeviceFlags(xmlstr, device_flags)

                    except libvirt.libvirtError:
                        # If operation fails, we try the other way, where each
                        # function is attached individually
                        pass
                    else:
                        rollback.prependDefer(dom.detachDeviceFlags, xmlstr,
                                              device_flags)
                        rollback.commitAll()
                        if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
                            cb('OK', True)
                        return

                # attach each function individually (multi or single function)
                for pci_info in pci_infos:
                    pci_info['detach_driver'] = driver
                    xmlstr = self._get_pci_device_xml(pci_info,
                                                      slot,
                                                      is_multifunction)
                    try:
                        dom.attachDeviceFlags(xmlstr, device_flags)

                    except libvirt.libvirtError:
                        msg = WokMessage('KCHVMHDEV0007E',
                                         {'device': pci_info['name'],
                                          'vm': vmid})
                        cb(msg.get_text(), False)
                        wok_log.error(
                            'Failed to attach host device %s to VM %s: \n%s',
                            pci_info['name'], vmid, xmlstr)
                        raise

                    rollback.prependDefer(dom.detachDeviceFlags,
                                          xmlstr, device_flags)

                rollback.commitAll()

        if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
            cb('OK', True)
示例#35
0
    def _attach_pci_device(self, cb, params):
        cb('Attaching PCI device')
        vmid = params['vmid']
        dev_info = params['dev_info']
        self._validate_pci_passthrough_env()

        dom = VMModel.get_vm(vmid, self.conn)
        # Due to libvirt limitation, we don't support live assigne device to
        # vfio driver.
        driver = ('vfio' if DOM_STATE_MAP[dom.info()[0]] == "shutoff" and
                  self.caps.kernel_vfio else 'kvm')

        # on powerkvm systems it must be vfio driver.
        distro, _, _ = platform.linux_distribution()
        if distro == 'IBM_PowerKVM':
            driver = 'vfio'

        # Attach all PCI devices in the same IOMMU group
        dev_model = DeviceModel(conn=self.conn)
        devs_model = DevicesModel(conn=self.conn)
        affected_names = devs_model.get_list(
            _passthrough_affected_by=dev_info['name'])
        passthrough_names = devs_model.get_list(
            _cap='pci', _passthrough='true')
        group_names = list(set(affected_names) & set(passthrough_names))
        pci_infos = [dev_model.lookup(dev_name) for dev_name in group_names]
        pci_infos.append(dev_info)

        is_multifunction = len(pci_infos) > 1
        pci_infos = sorted(pci_infos, key=itemgetter('name'))

        # does not allow hot-plug of 3D graphic cards
        is_3D_device = dev_model.is_device_3D_controller(dev_info)
        if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
            raise InvalidOperation('KCHVMHDEV0006E',
                                   {'name': dev_info['name']})

        # all devices in the group that is going to be attached to the vm
        # must be detached from the host first
        with RollbackContext() as rollback:
            for pci_info in pci_infos:
                try:
                    dev = self.conn.get().nodeDeviceLookupByName(
                        pci_info['name'])
                    dev.dettach()
                except Exception:
                    raise OperationFailed('KCHVMHDEV0005E',
                                          {'name': pci_info['name']})
                else:
                    rollback.prependDefer(dev.reAttach)

            rollback.commitAll()

        device_flags = get_vm_config_flag(dom, mode='all')

        # when attaching a 3D graphic device it might be necessary to increase
        # the window size memory in order to be able to attach more than one
        # device to the same guest
        if is_3D_device:
            self.update_mmio_guest(vmid, True)

        slot = 0
        if is_multifunction:
            # search for the first available slot in guest xml
            slot = self._available_slot(dom)

        with RollbackContext() as rollback:
            # multifunction hotplug is a special case where all functions
            # must be attached together within one xml file, the same does
            # not happen to multifunction coldplug - where each function is
            # attached individually
            if DOM_STATE_MAP[dom.info()[0]] != 'shutoff' and is_multifunction:
                xmlstr = self._get_pci_devices_xml(pci_infos, slot, driver)

                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    wok_log.error(
                        'Failed to attach mutifunction device VM %s: \n%s',
                        vmid, xmlstr)
                    raise

                rollback.prependDefer(dom.detachDeviceFlags, xmlstr,
                                      device_flags)
                rollback.commitAll()
                cb('OK', True)
                return

            for pci_info in pci_infos:
                pci_info['detach_driver'] = driver
                cb('Reading source device XML')
                xmlstr = self._get_pci_device_xml(pci_info,
                                                  slot,
                                                  is_multifunction)
                try:
                    cb('Attaching device to VM')
                    dom.attachDeviceFlags(xmlstr, device_flags)
                except libvirt.libvirtError:
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        pci_info['name'], vmid, xmlstr)
                    raise
                rollback.prependDefer(dom.detachDeviceFlags,
                                      xmlstr, device_flags)
            rollback.commitAll()

        cb('OK', True)
示例#36
0
    def _attach_pci_device(self, cb, params):
        cb('Attaching PCI device')
        vmid = params['vmid']
        dev_info = params['dev_info']
        self._validate_pci_passthrough_env()

        dom = VMModel.get_vm(vmid, self.conn)
        # Due to libvirt limitation, we don't support live assigne device to
        # vfio driver.
        driver = ('vfio' if DOM_STATE_MAP[dom.info()[0]] == "shutoff"
                  and self.caps.kernel_vfio else 'kvm')

        # on powerkvm systems it must be vfio driver.
        distro, _, _ = platform.linux_distribution()
        if distro == 'IBM_PowerKVM':
            driver = 'vfio'

        # Attach all PCI devices in the same IOMMU group
        dev_model = DeviceModel(conn=self.conn)
        devs_model = DevicesModel(conn=self.conn)
        affected_names = devs_model.get_list(
            _passthrough_affected_by=dev_info['name'])
        passthrough_names = devs_model.get_list(_cap='pci',
                                                _passthrough='true')
        group_names = list(set(affected_names) & set(passthrough_names))
        pci_infos = [dev_model.lookup(dev_name) for dev_name in group_names]
        pci_infos.append(dev_info)

        is_multifunction = len(pci_infos) > 1
        pci_infos = sorted(pci_infos, key=itemgetter('name'))

        # does not allow hot-plug of 3D graphic cards
        is_3D_device = dev_model.is_device_3D_controller(dev_info)
        if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
            raise InvalidOperation('KCHVMHDEV0006E',
                                   {'name': dev_info['name']})

        # all devices in the group that is going to be attached to the vm
        # must be detached from the host first
        with RollbackContext() as rollback:
            for pci_info in pci_infos:
                try:
                    dev = self.conn.get().nodeDeviceLookupByName(
                        pci_info['name'])
                    dev.dettach()
                except Exception:
                    raise OperationFailed('KCHVMHDEV0005E',
                                          {'name': pci_info['name']})
                else:
                    rollback.prependDefer(dev.reAttach)

            rollback.commitAll()

        device_flags = get_vm_config_flag(dom, mode='all')

        # when attaching a 3D graphic device it might be necessary to increase
        # the window size memory in order to be able to attach more than one
        # device to the same guest
        if is_3D_device:
            self.update_mmio_guest(vmid, True)

        slot = 0
        if is_multifunction:
            # search for the first available slot in guest xml
            slot = self._available_slot(dom)

        with RollbackContext() as rollback:
            # multifunction hotplug is a special case where all functions
            # must be attached together within one xml file, the same does
            # not happen to multifunction coldplug - where each function is
            # attached individually
            if DOM_STATE_MAP[dom.info()[0]] != 'shutoff' and is_multifunction:
                xmlstr = self._get_pci_devices_xml(pci_infos, slot, driver)

                try:
                    dom.attachDeviceFlags(xmlstr, device_flags)

                except libvirt.libvirtError:
                    wok_log.error(
                        'Failed to attach mutifunction device VM %s: \n%s',
                        vmid, xmlstr)
                    raise

                rollback.prependDefer(dom.detachDeviceFlags, xmlstr,
                                      device_flags)
                rollback.commitAll()
                cb('OK', True)
                return

            for pci_info in pci_infos:
                pci_info['detach_driver'] = driver
                cb('Reading source device XML')
                xmlstr = self._get_pci_device_xml(pci_info, slot,
                                                  is_multifunction)
                try:
                    cb('Attaching device to VM')
                    dom.attachDeviceFlags(xmlstr, device_flags)
                except libvirt.libvirtError:
                    wok_log.error(
                        'Failed to attach host device %s to VM %s: \n%s',
                        pci_info['name'], vmid, xmlstr)
                    raise
                rollback.prependDefer(dom.detachDeviceFlags, xmlstr,
                                      device_flags)
            rollback.commitAll()

        cb('OK', True)
示例#37
0
 def _attach_usb_device(self, vmid, dev_info):
     xmlstr = self._get_usb_device_xml(dev_info)
     dom = VMModel.get_vm(vmid, self.conn)
     dom.attachDeviceFlags(xmlstr, get_vm_config_flag(dom, mode='all'))
     return dev_info['name']
示例#38
0
    def _attach_pci_device(self, cb, params):
        cb('Attaching PCI device')
        self._cb = cb
        vmid = params['vmid']
        dev_info = params['dev_info']
        lock = params['lock']

        try:
            self._passthrough_device_validate(dev_info['name'])

        except InvalidParameter as e:
            cb(e.message, False)
            raise

        with lock:
            try:
                self._validate_pci_passthrough_env()

            except InvalidOperation as e:
                cb(e.message, False)
                raise

            dom = VMModel.get_vm(vmid, self.conn)
            driver = 'vfio' if self.caps.kernel_vfio else 'kvm'

            # 'vfio' systems requires a usb controller in order to support pci
            # hotplug on Power.
            if driver == 'vfio' and platform.machine().startswith('ppc') and \
               DOM_STATE_MAP[dom.info()[0]] != "shutoff" and \
               not self.have_usb_controller(vmid):
                msg = WokMessage('KCHVMHDEV0008E', {'vmid': vmid})
                cb(msg.get_text(), False)
                raise InvalidOperation("KCHVMHDEV0008E", {'vmid': vmid})

            # Attach all PCI devices in the same IOMMU group
            affected_names = self.devs_model.get_list(
                _passthrough_affected_by=dev_info['name'])
            passthrough_names = self.devs_model.get_list(_cap='pci',
                                                         _passthrough='true')
            group_names = list(set(affected_names) & set(passthrough_names))
            pci_infos = [
                self.dev_model.lookup(dev_name) for dev_name in group_names
            ]
            pci_infos.append(dev_info)

            is_multifunction = len(pci_infos) > 1
            pci_infos = sorted(pci_infos, key=itemgetter('name'))

            # does not allow hot-plug of 3D graphic cards
            is_3D_device = self.dev_model.is_device_3D_controller(dev_info)
            if is_3D_device and DOM_STATE_MAP[dom.info()[0]] != "shutoff":
                msg = WokMessage('KCHVMHDEV0006E', {'name': dev_info['name']})
                cb(msg.get_text(), False)
                raise InvalidOperation('KCHVMHDEV0006E',
                                       {'name': dev_info['name']})

            # all devices in the group that is going to be attached to the vm
            # must be detached from the host first
            with RollbackContext() as rollback:
                for pci_info in pci_infos:
                    try:
                        dev = self.conn.get().nodeDeviceLookupByName(
                            pci_info['name'])
                        dev.dettach()
                    except Exception:
                        msg = WokMessage('KCHVMHDEV0005E',
                                         {'name': pci_info['name']})
                        cb(msg.get_text(), False)
                        raise OperationFailed('KCHVMHDEV0005E',
                                              {'name': pci_info['name']})
                    else:
                        rollback.prependDefer(dev.reAttach)

                rollback.commitAll()

            device_flags = get_vm_config_flag(dom, mode='all')

            # when attaching a 3D graphic device it might be necessary to
            # increase the window size memory in order to be able to attach
            # more than one device to the same guest
            if is_3D_device:
                self.update_mmio_guest(vmid, True)

            slot = 0
            if is_multifunction:
                # search for the first available slot in guest xml
                slot = self._available_slot(dom)

            with RollbackContext() as rollback:
                # multifuction: try to attach all functions together within one
                # xml file. It requires libvirt support.
                if is_multifunction:
                    xmlstr = self._get_pci_devices_xml(pci_infos, slot, driver)

                    try:
                        dom.attachDeviceFlags(xmlstr, device_flags)

                    except libvirt.libvirtError:
                        # If operation fails, we try the other way, where each
                        # function is attached individually
                        pass
                    else:
                        rollback.prependDefer(dom.detachDeviceFlags, xmlstr,
                                              device_flags)
                        rollback.commitAll()
                        if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
                            cb('OK', True)
                        return

                # attach each function individually (multi or single function)
                for pci_info in pci_infos:
                    pci_info['detach_driver'] = driver
                    xmlstr = self._get_pci_device_xml(pci_info, slot,
                                                      is_multifunction)
                    try:
                        dom.attachDeviceFlags(xmlstr, device_flags)

                    except libvirt.libvirtError:
                        msg = WokMessage('KCHVMHDEV0007E', {
                            'device': pci_info['name'],
                            'vm': vmid
                        })
                        cb(msg.get_text(), False)
                        wok_log.error(
                            'Failed to attach host device %s to VM %s: \n%s',
                            pci_info['name'], vmid, xmlstr)
                        raise

                    rollback.prependDefer(dom.detachDeviceFlags, xmlstr,
                                          device_flags)

                rollback.commitAll()

        if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
            cb('OK', True)
示例#39
0
    def create(self, vm_name, params):
        vol_model = None
        # Path will never be blank due to API.json verification.
        # There is no need to cover this case here.
        if not ('vol' in params) ^ ('path' in params):
            raise InvalidParameter("KCHVMSTOR0017E")

        dom = VMModel.get_vm(vm_name, self.conn)
        params['bus'] = _get_device_bus(params['type'], dom)
        params['format'] = 'raw'

        dev_list = [dev for dev, bus in get_vm_disks(dom).iteritems()
                    if bus == params['bus']]
        dev_list.sort()
        if len(dev_list) == 0:
            params['index'] = 0
        else:
            char = dev_list.pop()[2]
            params['index'] = string.ascii_lowercase.index(char) + 1

        if (params['bus'] not in HOTPLUG_TYPE and
           DOM_STATE_MAP[dom.info()[0]] != 'shutoff'):
            raise InvalidOperation('KCHVMSTOR0011E')

        if params.get('vol'):
            try:
                pool = params['pool']
                vol_model = StorageVolumeModel(conn=self.conn,
                                               objstore=self.objstore)
                vol_info = vol_model.lookup(pool, params['vol'])
            except KeyError:
                raise InvalidParameter("KCHVMSTOR0012E")
            except Exception as e:
                raise InvalidParameter("KCHVMSTOR0015E", {'error': e})
            if len(vol_info['used_by']) != 0:
                raise InvalidParameter("KCHVMSTOR0016E")

            valid_format = {
                "disk": ["raw", "bochs", "qcow", "qcow2", "qed", "vmdk"],
                "cdrom": "iso"}

            if vol_info['type'] == 'file':
                if (params['type'] == 'disk' and
                        vol_info['format'] in valid_format[params['type']]):
                    params['format'] = vol_info['format']
                else:
                    raise InvalidParameter("KCHVMSTOR0018E",
                                           {"format": vol_info['format'],
                                            "type": params['type']})

            if (params['format'] == 'raw' and not vol_info['isvalid']):
                message = 'This is not a valid RAW disk image.'
                raise OperationFailed('KCHVMSTOR0008E', {'error': message})

            params['path'] = vol_info['path']
            params['disk'] = vol_info['type']

        params.update(self._get_available_bus_address(params['bus'], vm_name))

        # Add device to VM
        dev, xml = get_disk_xml(params)
        try:
            dom = VMModel.get_vm(vm_name, self.conn)
            dom.attachDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed("KCHVMSTOR0008E", {'error': e.message})

        # Don't put a try-block here. Let the exception be raised. If we
        #   allow disks used_by to be out of sync, data corruption could
        #   occour if a disk is added to two guests unknowingly.
        if params.get('vol'):
            used_by = vol_info['used_by']
            used_by.append(vm_name)
            set_disk_used_by(self.objstore, params['path'], used_by)

        return dev
示例#40
0
    def create(self, vm_name, params):
        vol_model = None
        # Path will never be blank due to API.json verification.
        # There is no need to cover this case here.
        if not ('vol' in params) ^ ('path' in params):
            raise InvalidParameter("KCHVMSTOR0017E")

        dom = VMModel.get_vm(vm_name, self.conn)
        params['bus'] = _get_device_bus(params['type'], dom)
        params['format'] = 'raw'

        dev_list = [dev for dev, bus in get_vm_disks(dom).iteritems()
                    if bus == params['bus']]
        dev_list.sort()
        if len(dev_list) == 0:
            params['index'] = 0
        else:
            char = dev_list.pop()[2]
            params['index'] = string.ascii_lowercase.index(char) + 1

        if (params['bus'] not in HOTPLUG_TYPE and
           DOM_STATE_MAP[dom.info()[0]] != 'shutoff'):
            raise InvalidOperation('KCHVMSTOR0011E')

        if params.get('vol'):
            try:
                pool = params['pool']
                vol_model = StorageVolumeModel(conn=self.conn,
                                               objstore=self.objstore)
                vol_info = vol_model.lookup(pool, params['vol'])
            except KeyError:
                raise InvalidParameter("KCHVMSTOR0012E")
            except Exception as e:
                raise InvalidParameter("KCHVMSTOR0015E", {'error': e})
            if len(vol_info['used_by']) != 0:
                raise InvalidParameter("KCHVMSTOR0016E")

            valid_format = {
                "disk": ["raw", "qcow", "qcow2", "qed", "vmdk", "vpc"],
                "cdrom": "iso"}

            if vol_info['type'] == 'file':
                if (params['type'] == 'disk' and
                        vol_info['format'] in valid_format[params['type']]):
                    params['format'] = vol_info['format']
                else:
                    raise InvalidParameter("KCHVMSTOR0018E",
                                           {"format": vol_info['format'],
                                            "type": params['type']})

            if (params['format'] == 'raw' and not vol_info['isvalid']):
                message = 'This is not a valid RAW disk image.'
                raise OperationFailed('KCHVMSTOR0008E', {'error': message})

            params['path'] = vol_info['path']
            params['disk'] = vol_info['type']

        params.update(self._get_available_bus_address(params['bus'], vm_name))

        # Add device to VM
        dev, xml = get_disk_xml(params)
        try:
            dom = VMModel.get_vm(vm_name, self.conn)
            dom.attachDeviceFlags(xml, get_vm_config_flag(dom, 'all'))
        except Exception as e:
            raise OperationFailed("KCHVMSTOR0008E", {'error': e.message})

        # Don't put a try-block here. Let the exception be raised. If we
        #   allow disks used_by to be out of sync, data corruption could
        #   occour if a disk is added to two guests unknowingly.
        if params.get('vol'):
            used_by = vol_info['used_by']
            used_by.append(vm_name)
            set_disk_used_by(self.objstore, params['path'], used_by)

        return dev