From 2cad33d0e6e70abf9d065a9ac757a263d635f951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Thu, 22 Feb 2024 01:09:11 +0100 Subject: [PATCH] Made some minor adjustments --- src/backing.c | 10 ++++++++ src/domain.c | 8 +++++++ src/domain.h | 3 +++ src/pci.c | 7 ++---- src/sandbox.c | 4 +--- src/sandbox.h | 2 ++ src/xml.c | 34 ++++++++++++++++----------- src/xml.h | 16 ++++++------- xml | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 src/domain.c create mode 100644 src/domain.h create mode 100644 xml diff --git a/src/backing.c b/src/backing.c index 80fac2f..6a2ae38 100755 --- a/src/backing.c +++ b/src/backing.c @@ -447,6 +447,16 @@ result_t get_backing_info(disk_info_t* _info, const char* backing) { } result_t sync_backing_pool(void) { + // Check that the file /etc/sandbox.d/sync exists + struct stat st; + errno = 0; + if (stat("/etc/sandbox.d/sync", &st) != 0) { + if (errno == ENOENT) + return success(); + else + return failure("Failed to check if /etc/sandbox.d/sync exists (%s).", strerror(errno)); + } + // Execute /etc/sandbox.d/sync with the backing pool as the working directory int exit_code; // char* stdoutbuf; diff --git a/src/domain.c b/src/domain.c new file mode 100644 index 0000000..253e4bf --- /dev/null +++ b/src/domain.c @@ -0,0 +1,8 @@ +#include "domain.h" + +#include "sandbox.h" +#include "container.h" +#include "pci.h" +#include "xml.h" + +#include diff --git a/src/domain.h b/src/domain.h new file mode 100644 index 0000000..5b8741a --- /dev/null +++ b/src/domain.h @@ -0,0 +1,3 @@ +#pragma once + +#include "utils.h" diff --git a/src/pci.c b/src/pci.c index 48a993d..13d2855 100644 --- a/src/pci.c +++ b/src/pci.c @@ -73,7 +73,7 @@ result_t get_iommu_group(int* _group, const char* pci) { struct stat st; if (lstat(path, &st) != 0 || !S_ISLNK(st.st_mode)) { free(path); - return failure("PCI address '%s' does not have an IOMMU group.", pci); + return failure("PCI address '%s' does not have an IOMMU group. Please ensure that the IOMMU is enabled in the kernel.", pci); } // Read the IOMMU group by getting the path of the symlink, and getting the basename of the path @@ -124,14 +124,11 @@ result_t get_iommu_groups(int** _groups, int* _count, const char** pcis) { for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (groups[i] == groups[j]) { - // Shift the elements to the left + // Shift the elements to the left and decrement the count for (int k = j; k < count - 1; k++) groups[k] = groups[k + 1]; - // Decrement the count count--; - - // Decrement the index j--; } } diff --git a/src/sandbox.c b/src/sandbox.c index 3234474..a644c3c 100755 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -3,15 +3,13 @@ #include "utils.h" #include "backing.h" #include "container.h" +#include "domain.h" #include #include #include #include -#include "pci.h" -#include "xml.h" - #define ALIAS(...) \ (const char*[]) { __VA_ARGS__, NULL } diff --git a/src/sandbox.h b/src/sandbox.h index 8347a0e..72a97b0 100755 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -4,7 +4,9 @@ #define SANDBOX_VERSION "0.1.4" #define SANDBOX_USER "sandbox" + #define LIBVIRT_DOMAIN "sandbox" +#define LIBVIRT_DRIVER "qemu:///system" #define CONFIG_FILE "/etc/sandbox.d/sandbox.conf" #define SYNC_FILE "/etc/sandbox.d/sync" diff --git a/src/xml.c b/src/xml.c index c4727d9..88f391b 100644 --- a/src/xml.c +++ b/src/xml.c @@ -7,7 +7,7 @@ #include #include -result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t memory, char** pcis, char** iso_paths, int vnc_port, char* vnc_password) { +result_t generate_container_xml(char** _xml, const char* container, int cpu, uint64_t memory, const char** pcis, const char** iso_paths, int vnc_port, const char* vnc_password) { // Initialize the output parameters *_xml = NULL; @@ -106,7 +106,7 @@ result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t return result; } -result_t generate_iso_xml(char** _xml, char* iso_path, int index) { +result_t generate_iso_xml(char** _xml, const char* iso_path, int index) { // Initialize the output parameters *_xml = NULL; @@ -121,10 +121,10 @@ result_t generate_iso_xml(char** _xml, char* iso_path, int index) { "\n" "\n" "\n", - iso_path, 'b' + index, index + 2); // sda is reserved for the hard drive + iso_path, 'a' + index, index + 1); // sda is reserved for the hard drive } -result_t generate_multi_iso_xml(char** _xml, char** iso_paths) { +result_t generate_multi_iso_xml(char** _xml, const char** iso_paths) { // Initialize the output parameters *_xml = NULL; @@ -132,11 +132,16 @@ result_t generate_multi_iso_xml(char** _xml, char** iso_paths) { if (xml == NULL) return failure("Failed to allocate memory for XML"); + if (iso_paths == NULL) { + *_xml = xml; + return success(); + } + // For each ISO path, generate the XML and append it to the result for (int i = 0; iso_paths[i] != NULL; i++) { // Generate the XML for the ISO char* iso_xml; - result_t result = generate_iso_xml(&iso_xml, iso_paths[i], i); + result_t result = generate_iso_xml(&iso_xml, iso_paths[i], i + 1); // +1 to skip the hard drive if (result != success()) { free(xml); return result; @@ -163,7 +168,7 @@ result_t generate_multi_iso_xml(char** _xml, char** iso_paths) { return success(); } -result_t generate_pci_xml(char** _xml, char* pci) { +result_t generate_pci_xml(char** _xml, const char* pci) { // Initialize the output parameters *_xml = NULL; @@ -173,21 +178,19 @@ result_t generate_pci_xml(char** _xml, char* pci) { return result; // Split the PCI address into its components - long domain = 0; - long bus = 0; - long slot = 0; - long function = 0; + unsigned int domain, bus, slot, function; + sscanf(pci, "%04x:%02x:%02x.%01x", &domain, &bus, &slot, &function); // Generate the XML return format(_xml, "\n" "\n" - "
\n" + "
\n" "\n" "\n", domain, bus, slot, function); } -result_t generate_multi_pci_xml(char** _xml, char** pcis) { +result_t generate_multi_pci_xml(char** _xml, const char** pcis) { // Initialize the output parameters *_xml = NULL; @@ -195,6 +198,11 @@ result_t generate_multi_pci_xml(char** _xml, char** pcis) { if (xml == NULL) return failure("Failed to allocate memory for XML"); + if (pcis == NULL) { + *_xml = xml; + return success(); + } + // For each PCI address, generate the XML and append it to the result for (int i = 0; pcis[i] != NULL; i++) { // Generate the XML for the PCI address @@ -226,7 +234,7 @@ result_t generate_multi_pci_xml(char** _xml, char** pcis) { return success(); } -result_t generate_vnc_xml(char** _xml, int vnc_port, char* password) { +result_t generate_vnc_xml(char** _xml, int vnc_port, const char* password) { // Initialize the output parameters *_xml = NULL; diff --git a/src/xml.h b/src/xml.h index ef762d4..8062cc2 100644 --- a/src/xml.h +++ b/src/xml.h @@ -7,41 +7,41 @@ /// @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 pcis A null-terminated array of PCI devices to pass through to the container. -/// @param iso_paths A null-terminated array of ISO image paths to attach to the container. +/// @param pcis A null-terminated array of PCI devices to pass through to the container. If NULL, no PCI devices will be passed through. +/// @param iso_paths A null-terminated array of ISO image paths to attach to the container. If NULL, no ISO images will be attached. /// @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** pcis, char** iso_paths, int vnc_port, char* vnc_password); +result_t generate_container_xml(char** _xml, const char* container, int cpu, uint64_t memory, const char** pcis, const char** iso_paths, int vnc_port, const 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); +result_t generate_iso_xml(char** _xml, const char* iso_path, int index); /// @brief Generate the libvirt XML used to attach multiple ISO images to a container. /// @param _xml The string pointer to store the resulting XML in. /// @param iso_paths The paths to the ISO images to attach. /// @return The result of the operation. -result_t generate_multi_iso_xml(char** _xml, char** iso_paths); +result_t generate_multi_iso_xml(char** _xml, const char** iso_paths); /// @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); +result_t generate_pci_xml(char** _xml, const char* pci); /// @brief Generate the libvirt XML used to pass through multiple PCI devices to a container. /// @param _xml The string pointer to store the resulting XML in. /// @param pcis The PCI devices to generate the XML for. /// @return The result of the operation. -result_t generate_multi_pci_xml(char** _xml, char** pcis); +result_t generate_multi_pci_xml(char** _xml, const char** pcis); /// @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); +result_t generate_vnc_xml(char** _xml, int vnc_port, const char* password); diff --git a/xml b/xml new file mode 100644 index 0000000..d1004f6 --- /dev/null +++ b/xml @@ -0,0 +1,65 @@ + +sandbox + + +1073741824 +1 + + + + +hvm + + + + + + + + + + + + + + + + + +destroy +destroy +destroy + + + + + + + +/usr/bin/qemu-system-x86_64 + + + + + + + + + + + + + + + + + +
+ + + + + + + +