|
|
"""Small, synchronous libvirt service for one remote QEMU host.
|
|
|
|
|
|
The URI normally has the form ``qemu+ssh://user@host/system``. The web
|
|
|
process must already have a non-interactive, trusted way to authenticate to
|
|
|
that host; libvirt-python does not turn an interactive SSH prompt into an API.
|
|
|
"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import hashlib
|
|
|
import logging
|
|
|
import posixpath
|
|
|
import re
|
|
|
import secrets
|
|
|
import threading
|
|
|
import time
|
|
|
import uuid as uuidlib
|
|
|
import xml.etree.ElementTree as ET
|
|
|
from contextlib import contextmanager
|
|
|
from datetime import datetime, timezone
|
|
|
from pathlib import Path
|
|
|
from typing import Any, Callable, Iterator
|
|
|
|
|
|
import libvirt
|
|
|
|
|
|
from virsh_panel.cloud_init import build_seed_iso, read_seed_network, validate_cloud_network
|
|
|
from virsh_panel.metrics import RawVMStats
|
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
GIB = 1024**3
|
|
|
PANEL_NS = "urn:virsh-panel:1"
|
|
|
NAME_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,62}\Z")
|
|
|
BRIDGE_NAME_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,14}\Z")
|
|
|
TEMPLATE_VOLUME_RE = re.compile(r"panel-template-alma(?:9|10)-[0-9a-f]{8}-[0-9a-f]{16}\.qcow2\Z")
|
|
|
RESCUE_VOLUME_RE = re.compile(r"panel-rescue-[0-9a-f]{32}\.iso\Z")
|
|
|
_CREATE_LOCK = threading.RLock()
|
|
|
REINSTALL_SHUTDOWN_TIMEOUT = 120
|
|
|
REINSTALL_SHUTDOWN_POLL_INTERVAL = 1
|
|
|
MIN_RESCUE_ISO_BYTES = 1024 * 1024
|
|
|
MAX_RESCUE_ISO_BYTES = 8 * GIB
|
|
|
STATES = {
|
|
|
0: "nostate",
|
|
|
1: "running",
|
|
|
2: "blocked",
|
|
|
3: "paused",
|
|
|
4: "shutdown",
|
|
|
5: "shutoff",
|
|
|
6: "crashed",
|
|
|
7: "pmsuspended",
|
|
|
}
|
|
|
|
|
|
|
|
|
class VMError(Exception):
|
|
|
"""An error safe to report to the authenticated panel user."""
|
|
|
|
|
|
def __init__(self, message: str, status: int = 400) -> None:
|
|
|
super().__init__(message)
|
|
|
self.status = status
|
|
|
self.status_code = status
|
|
|
|
|
|
|
|
|
class VMService:
|
|
|
"""Operate on one host through libvirt's native API, never a shell."""
|
|
|
|
|
|
def __init__(self, uri: str, storage_pool: str, iso_pool: str, network: str,
|
|
|
rescue_iso_pool: str = "") -> None:
|
|
|
if not all(isinstance(value, str) and value for value in
|
|
|
(uri, storage_pool, iso_pool, network)):
|
|
|
raise ValueError("libvirt URI, storage pools, and network are required")
|
|
|
if not isinstance(rescue_iso_pool, str):
|
|
|
raise ValueError("rescue ISO pool must be a string")
|
|
|
self.uri = uri
|
|
|
self.storage_pool = storage_pool
|
|
|
self.iso_pool = iso_pool
|
|
|
self.rescue_iso_pool = rescue_iso_pool
|
|
|
self.network = network
|
|
|
self.bridge_name = None
|
|
|
if network.startswith("bridge:"):
|
|
|
self.bridge_name = network.removeprefix("bridge:")
|
|
|
if not BRIDGE_NAME_RE.fullmatch(self.bridge_name):
|
|
|
raise ValueError("bridge network must name a host interface of at most 15 characters")
|
|
|
|
|
|
@contextmanager
|
|
|
def _connect(self) -> Iterator[Any]:
|
|
|
try:
|
|
|
conn = libvirt.open(self.uri)
|
|
|
if conn is None:
|
|
|
raise VMError("无法连接虚拟化宿主机", 502)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("libvirt connection failed: %s", exc)
|
|
|
raise VMError("无法连接虚拟化宿主机", 502) from exc
|
|
|
try:
|
|
|
yield conn
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("libvirt operation failed: %s", exc)
|
|
|
raise VMError("宿主机操作失败,请查看面板服务日志", 502) from exc
|
|
|
finally:
|
|
|
try:
|
|
|
conn.close()
|
|
|
except libvirt.libvirtError:
|
|
|
LOG.exception("Could not close libvirt connection")
|
|
|
|
|
|
@staticmethod
|
|
|
def _uuid(value: str) -> str:
|
|
|
try:
|
|
|
return str(uuidlib.UUID(value))
|
|
|
except (TypeError, ValueError, AttributeError) as exc:
|
|
|
raise VMError("虚拟机 UUID 无效", 400) from exc
|
|
|
|
|
|
def _domain(self, conn: Any, value: str) -> Any:
|
|
|
vm_uuid = self._uuid(value)
|
|
|
for domain in conn.listAllDomains(0):
|
|
|
if domain.UUIDString() == vm_uuid:
|
|
|
return domain
|
|
|
raise VMError("虚拟机不存在", 404)
|
|
|
|
|
|
@staticmethod
|
|
|
def _pool_path(reference: str) -> str | None:
|
|
|
if not reference.startswith("/"):
|
|
|
return None
|
|
|
if ("\x00" in reference or reference.startswith("//")
|
|
|
or any(part in (".", "..") for part in reference.split("/"))):
|
|
|
raise VMError("存储池目录必须是不含 . 或 .. 的绝对路径")
|
|
|
path = posixpath.normpath(reference)
|
|
|
if path == "/":
|
|
|
raise VMError("不能将宿主机根目录用作存储池")
|
|
|
return path
|
|
|
|
|
|
@staticmethod
|
|
|
def _file_pool_path(pool: Any) -> str | None:
|
|
|
"""Compare paths for file-backed pools, including fs/netfs aliases."""
|
|
|
try:
|
|
|
root = ET.fromstring(pool.XMLDesc(0))
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("宿主机返回的存储池配置 XML 无效", 502) from exc
|
|
|
if root.get("type") not in {"dir", "fs", "netfs"}:
|
|
|
return None
|
|
|
path = root.findtext("./target/path")
|
|
|
return posixpath.normpath(path) if path and path.startswith("/") else None
|
|
|
|
|
|
@staticmethod
|
|
|
def _directory_pool_path(pool: Any) -> str | None:
|
|
|
try:
|
|
|
root = ET.fromstring(pool.XMLDesc(0))
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("宿主机返回的存储池配置 XML 无效", 502) from exc
|
|
|
if root.get("type") != "dir":
|
|
|
return None
|
|
|
path = root.findtext("./target/path")
|
|
|
return posixpath.normpath(path) if path and path.startswith("/") else None
|
|
|
|
|
|
def _lookup_pool(self, conn: Any, reference: str) -> Any | None:
|
|
|
path = self._pool_path(reference)
|
|
|
if path is None:
|
|
|
try:
|
|
|
return conn.storagePoolLookupByName(reference)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.info("Storage pool %s is not defined: %s", reference, exc)
|
|
|
return None
|
|
|
matches = [pool for pool in conn.listAllStoragePools(0)
|
|
|
if self._file_pool_path(pool) == path]
|
|
|
if len(matches) > 1:
|
|
|
raise VMError(f"目录 {path} 对应多个 libvirt 存储池,请在宿主机处理重复配置", 409)
|
|
|
return matches[0] if matches else None
|
|
|
|
|
|
def _active_pool(self, conn: Any, reference: str) -> Any:
|
|
|
pool = self._lookup_pool(conn, reference)
|
|
|
if pool is None:
|
|
|
if self._pool_path(reference) is not None:
|
|
|
raise VMError(f"目录 {reference} 尚未配置为 libvirt 存储池,请先保存宿主机设置", 409)
|
|
|
raise VMError(f"存储池 {reference} 不存在", 404)
|
|
|
if not pool.isActive():
|
|
|
raise VMError(f"存储池 {pool.name()} 未启动", 409)
|
|
|
return pool
|
|
|
|
|
|
@classmethod
|
|
|
def _same_pool_location(cls, first: Any, second: Any) -> bool:
|
|
|
if first.name() == second.name():
|
|
|
return True
|
|
|
first_path = cls._file_pool_path(first)
|
|
|
second_path = cls._file_pool_path(second)
|
|
|
return first_path is not None and first_path == second_path
|
|
|
|
|
|
def _active_rescue_pool(self, conn: Any) -> Any:
|
|
|
if not self.rescue_iso_pool:
|
|
|
raise VMError("请先在宿主机设置中配置救援 ISO 目录", 409)
|
|
|
pool = self._active_pool(conn, self.rescue_iso_pool)
|
|
|
normal_pool = self._lookup_pool(conn, self.iso_pool)
|
|
|
if normal_pool is not None and self._same_pool_location(pool, normal_pool):
|
|
|
raise VMError("救援 ISO 目录不能与安装 ISO 目录相同", 409)
|
|
|
return pool
|
|
|
|
|
|
@classmethod
|
|
|
def _refresh_directory_pool(cls, pool: Any) -> None:
|
|
|
# ISO files and disk images may be placed into these directories
|
|
|
# outside libvirt. Refresh before looking up names or checking for
|
|
|
# collisions so libvirt's cached volume list is not stale.
|
|
|
if cls._directory_pool_path(pool) is not None:
|
|
|
pool.refresh(0)
|
|
|
|
|
|
@staticmethod
|
|
|
def _directory_pool_xml(name: str, path: str) -> str:
|
|
|
root = ET.Element("pool", type="dir")
|
|
|
ET.SubElement(root, "name").text = name
|
|
|
target = ET.SubElement(root, "target")
|
|
|
ET.SubElement(target, "path").text = path
|
|
|
return ET.tostring(root, encoding="unicode")
|
|
|
|
|
|
@staticmethod
|
|
|
def _rollback_new_pool(pool: Any) -> bool:
|
|
|
"""Remove only a definition created by this ensure call, not its files."""
|
|
|
restored = True
|
|
|
try:
|
|
|
if pool.isActive():
|
|
|
pool.destroy()
|
|
|
except libvirt.libvirtError:
|
|
|
LOG.exception("Could not stop newly defined pool %s during rollback", pool.name())
|
|
|
restored = False
|
|
|
try:
|
|
|
pool.undefine()
|
|
|
except libvirt.libvirtError:
|
|
|
LOG.exception("Could not undefine newly defined pool %s during rollback", pool.name())
|
|
|
restored = False
|
|
|
return restored
|
|
|
|
|
|
def _ensure_pool(self, conn: Any, reference: str, created_pools: list[Any]) -> Any:
|
|
|
path = self._pool_path(reference)
|
|
|
pool = self._lookup_pool(conn, reference)
|
|
|
if pool is None:
|
|
|
if path is None:
|
|
|
raise VMError(f"存储池 {reference} 不存在;请填写已存在的池名或宿主机绝对目录", 404)
|
|
|
# A path is a host directory, never an XML pool name. A stable
|
|
|
# name keeps domain XML valid across panel and libvirt restarts.
|
|
|
name = "panel-dir-" + hashlib.sha256(path.encode("utf-8")).hexdigest()[:48]
|
|
|
try:
|
|
|
collision = conn.storagePoolLookupByName(name)
|
|
|
except libvirt.libvirtError:
|
|
|
collision = None
|
|
|
if collision is not None:
|
|
|
raise VMError(f"自动生成的存储池名称 {name} 已被其他目录占用", 409)
|
|
|
try:
|
|
|
pool = conn.storagePoolDefineXML(self._directory_pool_xml(name, path), 0)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("Could not define directory pool %s for %s: %s", name, path, exc)
|
|
|
raise VMError(f"无法为目录 {path} 定义 libvirt 存储池", 502) from exc
|
|
|
if pool is None:
|
|
|
raise VMError(f"无法为目录 {path} 定义 libvirt 存储池", 502)
|
|
|
created_pools.append(pool)
|
|
|
if not pool.isPersistent():
|
|
|
raise VMError(f"存储池 {pool.name()} 是临时池,无法在宿主机重启后自动启动", 409)
|
|
|
try:
|
|
|
if not pool.isActive():
|
|
|
pool.create(0)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("Could not start directory pool %s: %s", pool.name(), exc)
|
|
|
if path is not None:
|
|
|
raise VMError(f"目录 {path} 不存在或 libvirt 无权访问;请检查宿主机目录和 SELinux 权限", 409) from exc
|
|
|
raise VMError(f"存储池 {reference} 无法启动", 409) from exc
|
|
|
if not pool.autostart():
|
|
|
try:
|
|
|
pool.setAutostart(1)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("Could not enable autostart for pool %s: %s", pool.name(), exc)
|
|
|
raise VMError(f"无法设置存储池 {pool.name()} 随宿主机启动", 502) from exc
|
|
|
return pool
|
|
|
|
|
|
def ensure_storage_pools(self) -> dict[str, str]:
|
|
|
"""Explicitly define/start configured directory pools; never build paths."""
|
|
|
with self._connect() as conn:
|
|
|
created_pools: list[Any] = []
|
|
|
try:
|
|
|
disk_pool = self._ensure_pool(conn, self.storage_pool, created_pools)
|
|
|
iso_pool = self._ensure_pool(conn, self.iso_pool, created_pools)
|
|
|
pools = {"storage_pool": disk_pool.name(), "iso_pool": iso_pool.name()}
|
|
|
if self.rescue_iso_pool:
|
|
|
rescue_pool = self._ensure_pool(
|
|
|
conn, self.rescue_iso_pool, created_pools)
|
|
|
if self._same_pool_location(iso_pool, rescue_pool):
|
|
|
raise VMError("救援 ISO 目录不能与安装 ISO 目录相同", 409)
|
|
|
pools["rescue_iso_pool"] = rescue_pool.name()
|
|
|
return pools
|
|
|
except Exception as exc:
|
|
|
rolled_back = True
|
|
|
for pool in reversed(created_pools):
|
|
|
if not self._rollback_new_pool(pool):
|
|
|
rolled_back = False
|
|
|
if not rolled_back:
|
|
|
raise VMError("存储池配置失败且新建存储池回滚失败;请检查宿主机", 502) from exc
|
|
|
raise
|
|
|
|
|
|
@staticmethod
|
|
|
def _vm_xml(domain: Any) -> ET.Element:
|
|
|
try:
|
|
|
return ET.fromstring(domain.XMLDesc(0))
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("宿主机返回的虚拟机配置 XML 无效", 502) from exc
|
|
|
|
|
|
@staticmethod
|
|
|
def _disk_rows(root: ET.Element) -> list[dict[str, Any]]:
|
|
|
rows: list[dict[str, Any]] = []
|
|
|
for disk in root.findall("./devices/disk"):
|
|
|
if disk.get("device") != "disk":
|
|
|
continue
|
|
|
source = disk.find("source")
|
|
|
target = disk.find("target")
|
|
|
driver = disk.find("driver")
|
|
|
source_name = ""
|
|
|
if source is not None:
|
|
|
source_name = (source.get("volume") or source.get("file")
|
|
|
or source.get("dev") or "")
|
|
|
rows.append({
|
|
|
"device": disk.get("device", "disk"),
|
|
|
"source": source_name,
|
|
|
"target": target.get("dev", "") if target is not None else "",
|
|
|
"format": driver.get("type", "") if driver is not None else "",
|
|
|
})
|
|
|
return rows
|
|
|
|
|
|
@staticmethod
|
|
|
def _vnc_port(root: ET.Element) -> int | None:
|
|
|
for graphics in root.findall("./devices/graphics"):
|
|
|
if graphics.get("type") == "vnc":
|
|
|
# The relay always connects to the hypervisor's IPv4 loopback.
|
|
|
# Do not advertise a console that listens on another address.
|
|
|
listeners = [graphics.get("listen")]
|
|
|
listeners.extend(node.get("address") for node in graphics.findall("listen"))
|
|
|
if not listeners or any(address not in (None, "127.0.0.1") for address in listeners):
|
|
|
return None
|
|
|
if all(address is None for address in listeners):
|
|
|
return None
|
|
|
try:
|
|
|
port = int(graphics.get("port", "-1"))
|
|
|
except ValueError:
|
|
|
return None
|
|
|
return port if 1 <= port <= 65535 else None
|
|
|
return None
|
|
|
|
|
|
def _vm_dict(self, domain: Any) -> dict[str, Any]:
|
|
|
info = domain.info()
|
|
|
root = self._vm_xml(domain)
|
|
|
disks = self._disk_rows(root)
|
|
|
for disk in disks:
|
|
|
identifier = disk["target"] or disk["source"]
|
|
|
if not identifier:
|
|
|
continue
|
|
|
try:
|
|
|
capacity, _allocation, _physical = domain.blockInfo(identifier, 0)
|
|
|
except libvirt.libvirtError:
|
|
|
# The domain remains manageable when an external storage
|
|
|
# backend cannot report its capacity (for example, RBD).
|
|
|
continue
|
|
|
if isinstance(capacity, int) and capacity > 0:
|
|
|
disk["capacity_gib"] = round(capacity / GIB, 2)
|
|
|
managed = root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
rescue = root.find(f"./metadata/{{{PANEL_NS}}}rescue")
|
|
|
rescue_staged = False
|
|
|
inactive_rescue = None
|
|
|
if domain.isPersistent():
|
|
|
try:
|
|
|
inactive_root = ET.fromstring(
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("宿主机返回的虚拟机持久配置 XML 无效", 502) from exc
|
|
|
inactive_rescue = inactive_root.find(f"./metadata/{{{PANEL_NS}}}rescue")
|
|
|
# Defining the next boot does not replace the live QEMU process.
|
|
|
# A stopped VM with a rescue definition is staged until first boot.
|
|
|
rescue_staged = inactive_rescue is not None and (
|
|
|
not domain.isActive() or rescue is None)
|
|
|
os_source = managed.get("os-source") if managed is not None else None
|
|
|
if os_source not in {"cloud_template", "iso"}:
|
|
|
os_source = "unknown"
|
|
|
try:
|
|
|
autostart = bool(domain.autostart())
|
|
|
except libvirt.libvirtError:
|
|
|
autostart = False
|
|
|
return {
|
|
|
"uuid": domain.UUIDString(),
|
|
|
"name": domain.name(),
|
|
|
"state": STATES.get(info[0], "unknown"),
|
|
|
"vcpu": info[3],
|
|
|
"memory_mib": info[1] // 1024,
|
|
|
"disks": disks,
|
|
|
"autostart": autostart,
|
|
|
"managed": managed is not None,
|
|
|
"public_ip_cidr": managed.get("public-ip-cidr") if managed is not None else None,
|
|
|
"created_at": managed.get("created-at") if managed is not None else None,
|
|
|
"credential_generation": managed.get("seed-volume") if os_source == "cloud_template" else None,
|
|
|
"os_name": managed.get("os-name") if os_source == "cloud_template" else None,
|
|
|
"os_source": os_source,
|
|
|
"install_iso": managed.get("install-iso") if os_source == "iso" else None,
|
|
|
"rescue_mode": rescue is not None,
|
|
|
"rescue_staged": rescue_staged,
|
|
|
"rescue_volume_name": (
|
|
|
rescue.get("volume") if rescue is not None else
|
|
|
inactive_rescue.get("volume") if inactive_rescue is not None else None),
|
|
|
"console_port": self._vnc_port(root),
|
|
|
}
|
|
|
|
|
|
@staticmethod
|
|
|
def _enable_vm_autostart(domain: Any) -> None:
|
|
|
"""Require host-boot autostart on a new persistent VM definition."""
|
|
|
try:
|
|
|
domain.setAutostart(1)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("Could not enable VM autostart: %s", exc)
|
|
|
raise VMError("无法设置新虚拟机随宿主机开机自启", 502) from exc
|
|
|
|
|
|
def list_vms(self) -> list[dict[str, Any]]:
|
|
|
with self._connect() as conn:
|
|
|
return sorted(
|
|
|
(self._vm_dict(domain) for domain in conn.listAllDomains(0)),
|
|
|
key=lambda row: row["name"].casefold(),
|
|
|
)
|
|
|
|
|
|
def get_vm(self, uuid: str) -> dict[str, Any]:
|
|
|
with self._connect() as conn:
|
|
|
return self._vm_dict(self._domain(conn, uuid))
|
|
|
|
|
|
def metric_counters(self) -> list[RawVMStats]:
|
|
|
"""Fetch running-domain counters in one libvirt API round trip.
|
|
|
|
|
|
getAllDomainStats(stats, flags) returns ``(domain, dict)`` pairs in
|
|
|
libvirt-python. Network rx/tx fields are from the guest's viewpoint.
|
|
|
Missing per-device values are preserved as missing by the sampler.
|
|
|
"""
|
|
|
groups = (libvirt.VIR_DOMAIN_STATS_CPU_TOTAL | libvirt.VIR_DOMAIN_STATS_BALLOON
|
|
|
| libvirt.VIR_DOMAIN_STATS_BLOCK | libvirt.VIR_DOMAIN_STATS_INTERFACE)
|
|
|
running = libvirt.VIR_CONNECT_GET_ALL_DOMAINS_STATS_RUNNING
|
|
|
with self._connect() as conn:
|
|
|
rows: list[RawVMStats] = []
|
|
|
for domain, values in conn.getAllDomainStats(groups, running):
|
|
|
# A guest can stop or be undefined after bulk stats are read.
|
|
|
# Do not lose all other guests' samples for that transient race.
|
|
|
try:
|
|
|
info = domain.info()
|
|
|
generation = domain.ID()
|
|
|
vm_id = domain.UUIDString()
|
|
|
except libvirt.libvirtError:
|
|
|
continue
|
|
|
if info[0] != 1 or generation < 0:
|
|
|
continue
|
|
|
rows.append(RawVMStats(
|
|
|
vm_id=vm_id, generation=generation,
|
|
|
vcpus=info[3], values=values,
|
|
|
))
|
|
|
return rows
|
|
|
|
|
|
def resources(self) -> dict[str, Any]:
|
|
|
with self._connect() as conn:
|
|
|
disk_pool = self._active_pool(conn, self.storage_pool)
|
|
|
iso_pool = self._active_pool(conn, self.iso_pool)
|
|
|
self._refresh_directory_pool(iso_pool)
|
|
|
if self.bridge_name is None:
|
|
|
network = conn.networkLookupByName(self.network)
|
|
|
active_networks = [self.network] if network.isActive() else []
|
|
|
else:
|
|
|
# Linux bridges live outside libvirt's virtual-network API.
|
|
|
# Advertise the configured selector and let domain startup
|
|
|
# report a missing/inactive bridge on the actual host.
|
|
|
active_networks = [self.network]
|
|
|
isos = sorted(name for name in iso_pool.listVolumes()
|
|
|
if name.lower().endswith(".iso")
|
|
|
and not RESCUE_VOLUME_RE.fullmatch(name))
|
|
|
pool_info = disk_pool.info()
|
|
|
return {
|
|
|
"storage_pool": self.storage_pool,
|
|
|
"networks": active_networks,
|
|
|
"isos": isos,
|
|
|
"storage_available_gib": pool_info[3] // GIB,
|
|
|
}
|
|
|
|
|
|
def _validate_common_create(self, spec: dict[str, Any]) -> dict[str, Any]:
|
|
|
if not isinstance(spec, dict):
|
|
|
raise VMError("虚拟机配置无效")
|
|
|
name = spec.get("name")
|
|
|
if not isinstance(name, str) or not NAME_RE.fullmatch(name):
|
|
|
raise VMError("名称只能包含字母、数字、点、下划线或短横线,最长 63 字符")
|
|
|
bounds = {
|
|
|
"vcpu": (1, 64),
|
|
|
"memory_mib": (512, 262144),
|
|
|
"disk_gib": (1, 4096),
|
|
|
}
|
|
|
validated: dict[str, Any] = {"name": name}
|
|
|
for key, (minimum, maximum) in bounds.items():
|
|
|
value = spec.get(key)
|
|
|
if isinstance(value, bool) or not isinstance(value, int) or not minimum <= value <= maximum:
|
|
|
raise VMError(f"{key} 必须是 {minimum} 到 {maximum} 的整数")
|
|
|
validated[key] = value
|
|
|
if spec.get("network") != self.network:
|
|
|
raise VMError("只能选择面板配置的虚拟网络或网桥")
|
|
|
return validated
|
|
|
|
|
|
def _validate_create(self, spec: dict[str, Any]) -> dict[str, Any]:
|
|
|
validated = self._validate_common_create(spec)
|
|
|
iso = spec.get("iso")
|
|
|
if not isinstance(iso, str) or not iso.lower().endswith(".iso"):
|
|
|
raise VMError("请选择 ISO 存储池内的安装镜像")
|
|
|
if RESCUE_VOLUME_RE.fullmatch(iso):
|
|
|
raise VMError("救援镜像不能用作虚拟机安装 ISO", 409)
|
|
|
validated["iso"] = iso
|
|
|
return validated
|
|
|
|
|
|
@staticmethod
|
|
|
def _validated_os_name(value: Any) -> str | None:
|
|
|
if value is None:
|
|
|
return None
|
|
|
if (not isinstance(value, str) or not 1 <= len(value) <= 64
|
|
|
or value != value.strip() or any(ord(char) < 32 for char in value)):
|
|
|
raise VMError("云镜像模板名称无效")
|
|
|
return value
|
|
|
|
|
|
@staticmethod
|
|
|
def _volume_xml(name: str, disk_gib: int) -> str:
|
|
|
return VMService._volume_xml_bytes(name, disk_gib * GIB, "qcow2")
|
|
|
|
|
|
@staticmethod
|
|
|
def _volume_xml_bytes(name: str, capacity: int, fmt: str) -> str:
|
|
|
root = ET.Element("volume")
|
|
|
ET.SubElement(root, "name").text = name
|
|
|
ET.SubElement(root, "capacity", unit="bytes").text = str(capacity)
|
|
|
target = ET.SubElement(root, "target")
|
|
|
ET.SubElement(target, "format", type=fmt)
|
|
|
return ET.tostring(root, encoding="unicode")
|
|
|
|
|
|
@classmethod
|
|
|
def _volume_source(cls, pool: Any, volume: Any) -> tuple[str, dict[str, str]]:
|
|
|
"""Use the actual host path for directory-backed storage volumes."""
|
|
|
directory = cls._directory_pool_path(pool)
|
|
|
if directory is None:
|
|
|
return "volume", {"pool": pool.name(), "volume": volume.name()}
|
|
|
path = volume.path()
|
|
|
if (not isinstance(path, str) or not posixpath.isabs(path)
|
|
|
or posixpath.dirname(posixpath.normpath(path)) != directory):
|
|
|
raise VMError("存储卷路径不在面板配置的宿主机目录中", 502)
|
|
|
return "file", {"file": posixpath.normpath(path)}
|
|
|
|
|
|
def _domain_xml(self, spec: dict[str, Any], vm_uuid: str, disk_name: str,
|
|
|
disk_source: tuple[str, dict[str, str]],
|
|
|
iso_source: tuple[str, dict[str, str]], *,
|
|
|
install_phase: bool) -> str:
|
|
|
root = ET.Element("domain", type="kvm")
|
|
|
ET.SubElement(root, "name").text = spec["name"]
|
|
|
ET.SubElement(root, "uuid").text = vm_uuid
|
|
|
ET.SubElement(root, "memory", unit="MiB").text = str(spec["memory_mib"])
|
|
|
ET.SubElement(root, "vcpu", placement="static").text = str(spec["vcpu"])
|
|
|
os_node = ET.SubElement(root, "os", firmware="efi")
|
|
|
# AlmaLinux 10's x86_64 OVMF descriptors target Q35 machines. An
|
|
|
# unspecified machine can resolve to i440fx and make firmware='efi'
|
|
|
# fail even when virt-install --boot uefi succeeds on the same host.
|
|
|
ET.SubElement(os_node, "type", arch="x86_64", machine="q35").text = "hvm"
|
|
|
if install_phase:
|
|
|
ET.SubElement(os_node, "boot", dev="cdrom")
|
|
|
ET.SubElement(os_node, "boot", dev="hd")
|
|
|
# x86 UEFI firmware requires ACPI; virt-install also enables APIC.
|
|
|
features = ET.SubElement(root, "features")
|
|
|
ET.SubElement(features, "acpi")
|
|
|
ET.SubElement(features, "apic")
|
|
|
ET.SubElement(root, "cpu", mode="host-passthrough")
|
|
|
if install_phase:
|
|
|
# The installer's first reboot shuts the transient guest off.
|
|
|
# The next panel Start uses the persistent, ISO-free definition.
|
|
|
ET.SubElement(root, "on_reboot").text = "destroy"
|
|
|
metadata = ET.SubElement(root, "metadata")
|
|
|
managed = ET.SubElement(metadata, f"{{{PANEL_NS}}}managed", version="2",
|
|
|
**{"disk-volume": disk_name})
|
|
|
if spec.get("created_at"):
|
|
|
managed.set("created-at", spec["created_at"])
|
|
|
if "iso" in spec:
|
|
|
# The installation media does not prove which OS was installed.
|
|
|
managed.set("os-source", "iso")
|
|
|
managed.set("install-iso", spec["iso"])
|
|
|
devices = ET.SubElement(root, "devices")
|
|
|
|
|
|
disk = ET.SubElement(devices, "disk", type=disk_source[0], device="disk")
|
|
|
ET.SubElement(disk, "driver", name="qemu", type="qcow2",
|
|
|
cache="none", discard="unmap")
|
|
|
ET.SubElement(disk, "source", **disk_source[1])
|
|
|
ET.SubElement(disk, "target", dev="vda", bus="virtio")
|
|
|
|
|
|
cdrom = ET.SubElement(devices, "disk", type=iso_source[0] if install_phase else "file",
|
|
|
device="cdrom")
|
|
|
ET.SubElement(cdrom, "driver", name="qemu", type="raw")
|
|
|
if install_phase:
|
|
|
ET.SubElement(cdrom, "source", **iso_source[1])
|
|
|
ET.SubElement(cdrom, "target", dev="sda", bus="sata")
|
|
|
ET.SubElement(cdrom, "readonly")
|
|
|
|
|
|
if self.bridge_name is None:
|
|
|
interface = ET.SubElement(devices, "interface", type="network")
|
|
|
ET.SubElement(interface, "source", network=self.network)
|
|
|
else:
|
|
|
interface = ET.SubElement(devices, "interface", type="bridge")
|
|
|
ET.SubElement(interface, "source", bridge=self.bridge_name)
|
|
|
ET.SubElement(interface, "model", type="virtio")
|
|
|
# RHEL 10 recommends one virtio-net queue per vCPU, up to 16.
|
|
|
ET.SubElement(interface, "driver", queues=str(min(spec["vcpu"], 16)))
|
|
|
graphics = ET.SubElement(devices, "graphics", type="vnc", port="-1",
|
|
|
autoport="yes", listen="127.0.0.1")
|
|
|
ET.SubElement(graphics, "listen", type="address", address="127.0.0.1")
|
|
|
video = ET.SubElement(devices, "video")
|
|
|
ET.SubElement(video, "model", type="vga")
|
|
|
ET.SubElement(devices, "input", type="tablet", bus="usb")
|
|
|
channel = ET.SubElement(devices, "channel", type="unix")
|
|
|
ET.SubElement(channel, "target", type="virtio", name="org.qemu.guest_agent.0")
|
|
|
memballoon = ET.SubElement(devices, "memballoon", model="virtio",
|
|
|
freePageReporting="on")
|
|
|
ET.SubElement(memballoon, "stats", period="10")
|
|
|
return ET.tostring(root, encoding="unicode")
|
|
|
|
|
|
def _rollback_new_vm(self, conn: Any, vm_uuid: str, name: str,
|
|
|
pool: Any, volume: Any, disk_name: str) -> bool:
|
|
|
"""Delete the new disk only after verifying its VM no longer exists."""
|
|
|
try:
|
|
|
domains = [domain for domain in conn.listAllDomains(0)
|
|
|
if domain.UUIDString() == vm_uuid]
|
|
|
if len(domains) > 1 or any(domain.name() != name for domain in domains):
|
|
|
LOG.error("Cannot safely identify new domain %s during rollback", vm_uuid)
|
|
|
return False
|
|
|
for domain in domains:
|
|
|
persistent = bool(domain.isPersistent())
|
|
|
if domain.isActive():
|
|
|
domain.destroy()
|
|
|
if persistent:
|
|
|
self._undefine_domain(domain, remove_nvram=True)
|
|
|
remaining = conn.listAllDomains(0)
|
|
|
if any(domain.UUIDString() == vm_uuid for domain in remaining):
|
|
|
LOG.error("New domain %s still exists after rollback", vm_uuid)
|
|
|
return False
|
|
|
# An external actor could have attached this newly created volume
|
|
|
# while the install was starting. Inspect both running and saved
|
|
|
# definitions before deleting it; uncertainty means retain it.
|
|
|
volume_path = volume.path()
|
|
|
pool_name = pool.name()
|
|
|
if not isinstance(volume_path, str) or not volume_path:
|
|
|
LOG.error("Cannot resolve volume %s path during rollback", disk_name)
|
|
|
return False
|
|
|
for other in remaining:
|
|
|
flags = [0]
|
|
|
if other.isActive() and other.isPersistent():
|
|
|
flags.append(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
for flag in flags:
|
|
|
root = ET.fromstring(other.XMLDesc(flag))
|
|
|
for source in self._all_disk_sources(root):
|
|
|
if ((source.get("pool") == pool_name
|
|
|
and source.get("volume") == disk_name)
|
|
|
or volume_path in (source.get("file"), source.get("dev"))):
|
|
|
LOG.error("Another domain references volume %s; retaining it", disk_name)
|
|
|
return False
|
|
|
except Exception:
|
|
|
LOG.exception("Could not roll back domain %s; retaining disk %s", vm_uuid, disk_name)
|
|
|
return False
|
|
|
try:
|
|
|
volume.delete(0)
|
|
|
except Exception:
|
|
|
LOG.exception("Could not roll back volume %s", disk_name)
|
|
|
return False
|
|
|
return True
|
|
|
|
|
|
@staticmethod
|
|
|
def _upload_volume(conn: Any, volume: Any, chunks: Iterator[bytes], size: int) -> None:
|
|
|
"""Upload a local image through libvirt's authenticated SSH transport."""
|
|
|
stream = conn.newStream(0)
|
|
|
try:
|
|
|
volume.upload(stream, 0, size, 0)
|
|
|
uploaded = 0
|
|
|
for chunk in chunks:
|
|
|
if not isinstance(chunk, bytes):
|
|
|
raise VMError("镜像上传数据无效", 502)
|
|
|
pending = chunk
|
|
|
while pending:
|
|
|
sent = stream.send(pending)
|
|
|
if not isinstance(sent, int) or sent <= 0:
|
|
|
raise VMError("宿主机镜像上传中断", 502)
|
|
|
uploaded += sent
|
|
|
pending = pending[sent:]
|
|
|
if uploaded != size:
|
|
|
raise VMError("宿主机镜像上传长度不匹配", 502)
|
|
|
stream.finish()
|
|
|
except Exception:
|
|
|
try:
|
|
|
stream.abort()
|
|
|
except Exception:
|
|
|
LOG.exception("Could not abort failed libvirt volume upload")
|
|
|
raise
|
|
|
|
|
|
@staticmethod
|
|
|
def _download_seed_volume(conn: Any, volume: Any) -> bytes:
|
|
|
"""Read only a small CIDATA image through libvirt's SSH transport."""
|
|
|
size = int(volume.info()[1])
|
|
|
if not 0 < size <= 1024 * 1024:
|
|
|
raise VMError("原 cloud-init 配置盘大小无效,无法自动恢复网络", 409)
|
|
|
stream = conn.newStream(0)
|
|
|
try:
|
|
|
volume.download(stream, 0, size, 0)
|
|
|
chunks = []
|
|
|
remaining = size
|
|
|
while remaining:
|
|
|
chunk = stream.recv(min(65536, remaining))
|
|
|
if not isinstance(chunk, bytes) or not chunk:
|
|
|
raise VMError("原 cloud-init 配置盘读取不完整", 502)
|
|
|
chunks.append(chunk)
|
|
|
remaining -= len(chunk)
|
|
|
stream.finish()
|
|
|
return b"".join(chunks)
|
|
|
except Exception:
|
|
|
try:
|
|
|
stream.abort()
|
|
|
except Exception:
|
|
|
LOG.exception("Could not abort failed libvirt volume download")
|
|
|
raise
|
|
|
|
|
|
def import_template(self, image_path: Path, volume_name: str,
|
|
|
virtual_size: int) -> dict[str, Any]:
|
|
|
"""Place a verified qcow2 image in the configured host disk pool."""
|
|
|
if not TEMPLATE_VOLUME_RE.fullmatch(volume_name):
|
|
|
raise VMError("模板卷名称无效")
|
|
|
if not isinstance(virtual_size, int) or not GIB <= virtual_size <= 4096 * GIB:
|
|
|
raise VMError("模板虚拟容量无效")
|
|
|
image_path = Path(image_path)
|
|
|
image_size = image_path.stat().st_size
|
|
|
with self._connect() as conn:
|
|
|
pool = self._active_pool(conn, self.storage_pool)
|
|
|
self._refresh_directory_pool(pool)
|
|
|
if volume_name in pool.listVolumes():
|
|
|
raise VMError("同名模板镜像已存在,不会覆盖", 409)
|
|
|
if image_size > pool.info()[3]:
|
|
|
raise VMError("宿主机存储池空间不足,无法导入模板", 409)
|
|
|
volume = pool.createXML(self._volume_xml_bytes(
|
|
|
volume_name, virtual_size, "qcow2"), 0)
|
|
|
if volume is None:
|
|
|
raise VMError("宿主机未能创建模板卷", 502)
|
|
|
try:
|
|
|
def read_chunks() -> Iterator[bytes]:
|
|
|
with image_path.open("rb") as image:
|
|
|
while chunk := image.read(1024 * 1024):
|
|
|
yield chunk
|
|
|
|
|
|
self._upload_volume(conn, volume, read_chunks(), image_size)
|
|
|
self._refresh_directory_pool(pool)
|
|
|
uploaded = pool.storageVolLookupByName(volume_name)
|
|
|
uploaded_xml = ET.fromstring(uploaded.XMLDesc(0))
|
|
|
uploaded_format = uploaded_xml.find("./target/format")
|
|
|
if (uploaded_format is None or uploaded_format.get("type") != "qcow2"
|
|
|
or int(uploaded.info()[1]) != virtual_size):
|
|
|
raise VMError("宿主机模板卷的格式或容量与原镜像不一致", 502)
|
|
|
return {"volume_name": volume_name, "virtual_size": virtual_size,
|
|
|
"image_bytes": image_size}
|
|
|
except Exception as exc:
|
|
|
try:
|
|
|
volume.delete(0)
|
|
|
except Exception:
|
|
|
LOG.exception("Template upload failed and volume %s remains", volume_name)
|
|
|
raise VMError(
|
|
|
f"模板导入失败且宿主机卷 {volume_name} 未能清理,请人工检查", 502,
|
|
|
) from exc
|
|
|
if isinstance(exc, VMError):
|
|
|
raise
|
|
|
LOG.warning("Template upload failed for %s: %s", volume_name, exc)
|
|
|
raise VMError("模板镜像上传到宿主机失败", 502) from exc
|
|
|
|
|
|
@staticmethod
|
|
|
def _check_rescue_volume_name(volume_name: str) -> None:
|
|
|
if not isinstance(volume_name, str) or not RESCUE_VOLUME_RE.fullmatch(volume_name):
|
|
|
raise VMError("救援镜像卷名称无效")
|
|
|
|
|
|
def _lookup_rescue_volume(self, conn: Any, volume_name: str) -> tuple[Any, Any]:
|
|
|
self._check_rescue_volume_name(volume_name)
|
|
|
pool = self._active_rescue_pool(conn)
|
|
|
self._refresh_directory_pool(pool)
|
|
|
if volume_name not in pool.listVolumes():
|
|
|
raise VMError("救援镜像在宿主机上不存在,请重新上传", 404)
|
|
|
volume = pool.storageVolLookupByName(volume_name)
|
|
|
self._volume_source(pool, volume)
|
|
|
return pool, volume
|
|
|
|
|
|
def _rescue_volume(self, conn: Any, volume_name: str) -> tuple[Any, Any]:
|
|
|
pool, volume = self._lookup_rescue_volume(conn, volume_name)
|
|
|
try:
|
|
|
root = ET.fromstring(volume.XMLDesc(0))
|
|
|
fmt = root.find("./target/format")
|
|
|
size = int(volume.info()[1])
|
|
|
except (ET.ParseError, TypeError, ValueError) as exc:
|
|
|
raise VMError("救援镜像卷信息无效", 502) from exc
|
|
|
if (fmt is None or fmt.get("type") != "raw"
|
|
|
or not MIN_RESCUE_ISO_BYTES <= size <= MAX_RESCUE_ISO_BYTES):
|
|
|
raise VMError("救援镜像必须是有效大小的 raw ISO 卷", 409)
|
|
|
return pool, volume
|
|
|
|
|
|
def import_rescue_iso(self, image_path: Path, volume_name: str) -> dict[str, Any]:
|
|
|
"""Upload one administrator-supplied ISO to the dedicated rescue pool."""
|
|
|
self._check_rescue_volume_name(volume_name)
|
|
|
image_path = Path(image_path)
|
|
|
if image_path.is_symlink() or not image_path.is_file():
|
|
|
raise VMError("救援镜像文件无效")
|
|
|
image_size = image_path.stat().st_size
|
|
|
if not MIN_RESCUE_ISO_BYTES <= image_size <= MAX_RESCUE_ISO_BYTES:
|
|
|
raise VMError("救援 ISO 大小必须在 1 MiB 至 8 GiB 之间", 422)
|
|
|
with image_path.open("rb") as image:
|
|
|
image.seek(32769)
|
|
|
if image.read(5) != b"CD001":
|
|
|
raise VMError("上传的文件不是 ISO 9660 镜像", 422)
|
|
|
with self._connect() as conn:
|
|
|
pool = self._active_rescue_pool(conn)
|
|
|
self._refresh_directory_pool(pool)
|
|
|
if volume_name in pool.listVolumes():
|
|
|
raise VMError("同名救援镜像已存在,不会覆盖", 409)
|
|
|
if image_size > pool.info()[3]:
|
|
|
raise VMError("宿主机救援 ISO 存储池空间不足", 409)
|
|
|
volume = pool.createXML(self._volume_xml_bytes(
|
|
|
volume_name, image_size, "raw"), 0)
|
|
|
if volume is None:
|
|
|
raise VMError("宿主机未能创建救援镜像卷", 502)
|
|
|
try:
|
|
|
def read_chunks() -> Iterator[bytes]:
|
|
|
with image_path.open("rb") as image:
|
|
|
while chunk := image.read(1024 * 1024):
|
|
|
yield chunk
|
|
|
|
|
|
self._upload_volume(conn, volume, read_chunks(), image_size)
|
|
|
self._rescue_volume(conn, volume_name)
|
|
|
return {"volume_name": volume_name, "image_bytes": image_size}
|
|
|
except Exception as exc:
|
|
|
try:
|
|
|
volume.delete(0)
|
|
|
except Exception:
|
|
|
LOG.exception("Rescue ISO upload failed and volume %s remains", volume_name)
|
|
|
raise VMError(
|
|
|
f"救援镜像上传失败且宿主机卷 {volume_name} 未能清理,请人工检查", 502,
|
|
|
) from exc
|
|
|
if isinstance(exc, VMError):
|
|
|
raise
|
|
|
LOG.warning("Rescue ISO upload failed for %s: %s", volume_name, exc)
|
|
|
raise VMError("救援镜像上传到宿主机失败", 502) from exc
|
|
|
|
|
|
def delete_rescue_iso(self, volume_name: str) -> dict[str, Any]:
|
|
|
"""Delete only a panel-named rescue ISO unused by every domain."""
|
|
|
with _CREATE_LOCK, self._connect() as conn:
|
|
|
pool, volume = self._lookup_rescue_volume(conn, volume_name)
|
|
|
if self._other_domain_uses_volume(
|
|
|
conn, "", pool.name(), volume_name, volume.path()):
|
|
|
raise VMError("救援镜像仍被虚拟机引用,不能删除", 409)
|
|
|
volume.delete(0)
|
|
|
return {"volume_name": volume_name, "deleted": True}
|
|
|
|
|
|
@staticmethod
|
|
|
def _rescue_original_hash(original_xml: str) -> str:
|
|
|
if not isinstance(original_xml, str) or not original_xml:
|
|
|
raise VMError("原虚拟机配置备份无效", 409)
|
|
|
return hashlib.sha256(original_xml.encode("utf-8")).hexdigest()
|
|
|
|
|
|
@staticmethod
|
|
|
def _rescue_xml_matches(first: str, second: str) -> bool:
|
|
|
try:
|
|
|
return ET.canonicalize(first) == ET.canonicalize(second)
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("虚拟机配置 XML 无效", 502) from exc
|
|
|
|
|
|
@staticmethod
|
|
|
def _rescue_vm_preflight(domain: Any) -> str:
|
|
|
if not domain.isPersistent():
|
|
|
raise VMError("只有持久化虚拟机可以进入救援模式", 409)
|
|
|
if getattr(domain, "hasManagedSaveImage", lambda _flags: 0)(0):
|
|
|
raise VMError("虚拟机存在托管保存状态,请先处理后再救援", 409)
|
|
|
original_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
try:
|
|
|
root = ET.fromstring(original_xml)
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("虚拟机配置 XML 无效", 502) from exc
|
|
|
if root.find(f"./metadata/{{{PANEL_NS}}}rescue") is not None:
|
|
|
raise VMError("虚拟机已处于救援模式", 409)
|
|
|
if root.find("./devices/disk[@device='disk']") is None:
|
|
|
raise VMError("虚拟机没有系统磁盘,不能进入救援模式", 409)
|
|
|
return original_xml
|
|
|
|
|
|
@staticmethod
|
|
|
def _rescue_xml(original_xml: str, vm_uuid: str, volume_name: str,
|
|
|
source: tuple[str, dict[str, str]], autostart: bool) -> str:
|
|
|
try:
|
|
|
root = ET.fromstring(original_xml)
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("原虚拟机配置备份无效", 409) from exc
|
|
|
if root.findtext("uuid") != vm_uuid:
|
|
|
raise VMError("原虚拟机配置备份与目标虚拟机不匹配", 409)
|
|
|
os_node = root.find("os")
|
|
|
devices = root.find("devices")
|
|
|
if os_node is None or devices is None:
|
|
|
raise VMError("原虚拟机配置缺少启动或设备信息", 409)
|
|
|
disks = [disk for disk in devices.findall("disk")
|
|
|
if disk.get("device") == "disk"]
|
|
|
if not disks:
|
|
|
raise VMError("虚拟机没有系统磁盘,不能进入救援模式", 409)
|
|
|
explicitly_booted = [disk for disk in disks
|
|
|
if (disk.find("boot") is not None
|
|
|
and disk.find("boot").get("order") == "1")]
|
|
|
if len(explicitly_booted) == 1:
|
|
|
primary = explicitly_booted[0]
|
|
|
else:
|
|
|
primary = next((disk for disk in disks
|
|
|
if disk.find("target") is not None
|
|
|
and disk.find("target").get("dev") == "vda"), None)
|
|
|
if primary is None and len(disks) == 1:
|
|
|
primary = disks[0]
|
|
|
if primary is None:
|
|
|
raise VMError("无法确定多磁盘虚拟机的系统盘,请先确认原启动顺序", 409)
|
|
|
# A cloud-init seed ISO often occupies the existing CD-ROM. Booting
|
|
|
# "cdrom" as a class could select the wrong image, so target the new
|
|
|
# device explicitly and keep every original device unchanged in the
|
|
|
# saved XML used on exit.
|
|
|
for boot in list(os_node.findall("boot")):
|
|
|
os_node.remove(boot)
|
|
|
for device in devices:
|
|
|
for boot in list(device.findall("boot")):
|
|
|
device.remove(boot)
|
|
|
ET.SubElement(primary, "boot", order="2")
|
|
|
used_targets = {target.get("dev") for target in devices.findall("./disk/target")}
|
|
|
rescue_target = next((f"sd{chr(code)}" for code in range(ord("a"), ord("z") + 1)
|
|
|
if f"sd{chr(code)}" not in used_targets), None)
|
|
|
if rescue_target is None:
|
|
|
raise VMError("虚拟机没有可用的 SATA 光驱目标位置", 409)
|
|
|
cdrom = ET.SubElement(devices, "disk", type=source[0], device="cdrom")
|
|
|
ET.SubElement(cdrom, "driver", name="qemu", type="raw")
|
|
|
ET.SubElement(cdrom, "source", **source[1])
|
|
|
ET.SubElement(cdrom, "target", dev=rescue_target, bus="sata")
|
|
|
ET.SubElement(cdrom, "readonly")
|
|
|
ET.SubElement(cdrom, "boot", order="1")
|
|
|
metadata = root.find("metadata")
|
|
|
if metadata is None:
|
|
|
metadata = ET.SubElement(root, "metadata")
|
|
|
ET.SubElement(metadata, f"{{{PANEL_NS}}}rescue",
|
|
|
volume=volume_name,
|
|
|
**{"original-sha256": VMService._rescue_original_hash(original_xml),
|
|
|
"original-autostart": "1" if autostart else "0"})
|
|
|
return ET.tostring(root, encoding="unicode")
|
|
|
|
|
|
@staticmethod
|
|
|
def _restore_rescue_autostart(domain: Any, enabled: bool) -> None:
|
|
|
if bool(domain.autostart()) != enabled:
|
|
|
domain.setAutostart(int(enabled))
|
|
|
|
|
|
@staticmethod
|
|
|
def _wait_for_rescue_shutdown(domain: Any, phase: str) -> None:
|
|
|
if not domain.isActive():
|
|
|
return
|
|
|
if phase == "救援启动":
|
|
|
next_step = (
|
|
|
"救援 ISO 已写入下次冷启动配置。请先刷新虚拟机状态;如仍在运行,"
|
|
|
"可在救援页面点击“强制重启进入救援”(强制断电后从救援配置开机),"
|
|
|
"也可以手动“强制断电”后再点“开机”。"
|
|
|
"普通“强制重置”只重置运行中的 QEMU,不会加载新的启动配置;"
|
|
|
"强制断电可能导致未保存数据丢失"
|
|
|
)
|
|
|
else:
|
|
|
next_step = (
|
|
|
"原系统启动配置尚未恢复。请先刷新虚拟机状态;如仍在运行,"
|
|
|
"可通过救援系统的 VNC 控制台手动关机,待显示已关机后重试退出救援"
|
|
|
)
|
|
|
try:
|
|
|
domain.shutdown()
|
|
|
except libvirt.libvirtError as exc:
|
|
|
raise VMError(
|
|
|
f"无法请求虚拟机正常关机,{phase}尚未完成;面板未强制断电。{next_step}",
|
|
|
409,
|
|
|
) from exc
|
|
|
deadline = time.monotonic() + REINSTALL_SHUTDOWN_TIMEOUT
|
|
|
while domain.isActive():
|
|
|
if time.monotonic() >= deadline:
|
|
|
raise VMError(
|
|
|
f"正常关机超时,{phase}尚未完成;虚拟机可能随后自行关机。"
|
|
|
f"面板未强制断电。{next_step}", 409)
|
|
|
time.sleep(REINSTALL_SHUTDOWN_POLL_INTERVAL)
|
|
|
|
|
|
def prepare_rescue_vm(self, vm_id: str, rescue_volume_name: str) -> str:
|
|
|
"""Return inactive XML for durable storage before changing this VM."""
|
|
|
with _CREATE_LOCK, self._connect() as conn:
|
|
|
self._rescue_volume(conn, rescue_volume_name)
|
|
|
return self._rescue_vm_preflight(self._domain(conn, vm_id))
|
|
|
|
|
|
def enter_rescue_vm(self, vm_id: str, rescue_volume_name: str,
|
|
|
original_xml: str, *,
|
|
|
progress: Callable[[str], None] | None = None) -> dict[str, Any]:
|
|
|
"""Stage a durable rescue boot, gracefully stop, then start the ISO."""
|
|
|
def phase(value: str) -> None:
|
|
|
if progress is not None:
|
|
|
progress(value)
|
|
|
|
|
|
with _CREATE_LOCK, self._connect() as conn:
|
|
|
vm_uuid = self._uuid(vm_id)
|
|
|
domain = self._domain(conn, vm_uuid)
|
|
|
current_xml = self._rescue_vm_preflight(domain)
|
|
|
if not self._rescue_xml_matches(current_xml, original_xml):
|
|
|
raise VMError("虚拟机配置已变化,请重新提交救援操作", 409)
|
|
|
pool, volume = self._rescue_volume(conn, rescue_volume_name)
|
|
|
original_autostart = bool(domain.autostart())
|
|
|
rescue_xml = self._rescue_xml(
|
|
|
original_xml, vm_uuid, rescue_volume_name,
|
|
|
self._volume_source(pool, volume), original_autostart)
|
|
|
was_running = bool(domain.isActive())
|
|
|
switched = False
|
|
|
rescue_domain = None
|
|
|
try:
|
|
|
phase("mounting")
|
|
|
rescue_domain = conn.defineXML(rescue_xml)
|
|
|
if rescue_domain is None:
|
|
|
raise VMError("宿主机未能保存救援启动配置", 502)
|
|
|
switched = True
|
|
|
self._restore_rescue_autostart(rescue_domain, original_autostart)
|
|
|
staged_xml = rescue_domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
except Exception as exc:
|
|
|
restored = True
|
|
|
if switched:
|
|
|
try:
|
|
|
restored_domain = conn.defineXML(original_xml)
|
|
|
if restored_domain is None:
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
self._restore_rescue_autostart(restored_domain, original_autostart)
|
|
|
except Exception:
|
|
|
LOG.exception("Could not restore original VM XML for %s", vm_uuid)
|
|
|
restored = False
|
|
|
if not restored:
|
|
|
raise VMError(
|
|
|
"救援 ISO 预挂载失败且原虚拟机持久配置未能恢复;"
|
|
|
"没有强制断电,请检查宿主机", 502,
|
|
|
) from exc
|
|
|
raise VMError("救援 ISO 预挂载失败,原虚拟机未受影响", 502) from exc
|
|
|
|
|
|
phase("shutting_down")
|
|
|
try:
|
|
|
self._wait_for_rescue_shutdown(domain, "救援启动")
|
|
|
except VMError as exc:
|
|
|
if domain.isActive():
|
|
|
vm = self._vm_dict(domain)
|
|
|
if vm["rescue_mode"] and not vm["rescue_staged"]:
|
|
|
# The guest may have shut down and been cold-started
|
|
|
# by an external actor while the request timed out.
|
|
|
return {"vm": vm, "warning":
|
|
|
"救援配置已运行,但宿主机关机状态返回异常;请通过 VNC 确认已进入救援系统"}
|
|
|
# Never discard the persistent rescue boot here: the user
|
|
|
# can safely decide whether to power-cycle manually.
|
|
|
phase("pending_manual_restart")
|
|
|
return {"vm": vm,
|
|
|
"pending_manual_restart": True,
|
|
|
"warning": str(exc)}
|
|
|
# The guest stopped after the error; continue with a cold
|
|
|
# start from the already-staged rescue definition.
|
|
|
latest_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
if not self._rescue_xml_matches(latest_xml, staged_xml):
|
|
|
raise VMError("关机后救援启动配置已变化,救援已停止;请检查虚拟机状态", 409)
|
|
|
try:
|
|
|
phase("starting")
|
|
|
rescue_domain.create()
|
|
|
except Exception as exc:
|
|
|
if switched and rescue_domain is not None:
|
|
|
try:
|
|
|
if rescue_domain.isActive() and self._vm_dict(rescue_domain)["rescue_mode"]:
|
|
|
phase("completed")
|
|
|
return {"vm": self._vm_dict(rescue_domain),
|
|
|
"warning": "救援系统可能已启动,但宿主机启动返回错误;请检查控制台"}
|
|
|
except Exception:
|
|
|
LOG.exception("Could not inspect rescue VM %s after start error", vm_uuid)
|
|
|
restored = True
|
|
|
restart_domain = domain
|
|
|
if switched:
|
|
|
try:
|
|
|
restored_domain = conn.defineXML(original_xml)
|
|
|
if restored_domain is None:
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
self._restore_rescue_autostart(restored_domain, original_autostart)
|
|
|
restart_domain = restored_domain
|
|
|
except Exception:
|
|
|
LOG.exception("Could not restore original VM XML for %s", vm_uuid)
|
|
|
restored = False
|
|
|
if restored and was_running:
|
|
|
try:
|
|
|
restart_domain.create()
|
|
|
except Exception:
|
|
|
LOG.exception("Could not restart original VM %s after rescue failure", vm_uuid)
|
|
|
restored = False
|
|
|
if not restored:
|
|
|
raise VMError(
|
|
|
"救援启动失败且原虚拟机未能完全恢复;没有强制断电,请检查宿主机", 502,
|
|
|
) from exc
|
|
|
raise VMError("救援启动失败,已恢复原虚拟机配置及运行状态", 502) from exc
|
|
|
phase("completed")
|
|
|
return {"vm": self._vm_dict(rescue_domain)}
|
|
|
|
|
|
def force_start_staged_rescue_vm(self, vm_id: str,
|
|
|
original_xml: str) -> dict[str, Any]:
|
|
|
"""Cold power-cycle only a verified, pre-staged rescue VM.
|
|
|
|
|
|
libvirt's reset() acts on the live QEMU process and cannot load a
|
|
|
changed inactive boot definition. destroy()+create() deliberately
|
|
|
stops that process and starts a new one from the persistent XML.
|
|
|
"""
|
|
|
with _CREATE_LOCK, self._connect() as conn:
|
|
|
vm_uuid = self._uuid(vm_id)
|
|
|
domain = self._domain(conn, vm_uuid)
|
|
|
if not domain.isPersistent():
|
|
|
raise VMError("救援虚拟机不是持久化定义,不能强制重启", 409)
|
|
|
try:
|
|
|
original_root = ET.fromstring(original_xml)
|
|
|
except (TypeError, ET.ParseError) as exc:
|
|
|
raise VMError("原虚拟机配置备份无效", 409) from exc
|
|
|
if (original_root.findtext("uuid") != vm_uuid
|
|
|
or original_root.find(f"./metadata/{{{PANEL_NS}}}rescue") is not None):
|
|
|
raise VMError("原虚拟机配置备份与目标虚拟机不匹配", 409)
|
|
|
try:
|
|
|
staged_root = ET.fromstring(
|
|
|
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE))
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("救援虚拟机持久配置 XML 无效", 502) from exc
|
|
|
marker = staged_root.find(f"./metadata/{{{PANEL_NS}}}rescue")
|
|
|
if (marker is None or marker.get("original-sha256") !=
|
|
|
self._rescue_original_hash(original_xml)
|
|
|
or not RESCUE_VOLUME_RE.fullmatch(marker.get("volume", ""))
|
|
|
or marker.get("original-autostart") not in {"0", "1"}):
|
|
|
raise VMError("救援预挂载记录与原配置备份不匹配,已拒绝强制重启", 409)
|
|
|
if domain.isActive() and self._vm_xml(domain).find(
|
|
|
f"./metadata/{{{PANEL_NS}}}rescue") is not None:
|
|
|
raise VMError("虚拟机已从救援配置运行,无需再次强制重启", 409)
|
|
|
pool, volume = self._rescue_volume(conn, marker.get("volume"))
|
|
|
source_type, source_attrs = self._volume_source(pool, volume)
|
|
|
if not any(
|
|
|
disk.get("type") == source_type
|
|
|
and disk.find("source") is not None
|
|
|
and all(disk.find("source").get(key) == value
|
|
|
for key, value in source_attrs.items())
|
|
|
and disk.find("boot") is not None
|
|
|
and disk.find("boot").get("order") == "1"
|
|
|
for disk in staged_root.findall("./devices/disk[@device='cdrom']")):
|
|
|
raise VMError("救援 ISO 启动设备与预挂载记录不匹配,已拒绝强制重启", 409)
|
|
|
if domain.isActive():
|
|
|
try:
|
|
|
domain.destroy()
|
|
|
except libvirt.libvirtError as exc:
|
|
|
if domain.isActive():
|
|
|
raise VMError("强制断电失败;救援配置已保留,请检查虚拟机状态", 502) from exc
|
|
|
try:
|
|
|
domain.create()
|
|
|
except libvirt.libvirtError as exc:
|
|
|
try:
|
|
|
vm = self._vm_dict(domain)
|
|
|
if (vm["state"] == "running" and vm["rescue_mode"]
|
|
|
and not vm["rescue_staged"]):
|
|
|
return {"vm": vm,
|
|
|
"warning": "救援系统可能已启动,但宿主机返回错误;请检查控制台"}
|
|
|
except Exception:
|
|
|
LOG.exception("Could not inspect staged rescue VM %s after start error", vm_uuid)
|
|
|
raise VMError(
|
|
|
"救援系统强制重启后未能启动;救援 ISO 配置已保留,"
|
|
|
"可重试开机或取消救援。强制断电可能导致未保存数据丢失", 502,
|
|
|
) from exc
|
|
|
vm = self._vm_dict(domain)
|
|
|
if not domain.isActive() or not vm["rescue_mode"] or vm["rescue_staged"]:
|
|
|
raise VMError(
|
|
|
"已尝试强制启动,但救援系统未进入运行状态;"
|
|
|
"救援 ISO 配置已保留,请检查控制台或重试开机", 502,
|
|
|
)
|
|
|
return {"vm": vm}
|
|
|
|
|
|
def exit_rescue_vm(self, vm_id: str, original_xml: str, *,
|
|
|
progress: Callable[[str], None] | None = None) -> dict[str, Any]:
|
|
|
"""Gracefully stop rescue mode, restore original XML, and start OS."""
|
|
|
def phase(value: str) -> None:
|
|
|
if progress is not None:
|
|
|
progress(value)
|
|
|
|
|
|
with _CREATE_LOCK, self._connect() as conn:
|
|
|
vm_uuid = self._uuid(vm_id)
|
|
|
domain = self._domain(conn, vm_uuid)
|
|
|
if not domain.isPersistent():
|
|
|
raise VMError("救援虚拟机不是持久化定义,无法恢复", 409)
|
|
|
try:
|
|
|
original_root = ET.fromstring(original_xml)
|
|
|
except (TypeError, ET.ParseError) as exc:
|
|
|
raise VMError("原虚拟机配置备份无效", 409) from exc
|
|
|
if (original_root.findtext("uuid") != vm_uuid
|
|
|
or original_root.find(f"./metadata/{{{PANEL_NS}}}rescue") is not None):
|
|
|
raise VMError("原虚拟机配置备份与目标虚拟机不匹配", 409)
|
|
|
rescue_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
try:
|
|
|
rescue_root = ET.fromstring(rescue_xml)
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("救援虚拟机配置 XML 无效", 502) from exc
|
|
|
marker = rescue_root.find(f"./metadata/{{{PANEL_NS}}}rescue")
|
|
|
if (marker is None or marker.get("original-sha256") !=
|
|
|
self._rescue_original_hash(original_xml)
|
|
|
or not RESCUE_VOLUME_RE.fullmatch(marker.get("volume", ""))
|
|
|
or marker.get("original-autostart") not in {"0", "1"}):
|
|
|
raise VMError("虚拟机救援标记与原配置备份不匹配,已停止恢复", 409)
|
|
|
staged_running = False
|
|
|
if domain.isActive():
|
|
|
live_marker = self._vm_xml(domain).find(
|
|
|
f"./metadata/{{{PANEL_NS}}}rescue")
|
|
|
staged_running = live_marker is None
|
|
|
if (live_marker is not None
|
|
|
and (live_marker.get("original-sha256") !=
|
|
|
marker.get("original-sha256")
|
|
|
or live_marker.get("volume") != marker.get("volume"))):
|
|
|
raise VMError("虚拟机当前并未从已记录的救援配置运行,已停止恢复", 409)
|
|
|
original_autostart = marker.get("original-autostart") == "1"
|
|
|
rescue_autostart = bool(domain.autostart())
|
|
|
if staged_running:
|
|
|
# Rescue was saved for the next cold boot, but the original
|
|
|
# guest is still running. Cancelling the staged boot must not
|
|
|
# shut down or restart that guest.
|
|
|
switched = False
|
|
|
try:
|
|
|
phase("restoring")
|
|
|
normal_domain = conn.defineXML(original_xml)
|
|
|
if normal_domain is None:
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
switched = True
|
|
|
self._restore_rescue_autostart(normal_domain, original_autostart)
|
|
|
except Exception as exc:
|
|
|
if switched:
|
|
|
try:
|
|
|
rescue_domain = conn.defineXML(rescue_xml)
|
|
|
if rescue_domain is None:
|
|
|
raise VMError("宿主机未能恢复救援启动配置", 502)
|
|
|
self._restore_rescue_autostart(
|
|
|
rescue_domain, rescue_autostart)
|
|
|
except Exception:
|
|
|
LOG.exception("Could not restore staged rescue XML for %s", vm_uuid)
|
|
|
raise VMError(
|
|
|
"取消救援预挂载失败,原虚拟机仍在运行,但持久配置状态不确定;"
|
|
|
"请检查宿主机", 502,
|
|
|
) from exc
|
|
|
raise VMError("取消救援预挂载失败,原虚拟机仍在运行", 502) from exc
|
|
|
phase("completed")
|
|
|
return {"vm": self._vm_dict(normal_domain)}
|
|
|
was_running = bool(domain.isActive())
|
|
|
phase("shutting_down")
|
|
|
self._wait_for_rescue_shutdown(domain, "退出救援")
|
|
|
latest_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
if not self._rescue_xml_matches(latest_xml, rescue_xml):
|
|
|
raise VMError("关机后救援配置已变化,退出救援已停止;请检查虚拟机状态", 409)
|
|
|
switched = False
|
|
|
normal_domain = None
|
|
|
try:
|
|
|
phase("starting")
|
|
|
normal_domain = conn.defineXML(original_xml)
|
|
|
if normal_domain is None:
|
|
|
raise VMError("宿主机未能恢复原虚拟机配置", 502)
|
|
|
switched = True
|
|
|
self._restore_rescue_autostart(normal_domain, original_autostart)
|
|
|
normal_domain.create()
|
|
|
except Exception as exc:
|
|
|
if switched and normal_domain is not None:
|
|
|
try:
|
|
|
if normal_domain.isActive() and not self._vm_dict(normal_domain)["rescue_mode"]:
|
|
|
phase("completed")
|
|
|
return {"vm": self._vm_dict(normal_domain),
|
|
|
"warning": "原系统可能已启动,但宿主机启动返回错误;请检查控制台"}
|
|
|
except Exception:
|
|
|
LOG.exception("Could not inspect VM %s after rescue exit error", vm_uuid)
|
|
|
restored = True
|
|
|
restart_domain = domain
|
|
|
if switched:
|
|
|
try:
|
|
|
restored_domain = conn.defineXML(rescue_xml)
|
|
|
if restored_domain is None:
|
|
|
raise VMError("宿主机未能恢复救援启动配置", 502)
|
|
|
self._restore_rescue_autostart(restored_domain, rescue_autostart)
|
|
|
restart_domain = restored_domain
|
|
|
except Exception:
|
|
|
LOG.exception("Could not restore rescue VM XML for %s", vm_uuid)
|
|
|
restored = False
|
|
|
if restored and was_running:
|
|
|
try:
|
|
|
restart_domain.create()
|
|
|
except Exception:
|
|
|
LOG.exception("Could not restart rescue VM %s after exit failure", vm_uuid)
|
|
|
restored = False
|
|
|
if not restored:
|
|
|
raise VMError(
|
|
|
"退出救援失败且救援虚拟机未能完全恢复;没有强制断电,请检查宿主机", 502,
|
|
|
) from exc
|
|
|
raise VMError("退出救援失败,已恢复救援启动配置及运行状态", 502) from exc
|
|
|
phase("completed")
|
|
|
vm = self._vm_dict(normal_domain)
|
|
|
if vm["rescue_mode"]:
|
|
|
raise VMError("原系统已启动但仍显示救援模式,请检查虚拟机配置", 502)
|
|
|
return {"vm": vm}
|
|
|
|
|
|
@staticmethod
|
|
|
def _mac_for_new_vm(conn: Any) -> str:
|
|
|
used = {
|
|
|
(node.get("address") or "").lower()
|
|
|
for domain in conn.listAllDomains(0)
|
|
|
for node in VMService._vm_xml(domain).findall("./devices/interface/mac")
|
|
|
}
|
|
|
for _ in range(32):
|
|
|
mac = "52:54:00:" + ":".join(f"{byte:02x}" for byte in secrets.token_bytes(3))
|
|
|
if mac not in used:
|
|
|
return mac
|
|
|
raise VMError("无法生成不重复的虚拟网卡 MAC", 503)
|
|
|
|
|
|
def _cloud_domain_xml(self, spec: dict[str, Any], vm_uuid: str,
|
|
|
disk_name: str, disk_source: tuple[str, dict[str, str]],
|
|
|
seed_name: str, seed_source: tuple[str, dict[str, str]],
|
|
|
mac: str, public_ip_cidr: str) -> str:
|
|
|
root = ET.fromstring(self._domain_xml(
|
|
|
spec, vm_uuid, disk_name, disk_source, ("file", {}),
|
|
|
install_phase=False))
|
|
|
interface = root.find("./devices/interface")
|
|
|
if interface is None:
|
|
|
raise VMError("无法生成虚拟网卡配置", 500)
|
|
|
interface.insert(0, ET.Element("mac", address=mac))
|
|
|
cdrom = root.find("./devices/disk[@device='cdrom']")
|
|
|
if cdrom is None:
|
|
|
raise VMError("无法生成 cloud-init 配置盘", 500)
|
|
|
cdrom.set("type", seed_source[0])
|
|
|
target = cdrom.find("target")
|
|
|
source = ET.Element("source", **seed_source[1])
|
|
|
if target is not None:
|
|
|
cdrom.insert(list(cdrom).index(target), source)
|
|
|
else:
|
|
|
cdrom.append(source)
|
|
|
managed = root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
if managed is None:
|
|
|
raise VMError("无法生成面板元数据", 500)
|
|
|
managed.set("seed-volume", seed_name)
|
|
|
managed.set("public-ip-cidr", public_ip_cidr)
|
|
|
managed.set("os-source", "cloud_template")
|
|
|
if spec.get("os_name") is not None:
|
|
|
managed.set("os-name", spec["os_name"])
|
|
|
return ET.tostring(root, encoding="unicode")
|
|
|
|
|
|
def create_cloud_vm(self, spec: dict[str, Any], template_volume_name: str) -> dict[str, Any]:
|
|
|
"""Clone a trusted cloud image and supply static public networking."""
|
|
|
validated = self._validate_common_create(spec)
|
|
|
validated["created_at"] = datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
|
|
|
validated["os_name"] = self._validated_os_name(spec.get("os_name"))
|
|
|
requested_uuid = spec.get("vm_uuid")
|
|
|
if requested_uuid is not None:
|
|
|
try:
|
|
|
requested_uuid = str(uuidlib.UUID(requested_uuid))
|
|
|
except (TypeError, ValueError, AttributeError) as exc:
|
|
|
raise VMError("虚拟机 UUID 无效") from exc
|
|
|
if self.bridge_name is None:
|
|
|
raise VMError("公网模板创建需要先在宿主机设置 Linux 网桥,例如 bridge:br0", 409)
|
|
|
if not TEMPLATE_VOLUME_RE.fullmatch(template_volume_name):
|
|
|
raise VMError("只能使用面板导入的云镜像模板")
|
|
|
try:
|
|
|
network = validate_cloud_network(
|
|
|
spec.get("public_ip_cidr"), spec.get("gateway"),
|
|
|
spec.get("dns_servers"), spec.get("guest_ssh_public_key", ""),
|
|
|
spec.get("root_password_hash"))
|
|
|
except ValueError as exc:
|
|
|
raise VMError(str(exc)) from exc
|
|
|
|
|
|
with _CREATE_LOCK, self._connect() as conn:
|
|
|
domains = conn.listAllDomains(0)
|
|
|
if any(domain.name() == validated["name"] for domain in domains):
|
|
|
raise VMError("虚拟机名称已存在", 409)
|
|
|
for domain in domains:
|
|
|
managed = self._vm_xml(domain).find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
if managed is not None and managed.get("public-ip-cidr", "").split("/")[0] == \
|
|
|
network["public_ip_cidr"].split("/")[0]:
|
|
|
raise VMError("该公网 IP 已分配给面板中的另一台虚拟机", 409)
|
|
|
if conn.getInfo()[0] != "x86_64":
|
|
|
raise VMError("当前云镜像模板仅支持 x86_64 宿主机", 422)
|
|
|
pool = self._active_pool(conn, self.storage_pool)
|
|
|
self._refresh_directory_pool(pool)
|
|
|
if template_volume_name not in pool.listVolumes():
|
|
|
raise VMError("模板卷在宿主机上不存在,请重新导入", 404)
|
|
|
template = pool.storageVolLookupByName(template_volume_name)
|
|
|
template_xml = ET.fromstring(template.XMLDesc(0))
|
|
|
if template_xml.find("./target/format") is None or \
|
|
|
template_xml.find("./target/format").get("type") != "qcow2":
|
|
|
raise VMError("模板卷不是 qcow2 格式", 409)
|
|
|
template_capacity = int(template.info()[1])
|
|
|
target_capacity = validated["disk_gib"] * GIB
|
|
|
if target_capacity < template_capacity:
|
|
|
raise VMError("虚拟磁盘不能小于云镜像模板的原始容量", 409)
|
|
|
if target_capacity > pool.info()[3]:
|
|
|
raise VMError("存储池剩余空间不足", 409)
|
|
|
vm_uuid = requested_uuid or str(uuidlib.uuid4())
|
|
|
disk_name = f"{validated['name']}.qcow2"
|
|
|
seed_name = f"panel-seed-{vm_uuid}.iso"
|
|
|
if disk_name in pool.listVolumes() or seed_name in pool.listVolumes():
|
|
|
raise VMError("同名虚拟磁盘或配置盘已存在,不会覆盖", 409)
|
|
|
mac = self._mac_for_new_vm(conn)
|
|
|
try:
|
|
|
seed_data = build_seed_iso(vm_uuid, validated["name"], mac, network)
|
|
|
except ImportError as exc:
|
|
|
raise VMError("面板缺少 pycdlib,需安装 requirements.txt 中的依赖", 503) from exc
|
|
|
created: list[tuple[Any, str]] = []
|
|
|
try:
|
|
|
disk = pool.createXMLFrom(self._volume_xml_bytes(
|
|
|
disk_name, template_capacity, "qcow2"), template, 0)
|
|
|
if disk is None:
|
|
|
raise VMError("宿主机未能克隆云镜像模板", 502)
|
|
|
created.append((disk, disk_name))
|
|
|
if target_capacity > template_capacity:
|
|
|
disk.resize(target_capacity, 0)
|
|
|
seed = pool.createXML(self._volume_xml_bytes(
|
|
|
seed_name, len(seed_data), "raw"), 0)
|
|
|
if seed is None:
|
|
|
raise VMError("宿主机未能创建 cloud-init 配置盘", 502)
|
|
|
created.append((seed, seed_name))
|
|
|
self._upload_volume(conn, seed, iter((seed_data,)), len(seed_data))
|
|
|
disk_source = self._volume_source(pool, disk)
|
|
|
seed_source = self._volume_source(pool, seed)
|
|
|
domain_xml = self._cloud_domain_xml(
|
|
|
validated, vm_uuid, disk_name, disk_source,
|
|
|
seed_name, seed_source, mac, network["public_ip_cidr"])
|
|
|
domain = conn.defineXML(domain_xml)
|
|
|
if domain is None:
|
|
|
raise VMError("宿主机未能定义云镜像虚拟机", 502)
|
|
|
self._enable_vm_autostart(domain)
|
|
|
domain.create()
|
|
|
return self._vm_dict(domain)
|
|
|
except Exception as exc:
|
|
|
clean = True
|
|
|
for volume, volume_name in reversed(created):
|
|
|
if not self._rollback_new_vm(conn, vm_uuid, validated["name"],
|
|
|
pool, volume, volume_name):
|
|
|
clean = False
|
|
|
break
|
|
|
if not clean:
|
|
|
raise VMError(
|
|
|
f"云虚拟机创建失败且回滚不完整;已保留相关文件,请检查 {validated['name']}",
|
|
|
502,
|
|
|
) from exc
|
|
|
if isinstance(exc, VMError):
|
|
|
raise
|
|
|
LOG.warning("Could not create cloud VM %s: %s", validated["name"], exc)
|
|
|
raise VMError("无法创建云镜像虚拟机,请检查网桥、磁盘权限和宿主机日志", 502) from exc
|
|
|
|
|
|
def reinstall_cloud_vm(
|
|
|
self,
|
|
|
vm_id: str,
|
|
|
template_volume_name: str,
|
|
|
*,
|
|
|
os_name: str | None = None,
|
|
|
ssh_public_key: str = "",
|
|
|
root_password_hash: str | None = None,
|
|
|
progress: Callable[[str], None] | None = None,
|
|
|
) -> dict[str, Any]:
|
|
|
"""Replace one panel cloud VM's disk after a graceful shutdown.
|
|
|
|
|
|
The old disk remains untouched until the replacement domain has
|
|
|
started. Failure before that point restores the old definition and
|
|
|
retains the old disk. A cleanup failure after start is a warning, not
|
|
|
a rollback: some old volumes may already have been removed.
|
|
|
"""
|
|
|
if not TEMPLATE_VOLUME_RE.fullmatch(template_volume_name):
|
|
|
raise VMError("只能使用面板导入的云镜像模板")
|
|
|
os_name = self._validated_os_name(os_name)
|
|
|
if self.bridge_name is None:
|
|
|
raise VMError("公网模板重装需要原 Linux 网桥配置", 409)
|
|
|
|
|
|
def phase(value: str) -> None:
|
|
|
if progress is not None:
|
|
|
progress(value)
|
|
|
|
|
|
with _CREATE_LOCK, self._connect() as conn:
|
|
|
domain = self._domain(conn, vm_id)
|
|
|
if not domain.isPersistent():
|
|
|
raise VMError("只能重装持久化虚拟机", 409)
|
|
|
if getattr(domain, "hasManagedSaveImage", lambda _flags: 0)(0):
|
|
|
raise VMError("虚拟机存在托管保存状态,请先处理后再重装", 409)
|
|
|
if domain.snapshotNum(0) > 0:
|
|
|
raise VMError("虚拟机存在快照,请先处理后再重装", 409)
|
|
|
original_root = self._vm_xml(domain)
|
|
|
managed = original_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
if (managed is None or managed.get("version") != "2"
|
|
|
or not managed.get("seed-volume")
|
|
|
or not managed.get("public-ip-cidr")):
|
|
|
raise VMError("只有面板创建的公网云镜像虚拟机可以自动重装", 409)
|
|
|
disks = original_root.findall("./devices/disk[@device='disk']")
|
|
|
cdroms = original_root.findall("./devices/disk[@device='cdrom']")
|
|
|
interfaces = original_root.findall("./devices/interface")
|
|
|
if len(disks) != 1 or len(cdroms) != 1 or len(interfaces) != 1:
|
|
|
raise VMError("虚拟机磁盘或网卡配置已变化,不能安全自动重装", 409)
|
|
|
interface = interfaces[0]
|
|
|
source = interface.find("source")
|
|
|
mac_node = interface.find("mac")
|
|
|
if (interface.get("type") != "bridge" or source is None
|
|
|
or source.get("bridge") != self.bridge_name
|
|
|
or mac_node is None or not mac_node.get("address")):
|
|
|
raise VMError("虚拟机网桥或 MAC 已变化,不能自动沿用原网络", 409)
|
|
|
mac = mac_node.get("address").lower()
|
|
|
old_disk = self._owned_volume(conn, domain)
|
|
|
old_seed = self._owned_seed_volume(conn, domain)
|
|
|
if old_seed is None:
|
|
|
raise VMError("找不到原 cloud-init 配置盘", 409)
|
|
|
try:
|
|
|
original_network = read_seed_network(self._download_seed_volume(conn, old_seed))
|
|
|
network = validate_cloud_network(
|
|
|
original_network["public_ip_cidr"], original_network["gateway"],
|
|
|
original_network["dns_servers"], ssh_public_key, root_password_hash,
|
|
|
)
|
|
|
except (ImportError, ValueError) as exc:
|
|
|
raise VMError("无法校验原 cloud-init 网络配置,已停止重装", 409) from exc
|
|
|
if (original_network["mac"] != mac
|
|
|
or network["public_ip_cidr"] != managed.get("public-ip-cidr")):
|
|
|
raise VMError("原配置盘的公网 IP 或 MAC 与虚拟机不一致,已停止重装", 409)
|
|
|
|
|
|
pool = self._active_pool(conn, self.storage_pool)
|
|
|
self._refresh_directory_pool(pool)
|
|
|
if template_volume_name not in pool.listVolumes():
|
|
|
raise VMError("模板卷在宿主机上不存在,请重新导入", 404)
|
|
|
template = pool.storageVolLookupByName(template_volume_name)
|
|
|
template_format = ET.fromstring(template.XMLDesc(0)).find("./target/format")
|
|
|
if template_format is None or template_format.get("type") != "qcow2":
|
|
|
raise VMError("模板卷不是 qcow2 格式", 409)
|
|
|
target_capacity = int(old_disk.info()[1])
|
|
|
template_capacity = int(template.info()[1])
|
|
|
if template_capacity > target_capacity:
|
|
|
raise VMError("所选模板容量超过原系统盘,无法保留原磁盘大小", 409)
|
|
|
if target_capacity > pool.info()[3]:
|
|
|
raise VMError("存储池空间不足,无法创建新系统盘", 409)
|
|
|
|
|
|
phase("shutting_down")
|
|
|
if domain.isActive():
|
|
|
domain.shutdown()
|
|
|
deadline = time.monotonic() + REINSTALL_SHUTDOWN_TIMEOUT
|
|
|
while domain.isActive():
|
|
|
if time.monotonic() >= deadline:
|
|
|
raise VMError("正常关机超时,重装已停止;旧系统盘未改变", 409)
|
|
|
time.sleep(REINSTALL_SHUTDOWN_POLL_INTERVAL)
|
|
|
|
|
|
# Inspect the now-inactive definition again, so a concurrent
|
|
|
# external edit cannot quietly alter the storage or network.
|
|
|
old_xml = domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
inactive_root = ET.fromstring(old_xml)
|
|
|
inactive_managed = inactive_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
inactive_interface = inactive_root.find("./devices/interface")
|
|
|
inactive_mac = (inactive_interface.find("mac") if inactive_interface is not None
|
|
|
else None)
|
|
|
inactive_source = (inactive_interface.find("source")
|
|
|
if inactive_interface is not None else None)
|
|
|
if (inactive_managed is None
|
|
|
or inactive_managed.get("disk-volume") != old_disk.name()
|
|
|
or inactive_managed.get("seed-volume") != old_seed.name()
|
|
|
or inactive_managed.get("public-ip-cidr") != network["public_ip_cidr"]
|
|
|
or inactive_mac is None or (inactive_mac.get("address") or "").lower() != mac
|
|
|
or inactive_source is None
|
|
|
or inactive_source.get("bridge") != self.bridge_name):
|
|
|
raise VMError("关机后虚拟机配置已变化,重装已停止", 409)
|
|
|
if self._owned_volume(conn, domain).name() != old_disk.name() or \
|
|
|
self._owned_seed_volume(conn, domain).name() != old_seed.name():
|
|
|
raise VMError("关机后存储卷已变化,重装已停止", 409)
|
|
|
|
|
|
disk_name = f"panel-r-{uuidlib.uuid4().hex}.qcow2"
|
|
|
seed_uuid = str(uuidlib.uuid4())
|
|
|
seed_name = f"panel-seed-{seed_uuid}.iso"
|
|
|
if disk_name in pool.listVolumes() or seed_name in pool.listVolumes():
|
|
|
raise VMError("新系统盘或配置盘名称冲突,已停止重装", 409)
|
|
|
try:
|
|
|
seed_data = build_seed_iso(seed_uuid, domain.name(), mac, network)
|
|
|
except ImportError as exc:
|
|
|
raise VMError("面板缺少 pycdlib,需安装 requirements.txt 中的依赖", 503) from exc
|
|
|
created: list[Any] = []
|
|
|
switched = False
|
|
|
phase("cloning")
|
|
|
try:
|
|
|
disk = pool.createXMLFrom(self._volume_xml_bytes(
|
|
|
disk_name, template_capacity, "qcow2"), template, 0)
|
|
|
if disk is None:
|
|
|
raise VMError("宿主机未能克隆新系统盘", 502)
|
|
|
created.append(disk)
|
|
|
if target_capacity > template_capacity:
|
|
|
disk.resize(target_capacity, 0)
|
|
|
seed = pool.createXML(self._volume_xml_bytes(
|
|
|
seed_name, len(seed_data), "raw"), 0)
|
|
|
if seed is None:
|
|
|
raise VMError("宿主机未能创建新配置盘", 502)
|
|
|
created.append(seed)
|
|
|
self._upload_volume(conn, seed, iter((seed_data,)), len(seed_data))
|
|
|
|
|
|
new_root = ET.fromstring(old_xml)
|
|
|
for device, volume in (("disk", disk), ("cdrom", seed)):
|
|
|
node = new_root.find(f"./devices/disk[@device='{device}']")
|
|
|
if node is None:
|
|
|
raise VMError("虚拟机磁盘配置已变化,无法切换", 409)
|
|
|
source_type, source_attrs = self._volume_source(pool, volume)
|
|
|
node.set("type", source_type)
|
|
|
old_source = node.find("source")
|
|
|
if old_source is None:
|
|
|
raise VMError("虚拟机磁盘来源已变化,无法切换", 409)
|
|
|
source_index = list(node).index(old_source)
|
|
|
node.remove(old_source)
|
|
|
node.insert(source_index, ET.Element("source", **source_attrs))
|
|
|
new_managed = new_root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
if new_managed is None:
|
|
|
raise VMError("虚拟机面板元数据已变化,无法切换", 409)
|
|
|
new_managed.set("disk-volume", disk_name)
|
|
|
new_managed.set("seed-volume", seed_name)
|
|
|
# A different reinstall template invalidates the old label.
|
|
|
new_managed.attrib.pop("os-source", None)
|
|
|
new_managed.attrib.pop("os-name", None)
|
|
|
new_managed.attrib.pop("install-iso", None)
|
|
|
new_managed.set("os-source", "cloud_template")
|
|
|
if os_name is not None:
|
|
|
new_managed.set("os-name", os_name)
|
|
|
phase("starting")
|
|
|
new_domain = conn.defineXML(ET.tostring(new_root, encoding="unicode"))
|
|
|
if new_domain is None:
|
|
|
raise VMError("宿主机未能切换虚拟机定义", 502)
|
|
|
switched = True
|
|
|
new_domain.create()
|
|
|
except Exception as exc:
|
|
|
if switched:
|
|
|
try:
|
|
|
if new_domain.isActive():
|
|
|
live_managed = self._vm_xml(new_domain).find(
|
|
|
f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
if live_managed is not None and \
|
|
|
live_managed.get("disk-volume") == disk_name:
|
|
|
# A transport error can occur after QEMU has
|
|
|
# actually started. Keep both generations and
|
|
|
# report the new credential instead of
|
|
|
# restoring only the inactive definition.
|
|
|
LOG.error("VM %s may have started on replacement disk after error",
|
|
|
vm_id, exc_info=True)
|
|
|
phase("completed")
|
|
|
return {
|
|
|
"vm": {"uuid": vm_id,
|
|
|
"name": inactive_root.findtext("name") or vm_id,
|
|
|
"state": "running"},
|
|
|
"warning": "新系统可能已启动,但启动返回错误;旧系统盘已保留,"
|
|
|
"请检查虚拟机状态和存储卷后手工清理",
|
|
|
}
|
|
|
except Exception:
|
|
|
LOG.exception("Could not inspect VM %s after start error", vm_id)
|
|
|
restored = True
|
|
|
if switched:
|
|
|
try:
|
|
|
conn.defineXML(old_xml)
|
|
|
except Exception:
|
|
|
LOG.exception("Could not restore original definition for %s", vm_id)
|
|
|
restored = False
|
|
|
if restored:
|
|
|
for volume in reversed(created):
|
|
|
try:
|
|
|
if self._other_domain_uses_volume(
|
|
|
conn, "", pool.name(), volume.name(), volume.path()):
|
|
|
restored = False
|
|
|
continue
|
|
|
volume.delete(0)
|
|
|
except Exception:
|
|
|
LOG.exception("Could not remove replacement volume %s", volume.name())
|
|
|
restored = False
|
|
|
if not restored:
|
|
|
raise VMError(
|
|
|
"重装失败且自动回滚不完整;旧系统盘已保留,请检查虚拟机与新建存储卷",
|
|
|
502,
|
|
|
) from exc
|
|
|
if isinstance(exc, VMError):
|
|
|
raise
|
|
|
LOG.warning("Could not reinstall VM %s: %s", vm_id, exc)
|
|
|
raise VMError("重装失败,已恢复原虚拟机定义及旧系统盘", 502) from exc
|
|
|
|
|
|
# QEMU started with the new disk. This does not prove the guest
|
|
|
# booted or has working SSH; the operator explicitly selected
|
|
|
# automatic removal of the old disk after this point.
|
|
|
phase("cleaning_up")
|
|
|
warnings = []
|
|
|
for volume in (old_seed, old_disk):
|
|
|
try:
|
|
|
if self._other_domain_uses_volume(
|
|
|
conn, "", pool.name(), volume.name(), volume.path()):
|
|
|
warnings.append(f"旧存储卷 {volume.name()} 仍被引用,未删除")
|
|
|
continue
|
|
|
volume.delete(0)
|
|
|
except Exception:
|
|
|
LOG.exception("Reinstalled VM %s, but old volume %s remains", vm_id,
|
|
|
volume.name())
|
|
|
warnings.append(f"旧存储卷 {volume.name()} 删除失败,请检查宿主机")
|
|
|
try:
|
|
|
vm_state = self._vm_dict(new_domain)
|
|
|
except Exception:
|
|
|
LOG.exception("VM %s started after reinstall, but its details could not be read", vm_id)
|
|
|
vm_state = {"uuid": vm_id, "name": inactive_root.findtext("name") or vm_id,
|
|
|
"state": "running"}
|
|
|
warnings.append("新系统已启动,但面板未能读取完整虚拟机状态,请检查宿主机")
|
|
|
result: dict[str, Any] = {"vm": vm_state, "credential_generation": seed_name}
|
|
|
if warnings:
|
|
|
result["warning"] = ";".join(warnings)
|
|
|
phase("completed")
|
|
|
return result
|
|
|
|
|
|
def create_vm(self, spec: dict[str, Any]) -> dict[str, Any]:
|
|
|
validated = self._validate_create(spec)
|
|
|
validated["created_at"] = datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
|
|
|
requested_uuid = spec.get("vm_uuid")
|
|
|
if requested_uuid is not None:
|
|
|
try:
|
|
|
requested_uuid = str(uuidlib.UUID(requested_uuid))
|
|
|
except (TypeError, ValueError, AttributeError) as exc:
|
|
|
raise VMError("虚拟机 UUID 无效") from exc
|
|
|
with self._connect() as conn:
|
|
|
if any(domain.name() == validated["name"] for domain in conn.listAllDomains(0)):
|
|
|
raise VMError("虚拟机名称已存在", 409)
|
|
|
host_arch = conn.getInfo()[0]
|
|
|
if host_arch != "x86_64":
|
|
|
raise VMError("当前创建模板只支持 x86_64 宿主机", 422)
|
|
|
disk_pool = self._active_pool(conn, self.storage_pool)
|
|
|
self._refresh_directory_pool(disk_pool)
|
|
|
if validated["disk_gib"] * GIB > disk_pool.info()[3]:
|
|
|
raise VMError("存储池剩余空间不足", 409)
|
|
|
iso_pool = self._active_pool(conn, self.iso_pool)
|
|
|
self._refresh_directory_pool(iso_pool)
|
|
|
if validated["iso"] not in iso_pool.listVolumes():
|
|
|
raise VMError("安装镜像不在配置的 ISO 存储池中", 404)
|
|
|
if self.bridge_name is None:
|
|
|
network = conn.networkLookupByName(self.network)
|
|
|
if not network.isActive():
|
|
|
raise VMError("配置的虚拟网络未启动", 409)
|
|
|
|
|
|
vm_uuid = requested_uuid or str(uuidlib.uuid4())
|
|
|
disk_name = f"{validated['name']}.qcow2"
|
|
|
if disk_name in disk_pool.listVolumes():
|
|
|
raise VMError(f"磁盘文件 {disk_name} 已存在,不会覆盖现有数据", 409)
|
|
|
iso_volume = iso_pool.storageVolLookupByName(validated["iso"])
|
|
|
iso_source = self._volume_source(iso_pool, iso_volume)
|
|
|
volume = disk_pool.createXML(self._volume_xml(disk_name, validated["disk_gib"]), 0)
|
|
|
try:
|
|
|
disk_source = self._volume_source(disk_pool, volume)
|
|
|
install_xml = self._domain_xml(
|
|
|
validated, vm_uuid, disk_name, disk_source, iso_source,
|
|
|
install_phase=True)
|
|
|
final_xml = self._domain_xml(
|
|
|
validated, vm_uuid, disk_name, disk_source, iso_source,
|
|
|
install_phase=False)
|
|
|
try:
|
|
|
install_domain = conn.createXML(install_xml, 0)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("Could not start installation VM %s: %s", validated["name"], exc)
|
|
|
bridge_hint = f"网桥 {self.bridge_name}、" if self.bridge_name else ""
|
|
|
raise VMError(
|
|
|
f"无法启动安装期虚拟机;请检查 {bridge_hint}Q35/UEFI 固件、"
|
|
|
"磁盘和 ISO 访问权限,并查看宿主机 libvirt 日志", 409,
|
|
|
) from exc
|
|
|
if install_domain is None:
|
|
|
raise VMError("宿主机未能启动安装期虚拟机", 502)
|
|
|
# Like virt-install, keep the live installation XML separate
|
|
|
# from the persistent definition used after the first stop.
|
|
|
try:
|
|
|
domain = conn.defineXML(final_xml)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.warning("Could not persist final VM %s: %s", validated["name"], exc)
|
|
|
raise VMError("安装期虚拟机已启动,但无法保存卸载 ISO 后的配置", 502) from exc
|
|
|
if domain is None:
|
|
|
raise VMError("宿主机未能保存卸载 ISO 后的虚拟机配置", 502)
|
|
|
self._enable_vm_autostart(domain)
|
|
|
return self._vm_dict(domain)
|
|
|
except Exception as exc:
|
|
|
if not self._rollback_new_vm(conn, vm_uuid, validated["name"],
|
|
|
disk_pool, volume, disk_name):
|
|
|
raise VMError(
|
|
|
"创建失败且自动回滚不完整;为避免误删运行中磁盘,请检查宿主机中的"
|
|
|
f"虚拟机 {validated['name']} 和磁盘 {disk_name}", 502,
|
|
|
) from exc
|
|
|
raise
|
|
|
|
|
|
def action(self, uuid: str, action: str) -> dict[str, Any]:
|
|
|
methods = {
|
|
|
"start": "create",
|
|
|
"shutdown": "shutdown",
|
|
|
"reboot": "reboot",
|
|
|
"poweroff": "destroy",
|
|
|
"reset": "reset",
|
|
|
}
|
|
|
if action not in methods:
|
|
|
raise VMError("不支持的虚拟机操作")
|
|
|
with self._connect() as conn:
|
|
|
domain = self._domain(conn, uuid)
|
|
|
active = bool(domain.isActive())
|
|
|
if action == "start" and active:
|
|
|
raise VMError("虚拟机已经在运行", 409)
|
|
|
if action != "start" and not active:
|
|
|
raise VMError("虚拟机未运行", 409)
|
|
|
if action == "reset":
|
|
|
domain.reset(0)
|
|
|
else:
|
|
|
getattr(domain, methods[action])()
|
|
|
return self._vm_dict(domain)
|
|
|
|
|
|
@staticmethod
|
|
|
def _disk_sources(root: ET.Element) -> list[ET.Element]:
|
|
|
return [source for disk in root.findall("./devices/disk")
|
|
|
if disk.get("device") == "disk"
|
|
|
for source in [disk.find("source")] if source is not None]
|
|
|
|
|
|
@staticmethod
|
|
|
def _all_disk_sources(root: ET.Element) -> list[ET.Element]:
|
|
|
# Include primary, backing, external data-file and mirror sources. A
|
|
|
# second VM can depend on this volume in any of those positions.
|
|
|
return root.findall("./devices/disk//source")
|
|
|
|
|
|
def _other_domain_uses_volume(self, conn: Any, vm_uuid: str,
|
|
|
pool_name: str, volume_name: str,
|
|
|
volume_path: str) -> bool:
|
|
|
for other in conn.listAllDomains(0):
|
|
|
if other.UUIDString() == vm_uuid:
|
|
|
continue
|
|
|
flags = [0]
|
|
|
if other.isActive() and other.isPersistent():
|
|
|
flags.append(libvirt.VIR_DOMAIN_XML_INACTIVE)
|
|
|
for flag in flags:
|
|
|
try:
|
|
|
root = ET.fromstring(other.XMLDesc(flag))
|
|
|
except ET.ParseError as exc:
|
|
|
raise VMError("其他虚拟机配置 XML 无效,无法安全删除磁盘", 502) from exc
|
|
|
for source in self._all_disk_sources(root):
|
|
|
if ((source.get("pool") == pool_name
|
|
|
and source.get("volume") == volume_name)
|
|
|
or volume_path in (source.get("file"), source.get("dev"))):
|
|
|
return True
|
|
|
return False
|
|
|
|
|
|
def _owned_volume(self, conn: Any, domain: Any) -> Any:
|
|
|
root = self._vm_xml(domain)
|
|
|
managed = root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
if managed is None:
|
|
|
raise VMError("只能删除由面板创建的磁盘", 403)
|
|
|
disk_name = managed.get("disk-volume") or f"panel-{domain.UUIDString()}.qcow2"
|
|
|
if (not disk_name.endswith(".qcow2") or "/" in disk_name
|
|
|
or not NAME_RE.fullmatch(disk_name.removesuffix(".qcow2"))):
|
|
|
raise VMError("面板记录的磁盘文件名无效,无法安全删除", 409)
|
|
|
pool = self._active_pool(conn, self.storage_pool)
|
|
|
pool_name = pool.name()
|
|
|
volume = pool.storageVolLookupByName(disk_name)
|
|
|
volume_path = volume.path()
|
|
|
sources = self._disk_sources(root)
|
|
|
if len(sources) != 1:
|
|
|
raise VMError("虚拟机磁盘配置已变化,无法安全删除磁盘", 409)
|
|
|
source = sources[0]
|
|
|
volume_source = (source.get("pool") == pool_name
|
|
|
and source.get("volume") == disk_name)
|
|
|
file_source = source.get("file") == volume_path
|
|
|
if not (volume_source or file_source):
|
|
|
raise VMError("虚拟机磁盘不属于面板配置的存储池", 409)
|
|
|
if self._other_domain_uses_volume(conn, domain.UUIDString(),
|
|
|
pool_name, disk_name, volume_path):
|
|
|
raise VMError("磁盘还被其他虚拟机使用", 409)
|
|
|
if domain.snapshotNum(0) > 0:
|
|
|
raise VMError("虚拟机有快照,请先处理快照再删除磁盘", 409)
|
|
|
return volume
|
|
|
|
|
|
def _owned_seed_volume(self, conn: Any, domain: Any) -> Any | None:
|
|
|
root = self._vm_xml(domain)
|
|
|
managed = root.find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
seed_name = managed.get("seed-volume") if managed is not None else None
|
|
|
if seed_name is None:
|
|
|
return None
|
|
|
if not re.fullmatch(
|
|
|
r"panel-seed-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-"
|
|
|
r"[0-9a-f]{4}-[0-9a-f]{12}\.iso", seed_name or ""):
|
|
|
raise VMError("面板记录的 cloud-init 配置盘名称无效", 409)
|
|
|
pool = self._active_pool(conn, self.storage_pool)
|
|
|
volume = pool.storageVolLookupByName(seed_name)
|
|
|
volume_path = volume.path()
|
|
|
sources = [source for cdrom in root.findall("./devices/disk[@device='cdrom']")
|
|
|
for source in [cdrom.find("source")] if source is not None]
|
|
|
if len(sources) != 1:
|
|
|
raise VMError("cloud-init 配置盘已被修改,无法安全删除", 409)
|
|
|
source = sources[0]
|
|
|
if not ((source.get("pool") == pool.name() and source.get("volume") == seed_name)
|
|
|
or source.get("file") == volume_path):
|
|
|
raise VMError("cloud-init 配置盘来源与面板记录不符", 409)
|
|
|
if self._other_domain_uses_volume(conn, domain.UUIDString(),
|
|
|
pool.name(), seed_name, volume_path):
|
|
|
raise VMError("cloud-init 配置盘还被其他虚拟机使用", 409)
|
|
|
return volume
|
|
|
|
|
|
@staticmethod
|
|
|
def _undefine_domain(domain: Any, *, remove_nvram: bool) -> None:
|
|
|
"""Handle UEFI NVRAM when removing a domain definition."""
|
|
|
if remove_nvram:
|
|
|
flags = getattr(libvirt, "VIR_DOMAIN_UNDEFINE_NVRAM", 0)
|
|
|
if not flags:
|
|
|
# Older bindings cannot request deletion; retain NVRAM so
|
|
|
# undefine still succeeds rather than leaving a partial VM.
|
|
|
flags = getattr(libvirt, "VIR_DOMAIN_UNDEFINE_KEEP_NVRAM", 0)
|
|
|
else:
|
|
|
flags = getattr(libvirt, "VIR_DOMAIN_UNDEFINE_KEEP_NVRAM", 0)
|
|
|
if flags:
|
|
|
domain.undefineFlags(flags)
|
|
|
else:
|
|
|
domain.undefine()
|
|
|
|
|
|
def delete_vm(self, uuid: str, delete_disks: bool) -> dict[str, Any]:
|
|
|
if not isinstance(delete_disks, bool):
|
|
|
raise VMError("delete_disks 必须是布尔值")
|
|
|
with self._connect() as conn:
|
|
|
domain = self._domain(conn, uuid)
|
|
|
if domain.isActive():
|
|
|
raise VMError("请先关闭虚拟机,再删除", 409)
|
|
|
if not domain.isPersistent():
|
|
|
raise VMError("无法删除临时虚拟机定义", 409)
|
|
|
volume = self._owned_volume(conn, domain) if delete_disks else None
|
|
|
seed_volume = self._owned_seed_volume(conn, domain) if delete_disks else None
|
|
|
vm_uuid = domain.UUIDString()
|
|
|
managed = self._vm_xml(domain).find(f"./metadata/{{{PANEL_NS}}}managed")
|
|
|
self._undefine_domain(
|
|
|
domain, remove_nvram=(delete_disks and managed is not None
|
|
|
and managed.get("version") == "2"),
|
|
|
)
|
|
|
deleted_disks: list[str] = []
|
|
|
for removable in (seed_volume, volume):
|
|
|
if removable is None:
|
|
|
continue
|
|
|
# Never use WITH_SNAPSHOTS or zeroing flags here.
|
|
|
volume_name = removable.name()
|
|
|
try:
|
|
|
removable.delete(0)
|
|
|
except libvirt.libvirtError as exc:
|
|
|
LOG.exception("Domain %s undefined but volume %s remains", vm_uuid, volume_name)
|
|
|
raise VMError("虚拟机定义已删除,但磁盘删除失败;请在宿主机检查该磁盘", 502) from exc
|
|
|
deleted_disks.append(volume_name)
|
|
|
return {"uuid": vm_uuid, "deleted": True, "deleted_disks": deleted_disks}
|
|
|
|
|
|
def console_port(self, uuid: str) -> int:
|
|
|
with self._connect() as conn:
|
|
|
domain = self._domain(conn, uuid)
|
|
|
if not domain.isActive():
|
|
|
raise VMError("请先启动虚拟机,再打开 VNC 控制台", 409)
|
|
|
port = self._vnc_port(self._vm_xml(domain))
|
|
|
if port is None:
|
|
|
raise VMError("虚拟机没有可用的 VNC 控制台", 409)
|
|
|
return port
|