mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(instances): a VM is not migrated to a host without its secrets
A shut-off VM whose disk, host device or vTPM names a secret was moved to a host that does not have that secret: libvirt defines the VM there, the source definition was removed, and the VM then did not start. A migration now names the secrets the destination lacks, by UUID or by usage, and is refused before anything changes, as it is for networks.
This commit is contained in:
parent
b7724928fd
commit
edc165cfc4
7 changed files with 297 additions and 238 deletions
|
|
@ -74,6 +74,7 @@ from vrtManager import util
|
|||
from vrtManager.connection import NVRAM_DIR, wvmConnect
|
||||
from vrtManager.nodedevs import check_hostdevs, pci_address, pci_label, usb_selector, usb_selector_label
|
||||
from vrtManager.storage import wvmStorage, wvmStorages
|
||||
from vrtManager.virtsecrets import REF_USAGE_TYPES
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -179,6 +180,9 @@ class wvmInstances(wvmConnect):
|
|||
if filters:
|
||||
raise util.OperationError(gettext_noop("The destination host does not have these network filters of the VM: %(names)s"),
|
||||
names=", ".join(filters))
|
||||
if secrets := self.unknown_secrets([etree.fromstring(xml) for xml in definitions]):
|
||||
raise util.OperationError(gettext_noop("The destination host does not have these secrets of the VM: %(names)s"),
|
||||
names=", ".join(secrets))
|
||||
copies = self.disk_copies(conn, definitions, pools or {})
|
||||
if copies:
|
||||
mode.append("disks copied: " + ", ".join(f"{copy['target']} to {copy['pool'].name()}" for copy in copies))
|
||||
|
|
@ -466,6 +470,21 @@ class wvmInstances(wvmConnect):
|
|||
with self._nvram_pool() as pool:
|
||||
pool.storageVolLookupByName(name).delete(0)
|
||||
|
||||
def unknown_secrets(self, trees):
|
||||
"""The secrets that the VM definitions in trees name (in their disks,
|
||||
host devices and vTPM) and this host does not have, by UUID or usage,
|
||||
sorted: libvirt defines such a VM, and it then does not start."""
|
||||
refs = {(ref.get("uuid"), REF_USAGE_TYPES.get(ref.get("type")), ref.get("usage"))
|
||||
for tree in trees for ref in tree.xpath("devices//secret")}
|
||||
refs |= {(uuid, None, None) for tree in trees for uuid in tree.xpath("devices/tpm/backend/encryption/@secret")}
|
||||
if not refs: # the host is asked only when the VM names one
|
||||
return []
|
||||
have = self.get_secrets_info()
|
||||
uuids = {secret["uuid"] for secret in have}
|
||||
usages = {(secret["usage_type"], secret["usage"]) for secret in have}
|
||||
return sorted(uuid or usage for uuid, usage_type, usage in refs
|
||||
if (uuid.lower() not in uuids if uuid else (usage_type, usage) not in usages))
|
||||
|
||||
def missing_disks(self, definitions, copied=()):
|
||||
"""(target, source) of the disks in the VM definitions (XML) that are
|
||||
not volumes of this host's active storage pools: the VM's own files,
|
||||
|
|
|
|||
|
|
@ -124,11 +124,13 @@ class FakePool:
|
|||
class FakeHost:
|
||||
"""The destination's libvirt connection. volumes: the paths its pools
|
||||
list; unlisted: files its pools show after a refresh; networks and
|
||||
nwfilters: the names of its networks and network filters."""
|
||||
nwfilters: the names of its networks and network filters; secrets: the
|
||||
UUID, usage type and usage of each of its secrets."""
|
||||
|
||||
def __init__(self, existing=(), volumes=(), unlisted=(), backing=None, inactive_pools=(), pool_types=None,
|
||||
networks=(), nwfilters=()):
|
||||
networks=(), nwfilters=(), secrets=()):
|
||||
self.existing = set(existing)
|
||||
self.secrets = list(secrets)
|
||||
self.networks = list(networks)
|
||||
self.nwfilters = list(nwfilters)
|
||||
self.defined = []
|
||||
|
|
@ -149,6 +151,11 @@ class FakeHost:
|
|||
def listNWFilters(self):
|
||||
return self.nwfilters
|
||||
|
||||
def listAllSecrets(self, flags=0):
|
||||
return [MagicMock(**{"UUIDString.return_value": uuid, "XMLDesc.return_value":
|
||||
f"<secret><usage type='{usage_type}'><name>{usage}</name></usage></secret>"})
|
||||
for uuid, usage_type, usage in self.secrets]
|
||||
|
||||
def storageVolLookupByPath(self, path):
|
||||
if path not in self.volumes:
|
||||
raise no_volume()
|
||||
|
|
|
|||
|
|
@ -83,6 +83,29 @@ class ShutOffMigrationTestCase(unittest.TestCase):
|
|||
destination(host).moveto(FakeSource(5, xml=card), "vm", live=False, unsafe=False, offline=True)
|
||||
self.assertEqual(host.defined, [card])
|
||||
|
||||
def test_a_secret_the_destination_does_not_have_is_refused(self):
|
||||
# libvirt defines the VM there without it: its disk or vTPM would not open
|
||||
key = "8f2c0000-0000-4000-8000-000000000001"
|
||||
tpm = "8f2c0000-0000-4000-8000-000000000003"
|
||||
xml = (f"<domain><name>vm</name><devices><disk type='network'><auth username='libvirt'>"
|
||||
f"<secret type='ceph' uuid='{key.upper()}'/></auth></disk><disk type='network'><auth username='u'>"
|
||||
f"<secret type='iscsi' usage='iqn.2026-11.test:lun'/></auth></disk><tpm model='tpm-crb'>"
|
||||
f"<backend type='emulator'><encryption secret='{tpm}'/></backend></tpm></devices></domain>")
|
||||
for host, why in ((FakeHost(), f"these secrets of the VM: {key.upper()}, {tpm}, iqn.2026-11.test:lun"),
|
||||
# the usage of another type is another secret
|
||||
(FakeHost(secrets=[(key, "ceph", "x"), (tpm, "vtpm", "t"), ("u-9", "ceph", "iqn.2026-11.test:lun")]),
|
||||
"these secrets of the VM: iqn.2026-11.test:lun$")):
|
||||
source = FakeSource(5, xml=xml)
|
||||
with self.subTest(why), self.assertRaisesRegex(util.OperationError, why):
|
||||
destination(host).moveto(source, "vm", live=False, unsafe=False, offline=True)
|
||||
self.assertEqual(host.defined, [])
|
||||
self.assertIsNone(source.instance.undefined)
|
||||
# moved as its definition: without the vTPM, whose state a shut-off VM carries by starting
|
||||
xml = xml.replace(xml[xml.index("<tpm"):xml.index("</devices>")], "")
|
||||
host = FakeHost(secrets=[(key, "ceph", "x"), ("u-9", "iscsi", "iqn.2026-11.test:lun")])
|
||||
destination(host).moveto(FakeSource(5, xml=xml), "vm", live=False, unsafe=False, offline=True)
|
||||
self.assertEqual(host.defined, [xml])
|
||||
|
||||
def test_a_failed_source_undefine_removes_the_new_definition(self):
|
||||
host = FakeHost()
|
||||
source = FakeSource(5, undefine_fails=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue