@@ -512,24 +512,14 @@ def on_domain_init_load(self, vm, event):
512
512
513
513
async def attach_and_notify (self , vm , assignment ):
514
514
# 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 )
533
523
534
524
@qubes .ext .handler ('domain-qdb-change:/qubes-usb-devices' )
535
525
def on_qdb_change (self , vm , event , path ):
@@ -692,9 +682,21 @@ async def on_device_detach_usb(self, vm, event, port):
692
682
@qubes .ext .handler ('domain-start' )
693
683
async def on_domain_start (self , vm , _event , ** _kwargs ):
694
684
# pylint: disable=unused-argument
685
+ to_attach = {}
686
+ assignments = get_assigned_devices (vm .devices ['usb' ])
695
687
# 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 ():
698
700
await self .attach_and_notify (vm , assignment )
699
701
700
702
@qubes .ext .handler ('domain-shutdown' )
0 commit comments