Skip to content

Commit

Permalink
q-dev: fix auto-attachment of block devices
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Jun 12, 2024
1 parent af8996f commit 9dec436
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 6 additions & 7 deletions qubes/ext/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ def on_device_pre_attached_block(self, vm, event, device, options):
vm.app.env.get_template('libvirt/devices/block.xml').render(
device=device, vm=vm, options=options))

def pre_attachment_internal(self, vm, device, options):
def pre_attachment_internal(
self, vm, device, options, expected_attachment=None):
if isinstance(device, qubes.device_protocol.UnknownDevice):
print(f'{device.devclass.capitalize()} device {device} '
'not available, skipping.', file=sys.stderr)
Expand Down Expand Up @@ -519,7 +520,7 @@ def pre_attachment_internal(self, vm, device, options):
file=sys.stderr)
return

if device.attachment:
if device.attachment != expected_attachment:
raise qubes.devices.DeviceAlreadyAttached(
'Device {!s} already attached to {!s}'.format(
device, device.attachment)
Expand All @@ -546,11 +547,9 @@ async def on_domain_start(self, vm, _event, **_kwargs):

def notify_auto_attached(self, vm, device, options):
# bypass DeviceCollection logic preventing double attach
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))
# we expected that these devices are already attached to this vm
self.pre_attachment_internal(
vm, device, options, expected_attachment=vm)

@qubes.ext.handler('domain-shutdown')
async def on_domain_shutdown(self, vm, event, **_kwargs):
Expand Down
5 changes: 5 additions & 0 deletions templates/libvirt/xen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
{# start external devices from xvdi #}
{% set counter = {'i': 4} %}
{% for assignment in vm.devices.block.get_assigned_devices(False) %}
{% set device = assignment.device %}
{% set options = assignment.options %}
{% include 'libvirt/devices/block.xml' %}
{% endfor %}
{% if vm.netvm %}
{% include 'libvirt/devices/net.xml' with context %}
Expand Down

0 comments on commit 9dec436

Please sign in to comment.