1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00
webvirtcloud/vrtManager/backup.py
catborise 022737f143 feat(vmbackups): backup targets: a storage pool per host, checked to be one directory
A backup target is a dir or NFS storage pool the administrator makes on
the hosts and registers on the new Backup targets page (Backups in the
menu, superusers only). Each compute's pool is kept with its UUID, so a
pool replaced by another of the same name is refused. A second compute's
pool is accepted only when it is the same directory as the target's
first (the exclusive-create check migrations use). The target page shows
each pool's space and warns when the target is inside another pool's
directory, as its backups would share that storage's failures.

The storage pages refuse to delete a target's pool, and a volume of a
backup (page and API): backups are deleted from the Backups pages.
2026-11-17 07:40:08 +03:00

43 lines
1.5 KiB
Python

"""VM backups: the target pools and what a VM's backup holds (app vmbackups)."""
import libvirt
from django.utils.translation import gettext_noop
from lxml import etree
from vrtManager.util import OperationError
# pools that keep their volumes as files in one directory
TARGET_TYPES = ("dir", "netfs")
def target_pool(conn, name, uuid=None):
"""The active dir or netfs pool name of conn's host; with uuid, only the
pool a target registered, not another made since under its name."""
try:
pool = conn.get_storage(name)
usable = (
pool.isActive()
and etree.fromstring(pool.XMLDesc(0)).get("type") in TARGET_TYPES
and uuid in (None, pool.UUIDString())
)
except libvirt.libvirtError as err:
if err.get_error_code() != libvirt.VIR_ERR_NO_STORAGE_POOL:
raise
usable = False
if not usable:
raise OperationError(
gettext_noop(
"Storage pool %(pool)s is not an active directory or NFS pool of this host, "
"or not the one the backup target registered"
),
pool=name,
)
return pool
def pool_space(pool):
"""{name, size, free, used_percent} of a pool, as the storage pages show
it; refreshed first, as files may have changed outside libvirt."""
pool.refresh(0)
_state, size, used, free = pool.info()
return {"name": pool.name(), "size": size, "free": free, "used_percent": used * 100 / size if size else 0}