Skip to content

Commit

Permalink
q-dev: block auto-attach
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed May 26, 2024
1 parent 38f7e0b commit b654864
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
28 changes: 23 additions & 5 deletions qubesadmin/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,12 @@ def serialize(self) -> bytes:
properties += b' ' + interfaces_prop

if self.parent_device is not None:
parent_ident = serialize_str(self.parent_device.ident)
parent_prop = (b'parent=' + parent_ident.encode('ascii'))
properties += b' ' + parent_prop
ident = serialize_str(self.parent_device.ident)
ident_prop = (b'parent_ident=' + ident.encode('ascii'))
properties += b' ' + ident_prop
devclass = serialize_str(self.parent_device.devclass)
devclass_prop = (b'parent_devclass=' + devclass.encode('ascii'))
properties += b' ' + devclass_prop

data = b' '.join(
f'_{prop}={serialize_str(value)}'.encode('ascii')
Expand Down Expand Up @@ -607,11 +610,14 @@ def _deserialize(
for i in range(0, len(interfaces), 7)]
properties['interfaces'] = interfaces

if 'parent' in properties:
if 'parent_ident' in properties:
properties['parent'] = Device(
backend_domain=expected_backend_domain,
ident=properties['parent']
ident=properties['parent_ident'],
devclass=properties['parent_devclass'],
)
del properties['parent_ident']
del properties['parent_devclass']

return cls(**properties)

Expand Down Expand Up @@ -705,6 +711,18 @@ def clone(self, **kwargs):
attr.update(kwargs)
return self.__class__(**attr)

@classmethod
def from_device(cls, device: Device, **kwargs) -> 'DeviceAssignment':
"""
Get assignment of the device.
"""
return cls(
backend_domain=device.backend_domain,
ident=device.ident,
devclass=device.devclass,
**kwargs
)

@property
def device(self) -> DeviceInfo:
"""Get DeviceInfo object corresponding to this DeviceAssignment"""
Expand Down
41 changes: 19 additions & 22 deletions qubesadmin/tools/qvm_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ def attach_device(args):
"""
vm = args.domains[0]
device = args.device
device_assignment = qubesadmin.devices.DeviceAssignment(
device.backend_domain, device.ident,
assignment = qubesadmin.devices.DeviceAssignment.from_device(
device,
# backward compatibility
attach_automatically=args.required, required=args.required)
options = dict(opt.split('=', 1) for opt in args.option or [])
if args.ro:
options['read-only'] = 'yes'
device_assignment.options = options
vm.devices[args.devclass].attach(device_assignment)
assignment.options = options
vm.devices[args.devclass].attach(assignment)
# backward compatibility
if args.required:
vm.devices[args.devclass].assign(device_assignment)
vm.devices[args.devclass].assign(assignment)


def detach_device(args):
Expand All @@ -157,9 +157,8 @@ def detach_device(args):
vm = args.domains[0]
if args.device:
device = args.device
device_assignment = qubesadmin.devices.DeviceAssignment(
device.backend_domain, device.ident)
vm.devices[args.devclass].detach(device_assignment)
assignment = qubesadmin.devices.DeviceAssignment.from_device(device)
vm.devices[args.devclass].detach(assignment)
else:
for device_assignment in (
vm.devices[args.devclass].get_attached_devices()):
Expand All @@ -172,16 +171,15 @@ def assign_device(args):
"""
vm = args.domains[0]
device = args.device
device_assignment = qubesadmin.devices.DeviceAssignment(
device.backend_domain, device.ident, devclass=device.devclass,
required=args.required, attach_automatically=True)
assignment = qubesadmin.devices.DeviceAssignment.from_device(
device, required=args.required, attach_automatically=True)
options = dict(opt.split('=', 1) for opt in args.option or [])
if args.ro:
options['read-only'] = 'yes'
options['identity'] = device.self_identity
device_assignment.options = options
vm.devices[args.devclass].assign(device_assignment)
if vm.is_running() and not device_assignment.attached:
assignment.options = options
vm.devices[args.devclass].assign(assignment)
if vm.is_running() and not assignment.attached:
print("Assigned. Now you can manually attach device or restart domain.")


Expand All @@ -192,10 +190,10 @@ def unassign_device(args):
vm = args.domains[0]
if args.device:
device = args.device
device_assignment = qubesadmin.devices.DeviceAssignment(
device.backend_domain, device.ident, frontend_domain=vm)
vm.devices[args.devclass].unassign(device_assignment)
if device_assignment.attached:
assignment = qubesadmin.devices.DeviceAssignment.from_device(
device, frontend_domain=vm)
vm.devices[args.devclass].unassign(assignment)
if assignment.attached:
print("Unassigned. Now you can manually detach device "
"or restart domain.")
else:
Expand All @@ -214,10 +212,9 @@ def info_device(args):
vm = args.domains[0]
if args.device:
device = args.device
device_assignment = qubesadmin.devices.DeviceAssignment(
device.backend_domain, device.ident)
print("description:", device_assignment.device.description)
print("data:", device_assignment.device.data)
assignment = qubesadmin.devices.DeviceAssignment.from_device(device)
print("description:", assignment.device.description)
print("data:", assignment.device.data)
else:
for device_assignment in (
vm.devices[args.devclass].get_dedicated_devices()):
Expand Down

0 comments on commit b654864

Please sign in to comment.