mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-01 08:26:03 +00:00
fix(vrtManager): a disk in an inactive pool keeps the files it may be made from
Only a running disk's definition has its backing chain; for the others the in-use check reads it from the volumes of active pools. With such a disk's overlay in an inactive pool (a stopped VM's, the next start's, a saved state's or a snapshot's), its backing file counted as unused, so deleting another VM with its disks, or the volume itself, could delete it. The check now refuses (InUse) while a disk that is not running and not raw has its file in an inactive pool, and says to start the pool. A snapshot delete that is done reports no left-behind files then, rather than failing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
effe2a33f0
commit
b91daf0525
8 changed files with 413 additions and 338 deletions
|
|
@ -176,6 +176,10 @@ request_finished.connect(_request_finished, dispatch_uid="vrtManager.connection.
|
|||
|
||||
# libvirt's per-host directory of the VMs' UEFI variables (NVRAM)
|
||||
NVRAM_DIR = "/var/lib/libvirt/qemu/nvram"
|
||||
# the files and devices of a definition's disks that may grow from others; a
|
||||
# raw one does not
|
||||
UNREAD_DISKS = etree.XPath("devices/disk[@device='disk'][not(driver/@type='raw')]/source/@file|"
|
||||
"devices/disk[@device='disk'][not(driver/@type='raw')]/source/@dev")
|
||||
|
||||
|
||||
class wvmConnection(object):
|
||||
|
|
@ -1127,13 +1131,16 @@ class wvmConnect(object):
|
|||
snapshots (overlays, memory, the disks they were taken on); and the
|
||||
backing files of volumes.
|
||||
skip_uuid leaves out one domain, own the volumes whose backing files
|
||||
do not count."""
|
||||
used = set()
|
||||
do not count. util.InUse when a disk not running has its file in an
|
||||
inactive pool: the files it is made from cannot be read."""
|
||||
used, unread = set(), set()
|
||||
for dom in self.wvm.listAllDomains():
|
||||
if dom.UUIDString() == skip_uuid:
|
||||
continue
|
||||
for flags in (0, libvirt.VIR_DOMAIN_XML_INACTIVE):
|
||||
used.update(self._source_paths(etree.fromstring(dom.XMLDesc(flags))))
|
||||
live, config = (etree.fromstring(dom.XMLDesc(flags)) for flags in (0, libvirt.VIR_DOMAIN_XML_INACTIVE))
|
||||
used.update(self._source_paths(live), self._source_paths(config))
|
||||
# a running disk's definition has its backing chain; the others' volumes have it
|
||||
unread.update(set(UNREAD_DISKS(config)) - (set(UNREAD_DISKS(live)) if dom.isActive() else set()))
|
||||
# a saved state starts with the disks it was saved with, detached since or not
|
||||
if dom.hasManagedSaveImage(0):
|
||||
try:
|
||||
|
|
@ -1145,16 +1152,24 @@ class wvmConnect(object):
|
|||
raise
|
||||
# the image went meanwhile: the VM may have started from it
|
||||
saved = dom.XMLDesc(0)
|
||||
used.update(self._source_paths(etree.fromstring(saved)))
|
||||
saved = etree.fromstring(saved)
|
||||
used.update(self._source_paths(saved))
|
||||
unread.update(UNREAD_DISKS(saved))
|
||||
for snap in dom.listAllSnapshots(0):
|
||||
tree = etree.fromstring(snap.getXMLDesc(0))
|
||||
used.update(tree.xpath("disks/disk/source/@file|memory/@file"))
|
||||
for saved in tree.xpath("domain|inactiveDomain"):
|
||||
used.update(self._source_paths(saved))
|
||||
unread.update(UNREAD_DISKS(saved))
|
||||
# A stopped domain's XML has no backing chain; the volumes have it.
|
||||
# Any volume backed by a path keeps that path, even an unattached one.
|
||||
for pool in self.wvm.listAllStoragePools():
|
||||
if not pool.isActive():
|
||||
target = util.get_xml_path(pool.XMLDesc(0), "/pool/target/path")
|
||||
if target and any(path.startswith(target.rstrip("/") + "/") for path in unread):
|
||||
raise util.InUse(gettext_noop(
|
||||
"Pool %(pool)s is not active: the files the disks of the VMs in it are made from cannot be "
|
||||
"checked; start the pool first"), pool=pool.name())
|
||||
continue
|
||||
with contextlib.suppress(libvirtError):
|
||||
pool.refresh(0)
|
||||
|
|
|
|||
|
|
@ -2076,8 +2076,9 @@ class wvmInstance(wvmConnect):
|
|||
an overlay on a snapshot point counts; a disk a revert detached is
|
||||
kept on purpose, and a file another VM on this host uses (or one
|
||||
below it) is not reported. A file outside every storage pool is not
|
||||
checked, nor is anything when the pools could not be refreshed: a
|
||||
stale pool still lists the removed files."""
|
||||
checked, nor is anything when the pools could not be refreshed (a
|
||||
stale pool still lists the removed files) or another VM's files cannot
|
||||
be read (paths_in_use)."""
|
||||
disks, own, points = before
|
||||
if not self._refresh_pools_after_snapshot():
|
||||
return []
|
||||
|
|
@ -2097,7 +2098,10 @@ class wvmInstance(wvmConnect):
|
|||
if left:
|
||||
# another VM on this host may use one; their own backing files
|
||||
# do not keep each other, but a file in use keeps the files below it
|
||||
in_use = self.paths_in_use(own=left)
|
||||
try:
|
||||
in_use = self.paths_in_use(own=left)
|
||||
except util.InUse:
|
||||
return []
|
||||
for path in [path for path in left if path in in_use]:
|
||||
while (path := self.backing_path(path)) and path not in in_use:
|
||||
in_use.add(path)
|
||||
|
|
|
|||
|
|
@ -612,6 +612,13 @@ class LeftBehindTestCase(unittest.TestCase):
|
|||
# their own backing files do not keep each other
|
||||
in_use.assert_called_once_with(own=["/pool/a.one", "/pool/vm.one.mem"])
|
||||
|
||||
def test_nothing_is_reported_when_another_vm_s_files_cannot_be_read(self):
|
||||
# a disk in an inactive pool: any file may be one it grows from; the delete itself is done
|
||||
refused = util.InUse("Pool %(pool)s is not active", pool="overlays")
|
||||
with patch.object(wvmInstance, "paths_in_use", side_effect=refused):
|
||||
left = self.run_delete({"/pool/a.one", "/pool/vm.one.mem", "/pool/a.two", "/pool/a.qcow2"})
|
||||
self.assertEqual(left, [])
|
||||
|
||||
def test_a_file_below_one_another_vm_uses_is_not_reported(self):
|
||||
# the other VM is shut off: its definition does not list the backing
|
||||
# file, and the in-use top's own backing reference does not count
|
||||
|
|
|
|||
|
|
@ -126,6 +126,39 @@ class PathsInUseTestCase(unittest.TestCase):
|
|||
conn.wvm.storagePoolLookupByName.return_value = conn.pool
|
||||
self.assertIn("/pool/disk.qcow2", conn.paths_in_use())
|
||||
|
||||
def test_a_disk_not_running_in_an_inactive_pool_is_refused(self):
|
||||
# its overlay's backing file is in the volume's XML, which an inactive pool does not give
|
||||
inactive = MagicMock(**{"isActive.return_value": False, "name.return_value": "overlays",
|
||||
"XMLDesc.return_value": "<pool><target><path>/overlays</path></target></pool>"})
|
||||
overlay = "<disk type='file' device='disk'><source file='/overlays/b.qcow2'/></disk>"
|
||||
raw = "<disk type='file' device='disk'><driver type='raw'/><source file='/overlays/b.qcow2'/></disk>"
|
||||
other = "<disk type='file' device='disk'><source file='/overlays-2/b.qcow2'/></disk>"
|
||||
|
||||
def check(live, config, active, refused, saved=None):
|
||||
dom = domain("u1")
|
||||
dom.XMLDesc.side_effect = lambda flags: f"<domain><devices>{config if flags else live}</devices></domain>"
|
||||
dom.isActive.return_value = active
|
||||
if saved:
|
||||
dom.hasManagedSaveImage.return_value = 1
|
||||
dom.managedSaveGetXMLDesc.return_value = f"<domain><devices>{saved}</devices></domain>"
|
||||
conn, _ = storage(domains=[dom])
|
||||
conn.wvm.listAllStoragePools.return_value = [conn.pool, inactive]
|
||||
if refused:
|
||||
with self.assertRaisesRegex(util.InUse, "Pool overlays is not active"):
|
||||
conn.paths_in_use()
|
||||
else:
|
||||
conn.paths_in_use() # answers
|
||||
|
||||
for case, args in (("stopped", (overlay, overlay, False, True)),
|
||||
("running: the live definition has the chain", (overlay, overlay, True, False)),
|
||||
("the next start's disk", ("", overlay, True, True)),
|
||||
("a raw disk grows from nothing", (raw, raw, False, False)),
|
||||
("another directory", (other, other, False, False))):
|
||||
with self.subTest(case):
|
||||
check(*args)
|
||||
with self.subTest("a saved state's disk"):
|
||||
check("", "", False, True, saved=overlay)
|
||||
|
||||
def test_a_volume_that_cannot_be_read_is_not_taken_as_unused(self):
|
||||
removed, unreadable = volume("/pool/gone.qcow2"), volume("/pool/top.qcow2")
|
||||
error = libvirtError("removed")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue