import uuid from pathlib import Path from typing import Optional, Iterable import libvirt from libvirt import virConnect, virDomain, virStoragePool from cloud_config import gen_cloud_config, _gen_cloudinit_iso_image def _gen_libvirt_volume_config(name: str, path: Path, backing_image: Path, capacity: int = 1000, image_format: str = 'qcow2') -> str: """ creates the XML config for a volume that will hold the data of a vm. :param name: volume name :param path: path to save volume to :param backing_image: path to a backing image to base this one on :param capacity: capacity in MiB :param image_format: qcow2, iso, raw or alike. A list of valid values can be found here: https://libvirt.org/storage.html#StorageBackendFS :return: XML config as string """ xml_config = f""" {name}.{image_format} {capacity} 0 {path} {backing_image} """ return xml_config def _gen_libvirt_storage_pool_config(name: str, path: Path) -> str: """ create the libvirt XML config for a storage pool with directory backend. :param name: name of the storage pool :param path: the path to save images in the pool to :return: XML config as string """ xml_config = f""" {name} {path} """ return xml_config def _gen_libvirt_config(name: str, hdd_path: Path, cdrom_path: Path, cpu: int = 1, mem: int = 512) -> str: """ return libvirt XML config for a virtual machine :param name: name of the machine :param hdd_path: path to the image to use as primary disk :param cdrom_path: path to the cloud-init image :param cpu: number of cpus :param mem: amount of ram in MiB :return: XML config as string """ vm_uuid = uuid.uuid4() xml_config = f""" {name} {vm_uuid} {mem} {mem} {cpu} hvm destroy restart destroy /usr/bin/qemu-system-x86_64