Skip to content

Commit a5a7fdb

Browse files
committed
q-dev: add device_identity to device assignment
1 parent bfbe0b9 commit a5a7fdb

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

qubes/device_protocol.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ class DeviceAssignment(Port):
822822
def __init__(
823823
self,
824824
port: Port,
825-
device_id=None,
825+
device_identity=None,
826826
frontend_domain=None,
827827
options=None,
828828
mode: Union[str, AssignmentMode] = "manual",
@@ -834,25 +834,38 @@ def __init__(
834834
else:
835835
self.mode = AssignmentMode(mode)
836836
self.frontend_domain = frontend_domain
837+
if device_identity == 'any':
838+
device_identity = None
839+
self.device_identity = device_identity
837840

838841
def clone(self, **kwargs):
839842
"""
840843
Clone object and substitute attributes with explicitly given.
841844
"""
845+
port = kwargs.get(
846+
"port", Port(self.backend_domain, self.ident, self.devclass))
842847
attr = {
843848
"options": self.options,
844-
"required": self.required,
845-
"attach_automatically": self.attach_automatically,
849+
"mode": self.mode,
850+
"device_identity": self.device_identity,
846851
"frontend_domain": self.frontend_domain,
847852
}
848853
attr.update(kwargs)
849-
return self.__class__(
850-
Port(self.backend_domain, self.ident, self.devclass), **attr)
854+
return self.__class__(port, **attr)
851855

852856
@property
853857
def device(self) -> DeviceInfo:
854858
"""Get DeviceInfo object corresponding to this DeviceAssignment"""
855-
return self.backend_domain.devices[self.devclass][self.ident]
859+
dev = self.backend_domain.devices[self.devclass][self.ident]
860+
if (self.device_identity is not None
861+
and self.device_identity != dev.self_identity):
862+
# raise ProtocolError(
863+
# "Device identity does not match, expected "
864+
# f"'{self.device_identity}' got '{dev.self_identity}'")
865+
# TODO
866+
return UnknownDevice(
867+
self.backend_domain, self.ident, devclass=self.devclass)
868+
return dev
856869

857870
@property
858871
def frontend_domain(self) -> Optional[QubesVM]:
@@ -923,6 +936,7 @@ def serialize(self) -> bytes:
923936
self.pack_property(key, value)
924937
for key, value in (
925938
('mode', self.mode.value),
939+
('device_identity', self.device_identity),
926940
('ident', self.ident),
927941
('devclass', self.devclass)))
928942

qubes/ext/block.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -498,19 +498,19 @@ def pre_attachment_internal(
498498
raise qubes.exc.QubesValueError(
499499
'devtype option can only have '
500500
'\'disk\' or \'cdrom\' value')
501-
elif option == 'identity':
502-
identity = value
503-
if identity not in ('any', device.self_identity):
504-
print("Unrecognized identity, skipping attachment of"
505-
f" {device}", file=sys.stderr)
506-
raise qubes.devices.UnrecognizedDevice(
507-
f"Device presented identity {device.self_identity} "
508-
f"does not match expected {identity}"
509-
)
510501
else:
511502
raise qubes.exc.QubesValueError(
512503
'Unsupported option {}'.format(option))
513504

505+
# identity = value # TODO!
506+
# if identity not in ('any', device.self_identity):
507+
# print("Unrecognized identity, skipping attachment of"
508+
# f" {device}", file=sys.stderr)
509+
# raise qubes.devices.UnrecognizedDevice(
510+
# f"Device presented identity {device.self_identity} "
511+
# f"does not match expected {identity}"
512+
# )
513+
514514
if 'read-only' not in options:
515515
options['read-only'] = 'yes' if device.mode == 'r' else 'no'
516516
if options.get('read-only', 'no') == 'no' and device.mode == 'r':

qubes/vm/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,19 @@ def load_extras(self):
305305
else:
306306
mode = (qubes.device_protocol.
307307
AssignmentMode.AUTO)
308-
308+
if 'identity' in options:
309+
identity = options.get('identity')
310+
del options['identity']
311+
else:
312+
identity = node.get('identity', 'any')
309313
device_assignment = qubes.device_protocol.DeviceAssignment(
310314
qubes.device_protocol.Port(
311315
backend_domain=self.app.domains[
312316
node.get('backend-domain')],
313317
ident=node.get('id'),
314318
devclass=devclass,
315319
),
320+
device_identity=identity,
316321
options=options,
317322
mode=mode,
318323
)
@@ -373,6 +378,8 @@ def __xml__(self):
373378
node.set('backend-domain', assignment.backend_domain.name)
374379
node.set('id', assignment.ident)
375380
node.set('mode', assignment.mode.value)
381+
identity = assignment.device_identity or 'any'
382+
node.set('identity', identity)
376383
for key, val in assignment.options.items():
377384
option_node = lxml.etree.Element('option')
378385
option_node.set('name', key)

0 commit comments

Comments
 (0)