1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vrtManager/backup.py
catborise 823d34cc18 feat(vmbackups): restore a backup as a new VM
A ready backup has a Restore button (Backups page and the VM's tab) and
POST /api/v1/backups/<uuid>/restore/. The form takes the new VM's name
(default <vm>-restored), a compute that has the target's pool, the pool
for its disks, Connect the network (off by default: the restored guest
keeps the VM's addresses and host name) and Start after restore. The job
page follows the restore.

The backup.restore job holds what a new VM's job holds, the backup
(shared) and the target. Before anything is made it checks again: the
host is not in maintenance; the name is free in the panel and on the
host (a VM made with virsh too); the backup's files are in the target,
the disks at their size and the small files with their SHA-256; the
host offers the definition's emulator, machine type and firmware, its
networks, bridges and network filters; the destination pool has room
and none of the copies' names. The new VM's UUID, MACs, volume names and
NVRAM path are recorded first. Then the disks are copied from the
target, the NVRAM file uploaded, maintenance checked once more, and the
VM defined last and given to the superuser who restored it.

The definition is the kept one rewritten as a clone's (rewrite_for_new_vm,
taken out of _clone_definition for both): new name, UUID and SMBIOS
UUID, MACs and filter parameters, a VNC password, no cloud-init seed of
the VM's; with a new generation ID, each disk's source the restored file
without the old chain, the NVRAM path, and links down unless connected.

Recovery records a VM defined by the recorded UUID; otherwise it removes
the copies and the NVRAM file by their recorded names.
2026-11-17 09:14:09 +03:00

264 lines
13 KiB
Python

"""VM backups: the target pools and what a VM's backup holds (app vmbackups)."""
import hashlib
import os
import libvirt
from django.utils.translation import gettext_noop
from lxml import etree
from vrtManager.connection import NVRAM_DIR, wvmConnect
from vrtManager.instance import PERSISTENT_XML, rewrite_for_new_vm, wvmInstance
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}
class wvmBackup(wvmInstance):
"""The VM side of a backup."""
def backup_plan(self):
"""What a backup of the VM copies, from its persistent definition:
{"definition", "disks": [{dev, path, pool, format, capacity}],
"nvram": path or None, "left_out": [{dev, why}]}. Refuses, naming
why, a VM whose backup could not be restored as it is."""
tree = etree.fromstring(self._XMLDesc(PERSISTENT_XML))
name = self.instance.name()
if self.instance.hasManagedSaveImage(0):
raise OperationError(gettext_noop("%(vm)s has a saved state; start it or remove the saved state first"), vm=name)
if tree.find("devices/tpm") is not None:
raise OperationError(gettext_noop("%(vm)s has TPM state; TPM backups come later"), vm=name)
if tree.find("devices/filesystem") is not None:
raise OperationError(gettext_noop("%(vm)s shares a host directory (filesystem); it cannot be backed up"), vm=name)
if tree.xpath("os/kernel|os/initrd|os/dtb"):
raise OperationError(gettext_noop("%(vm)s boots a kernel file of the host; it cannot be backed up"), vm=name)
if tree.xpath("devices/hostdev|devices/interface[@type='hostdev']"):
raise OperationError(gettext_noop("%(vm)s has a host device; it cannot be backed up"), vm=name)
disks, left_out = [], []
for disk in tree.xpath("devices/disk"):
dev = disk.find("target").get("dev")
if disk.get("device") in ("cdrom", "floppy"):
why = gettext_noop("CD-ROM: not copied") if disk.get("device") == "cdrom" else gettext_noop("Floppy: not copied")
left_out.append({"dev": dev, "why": why})
continue
if disk.xpath("encryption|source/encryption"):
raise OperationError(gettext_noop("Disk %(dev)s is encrypted; it cannot be backed up"), dev=dev)
path = disk.xpath("string(source/@file)")
if disk.get("type") != "file" or not path:
raise OperationError(gettext_noop("Disk %(dev)s is not a file; it cannot be backed up"), dev=dev)
if disk.find("shareable") is not None:
raise OperationError(gettext_noop("Disk %(dev)s is shareable; it cannot be backed up"), dev=dev)
disk_format = disk.xpath("string(driver/@type)") or "raw"
if disk_format not in ("qcow2", "raw"):
raise OperationError(gettext_noop("Disk %(dev)s is %(format)s; only qcow2 and raw disks are backed up"),
dev=dev, format=disk_format)
try:
vol = self.get_volume_by_path(path)
except libvirt.libvirtError as err:
if err.get_error_code() != libvirt.VIR_ERR_NO_STORAGE_VOL:
raise
raise OperationError(gettext_noop("Disk %(dev)s (%(path)s) is in no storage pool of the host; it cannot be backed up"),
dev=dev, path=path) from err
disks.append({"dev": dev, "path": path, "pool": vol.storagePoolLookupByVolume().name(),
"format": disk_format, "capacity": vol.info()[1]})
if self.instance.state()[0] != libvirt.VIR_DOMAIN_SHUTOFF:
# a running VM's disks are copied: the definition kept must name the same ones
live = etree.fromstring(self._XMLDesc(0))
copied = [(d["dev"], d["path"]) for d in disks]
running = [(d.find("target").get("dev"), d.xpath("string(source/@file)"))
for d in live.xpath("devices/disk[not(@device='cdrom' or @device='floppy')]")]
if copied != running:
changed = sorted({dev for dev, _path in set(copied) ^ set(running)})
raise OperationError(gettext_noop("Disks %(devs)s change at the next start; back the VM up after it restarts"),
devs=", ".join(changed))
nvram = tree.xpath("string(os/nvram/source/@file)") or (tree.findtext("os/nvram") or "").strip() or None
if nvram and os.path.dirname(nvram) != NVRAM_DIR:
raise OperationError(gettext_noop("The NVRAM file %(path)s is not in libvirt's NVRAM directory %(dir)s and cannot be copied"),
path=nvram, dir=NVRAM_DIR)
return {"definition": etree.tostring(tree).decode(), "disks": disks, "nvram": nvram, "left_out": left_out}
def copy_disk_offline(self, disk, pool, name, owner_uid, owner_gid):
"""A shut-off VM's disk copied into the target pool, flat, in its
format (libvirt's createXMLFrom). Returns the copy's path."""
return self.get_wvmStorage(pool).clone_volume(
self.get_volume_by_path(disk["path"]), name, vol_fmt=disk["format"],
disk_owner_uid=owner_uid, disk_owner_gid=owner_gid,
)
def make_live_targets(self, disks, pool, names, owner_uid, owner_gid):
"""The empty volumes a running VM's push backup writes into, in the
target pool; the targets begin_clone_backup takes."""
targets = []
for disk in disks:
target = {"dev": disk["dev"], "pool": pool, "name": names[disk["dev"]],
"capacity": disk["capacity"], "format": disk["format"]}
target["path"] = self.create_clone_target(target, owner_uid, owner_gid)
targets.append(target)
return targets
def read_nvram(self, path):
"""The bytes of the VM's NVRAM file; None when it was never made (the
VM never started)."""
with self._nvram_pool() as pool:
vol = self._find_volume(pool, os.path.basename(path))
return None if vol is None else self.download_volume(vol)
def backup_file_names(uuid, disks, nvram):
"""The names of a backup's files in the target pool, from its UUID only."""
prefix = f"wvc-{uuid}"
return {
"disks": {d["dev"]: f"{prefix}-{d['dev']}.{'qcow2' if d['format'] == 'qcow2' else 'raw'}" for d in disks},
"nvram": f"{prefix}-nvram.fd" if nvram else None,
"domain": f"{prefix}-domain.xml",
"manifest": f"{prefix}-manifest.json",
}
def check_target_space(pool, disks, free_percent, free_min_gib):
"""The target keeps the disks' virtual capacity plus free_percent of
it, at least free_min_gib GiB, free (the snapshots' and clones' rule)."""
size = sum(d["capacity"] for d in disks)
needed = size + max(size * free_percent / 100, free_min_gib * 1024**3)
pool.refresh(0)
free = pool.info()[3]
if free < needed:
raise OperationError(gettext_noop(
"Storage pool %(pool)s has %(free).1f GiB free; the backup needs %(size).1f GiB (the copied disks, "
"plus %(percent)g%% of them, at least %(minimum)g GiB)"),
pool=pool.name(), free=free / 1024**3, size=needed / 1024**3, percent=free_percent, minimum=free_min_gib)
def put_file(conn, pool, name, data, owner):
"""Writes a small file of a backup into the target pool as owner (uid,
gid), replacing one of that name, and reads it back. Returns its
SHA-256."""
remove_files(pool, [name])
vol = conn.upload_volume(pool, name, data, owner=owner)
if conn.download_volume(vol) != data:
vol.delete(0)
raise OperationError(gettext_noop("The file %(name)s read back from the backup target differs from the one written"), name=name)
return hashlib.sha256(data).hexdigest()
def get_file(conn, pool, name):
return conn.download_volume(pool.storageVolLookupByName(name))
def remove_files(pool, names):
"""Deletes the volumes names of pool; missing ones are passed over."""
for name in names:
vol = wvmConnect._find_volume(pool, name)
if vol is not None:
vol.delete(0)
def volume_exists(pool, name):
return wvmConnect._find_volume(pool, name) is not None
def manifest_names(pool):
pool.refresh(0)
return sorted(name for name in pool.listVolumes() if name.startswith("wvc-") and name.endswith("-manifest.json"))
def restore_definition(definition, *, name, uuid, macs, disks, nvram, connect):
"""A backup's kept definition made a new VM's: rewrite_for_new_vm's
changes (macs: one per NIC, in order), its media ejected, a new generation ID, each disk's
source the restored file (disks: {dev: (path, format)}) with nothing of
the old one's chain, the NVRAM file at nvram, and each NIC's link down
unless connect: the restored guest keeps the VM's addresses and names."""
tree = etree.fromstring(definition)
clone_data = {"name": name, "clone-title": tree.findtext("title") or "",
"clone-description": tree.findtext("description") or "",
**{f"clone-net-mac-{number}": mac for number, mac in enumerate(macs)}}
rewrite_for_new_vm(tree, clone_data, uuid)
# media are not in the backup: the restore host may not have them, and the VM would not start
for source in tree.xpath("devices/disk[@device='cdrom' or @device='floppy']/source"):
source.getparent().remove(source)
if (genid := tree.find("genid")) is not None:
genid.text = None # libvirt makes a new one
for disk in tree.xpath("devices/disk[@device='disk']"):
path, disk_format = disks[disk.find("target").get("dev")]
disk.set("type", "file")
for old in disk.xpath("source|backingStore"):
disk.remove(old)
driver = disk.find("driver")
if driver is None:
driver = etree.SubElement(disk, "driver", name="qemu")
driver.set("type", disk_format)
source = etree.Element("source", file=path)
disk.insert(list(disk).index(driver) + 1, source)
if (element := tree.find("os/nvram")) is not None:
if element.find("source") is not None:
element.find("source").set("file", nvram)
else:
element.text = nvram
if not connect:
for interface in tree.findall("devices/interface"):
link = interface.find("link")
if link is None:
link = etree.SubElement(interface, "link")
link.set("state", "down")
return etree.tostring(tree).decode()
def check_restore_host(conn, definition):
"""Refuses a host that cannot run the backup's definition: its emulator,
machine type and firmware, the networks, bridges and network filters
its NICs name."""
tree = etree.fromstring(definition)
emulator = tree.findtext("devices/emulator")
os_type = tree.find("os/type")
arch, machine = os_type.get("arch"), os_type.get("machine")
try:
caps = etree.fromstring(conn.wvm.getDomainCapabilities(emulator, arch, machine, tree.get("type"), 0))
except libvirt.libvirtError as err:
raise OperationError(gettext_noop("This host cannot run the VM's emulator %(emulator)s with machine type %(machine)s: %(error)s"),
emulator=emulator, machine=machine, error=err) from err
loader = (tree.findtext("os/loader") or "").strip()
if loader and loader not in caps.xpath("os/loader/value/text()"):
raise OperationError(gettext_noop("This host has no firmware %(loader)s"), loader=loader)
for network in sorted(set(tree.xpath("devices/interface[@type='network']/source/@network")) - set(conn.get_networks())):
raise OperationError(gettext_noop("This host has no network %(name)s"), name=network)
for bridge in sorted(set(tree.xpath("devices/interface[@type='bridge']/source/@bridge")) - set(conn.get_ifaces())):
raise OperationError(gettext_noop("This host has no bridge %(name)s"), name=bridge)
for nwfilter in sorted(set(tree.xpath("devices/interface/filterref/@filter")) - set(conn.get_nwfilters())):
raise OperationError(gettext_noop("This host has no network filter %(name)s"), name=nwfilter)