Skip to content

Commit

Permalink
q-dev: assignment.device
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 15, 2024
1 parent c2aac2d commit 2514885
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
17 changes: 17 additions & 0 deletions qubes/device_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,23 @@ def devices(self) -> List[DeviceInfo]:
result.append(dev)
return result

@property
def device(self) -> DeviceInfo:
"""
Get single DeviceInfo object or raise an error.
If port id is set we have exactly one device
since we can attach ony one device to one port.
If assignment is more general we can get 0 or many devices.
"""
devices = self.devices
if len(devices) == 1:
return devices[0]
if len(devices) > 1:
raise ProtocolError("Too many devices matches to assignment")
if len(devices) == 0:
raise ProtocolError("Any devices matches to assignment")

@property
def port(self) -> Port:
"""
Expand Down
6 changes: 3 additions & 3 deletions qubes/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ async def attach(self, assignment: DeviceAssignment):
raise ValueError(
f'Cannot attach ambiguous {assignment.devclass} device.')

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

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

Expand Down
2 changes: 1 addition & 1 deletion qubes/ext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def device_list_change(
for port_id, frontends in to_attach.items():
if len(frontends) > 1:
# unique
device = tuple(frontends.values())[0].devices[0]
device = tuple(frontends.values())[0].device
target_name = confirm_device_attachment(device, frontends)
for front in frontends:
if front.name == target_name:
Expand Down
18 changes: 10 additions & 8 deletions templates/libvirt/xen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,23 @@
{# start external devices from xvdi #}
{% set counter = {'i': 4} %}
{% for assignment in vm.devices.block.get_assigned_devices(True) %}
{% set device = assignment.devices[0] %}
{% set options = assignment.options %}
{% include 'libvirt/devices/block.xml' %}
{% for device in assignment.devices %}
{% set options = assignment.options %}
{% include 'libvirt/devices/block.xml' %}
{% endfor %}
{% endfor %}
{% if vm.netvm %}
{% include 'libvirt/devices/net.xml' with context %}
{% endif %}
{% for assignment in vm.devices.pci.get_assigned_devices(True) %}
{% set device = assignment.devices[0] %}
{% set options = assignment.options %}
{% set power_mgmt =
vm.app.domains[0].features.get('suspend-s0ix', False) %}
{% include 'libvirt/devices/pci.xml' %}
{% for device in assignment.devices %}
{% set options = assignment.options %}
{% set power_mgmt =
vm.app.domains[0].features.get('suspend-s0ix', False) %}
{% include 'libvirt/devices/pci.xml' %}
{% endfor %}
{% endfor %}
{% if vm.virt_mode == 'hvm' %}
Expand Down

0 comments on commit 2514885

Please sign in to comment.