Skip to content

Commit e1e6178

Browse files
committed
qvm_device: copy assign options to suggested attach command
When `qvm-<block,usb,pci> assign` prints the suggested attach command, include the same options (-o key=value) that were passed to assign, so the suggested command attaches with the same configuration. Fixes: QubesOS/qubes-issues#10203
1 parent 386de45 commit e1e6178

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

qubesadmin/tests/tools/qvm_device.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,36 @@ def test_032_assign_ask_and_options(self):
379379
app=self.app)
380380
self.assertIn('Assigned.', buf.getvalue())
381381
self.assertIn('now restart domain', buf.getvalue())
382+
self.assertIn('-o read-only=yes', buf.getvalue())
383+
self.assertAllCalled()
384+
385+
def test_032b_assign_option_in_hint(self):
386+
""" Test that -o options are copied to the suggested attach command """
387+
self.app.domains['test-vm2'].is_running = lambda: True
388+
self.app.expected_calls[(
389+
'test-vm2', 'admin.vm.device.testclass.Assign',
390+
'test-vm1+dev1+dead+beef+babe+u012345',
391+
b"device_id='dead:beef:babe:u012345' port_id='dev1' "
392+
b"devclass='testclass' backend_domain='test-vm1' "
393+
b"mode='auto-attach' frontend_domain='test-vm2' "
394+
b"_frontend-dev='xvdl'"
395+
)] = b'0\0'
396+
self.app.expected_calls[(
397+
'test-vm2', 'admin.vm.device.testclass.Attached', None, None
398+
)] = b'0\0'
399+
self.app.expected_calls[(
400+
'test-vm2', 'admin.vm.property.GetAll', None, None
401+
)] = b'2\0QubesDaemonNoResponseError\0\0err\0'
402+
self.app.expected_calls[(
403+
'test-vm2', 'admin.vm.property.Get', 'devices_denied', None
404+
)] = b'0\0default=False type=str '
405+
with qubesadmin.tests.tools.StdoutBuffer() as buf:
406+
qubesadmin.tools.qvm_device.main(
407+
['testclass', 'assign', '-o', 'frontend-dev=xvdl',
408+
'test-vm2', 'test-vm1:dev1'],
409+
app=self.app)
410+
self.assertIn('Assigned.', buf.getvalue())
411+
self.assertIn('-o frontend-dev=xvdl', buf.getvalue())
382412
self.assertAllCalled()
383413

384414
def test_033_assign_invalid(self):

qubesadmin/tools/qvm_device.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,18 @@ def assign_device(args):
356356
_print_attach_hint(assignment, vm)
357357

358358

359+
def _build_options_str(options):
360+
"""Build CLI option flags string from assignment options dict."""
361+
parts = [f"-o {key}={value}" for key, value in options.items()]
362+
return (" " + " ".join(parts)) if parts else ""
363+
364+
359365
def _print_attach_hint(assignment, vm):
360366
# pylint: disable=missing-function-docstring
361367
attached = vm.devices[assignment.devclass].get_attached_devices()
368+
options_str = _build_options_str(assignment.options)
362369
ports = [
363-
f"\tqvm-{assignment.devclass} attach {vm} "
370+
f"\tqvm-{assignment.devclass} attach{options_str} {vm} "
364371
f"{assignment.backend_domain}:{dev.port_id}"
365372
for dev in assignment.devices
366373
if dev not in attached and not isinstance(dev, UnknownDevice)

0 commit comments

Comments
 (0)