diff --git a/qubes/ext/block.py b/qubes/ext/block.py index 0bee71f8a..f1924340c 100644 --- a/qubes/ext/block.py +++ b/qubes/ext/block.py @@ -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) @@ -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) @@ -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): diff --git a/templates/libvirt/xen.xml b/templates/libvirt/xen.xml index 155f82e46..a9ce7c1db 100644 --- a/templates/libvirt/xen.xml +++ b/templates/libvirt/xen.xml @@ -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 %}