Skip to content

Commit

Permalink
q-dev: attaching block dev refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Jun 12, 2024
1 parent b89a89c commit 0193c0a
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions qubes/ext/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ def find_unused_frontend(vm, devtype='disk'):
@qubes.ext.handler('device-pre-attach:block')
def on_device_pre_attached_block(self, vm, event, device, options):
# pylint: disable=unused-argument
self.pre_attachment_internal(vm, device, options)

vm.libvirt_domain.attachDevice(
vm.app.env.get_template('libvirt/devices/block.xml').render(
device=device, vm=vm, options=options))

def pre_attachment_internal(self, vm, device, options):
if isinstance(device, qubes.device_protocol.UnknownDevice):
print(f'{device.devclass.capitalize()} device {device} '
'not available, skipping.', file=sys.stderr)
Expand Down Expand Up @@ -530,27 +537,21 @@ def on_device_pre_attached_block(self, vm, event, device, options):
options['frontend-dev'] = self.find_unused_frontend(
vm, options.get('devtype', 'disk'))

print(f'attaching {device} exposed by {device.backend_domain.name}')
vm.libvirt_domain.attachDevice(
vm.app.env.get_template('libvirt/devices/block.xml').render(
device=device, vm=vm, options=options))

@qubes.ext.handler('domain-start')
async def on_domain_start(self, vm, _event, **_kwargs):
# pylint: disable=unused-argument
for assignment in vm.devices['block'].get_assigned_devices():
asyncio.ensure_future(self.attach_and_notify(
vm, assignment.device, assignment.options))
self.notify_auto_attached(
vm, assignment.device, assignment.options)

async def attach_and_notify(self, vm, device, options):
def notify_auto_attached(self, vm, device, options):
pass
# bypass DeviceCollection logic preventing double attach
try:
self.on_device_pre_attached_block(
vm, 'device-pre-attach:block', device, options)
except qubes.devices.UnrecognizedDevice:
return
await vm.fire_event_async(
'device-attach:block', device=device, options=options)
self.pre_attachment_internal(vm, device, options)

vm.libvirt_domain.attachDevice(
vm.app.env.get_template('libvirt/devices/block.xml').render(
device=device, vm=vm, options=options))

@qubes.ext.handler('domain-shutdown')
async def on_domain_shutdown(self, vm, event, **_kwargs):
Expand Down

0 comments on commit 0193c0a

Please sign in to comment.