Skip to content

Commit 2514885

Browse files
committed
q-dev: assignment.device
1 parent c2aac2d commit 2514885

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

qubes/device_protocol.py

+17
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,23 @@ def devices(self) -> List[DeviceInfo]:
11271127
result.append(dev)
11281128
return result
11291129

1130+
@property
1131+
def device(self) -> DeviceInfo:
1132+
"""
1133+
Get single DeviceInfo object or raise an error.
1134+
1135+
If port id is set we have exactly one device
1136+
since we can attach ony one device to one port.
1137+
If assignment is more general we can get 0 or many devices.
1138+
"""
1139+
devices = self.devices
1140+
if len(devices) == 1:
1141+
return devices[0]
1142+
if len(devices) > 1:
1143+
raise ProtocolError("Too many devices matches to assignment")
1144+
if len(devices) == 0:
1145+
raise ProtocolError("Any devices matches to assignment")
1146+
11301147
@property
11311148
def port(self) -> Port:
11321149
"""

qubes/devices.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ async def attach(self, assignment: DeviceAssignment):
209209
raise ValueError(
210210
f'Cannot attach ambiguous {assignment.devclass} device.')
211211

212-
device = assignment.devices[0]
213-
if device in [ass.devices[0] for ass in self.get_attached_devices()]:
212+
device = assignment.device
213+
if device in [ass.device for ass in self.get_attached_devices()]:
214214
raise DeviceAlreadyAttached(
215215
'device {!s} of class {} already attached to {!s}'.format(
216216
device, self._bus, self._vm))
@@ -307,7 +307,7 @@ async def detach(self, port: Port):
307307
"You need to unassign device first.")
308308

309309
# use the local object, only one device can match
310-
port = assignment.devices[0].port
310+
port = assignment.device.port
311311
await self._vm.fire_event_async(
312312
'device-pre-detach:' + self._bus, pre_event=True, port=port)
313313

qubes/ext/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def device_list_change(
7979
for port_id, frontends in to_attach.items():
8080
if len(frontends) > 1:
8181
# unique
82-
device = tuple(frontends.values())[0].devices[0]
82+
device = tuple(frontends.values())[0].device
8383
target_name = confirm_device_attachment(device, frontends)
8484
for front in frontends:
8585
if front.name == target_name:

templates/libvirt/xen.xml

+10-8
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,23 @@
157157
{# start external devices from xvdi #}
158158
{% set counter = {'i': 4} %}
159159
{% for assignment in vm.devices.block.get_assigned_devices(True) %}
160-
{% set device = assignment.devices[0] %}
161-
{% set options = assignment.options %}
162-
{% include 'libvirt/devices/block.xml' %}
160+
{% for device in assignment.devices %}
161+
{% set options = assignment.options %}
162+
{% include 'libvirt/devices/block.xml' %}
163+
{% endfor %}
163164
{% endfor %}
164165
165166
{% if vm.netvm %}
166167
{% include 'libvirt/devices/net.xml' with context %}
167168
{% endif %}
168169
169170
{% for assignment in vm.devices.pci.get_assigned_devices(True) %}
170-
{% set device = assignment.devices[0] %}
171-
{% set options = assignment.options %}
172-
{% set power_mgmt =
173-
vm.app.domains[0].features.get('suspend-s0ix', False) %}
174-
{% include 'libvirt/devices/pci.xml' %}
171+
{% for device in assignment.devices %}
172+
{% set options = assignment.options %}
173+
{% set power_mgmt =
174+
vm.app.domains[0].features.get('suspend-s0ix', False) %}
175+
{% include 'libvirt/devices/pci.xml' %}
176+
{% endfor %}
175177
{% endfor %}
176178
177179
{% if vm.virt_mode == 'hvm' %}

0 commit comments

Comments
 (0)