mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 17:06:09 +00:00
feat(storages): the pool page tells which VMs use each volume
A volume left behind (a disk kept when its VM was deleted, an old image) was not told apart from one a VM needs until its deletion was refused. The pool page now has a "Used by" column: the VMs of this host whose disks, UEFI variables, saved state or snapshots are the volume or are built on it, "In use" for one only another volume is built on, else "Not used on this host"; buttons list each kind, and a VM's name finds its volumes. Its deletion still asks the other hosts, as one sharing the storage may use it. When the host's VMs cannot be read the page lists the volumes without the column. The scan that refuses deleting a used volume now also counts a VM's UEFI variables file and a snapshot's block device, and reads each VM once.
This commit is contained in:
parent
dc5b1dbb2a
commit
aeef837afa
12 changed files with 248 additions and 88 deletions
|
|
@ -1220,19 +1220,30 @@ class wvmConnect(object):
|
|||
return sorted(name for name, trees in self.domain_trees(running_only) if any(map(match, trees)))
|
||||
|
||||
def paths_in_use(self, skip_uuid=None, own=()):
|
||||
"""Paths (names for network disks) a domain uses as a disk or a
|
||||
backing file of one, in its saved state too, or as a file of its
|
||||
snapshots (overlays, memory, the disks they were taken on); and the
|
||||
backing files of volumes.
|
||||
"""The paths of path_users."""
|
||||
return set(self.path_users(skip_uuid, own))
|
||||
|
||||
def path_users(self, skip_uuid=None, own=()):
|
||||
"""{path: names of the domains that use it} for the paths (names for
|
||||
network disks) a domain uses as a disk or a backing file of one, as
|
||||
its UEFI variables, in its saved state too, or as a file of its
|
||||
snapshots (overlays, memory, the disks they were taken on); and,
|
||||
with no name, the backing files of volumes.
|
||||
skip_uuid leaves out one domain, own the volumes whose backing files
|
||||
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()
|
||||
users, unread = {}, set()
|
||||
for dom in self.wvm.listAllDomains():
|
||||
if dom.UUIDString() == skip_uuid:
|
||||
continue
|
||||
name = dom.name()
|
||||
|
||||
def use(paths):
|
||||
for path in paths:
|
||||
users.setdefault(path, set()).add(name)
|
||||
|
||||
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))
|
||||
use(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
|
||||
|
|
@ -1247,16 +1258,17 @@ class wvmConnect(object):
|
|||
# the image went meanwhile: the VM may have started from it
|
||||
saved = dom.XMLDesc(0)
|
||||
saved = etree.fromstring(saved)
|
||||
used.update(self._source_paths(saved))
|
||||
use(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"))
|
||||
use(tree.xpath("disks/disk/source/@file|disks/disk/source/@dev|disks/disk/source/@name|memory/@file"))
|
||||
for saved in tree.xpath("domain|inactiveDomain"):
|
||||
used.update(self._source_paths(saved))
|
||||
use(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.
|
||||
built_on = {} # a backing path: the volumes built on it
|
||||
for pool in self.wvm.listAllStoragePools():
|
||||
if not pool.isActive():
|
||||
target = util.get_xml_path(pool.XMLDesc(0), "/pool/target/path")
|
||||
|
|
@ -1271,18 +1283,30 @@ class wvmConnect(object):
|
|||
# only a volume removed meanwhile is skipped: one that cannot
|
||||
# be read may still keep a backing file
|
||||
try:
|
||||
if vol.path() not in own:
|
||||
used.update(etree.fromstring(vol.XMLDesc(0)).xpath("./backingStore/path/text()"))
|
||||
if (top := vol.path()) not in own:
|
||||
for path in etree.fromstring(vol.XMLDesc(0)).xpath("./backingStore/path/text()"):
|
||||
users.setdefault(path, set())
|
||||
built_on.setdefault(path, []).append(top)
|
||||
except libvirtError as err:
|
||||
if err.get_error_code() != libvirt.VIR_ERR_NO_STORAGE_VOL:
|
||||
raise
|
||||
return used
|
||||
# a backing file is used by the domains of the volumes built on it, down the chain
|
||||
changed = True
|
||||
while changed:
|
||||
changed = False
|
||||
for path, tops in built_on.items():
|
||||
names = set().union(*(users.get(top, set()) for top in tops)) - users[path]
|
||||
if names:
|
||||
users[path] |= names
|
||||
changed = True
|
||||
return users
|
||||
|
||||
def _source_paths(self, domain):
|
||||
"""The files, devices or network names of a domain definition's
|
||||
disks and of their backing chains ("//" reaches a running domain's
|
||||
backingStore); a pool volume source as its volume's path."""
|
||||
paths = []
|
||||
backingStore), and of its UEFI variables; a pool volume source as its volume's path."""
|
||||
paths = domain.xpath("os/nvram/text()|os/nvram/source/@file|os/nvram/source/@dev|os/nvram/source/@name|"
|
||||
"os/varstore/@path")
|
||||
for source in domain.xpath("devices/disk//source"):
|
||||
if source.get("pool") and source.get("volume"):
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ def rbd_source_children(pool):
|
|||
|
||||
|
||||
def volume_details(vol):
|
||||
"""A volume's name, capacity (size), allocation and format, from its XML:
|
||||
"""A volume's name, capacity (size), allocation, format and path, from its XML:
|
||||
one libvirt call, which refreshes the volume as info() does. Logical and
|
||||
iSCSI volumes have no format (None)."""
|
||||
def read(doc):
|
||||
|
|
@ -36,6 +36,7 @@ def volume_details(vol):
|
|||
"size": int(util.get_xpath(doc, "/volume/capacity")),
|
||||
"allocation": int(util.get_xpath(doc, "/volume/allocation")),
|
||||
"type": util.get_xpath(doc, "/volume/target/format/@type"),
|
||||
"path": util.get_xpath(doc, "/volume/target/path"),
|
||||
}
|
||||
return util.get_xml_path(vol.XMLDesc(0), func=read)
|
||||
|
||||
|
|
@ -275,6 +276,11 @@ class wvmStorage(wvmConnect):
|
|||
if path in elsewhere([path]):
|
||||
raise util.InUse(gettext_noop("Volume %(name)s is in use on another compute that shares this storage"), name=name)
|
||||
|
||||
def volume_users(self):
|
||||
"""{path: VM names, sorted} of the paths this host uses (path_users): none for
|
||||
a backing file only another volume is built on."""
|
||||
return {path: sorted(names) for path, names in self.path_users().items()}
|
||||
|
||||
def del_volume(self, name):
|
||||
"""libvirt deletes any volume; one a VM of this host still needs is refused."""
|
||||
vol = self.pool.storageVolLookupByName(name)
|
||||
|
|
|
|||
|
|
@ -264,7 +264,8 @@ class StoragesInfoTestCase(unittest.TestCase):
|
|||
|
||||
|
||||
def volume(name, capacity, allocation, fmt=None, error=None):
|
||||
target = f"<target><format type='{fmt}'/></target>" if fmt else "<target/>"
|
||||
fmt_xml = f"<format type='{fmt}'/>" if fmt else ""
|
||||
target = f"<target><path>/p/{name}</path>{fmt_xml}</target>"
|
||||
xml = (f"<volume type='file'><name>{name}</name><capacity unit='bytes'>{capacity}</capacity>"
|
||||
f"<allocation unit='bytes'>{allocation}</allocation>{target}</volume>")
|
||||
return MagicMock(**{"XMLDesc.side_effect": error} if error else {"XMLDesc.return_value": xml})
|
||||
|
|
@ -288,8 +289,8 @@ class VolumesTestCase(unittest.TestCase):
|
|||
vols = [volume("b.qcow2", 2048, 512, "qcow2"), volume("a-lv", 1024, 1024)]
|
||||
stg = self.storage(vols)
|
||||
self.assertEqual(stg.update_volumes(), [
|
||||
{"name": "a-lv", "size": 1024, "allocation": 1024, "type": None}, # a logical volume has no format
|
||||
{"name": "b.qcow2", "size": 2048, "allocation": 512, "type": "qcow2"},
|
||||
{"name": "a-lv", "size": 1024, "allocation": 1024, "type": None, "path": "/p/a-lv"}, # a logical volume has no format
|
||||
{"name": "b.qcow2", "size": 2048, "allocation": 512, "type": "qcow2", "path": "/p/b.qcow2"},
|
||||
])
|
||||
self.assertEqual([c[0] for c in stg.pool.mock_calls], ["refresh", "listAllVolumes"])
|
||||
for vol in vols:
|
||||
|
|
@ -312,7 +313,8 @@ class VolumesTestCase(unittest.TestCase):
|
|||
def test_one_volume(self):
|
||||
stg = self.storage([])
|
||||
stg.pool.storageVolLookupByName.return_value = volume("a.img", 4096, 0, "raw")
|
||||
self.assertEqual(stg.get_volume_details("a.img"), {"name": "a.img", "size": 4096, "allocation": 0, "type": "raw"})
|
||||
self.assertEqual(stg.get_volume_details("a.img"), {"name": "a.img", "size": 4096, "allocation": 0, "type": "raw",
|
||||
"path": "/p/a.img"})
|
||||
|
||||
def test_the_pool_size_is_one_info_call(self):
|
||||
stg = self.storage([])
|
||||
|
|
|
|||
|
|
@ -12,9 +12,10 @@ from vrtManager import util
|
|||
from vrtManager.storage import wvmStorage
|
||||
|
||||
|
||||
def domain(uuid, disks=(), snapshots=()):
|
||||
def domain(uuid, disks=(), snapshots=(), name=None):
|
||||
dom = MagicMock()
|
||||
dom.UUIDString.return_value = uuid
|
||||
dom.name.return_value = name or uuid
|
||||
dom.hasManagedSaveImage.return_value = 0
|
||||
devices = "".join(f"<disk type='file' device='disk'><source file='{d}'/></disk>" for d in disks)
|
||||
dom.XMLDesc.return_value = f"<domain><devices>{devices}</devices></domain>"
|
||||
|
|
@ -172,6 +173,40 @@ class PathsInUseTestCase(unittest.TestCase):
|
|||
conn.paths_in_use()
|
||||
|
||||
|
||||
class VolumeUsersTestCase(unittest.TestCase):
|
||||
def test_the_vms_that_use_each_path(self):
|
||||
conn, _ = storage(
|
||||
domains=[domain("u1", ["/pool/vm.now", "/pool/shared.iso"], [SNAPSHOT], name="vm1"),
|
||||
domain("u2", ["/pool/shared.iso"], name="vm2")],
|
||||
volumes=[volume("/pool/clone.qcow2", backing="/pool/template.qcow2"), volume("/pool/spare.qcow2")],
|
||||
)
|
||||
# a snapshot's files are its VM's; another volume's backing file is no VM's
|
||||
self.assertEqual(conn.volume_users(), {
|
||||
"/pool/vm.now": ["vm1"], "/pool/shared.iso": ["vm1", "vm2"],
|
||||
"/pool/vm.s": ["vm1"], "/pool/vm.s.mem": ["vm1"], "/pool/vm.qcow2": ["vm1"], "/pool/template.qcow2": [],
|
||||
})
|
||||
|
||||
def test_uefi_variables_and_a_snapshots_block_device_are_in_use(self):
|
||||
dom = domain("u1", ["/pool/vm.qcow2"], ["<domainsnapshot><disks><disk name='vda' snapshot='external'>"
|
||||
"<source dev='/dev/vg/vm-snap'/></disk></disks></domainsnapshot>"])
|
||||
dom.XMLDesc.return_value = ("<domain><os><nvram>/pool/vm_VARS.fd</nvram></os><devices><disk type='file' "
|
||||
"device='disk'><source file='/pool/vm.qcow2'/></disk></devices></domain>")
|
||||
conn, _ = storage(domains=[dom])
|
||||
self.assertEqual(conn.paths_in_use(), {"/pool/vm.qcow2", "/pool/vm_VARS.fd", "/dev/vg/vm-snap"})
|
||||
dom.XMLDesc.return_value = "<domain><os><nvram type='block'><source dev='/dev/vg/vars'/></nvram></os></domain>"
|
||||
dom.listAllSnapshots.return_value = []
|
||||
self.assertEqual(conn.paths_in_use(), {"/dev/vg/vars"})
|
||||
|
||||
def test_a_shut_off_vms_base_images_are_its(self):
|
||||
# its definition names the top volume only; the volumes tell what each is built on
|
||||
conn, _ = storage(domains=[domain("u1", ["/pool/top.qcow2"], name="vm1")],
|
||||
volumes=[volume("/pool/top.qcow2", backing="/pool/mid.qcow2"),
|
||||
volume("/pool/mid.qcow2", backing="/pool/base.qcow2"),
|
||||
volume("/pool/other.qcow2", backing="/pool/base.qcow2")])
|
||||
self.assertEqual(conn.volume_users(), {"/pool/top.qcow2": ["vm1"], "/pool/mid.qcow2": ["vm1"],
|
||||
"/pool/base.qcow2": ["vm1"]})
|
||||
|
||||
|
||||
class DeleteVolumeTestCase(unittest.TestCase):
|
||||
def test_a_volume_in_use_is_refused(self):
|
||||
for case, domains, volumes in (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue