1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +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:
catborise 2026-11-20 08:04:01 +03:00
parent dc5b1dbb2a
commit aeef837afa
12 changed files with 248 additions and 88 deletions

View file

@ -116,21 +116,47 @@
<h5 class="me-auto mb-0">{% trans "Volumes" %}</h5>
<input id="filter" class="form-control w-auto" type="search" placeholder="{% trans "Search volumes" %}" aria-label="{% trans "Search volumes" %}">
</div>
{% if users_read %}
{# which volumes this host's VMs use: table_filter.js counts each kind the search finds #}
<input type="hidden" id="state-filter" value="">
<nav class="mb-2" aria-label="{% trans "Volume use" %}">
<div class="nav nav-pills flex-wrap gap-1" id="state-pills">
<button type="button" class="nav-link active py-1 px-2" data-pill="" aria-pressed="true">
{% trans "All" %} <span class="badge text-bg-light ms-1" data-count-state=""></span>
</button>
<button type="button" class="nav-link py-1 px-2" data-pill="used" aria-pressed="false">
{% trans "In use" %} <span class="badge text-bg-light ms-1" data-count-state="used"></span>
</button>
<button type="button" class="nav-link py-1 px-2" data-pill="unused" aria-pressed="false">
{% trans "Not used on this host" %} <span class="badge text-bg-light ms-1" data-count-state="unused"></span>
</button>
</div>
</nav>
{% endif %}
<div class="table-responsive">
<table class="table table-hover align-middle" data-sortable>
<thead>
<tr>
<th scope="col">{% trans "Name" %}</th>
<th scope="col">{% trans "Format" %}</th>
{% if users_read %}<th scope="col" data-sort-method="text">{% trans "Used by" %}</th>{% endif %}
<th scope="col" class="d-none d-sm-table-cell" data-sort-method="number">{% trans "Capacity" %}</th>
<th scope="col" data-sort-method="none"><span class="visually-hidden">{% trans "Actions" %}</span></th>
</tr>
</thead>
<tbody class="searchable">
{% for volume in volumes %}
<tr data-search="{{ volume.name }}">
<tr data-search="{{ volume.name }}{% for vm in volume.users %} {{ vm }}{% endfor %}">
<td class="text-break">{{ volume.name }}</td>
<td><span class="badge text-bg-light border">{{ volume.type }}</span></td>
{% if users_read %}
{# a delete still asks the other hosts: one sharing this storage may use the volume #}
<td class="text-break small">
{% if volume.users %}<span data-state="used">{{ volume.users|join:", " }}</span>
{% elif volume.users is not None %}<span data-state="used" title="{% trans "Another volume is built on it" %}">{% trans "In use" %}</span>
{% else %}<span class="text-body-secondary" data-state="unused">{% trans "Not used on this host" %}</span>{% endif %}
</td>
{% endif %}
{# capacity: what the guest sees; allocated: what it takes on the storage now #}
<td class="d-none d-sm-table-cell" data-sort="{{ volume.size }}">
<div>{{ volume.size|filesizeformat }}</div>

View file

@ -25,7 +25,9 @@ class PoolPageTests(TestCase):
self.conn.is_active.return_value = True
self.conn.refresh.side_effect = libvirtError("pool 'default' has asynchronous jobs running.")
self.conn.get_volumes.return_value = ["base.qcow2"]
self.conn.update_volumes.return_value = [{"name": "base.qcow2", "size": 1, "allocation": 1, "type": "qcow2"}]
self.conn.update_volumes.return_value = [{"name": "base.qcow2", "path": "/p/base.qcow2", "size": 1, "allocation": 1,
"type": "qcow2"}]
self.conn.volume_users.return_value = {}
def test_the_page_answers_while_a_volume_is_cloned(self):
self.conn.get_size.return_value = (0, 0) # an iSCSI pool without LUNs
@ -113,7 +115,8 @@ class PoolPageTests(TestCase):
def test_volumes_are_searched_by_name_and_share_one_clone_dialog(self):
self.conn.get_size.return_value = (10, 4)
self.conn.update_volumes.return_value = [
{"name": name, "size": 1, "allocation": 1, "type": "raw"} for name in ("data.img", "install.iso", "Boot.ISO")]
{"name": name, "path": f"/p/{name}", "size": 1, "allocation": 1, "type": "raw"}
for name in ("data.img", "install.iso", "Boot.ISO")]
page = html.fromstring(self.client.get(reverse("storage", args=[self.compute.pk, "default"])).content)
self.assertEqual(page.xpath("//tbody/tr/@data-search"), ["data.img", "install.iso", "Boot.ISO"])
self.assertEqual(len(page.xpath("//div[contains(concat(' ', @class, ' '), ' modal ')][.//button[@name='cln_volume']]")), 1)
@ -122,6 +125,29 @@ class PoolPageTests(TestCase):
self.assertEqual(clone, {"data.img": True, "install.iso": False, "Boot.ISO": False})
self.assertTrue(page.xpath("//div[@id='no-match'][@hidden]"))
def test_each_volume_says_which_vms_use_it(self):
self.conn.get_size.return_value = (10, 4)
self.conn.update_volumes.return_value = [
{"name": name, "path": f"/p/{name}", "size": 1, "allocation": 1, "type": "raw"}
for name in ("a.img", "b.img", "c.img")]
self.conn.volume_users.return_value = {"/p/a.img": ["vm1", "vm2"], "/p/b.img": []}
page = html.fromstring(self.client.get(reverse("storage", args=[self.compute.pk, "default"])).content)
self.assertEqual([" ".join(t.split()) for t in page.xpath("//tbody/tr/td[3]//text()[normalize-space()]")],
["vm1, vm2", "In use", "Not used on this host"])
self.assertEqual(page.xpath("//tbody/tr//@data-state"), ["used", "used", "unused"])
self.assertEqual(page.xpath("//tbody/tr/@data-search"), ["a.img vm1 vm2", "b.img", "c.img"])
self.assertEqual(page.xpath("//div[@id='state-pills']/button/@data-pill"), ["", "used", "unused"])
def test_volumes_whose_use_cannot_be_read_are_listed(self):
self.conn.get_size.return_value = (10, 4)
self.conn.update_volumes.return_value = [{"name": "a.img", "path": "/p/a.img", "size": 1, "allocation": 1,
"type": "raw"}]
self.conn.volume_users.side_effect = libvirtError("cannot read the domains")
page = html.fromstring(self.client.get(reverse("storage", args=[self.compute.pk, "default"])).content)
self.assertEqual(page.xpath("//tbody/tr/td[1]/text()"), ["a.img"])
self.assertFalse(page.xpath("//tbody/tr//@data-state"))
self.assertFalse(page.xpath("//div[@id='state-pills']"))
def test_the_heading_offers_what_the_pool_state_allows(self):
# an active pool is stopped, a stopped one started or deleted; autostart either way
self.conn.get_size.return_value = (10, 4)

View file

@ -232,8 +232,14 @@ def storage(request, compute_id, pool):
# read only for the page: every action above redirects, but a refused new volume
# first: it refreshes the pool, and libvirt measures a pool's space only then
volumes = conn.update_volumes() if state else None
users = None
if volumes:
# which VMs of this host use each volume; the page lists them without it when it cannot be read
with contextlib.suppress(libvirtError):
users = conn.volume_users()
for volume in volumes or []:
volume["iso"] = is_iso(volume["name"])
volume["users"] = users.get(volume["path"]) if users is not None else None
try:
size, free = conn.get_size()
status = conn.get_status()
@ -251,6 +257,7 @@ def storage(request, compute_id, pool):
"form": form,
"state": state,
"volumes": volumes,
"users_read": users is not None,
"size": size,
"free": free,
"used": used,