Skip to content

Commit

Permalink
Partial fix for PCI devices reordering. See QubesOS/qubes-issues#6587 .
Browse files Browse the repository at this point in the history
  • Loading branch information
Vít Šesták authored and marmarek committed Oct 9, 2023
1 parent c965d85 commit 14d6a1a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
40 changes: 39 additions & 1 deletion qubes/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
`domain-qdb-change:path`) to detect changes and fire
`device-list-change:class` event.
'''
from collections import OrderedDict
from typing import Optional

import qubes.utils

class DeviceNotAttached(qubes.exc.QubesException, KeyError):
Expand Down Expand Up @@ -371,6 +374,41 @@ def assignments(self, persistent=None):
result.update(self._set)
return result

def assignments_list(self, persistent: Optional[bool]=None):
'''List assignments for devices which are (or may be) attached to the
vm.
Devices may be attached persistently (so they are included in
:file:`qubes.xml`) or not. Device can also be in :file:`qubes.xml`,
but be temporarily detached.
:param Optional[bool] persistent: only include devices which are or are not
attached persistently.
'''

try:
devices = self._vm.fire_event('device-list-attached:' + self._bus,
persistent=persistent)
except Exception: # pylint: disable=broad-except
self._vm.log.exception('Failed to list {} devices'.format(
self._bus))
if persistent is True:
# don't break app.save()
return list(self._set)
raise
result = []
if persistent is not False: # None or True
result.extend(self._set)
if not persistent: # None or False
for dev, options in devices:
if dev not in self._set:
result.append(
DeviceAssignment(
backend_domain=dev.backend_domain,
ident=dev.ident, options=options,
bus=self._bus))
return result

def available(self):
'''List devices exposed by this vm'''
devices = self._vm.fire_event('device-list:' + self._bus)
Expand Down Expand Up @@ -433,7 +471,7 @@ class PersistentCollection:
'''

def __init__(self):
self._dict = {}
self._dict = OrderedDict()

def add(self, assignment: DeviceAssignment):
''' Add assignment to collection '''
Expand Down
2 changes: 1 addition & 1 deletion qubes/vm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def __xml__(self):
for devclass in self.devices:
devices = lxml.etree.Element('devices')
devices.set('class', devclass)
for device in self.devices[devclass].assignments(persistent=True):
for device in self.devices[devclass].assignments_list(persistent=True):
node = lxml.etree.Element('device')
node.set('backend-domain', device.backend_domain.name)
node.set('id', device.ident)
Expand Down

0 comments on commit 14d6a1a

Please sign in to comment.