1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(vrtManager): a VM's new disk in an NFS or filesystem pool keeps its format

A new VM's disk and a disk added to a VM were built by a copy of the
volume XML of the Storage page that kept the format in dir pools only:
in an fs or netfs pool a qcow2 disk became a raw one allocated whole,
and a qcow disk got qcow2's compat and lazy_refcounts, which libvirt
refuses. Both now use the Storage page's builder, where every pool of
files keeps the format and the other pools make a raw volume without
metadata preallocation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-08 14:34:51 +03:00
parent 8f09812b7d
commit f952d4e0d0
4 changed files with 58 additions and 43 deletions

View file

@ -1,4 +1,3 @@
import contextlib
import io
import json
import string
@ -6,6 +5,7 @@ import string
import pycdlib
from vrtManager import util
from vrtManager.connection import wvmConnect
from vrtManager.storage import 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")
@ -61,41 +61,10 @@ def get_rbd_storage_data(stg):
class wvmCreate(wvmConnect):
def create_volume(self, storage, name, size, image_format, metadata=False, disk_owner_uid=0, disk_owner_gid=0):
size = int(size) * 1073741824
stg = self.get_storage(storage)
storage_type = util.get_xml_path(stg.XMLDesc(0), "/pool/@type")
if storage_type == "dir":
name += f".{image_format}" if image_format in ("qcow", "qcow2") else ".img"
alloc = 0
else:
image_format = 'raw'
alloc = size
metadata = False
xml = f"""
<volume>
<name>{name}</name>
<capacity>{size}</capacity>
<allocation>{alloc}</allocation>
<target>
<format type='{image_format}'/>
<permissions>
<owner>{disk_owner_uid}</owner>
<group>{disk_owner_gid}</group>
<mode>0644</mode>
<label>virt_image_t</label>
</permissions>
<compat>1.1</compat>
<features>
<lazy_refcounts/>
</features>
</target>
</volume>"""
stg.createXML(xml, metadata)
with contextlib.suppress(Exception):
stg.refresh(0)
vol = stg.storageVolLookupByName(name)
return vol.path()
"""A new volume of size GiB in the pool storage, as the Storage page makes one; its path."""
stg = wvmStorage(self.host, self.login, self.passwd, self.conn, storage)
name = stg.create_volume(name, size, image_format, metadata, disk_owner_uid, disk_owner_gid)
return stg.get_volume(name).path()
def get_volume_format_type(self, path):
vol = self.get_volume_by_path(path)

View file

@ -6,6 +6,9 @@ from lxml import etree
from vrtManager import util
from vrtManager.connection import wvmConnect
# pools whose volumes are files: they take any image format
FILE_POOL_TYPES = ("dir", "fs", "netfs")
def volume_details(vol):
"""A volume's name, capacity (size), allocation and format, from its XML:
@ -305,13 +308,15 @@ class wvmStorage(wvmConnect):
disk_owner_gid=0,
):
size = int(size) * 1073741824
storage_type = self.get_type()
alloc = size
if vol_fmt == "unknown":
vol_fmt = "raw"
if storage_type == "dir":
if self.get_type() in FILE_POOL_TYPES:
name += f".{vol_fmt}" if vol_fmt in ("qcow", "qcow2") else ".img"
alloc = 0
else: # a block device, an LVM volume or an RBD image is raw and allocated whole
vol_fmt = "raw"
alloc = size
metadata = False
if vol_fmt == "unknown":
vol_fmt = "raw"
xml = f"""
<volume>
<name>{util.xml_escape(name)}</name>
@ -338,7 +343,7 @@ class wvmStorage(wvmConnect):
def clone_name(self, target_file, vol_fmt, file_suffix="img"):
"""The name clone_volume gives a copy: a dir pool adds the format's suffix."""
if self.get_type() != "dir":
if self.get_type() not in FILE_POOL_TYPES:
return target_file
if vol_fmt in ("qcow", "qcow2"):
return f"{target_file}.{vol_fmt}"

View file

@ -0,0 +1,41 @@
"""A new volume: a pool of files (dir, fs, netfs) keeps the asked format,
the others make it raw; only qcow2 takes qcow2's options."""
from unittest.mock import patch
from django.test import SimpleTestCase
from lxml import etree
from vrtManager.create import wvmCreate
class NewVolumeTests(SimpleTestCase):
def created(self, pool_type, fmt, metadata=True):
"""The volume XML and flags a new VM disk of format fmt gets in a pool of pool_type."""
with patch("vrtManager.connection.connection_manager.get_connection") as get:
pool = get.return_value.storagePoolLookupByName.return_value
pool.XMLDesc.return_value = f"<pool type='{pool_type}'/>"
wvmCreate("host", "login", "pw", 1).create_volume("pool1", "vm", 1, fmt, metadata)
xml, flags = pool.createXML.call_args.args
return etree.fromstring(xml), flags
def test_a_file_pool_keeps_the_format(self):
for pool_type in ("dir", "fs", "netfs"):
with self.subTest(pool_type):
volume, flags = self.created(pool_type, "qcow2")
self.assertEqual((volume.findtext("name"), volume.find("target/format").get("type")), ("vm.qcow2", "qcow2"))
self.assertEqual((volume.findtext("allocation"), flags), ("0", True))
self.assertIsNotNone(volume.find("target/features/lazy_refcounts"))
def test_other_pools_make_a_whole_raw_volume(self):
for pool_type in ("logical", "rbd", "disk"):
with self.subTest(pool_type):
volume, flags = self.created(pool_type, "qcow2")
self.assertEqual((volume.findtext("name"), volume.find("target/format").get("type")), ("vm", "raw"))
self.assertEqual((volume.findtext("allocation"), flags), (str(1 << 30), False))
self.assertIsNone(volume.find("target/compat"))
def test_only_qcow2_takes_its_options(self):
volume, _flags = self.created("dir", "qcow")
self.assertEqual(volume.findtext("name"), "vm.qcow")
self.assertIsNone(volume.find("target/compat"))
self.assertIsNone(volume.find("target/features"))