Made some minor adjustments

This commit is contained in:
Alexei KADIR 2024-02-22 01:09:11 +01:00
parent 6f47b7f274
commit 2cad33d0e6
9 changed files with 120 additions and 29 deletions

View File

@ -447,6 +447,16 @@ result_t get_backing_info(disk_info_t* _info, const char* backing) {
} }
result_t sync_backing_pool(void) { 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 // Execute /etc/sandbox.d/sync with the backing pool as the working directory
int exit_code; int exit_code;
// char* stdoutbuf; // char* stdoutbuf;

8
src/domain.c Normal file
View File

@ -0,0 +1,8 @@
#include "domain.h"
#include "sandbox.h"
#include "container.h"
#include "pci.h"
#include "xml.h"
#include <libvirt/libvirt.h>

3
src/domain.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
#include "utils.h"

View File

@ -73,7 +73,7 @@ result_t get_iommu_group(int* _group, const char* pci) {
struct stat st; struct stat st;
if (lstat(path, &st) != 0 || !S_ISLNK(st.st_mode)) { if (lstat(path, &st) != 0 || !S_ISLNK(st.st_mode)) {
free(path); 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 // 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 i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) { for (int j = i + 1; j < count; j++) {
if (groups[i] == groups[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++) for (int k = j; k < count - 1; k++)
groups[k] = groups[k + 1]; groups[k] = groups[k + 1];
// Decrement the count
count--; count--;
// Decrement the index
j--; j--;
} }
} }

View File

@ -3,15 +3,13 @@
#include "utils.h" #include "utils.h"
#include "backing.h" #include "backing.h"
#include "container.h" #include "container.h"
#include "domain.h"
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
#include "pci.h"
#include "xml.h"
#define ALIAS(...) \ #define ALIAS(...) \
(const char*[]) { __VA_ARGS__, NULL } (const char*[]) { __VA_ARGS__, NULL }

View File

@ -4,7 +4,9 @@
#define SANDBOX_VERSION "0.1.4" #define SANDBOX_VERSION "0.1.4"
#define SANDBOX_USER "sandbox" #define SANDBOX_USER "sandbox"
#define LIBVIRT_DOMAIN "sandbox" #define LIBVIRT_DOMAIN "sandbox"
#define LIBVIRT_DRIVER "qemu:///system"
#define CONFIG_FILE "/etc/sandbox.d/sandbox.conf" #define CONFIG_FILE "/etc/sandbox.d/sandbox.conf"
#define SYNC_FILE "/etc/sandbox.d/sync" #define SYNC_FILE "/etc/sandbox.d/sync"

View File

@ -7,7 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
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 // Initialize the output parameters
*_xml = NULL; *_xml = NULL;
@ -106,7 +106,7 @@ result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t
return result; 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 // Initialize the output parameters
*_xml = NULL; *_xml = NULL;
@ -121,10 +121,10 @@ result_t generate_iso_xml(char** _xml, char* iso_path, int index) {
"<boot order='%d'/>\n" "<boot order='%d'/>\n"
"<readonly/>\n" "<readonly/>\n"
"</disk>\n", "</disk>\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 // Initialize the output parameters
*_xml = NULL; *_xml = NULL;
@ -132,11 +132,16 @@ result_t generate_multi_iso_xml(char** _xml, char** iso_paths) {
if (xml == NULL) if (xml == NULL)
return failure("Failed to allocate memory for XML"); 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 each ISO path, generate the XML and append it to the result
for (int i = 0; iso_paths[i] != NULL; i++) { for (int i = 0; iso_paths[i] != NULL; i++) {
// Generate the XML for the ISO // Generate the XML for the ISO
char* iso_xml; 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()) { if (result != success()) {
free(xml); free(xml);
return result; return result;
@ -163,7 +168,7 @@ result_t generate_multi_iso_xml(char** _xml, char** iso_paths) {
return success(); 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 // Initialize the output parameters
*_xml = NULL; *_xml = NULL;
@ -173,21 +178,19 @@ result_t generate_pci_xml(char** _xml, char* pci) {
return result; return result;
// Split the PCI address into its components // Split the PCI address into its components
long domain = 0; unsigned int domain, bus, slot, function;
long bus = 0; sscanf(pci, "%04x:%02x:%02x.%01x", &domain, &bus, &slot, &function);
long slot = 0;
long function = 0;
// Generate the XML // Generate the XML
return format(_xml, "<hostdev mode='subsystem' type='pci' managed='yes'>\n" return format(_xml, "<hostdev mode='subsystem' type='pci' managed='yes'>\n"
"<source>\n" "<source>\n"
"<address domain='0x%04lx' bus='0x%02lx' slot='0x%02lx' function='0x%01lx'/>\n" "<address domain='0x%04x' bus='0x%02x' slot='0x%02x' function='0x%01x'/>\n"
"</source>\n" "</source>\n"
"</hostdev>\n", "</hostdev>\n",
domain, bus, slot, function); 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 // Initialize the output parameters
*_xml = NULL; *_xml = NULL;
@ -195,6 +198,11 @@ result_t generate_multi_pci_xml(char** _xml, char** pcis) {
if (xml == NULL) if (xml == NULL)
return failure("Failed to allocate memory for XML"); 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 each PCI address, generate the XML and append it to the result
for (int i = 0; pcis[i] != NULL; i++) { for (int i = 0; pcis[i] != NULL; i++) {
// Generate the XML for the PCI address // Generate the XML for the PCI address
@ -226,7 +234,7 @@ result_t generate_multi_pci_xml(char** _xml, char** pcis) {
return success(); 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 // Initialize the output parameters
*_xml = NULL; *_xml = NULL;

View File

@ -7,41 +7,41 @@
/// @param container The container to generate the XML for. /// @param container The container to generate the XML for.
/// @param cpu The number of CPUs to allocate to the container. /// @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 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 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. /// @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_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. /// @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. /// @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. /// @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 _xml The string pointer to store the resulting XML in.
/// @param iso_path The path to the ISO image to attach. /// @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. /// @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. /// @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. /// @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 _xml The string pointer to store the resulting XML in.
/// @param iso_paths The paths to the ISO images to attach. /// @param iso_paths The paths to the ISO images to attach.
/// @return The result of the operation. /// @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. /// @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 _xml The string pointer to store the resulting XML in.
/// @param pci The PCI device to generate the XML for. /// @param pci The PCI device to generate the XML for.
/// @return The result of the operation. /// @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. /// @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 _xml The string pointer to store the resulting XML in.
/// @param pcis The PCI devices to generate the XML for. /// @param pcis The PCI devices to generate the XML for.
/// @return The result of the operation. /// @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. /// @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 _xml The string pointer to store the resulting XML in.
/// @param vnc_port The VNC port to use for the container. /// @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. /// @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. /// @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);

65
xml Normal file
View File

@ -0,0 +1,65 @@
<domain type='kvm'>
<name>sandbox</name>
<!-- Resources -->
<memory unit='B'>1073741824</memory>
<vcpu placement='static'>1</vcpu>
<cpu mode='host-passthrough'/>
<!-- OS -->
<os>
<type arch='x86_64' machine='q35'>hvm</type>
<bootmenu enable='no'/>
</os>
<!-- Features -->
<features>
<acpi/>
<apic/>
</features>
<!-- Clock -->
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<!-- Behavior -->
<on_poweroff>destroy</on_poweroff>
<on_reboot>destroy</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='yes'/>
<suspend-to-disk enabled='yes'/>
</pm>
<!-- Devices -->
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<!-- Disks -->
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/sandbox/containers/test'/>
<target dev='sda' bus='sata'/>
<boot order='1'/>
</disk>
<!-- ISOs -->
<!-- VNC -->
<graphics type='vnc' port='5900' autoport='no' listen='0.0.0.0' passwd='test'/>
<!-- PCIs -->
<hostdev mode='subsystem' type='pci' managed='yes'>
<source>
<address domain='0x0000' bus='0x00' slot='0x00' function='0x0'/>
</source>
</hostdev>
<!-- Misc -->
<watchdog model='itco' action='poweroff'/>
<memballoon model='none'/>
</devices>
</domain>