Started implementing XML generation for libvirt

This commit is contained in:
Alexei KADIR 2024-02-20 23:06:02 +01:00
parent 070a5dfe15
commit a36277c002
2 changed files with 64 additions and 0 deletions

29
src/xml.c Normal file
View File

@ -0,0 +1,29 @@
#include "xml.h"
result_t generate_iso_xml(char** _xml, char* iso_path, int index) {
// Initialize the output parameters
*_xml = NULL;
if (index > 25)
return failure("Too many ISO images");
// Generate the XML
return foramt(_xml, "<disk type='file' device='cdrom'>"
" <driver name='qemu' type='raw'/>"
" <source file='%s'/>"
" <target dev='sd%c' bus='sata'/>"
" <readonly/>"
"</disk>",
iso_path, 'a' + index);
}
result_t generate_vnc_xml(char** _xml, int vnc_port, char* password) {
// Initialize the output parameters
*_xml = NULL;
// Generate the XML
if (vnc_port == -1)
return format(_xml, "<graphics type='none'/>");
else
return format(_xml, "<graphics type='vnc' port='%d' autoport='no' listen='0.0.0.0' passwd='%s'/>", vnc_port, password);
}

35
src/xml.h Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include "utils.h"
/// @brief Generate the libvirt XML used to start a container with the given parameters.
/// @param _xml The string pointer to store the resulting XML in. The caller is responsible for freeing the string.
/// @param container The container to generate the XML for.
/// @param cpu The number of CPUs to allocate to the container.
/// @param memory The amount of memory to allocate to the container, in bytes.
/// @param pci A null-terminated array of PCI devices to pass through to the container.
/// @param iso_path A null-terminated array of ISO image paths to attach to the container.
/// @param vnc_port The VNC port to use for the container. If -1, no VNC server will be started.
/// @param vnc_password The password to use for the VNC server. This parameter is ignored if VNC is not enabled.
/// @return The result of the operation.
result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t memory, char** pci, char** iso_path, int vnc_port, char* vnc_password);
/// @brief Generate the libvirt XML used to attach an ISO image to a container.
/// @param _xml The string pointer to store the resulting XML in.
/// @param iso_path The path to the ISO image to attach.
/// @param index The index of the ISO image within the container. Must be positive, unique, and less than 26.
/// @return The result of the operation.
result_t generate_iso_xml(char** _xml, char* iso_path, int index);
/// @brief Generate the libvirt XML used to start a VNC server for a container.
/// @param _xml The string pointer to store the resulting XML in.
/// @param vnc_port The VNC port to use for the container.
/// @param password The password to use for the VNC server. This parameter is ignored if VNC is not enabled.
/// @return The result of the operation.
result_t generate_vnc_xml(char** _xml, int vnc_port, char* password);
/// @brief Generate the libvirt XML used to pass through a PCI device to a container.
/// @param _xml The string pointer to store the resulting XML in.
/// @param pci The PCI device to generate the XML for.
/// @return The result of the operation.
result_t generate_pci_xml(char** _xml, char* pci);