From 7ec56ae6bb786e631a15e56c0e6cacde223872c3 Mon Sep 17 00:00:00 2001 From: Piotr Bartman-Szwarc Date: Mon, 30 Sep 2024 22:59:53 +0200 Subject: [PATCH] q-dev: fixes of devices assignment fronted --- qubesadmin/device_protocol.py | 5 +++-- qubesadmin/tools/qvm_device.py | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/qubesadmin/device_protocol.py b/qubesadmin/device_protocol.py index 0f0d1b9e..018ee528 100644 --- a/qubesadmin/device_protocol.py +++ b/qubesadmin/device_protocol.py @@ -1187,8 +1187,9 @@ def device_id(self) -> str: def devices(self) -> List[DeviceInfo]: """Get DeviceInfo objects corresponding to this DeviceAssignment""" if self.port_id != '*': - # could return UnknownDevice - return [self.backend_domain.devices[self.devclass][self.port_id]] + dv = self.backend_domain.devices[self.devclass][self.port_id] + if isinstance(dv, UnknownDevice) or dv.device_id == self.device_id: + return [dv] result = [] if self.device_id == "0000:0000::?******": return result diff --git a/qubesadmin/tools/qvm_device.py b/qubesadmin/tools/qvm_device.py index 6c3d92e3..cd78c4fd 100644 --- a/qubesadmin/tools/qvm_device.py +++ b/qubesadmin/tools/qvm_device.py @@ -287,8 +287,25 @@ def assign_device(args): print("Warning: The assigned device is on the denied list: " f"{DEVICE_DENY_LIST}\n Auto-attach will work, " f"but make sure that the assignment is correct.") - if (vm.is_running() and not assignment.attached - and assignment.port_id != '*' and not args.quiet): + if vm.is_running() and not args.quiet: + _print_attach_hint(assignment, vm) + + +def _print_attach_hint(assignment, vm): + if assignment.port_id == '*': + return + + try: + plugged = assignment.backend_domain.devices[ + assignment.devclass][assignment.port_id] + except KeyError: + return + + if isinstance(plugged, UnknownDevice): + return + + attached = vm.devices[assignment.devclass].get_attached_devices() + if assignment.matches(plugged) and plugged not in attached: print("Assigned. To attach you can now restart domain or run: \n" f"\tqvm-{assignment.devclass} attach {vm} " f"{assignment.backend_domain}:{assignment.port_id}")