[PATCH 1/2] ch: Add config file support

Stefan Kober posted 2 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH 1/2] ch: Add config file support
Posted by Stefan Kober 3 months, 2 weeks ago
Similar to the QEMU driver, the ch driver receives support for
configuration files that allows doing certain configuration on the
virtchd daemon.

The initial use case will be setting the verbosity of the cloud
hypervisor instances started by virtchd, but the implementation allows
for adding further options.
---
 src/ch/ch_conf.c   | 22 ++++++++++++++++++++++
 src/ch/ch_conf.h   |  3 +++
 src/ch/ch_driver.c |  6 ++++++
 3 files changed, 31 insertions(+)

diff --git a/src/ch/ch_conf.c b/src/ch/ch_conf.c
index cab97639c4..7d3f600707 100644
--- a/src/ch/ch_conf.c
+++ b/src/ch/ch_conf.c
@@ -22,6 +22,7 @@
 
 #include "configmake.h"
 #include "vircommand.h"
+#include "virconf.h"
 #include "virfile.h"
 #include "virlog.h"
 #include "virobject.h"
@@ -81,6 +82,25 @@ virCaps *virCHDriverCapsInit(void)
     return g_steal_pointer(&caps);
 }
 
+int virCHDriverConfigLoadFile(virCHDriverConfig *cfg,
+                              const char *filename)
+{
+    g_autoptr(virConf) conf = NULL;
+
+    /* Just check the file is readable before opening it, otherwise
+     * libvirt emits an error.
+     */
+    if (access(filename, R_OK) == -1) {
+        VIR_INFO("Could not read ch config file %s", filename);
+        return 0;
+    }
+
+    if (!(conf = virConfReadFile(filename, 0)))
+        return -1;
+
+    return 0;
+}
+
 /**
  * virCHDriverGetCapabilities:
  *
@@ -149,6 +169,7 @@ virCHDriverConfigNew(bool privileged)
         cfg->logDir = g_strdup_printf("%s/log/libvirt/ch", LOCALSTATEDIR);
         cfg->stateDir = g_strdup_printf("%s/libvirt/ch", RUNSTATEDIR);
         cfg->saveDir = g_strdup_printf("%s/lib/libvirt/ch/save", LOCALSTATEDIR);
+        cfg->configDir = g_strdup_printf("%s/lib/libvirt/ch", LOCALSTATEDIR);
 
     } else {
         g_autofree char *rundir = NULL;
@@ -164,6 +185,7 @@ virCHDriverConfigNew(bool privileged)
 
         configbasedir = virGetUserConfigDirectory();
         cfg->saveDir = g_strdup_printf("%s/ch/save", configbasedir);
+        cfg->configDir = g_strdup_printf("%s/ch", configbasedir);
     }
 
     return cfg;
diff --git a/src/ch/ch_conf.h b/src/ch/ch_conf.h
index b08573476e..2f0d090d35 100644
--- a/src/ch/ch_conf.h
+++ b/src/ch/ch_conf.h
@@ -38,6 +38,7 @@ struct _virCHDriverConfig {
     GObject parent;
 
     char *stateDir;
+    char *configDir;
     char *logDir;
     char *saveDir;
 
@@ -103,6 +104,8 @@ struct _CHSaveXMLHeader {
     uint32_t unused[11];
 };
 
+int virCHDriverConfigLoadFile(virCHDriverConfig *cfg,
+                                const char *filename);
 virCaps *virCHDriverCapsInit(void);
 virCaps *virCHDriverGetCapabilities(virCHDriver *driver,
                                       bool refresh);
diff --git a/src/ch/ch_driver.c b/src/ch/ch_driver.c
index 3bdcf66ebd..cf6874f22e 100644
--- a/src/ch/ch_driver.c
+++ b/src/ch/ch_driver.c
@@ -1411,6 +1411,7 @@ chStateInitialize(bool privileged,
                   virStateInhibitCallback callback G_GNUC_UNUSED,
                   void *opaque G_GNUC_UNUSED)
 {
+    g_autofree char *driverConf = NULL;
     int ret = VIR_DRV_STATE_INIT_ERROR;
     int rv;
 
@@ -1447,6 +1448,11 @@ chStateInitialize(bool privileged,
     if (!(ch_driver->config = virCHDriverConfigNew(privileged)))
         goto cleanup;
 
+    driverConf = g_strdup_printf("%s/ch.conf", ch_driver->config->configDir);
+
+    if (virCHDriverConfigLoadFile(ch_driver->config, driverConf) < 0)
+        goto cleanup;
+
     if (!(ch_driver->hostdevMgr = virHostdevManagerGetDefault()))
         goto cleanup;
 
-- 
2.49.0
Re: [PATCH 1/2] ch: Add config file support
Posted by Michal Prívozník via Devel 3 months, 2 weeks ago
On 5/22/25 08:21, Stefan Kober wrote:
> Similar to the QEMU driver, the ch driver receives support for
> configuration files that allows doing certain configuration on the
> virtchd daemon.
> 
> The initial use case will be setting the verbosity of the cloud
> hypervisor instances started by virtchd, but the implementation allows
> for adding further options.
> ---
>  src/ch/ch_conf.c   | 22 ++++++++++++++++++++++
>  src/ch/ch_conf.h   |  3 +++
>  src/ch/ch_driver.c |  6 ++++++
>  3 files changed, 31 insertions(+)

This is lacking documentation. Users wouldn't know that this new config
file is read.

And for completeness, maybe we want to add some example file (for this
particular patch basically empty ch.conf with only a comment on top,
just like qemu.conf.in has it) and augeas file so that the config file
can be modified pragmatically?

Otherwise looking good.

Michal