From dd258888d2dbc3d435dc18e1b5e718cfcfe57b7e Mon Sep 17 00:00:00 2001 From: catborise Date: Sat, 14 Nov 2026 15:23:26 +0300 Subject: [PATCH] refactor(vrtManager): a new VM's copy of a template is made by clone_volume clone_from_template wrote its own copy XML next to wvmStorage.clone_volume, which the volume and VM clones use. clone_volume now takes the volume to copy, of its pool or another, so the template's copy goes through it too; it leaves metadata unallocated in a pool of no files, as the template copy did. A qcow2 template's copy in a file pool is now named .qcow2, like a new empty disk, not .img. --- storages/jobs.py | 2 +- storages/tests/test_volume_clone_job.py | 3 ++- vrtManager/create.py | 34 ++++--------------------- vrtManager/instance.py | 2 +- vrtManager/storage.py | 8 +++--- vrtManager/tests/test_volume_xml.py | 6 ++++- 6 files changed, 19 insertions(+), 36 deletions(-) diff --git a/storages/jobs.py b/storages/jobs.py index 71404c00..7a46d51f 100644 --- a/storages/jobs.py +++ b/storages/jobs.py @@ -80,7 +80,7 @@ class CloneVolume(Kind): _compute, stg = _storage(op.params) ctx.record_intent() ctx.set_phase(gettext_noop("Copying the volume")) - created = stg.clone_volume(op.params["image"], op.params["name"], op.params["format"] or None, + created = stg.clone_volume(stg.get_volume(op.params["image"]), op.params["name"], op.params["format"] or None, op.params["prealloc"]) ctx.record_intent(created=created) return {} diff --git a/storages/tests/test_volume_clone_job.py b/storages/tests/test_volume_clone_job.py index e89329ed..c9919c43 100644 --- a/storages/tests/test_volume_clone_job.py +++ b/storages/tests/test_volume_clone_job.py @@ -48,7 +48,8 @@ class VolumeCloneJobTestCase(TransactionTestCase): op.refresh_from_db() CloneVolume().run(op, Context(op, SimpleNamespace(identity="wvc-test:1:a"))) op.refresh_from_db() - self.stg.clone_volume.assert_called_once_with("base.qcow2", "copy", None, False) + self.stg.get_volume.assert_called_with("base.qcow2") + self.stg.clone_volume.assert_called_once_with(self.stg.get_volume.return_value, "copy", None, False) self.assertEqual(op.intent["created"], "copy.qcow2") self.assertIn(f"key=pool%3A{self.compute.pk}%3Apool-a", self.client.get(url).content.decode()) diff --git a/vrtManager/create.py b/vrtManager/create.py index 15e87be4..bf960eb4 100644 --- a/vrtManager/create.py +++ b/vrtManager/create.py @@ -5,7 +5,7 @@ import string import pycdlib from vrtManager import util from vrtManager.connection import wvmConnect -from vrtManager.storage import FILE_POOL_TYPES, rbd_source_children, wvmStorage +from vrtManager.storage import rbd_source_children, wvmStorage # libvirt's CPU modes a new VM can have; any other value is a CPU model's name (mode custom) CPU_MODES = ("host-model", "host-passthrough", "maximum") @@ -62,35 +62,11 @@ class wvmCreate(wvmConnect): return vol.storagePoolLookupByVolume() def clone_from_template(self, clone, template, storage=None, metadata=False, disk_owner_uid=0, disk_owner_gid=0): + """A copy of the template volume (its path) as the new VM's disk, in the pool storage or the template's; its path.""" vol = self.get_volume_by_path(template) - stg = self.get_storage(storage) if storage else vol.storagePoolLookupByVolume() - - storage_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type") - format = util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type") - if storage_type in FILE_POOL_TYPES: - clone += ".img" - else: - metadata = False - options = "1.1" if format == "qcow2" else "" - xml = f""" - - {clone} - 0 - 0 - - - - {disk_owner_uid} - {disk_owner_gid} - 0644 - - - {options} - - """ - stg.createXMLFrom(xml, vol, metadata) - clone_vol = stg.storageVolLookupByName(clone) - return clone_vol.path() + stg = wvmStorage(self.host, self.login, self.passwd, self.conn, storage or vol.storagePoolLookupByVolume().name()) + name = stg.clone_volume(vol, clone, metadata=metadata, disk_owner_uid=disk_owner_uid, disk_owner_gid=disk_owner_gid) + return stg.get_volume(name).path() def _defineXML(self, xml): self.wvm.defineXML(xml) diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 86a91e8a..1550b1dc 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -2673,7 +2673,7 @@ class wvmInstance(wvmConnect): vol = self.get_volume_by_path(source.get("dev")) pool_name = util.get_xml_path(vol.storagePoolLookupByVolume().XMLDesc(0), "/pool/name") storage = self.get_wvmStorage(pool_name) - name = storage.clone_volume(vol.name(), target_file) + name = storage.clone_volume(vol, target_file) new_path = storage.get_volume(name).path() created.append(new_path) paths[dev] = new_path diff --git a/vrtManager/storage.py b/vrtManager/storage.py index 3ee1b8a5..110995f4 100644 --- a/vrtManager/storage.py +++ b/vrtManager/storage.py @@ -370,7 +370,7 @@ class wvmStorage(wvmConnect): def clone_volume( self, - name, + vol, target_file, vol_fmt=None, metadata=False, @@ -379,9 +379,11 @@ class wvmStorage(wvmConnect): disk_owner_uid=0, disk_owner_gid=0, ): - vol = self.get_volume(name) + """A copy in this pool of vol, a volume of this pool or another; its name.""" if not vol_fmt: - vol_fmt = self.get_volume_format_type(name) + vol_fmt = util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type") + if self.get_type() not in FILE_POOL_TYPES: + metadata = False # only a file has metadata to allocate target_file = self.clone_name(target_file, vol_fmt, file_suffix) diff --git a/vrtManager/tests/test_volume_xml.py b/vrtManager/tests/test_volume_xml.py index 5e48aa0a..98b1f7d9 100644 --- a/vrtManager/tests/test_volume_xml.py +++ b/vrtManager/tests/test_volume_xml.py @@ -57,9 +57,13 @@ class TemplateCopyTests(SimpleTestCase): for pool_type in ("dir", "fs", "netfs"): with self.subTest(pool_type): volume, flags = self.copied(pool_type, "qcow2") - self.assertEqual((volume.findtext("name"), flags), ("vm.img", True)) + self.assertEqual((volume.findtext("name"), flags), ("vm.qcow2", True)) self.assertIsNotNone(volume.find("target/features/lazy_refcounts")) + def test_other_pools_take_no_metadata(self): + volume, flags = self.copied("logical", "raw") + self.assertEqual((volume.findtext("name"), flags), ("vm", False)) + def test_only_a_qcow2_copy_takes_its_options(self): for fmt in ("raw", "qcow"): with self.subTest(fmt):