Skip to content

Commit

Permalink
q-dev: add self_identity do device identity
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 15, 2024
1 parent 6b031a0 commit 9053c70
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
9 changes: 6 additions & 3 deletions qubes/api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,8 @@ async def vm_device_available(self, endpoint):
# the list is empty
self.enforce(len(devices) <= 1)
devices = self.fire_event_for_filter(devices, devclass=devclass)
dev_info = {dev.ident: dev.serialize().decode() for dev in devices}
dev_info = {f'{dev.ident}:{dev.self_identity}':
dev.serialize().decode() for dev in devices}
return ''.join('{} {}\n'.format(ident, dev_info[ident])
for ident in sorted(dev_info))

Expand Down Expand Up @@ -1253,7 +1254,8 @@ async def vm_device_list(self, endpoint):
device_assignments, devclass=devclass)

dev_info = {
f'{assignment.backend_domain}+{assignment.ident}':
(f'{assignment.backend_domain}'
f'+{assignment.ident}:{assignment.device_identity}'):
assignment.serialize().decode('ascii', errors="ignore")
for assignment in device_assignments}

Expand Down Expand Up @@ -1288,7 +1290,8 @@ async def vm_device_attached(self, endpoint):
devclass=devclass)

dev_info = {
f'{assignment.backend_domain}+{assignment.ident}':
(f'{assignment.backend_domain}'
f'+{assignment.ident}:{assignment.device_identity}'):
assignment.serialize().decode('ascii', errors="ignore")
for assignment in device_assignments}

Expand Down
16 changes: 12 additions & 4 deletions qubes/device_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ def check_device_properties(

if properties.get('ident', expected.ident) != expected.ident:
raise UnexpectedDeviceProperty(
f"Got device with id: {properties['ident']} "
f"when expected id: {expected.ident}.")
f"Got device from port: {properties['ident']} "
f"when expected port: {expected.ident}.")
properties['ident'] = expected.ident

if properties.get('devclass', expected.devclass) != expected.devclass:
Expand Down Expand Up @@ -966,12 +966,14 @@ def deserialize(
cls,
serialization: bytes,
expected_port: Port,
expected_identity: Optional[str],
) -> 'DeviceAssignment':
"""
Recovers a serialized object, see: :py:meth:`serialize`.
"""
try:
result = cls._deserialize(serialization, expected_port)
result = cls._deserialize(
serialization, expected_port, expected_identity)
except Exception as exc:
raise ProtocolError() from exc
return result
Expand All @@ -981,6 +983,7 @@ def _deserialize(
cls,
untrusted_serialization: bytes,
expected_port: Port,
expected_identity: Optional[str],
) -> 'DeviceAssignment':
"""
Actually deserializes the object.
Expand All @@ -993,4 +996,9 @@ def _deserialize(
del properties['ident']
del properties['devclass']

return cls(expected_port, **properties)
assignment = cls(expected_port, **properties)
if assignment.device.self_identity != expected_identity:
raise UnexpectedDeviceProperty(
f"Got device with identity {assignment.device.self_identity}"
f"when expected devices with identity {expected_identity}.")
return assignment
12 changes: 6 additions & 6 deletions qubes/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ async def assign(self, assignment: DeviceAssignment):
"""
if assignment.devclass != self._bus:
raise ValueError(
f'Trying to attach {assignment.devclass} device '
f'Trying to assign {assignment.devclass} device '
f'when {self._bus} device expected.')

device = assignment.device
if device in self.get_assigned_devices():
raise DeviceAlreadyAssigned(
'{} device {!s} already assigned to {!s}'.format(
self._bus, device, self._vm))
f'{self._bus} device {device!s} '
f'already assigned to {self._vm!s}')

self._set.add(assignment)

Expand Down Expand Up @@ -366,10 +366,10 @@ def get_assigned_devices(
Safe to access before libvirt bootstrap.
"""
for dev in self._set:
if required_only and not dev.required:
for ass in self._set:
if required_only and not ass.required:
continue
yield dev
yield ass

def get_exposed_devices(self) -> Iterable[DeviceInfo]:
"""
Expand Down

0 comments on commit 9053c70

Please sign in to comment.