Added functions to convert container ids to domain ids

This commit is contained in:
Alexei KADIR 2024-02-24 02:59:22 +01:00
parent 96944f1111
commit 788d46d071
3 changed files with 67 additions and 1 deletions

38
src/domain.c Normal file
View File

@ -0,0 +1,38 @@
#include "domain.h"
#include "sandbox.h"
#include <string.h>
result_t container_to_domain(char** _domain, const char* container) {
// Initialize the output parameter
*_domain = NULL;
// Convert the container identifier to a domain identifier
result_t result = format(_domain, "%s%s", DOMAIN_IDENTIFIER_PREFIX, container);
if (result != success())
return result;
return success();
}
result_t domain_to_container(char** _container, const char* domain) {
// Initialize the output parameter
*_container = NULL;
// Check that the domain identifier is not too short
size_t prefix_length = strlen(DOMAIN_IDENTIFIER_PREFIX);
if (strlen(domain) <= prefix_length)
return failure("Invalid domain identifier. The domain identifier is too short. A domain identifier must be prefixed with '%s'.", DOMAIN_IDENTIFIER_PREFIX);
// Check that the domain identifier has the correct prefix
if (strncmp(domain, DOMAIN_IDENTIFIER_PREFIX, prefix_length) != 0)
return failure("Invalid domain identifier. The domain identifier must be prefixed with '%s'.", DOMAIN_IDENTIFIER_PREFIX);
// Convert the domain identifier to a container identifier
result_t result = format(_container, "%s", domain + prefix_length);
if (result != success())
return result;
return success();
}

15
src/domain.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include "utils.h"
/// @brief Converts a container identifier to a domain identifier.
/// @param _domain The string pointer to store the resulting domain identifier in. The caller is responsible for freeing the string.
/// @param container The container identifier to convert.
/// @return The result of the operation.
result_t container_to_domain(char** _domain, const char* container);
/// @brief Converts a domain identifier to a container identifier.
/// @param _container The string pointer to store the resulting container identifier in. The caller is responsible for freeing the string.
/// @param domain The domain identifier to convert.
/// @return The result of the operation.
result_t domain_to_container(char** _container, const char* domain);

View File

@ -2,6 +2,7 @@
#include "pci.h" #include "pci.h"
#include "container.h" #include "container.h"
#include "domain.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -45,6 +46,17 @@ result_t generate_domain_xml(char** _xml, const char* container, uint64_t memory
return result; return result;
} }
// Get the domain identifier
char* domain;
result = container_to_domain(&domain, container);
if (result != success()) {
free(pci_xml);
free(iso_xml);
free(vnc_xml);
free(container_path);
return result;
}
// Generate the XML configuration for the domain // Generate the XML configuration for the domain
char* xml; char* xml;
result = format(&xml, "<domain type='kvm'>\n" result = format(&xml, "<domain type='kvm'>\n"
@ -98,11 +110,12 @@ result_t generate_domain_xml(char** _xml, const char* container, uint64_t memory
"<memballoon model='none'/>\n" "<memballoon model='none'/>\n"
"</devices>\n" "</devices>\n"
"</domain>\n", "</domain>\n",
container, memory, vcpus, container_path, pci_xml, iso_xml, vnc_xml); domain, memory, vcpus, container_path, pci_xml, iso_xml, vnc_xml);
free(pci_xml); free(pci_xml);
free(iso_xml); free(iso_xml);
free(vnc_xml); free(vnc_xml);
free(container_path); free(container_path);
free(domain);
if (result != success()) if (result != success())
return result; return result;