diff --git a/instances/jobs.py b/instances/jobs.py index 9d1343f4..aa8e7a93 100644 --- a/instances/jobs.py +++ b/instances/jobs.py @@ -23,6 +23,7 @@ from vrtManager.connection import NVRAM_DIR, wvmConnect from vrtManager.create import cpu_choices, wvmCreate from vrtManager.instance import HOST_DEVICES, check_snapshot_name, wvmInstance, wvmInstances from vrtManager.nodedevs import check_hostdevs +from vrtManager.storage import FILE_POOL_TYPES from accounts.models import UserInstance, UserSSHKey from appsettings.settings import app_settings @@ -803,9 +804,6 @@ class CloneInstance(Kind): return _log(op, state, f"clone from {source}", vm=op.intent["name"]) -# pools whose volumes are files a CD-ROM can read -SEED_POOL_TYPES = ("dir", "fs", "netfs") - def _seed_pool(conn, data): """The pool of a new VM's cloud-init seed: the one of its disk.""" @@ -861,7 +859,7 @@ class CreateInstance(Kind): if data["add_cdrom"] == "None": raise util.OperationError(_("The cloud-init seed is read from a CD-ROM: choose its bus")) pool = _seed_pool(conn, data) - if util.get_xml_path(pool.XMLDesc(0), "/pool/@type") not in SEED_POOL_TYPES: + if util.get_xml_path(pool.XMLDesc(0), "/pool/@type") not in FILE_POOL_TYPES: raise util.OperationError(_("The cloud-init seed needs a directory storage pool for the disk")) try: utils.nic_macs(data["mac"], data["networks"]) diff --git a/vrtManager/create.py b/vrtManager/create.py index 27af84bf..7d016c7a 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 wvmStorage +from vrtManager.storage import FILE_POOL_TYPES, 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") @@ -88,10 +88,11 @@ class wvmCreate(wvmConnect): 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 == "dir": + if storage_type in FILE_POOL_TYPES: clone += ".img" else: metadata = False + options = "1.1" if format == "qcow2" else "" xml = f""" {clone} @@ -105,10 +106,7 @@ class wvmCreate(wvmConnect): 0644 - 1.1 - - - + {options} """ stg.createXMLFrom(xml, vol, metadata) diff --git a/vrtManager/tests/test_volume_xml.py b/vrtManager/tests/test_volume_xml.py index 2a0ad30d..5e48aa0a 100644 --- a/vrtManager/tests/test_volume_xml.py +++ b/vrtManager/tests/test_volume_xml.py @@ -39,3 +39,31 @@ class NewVolumeTests(SimpleTestCase): self.assertEqual(volume.findtext("name"), "vm.qcow") self.assertIsNone(volume.find("target/compat")) self.assertIsNone(volume.find("target/features")) + + +class TemplateCopyTests(SimpleTestCase): + def copied(self, pool_type, fmt): + """The XML and flags of the copy of a template of format fmt into a pool of pool_type.""" + with patch("vrtManager.connection.connection_manager.get_connection") as get: + template = get.return_value.storageVolLookupByPath.return_value + template.XMLDesc.return_value = f"" + pool = get.return_value.storagePoolLookupByName.return_value + pool.XMLDesc.return_value = f"" + wvmCreate("host", "login", "pw", 1).clone_from_template("vm", "/t/base", "pool1", True) + xml, _template, flags = pool.createXMLFrom.call_args.args + return etree.fromstring(xml), flags + + def test_a_file_pool_takes_a_file_copy(self): + 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.assertIsNotNone(volume.find("target/features/lazy_refcounts")) + + def test_only_a_qcow2_copy_takes_its_options(self): + for fmt in ("raw", "qcow"): + with self.subTest(fmt): + volume, _flags = self.copied("dir", fmt) + self.assertEqual(volume.find("target/format").get("type"), fmt) + self.assertIsNone(volume.find("target/compat")) + self.assertIsNone(volume.find("target/features"))