[virt-tools-list] [PATCH virt-manager] addhardware: Deal with the conflict host device

Giuseppe Scrivano gscrivan at redhat.com
Tue Aug 12 08:25:12 UTC 2014


Lin Ma <lma at suse.com> writes:

> If a host device is in use by an active guest, Doesn't append it to
> list while adding hardware.
>
> If a host device is in use by inactive guests, Then warn user and
> let user make choice while adding hardware.

I don't like much the idea of hiding devices, I would just show a
warning in both cases.


> diff --git a/virtManager/addhardware.py b/virtManager/addhardware.py
> index 02cff57..4eb4eed 100644
> --- a/virtManager/addhardware.py
> +++ b/virtManager/addhardware.py
> @@ -835,6 +835,10 @@ class vmmAddHardware(vmmGObjectUI):
>  
>          devs = self.conn.get_nodedevs(devtype, devcap)
>          for dev in devs:
> +            if virtinst.VirtualHostDevice.is_using_by_active_vm(self.conn.get_backend(), devtype, dev):

this line goes over 80 chars.


> +    @staticmethod
> +    def is_using_by_active_vm(conn, devtype, dev):
> +        vms = conn.fetch_all_guests()
> +        for vm in vms:
> +            _backend = vm.conn.lookupByName(vm.name)
> +            if not _backend.isActive():
> +                continue
> +            for hostdev in vm.get_devices("hostdev"):
> +                if devtype == NodeDevice.CAPABILITY_TYPE_USBDEV and \
> +                        hostdev.type == "usb":
> +                    if hostdev.bus == dev.bus and \
> +                            hostdev.device == dev.device and \
> +                            hostdev.vendor == dev.vendor_id and \
> +                            hostdev.product == dev.product_id:
> +                        return True
> +                elif devtype == NodeDevice.CAPABILITY_TYPE_PCI and \
> +                        hostdev.type == "pci":
> +                    if str(int(hostdev.domain, 16)) == dev.domain and \
> +                            str(int(hostdev.bus, 16)) == dev.bus and \
> +                            str(int(hostdev.slot, 16)) == dev.slot and \
> +                            str(int(hostdev.function, 16)) == dev.function:
> +                        return True
> +        return False
> +
> +    @staticmethod
> +    def is_conflict_hostdev(conn, devtype, dev):
> +        ret = []
> +        vms = conn.fetch_all_guests()
> +        for vm in vms:
> +            _backend = vm.conn.lookupByName(vm.name)
> +            if _backend.isActive():
> +                continue
> +            for hostdev in vm.get_devices("hostdev"):
> +                if devtype == NodeDevice.CAPABILITY_TYPE_USBDEV and \
> +                        hostdev.type == "usb":
> +                    if hostdev.bus == dev.bus and \
> +                            hostdev.device == dev.device and \
> +                            hostdev.vendor == dev.vendor_id and \
> +                            hostdev.product == dev.product_id:
> +                        ret.append(vm.name)
> +                elif devtype == NodeDevice.CAPABILITY_TYPE_PCI and \
> +                        hostdev.type == "pci":
> +                    if str(int(hostdev.domain, 16)) == dev.domain and \
> +                            str(int(hostdev.bus, 16)) == dev.bus and \
> +                            str(int(hostdev.slot, 16)) == dev.slot and \
> +                            str(int(hostdev.function, 16)) == dev.function:
> +                        ret.append(vm.name)
> +        return ret
> +

it seems that the only difference between these two methods is a "not".
They can be refactored in a single method that accepts an additional
argument which switches between selecting active or not active guests.

Regards,
Giuseppe




More information about the virt-tools-list mailing list