authoravatarclover caruso<git@paperclover.net>2026-10-23 18:18:08-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-24 19:01:23-07:00
log4ae0f11857a0e606a692e8b3c90bd65ce8c05bc7
tree75deeae691ee1a9e61c56adaffc1dd50418d978a
parentd250b064f061df930c6febd22702fcb9250c9a2b
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Replace a guest-locked CD when changing VM media

Live media changes force the tray, so a guest that locked its drive still receives the new image. Assisted-by: claude-opus-5.5

2 files changed, 42 insertions(+), 1 deletions(-)

tools/dashboard-vms-test.py+41
......@@ -22,6 +22,47 @@ spec.loader.exec_module(host)
2222
2323
2424class VMTests(unittest.TestCase):
25 def test_media_replaces_a_guest_locked_cd_and_persists_until_ejected(self):
26 with tempfile.TemporaryDirectory() as temporary:
27 directory = Path(temporary).resolve()
28 installer = directory / 'installer.iso'
29 installer.write_bytes(b'installer')
30 vm = directory / 'fixture'
31 vm.mkdir()
32 seed = vm / 'seed.iso'
33 seed.write_bytes(b'setup')
34 definition = f'''<domain><os><type arch="x86_64" machine="q35"/></os>
35 <devices><disk type="file" device="cdrom"><driver name="qemu" type="raw"/>
36 <source file="{seed}"/><target dev="sdb" bus="sata"/><readonly/>
37 </disk></devices></domain>'''
38
39 def virsh(action, *args):
40 nonlocal definition
41 if action == 'dumpxml':
42 return definition
43 if action == 'domstate':
44 return 'running'
45 self.assertEqual(action, 'update-device')
46 self.assertIn('--live', args)
47 self.assertIn('--config', args)
48 if '--force' not in args:
49 raise vms.subprocess.CalledProcessError(1, action, stderr='tray locked')
50 cd = vms.ET.parse(args[1]).getroot()
51 self.assertIsNotNone(cd.find('readonly'))
52 definition = '<domain><devices>' + vms.ET.tostring(cd, encoding='unicode') + '</devices></domain>'
53 return ''
54
55 with patch.object(vms, 'DISKS', directory), patch.object(vms, 'resolve_image', return_value=installer), \
56 patch.object(vms, 'virsh', side_effect=virsh):
57 vms.change_media({'name': 'fixture', 'image': 'installer.iso'})
58 attached = Path(vms.ET.fromstring(definition).find('./devices/disk/source').get('file'))
59 self.assertEqual(attached.read_bytes(), installer.read_bytes())
60 self.assertFalse(seed.exists())
61 vms.change_media({'name': 'fixture', 'image': ''})
62 self.assertIsNone(vms.ET.fromstring(definition).find('./devices/disk/source'))
63 self.assertFalse(attached.exists())
64 self.assertTrue(installer.exists())
65
2566 def test_secure_preset_retains_efi_state_but_generates_new_login(self):
2667 with tempfile.TemporaryDirectory() as temporary:
2768 root = Path(temporary).resolve()
tools/vms.py+1-1
......@@ -525,7 +525,7 @@ def change_media(spec):
525525 if frozen:
526526 frozen.unlink(missing_ok=True)
527527 raise ValueError("Shut down this VM before adding its first CD drive.")
528 flags.append("--live")
528 flags.extend(["--live", "--force"])
529529 xml = directory / "media.xml"
530530 xml.write_bytes(ET.tostring(cd))
531531 try: