Skip to content

Commit f3b7507

Browse files
committed
q-dev: fix auto-attach
1 parent 5477f8c commit f3b7507

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

qubesusbproxy/core3ext.py

+22-20
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,14 @@ def on_domain_init_load(self, vm, event):
512512

513513
async def attach_and_notify(self, vm, assignment):
514514
# bypass DeviceCollection logic preventing double attach
515-
for device in assignment.devices:
516-
if not assignment.matches(device):
517-
print(
518-
"Unrecognized identity, skipping attachment of device "
519-
f"from the port {assignment}", file=sys.stderr)
520-
continue
521-
if assignment.mode.value == "ask-to-attach":
522-
if vm.name != utils.confirm_device_attachment(
523-
device, {vm: assignment}):
524-
return
525-
526-
try:
527-
await self.on_device_attach_usb(
528-
vm, 'device-pre-attach:usb', device, assignment.options)
529-
await vm.fire_event_async(
530-
'device-attach:usb', device=device, options=assignment.options)
531-
except qubes.devices.DeviceAlreadyAttached:
532-
pass
515+
device = assignment.device
516+
if assignment.mode.value == "ask-to-attach":
517+
if vm.name != utils.confirm_device_attachment(device, {vm: assignment}):
518+
return
519+
await self.on_device_attach_usb(
520+
vm, 'device-pre-attach:usb', device, assignment.options)
521+
await vm.fire_event_async(
522+
'device-attach:usb', device=device, options=assignment.options)
533523

534524
@qubes.ext.handler('domain-qdb-change:/qubes-usb-devices')
535525
def on_qdb_change(self, vm, event, path):
@@ -692,9 +682,21 @@ async def on_device_detach_usb(self, vm, event, port):
692682
@qubes.ext.handler('domain-start')
693683
async def on_domain_start(self, vm, _event, **_kwargs):
694684
# pylint: disable=unused-argument
685+
to_attach = {}
686+
assignments = get_assigned_devices(vm.devices['usb'])
695687
# the most specific assignments first
696-
for assignment in reversed(sorted(
697-
get_assigned_devices(vm.devices['usb']))):
688+
for assignment in reversed(sorted(assignments)):
689+
for device in assignment.devices:
690+
if not assignment.matches(device):
691+
print(
692+
"Unrecognized identity, skipping attachment of device "
693+
f"from the port {assignment}", file=sys.stderr)
694+
continue
695+
# chose first assignment (the most specific) and ignore rest
696+
if device not in to_attach:
697+
# make it unique
698+
to_attach[device] = assignment.clone(device=device)
699+
for assignment in to_attach.values():
698700
await self.attach_and_notify(vm, assignment)
699701

700702
@qubes.ext.handler('domain-shutdown')

0 commit comments

Comments
 (0)