]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
ch: Add config file support
authorStefan Kober <stefan.kober@cyberus-technology.de>
Mon, 18 Aug 2025 09:20:53 +0000 (11:20 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 18 Aug 2025 12:36:09 +0000 (14:36 +0200)
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.

Signed-off-by: Stefan Kober <stefan.kober@cyberus-technology.de>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/ch/ch.conf [new file with mode: 0644]
src/ch/ch_conf.c
src/ch/ch_conf.h
src/ch/ch_driver.c
src/ch/libvirtd_ch.aug [new file with mode: 0644]

diff --git a/src/ch/ch.conf b/src/ch/ch.conf
new file mode 100644 (file)
index 0000000..71bab49
--- /dev/null
@@ -0,0 +1,3 @@
+# Master configuration file for the CH driver.
+# All settings described here are optional - if omitted, sensible
+# defaults are used.
index cab97639c40400fef95817afd5734bff91160880..fbe1d172c8ea21ced09591e0c60409581e82a42b 100644 (file)
@@ -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 G_GNUC_UNUSED,
+                              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;
index b08573476edcd87a3935702e612dfa5d8c9bd615..2f0d090d357fa51c518f23b860a511bf7e71265a 100644 (file)
@@ -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);
index 3bdcf66ebdf020bcc04bc1bfa1a2fc1bb038c54a..cf6874f22e1549c12d656ad0db65dc58c595e238 100644 (file)
@@ -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;
 
diff --git a/src/ch/libvirtd_ch.aug b/src/ch/libvirtd_ch.aug
new file mode 100644 (file)
index 0000000..d0b0964
--- /dev/null
@@ -0,0 +1,40 @@
+(* /etc/libvirt/ch.conf *)
+
+module Libvirtd_ch =
+   autoload xfm
+
+   let eol   = del /[ \t]*\n/ "\n"
+   let value_sep   = del /[ \t]*=[ \t]*/  " = "
+   let indent = del /[ \t]*/ ""
+
+   let array_sep  = del /,[ \t\n]*/ ", "
+   let array_start = del /\[[ \t\n]*/ "[ "
+   let array_end = del /\]/ "]"
+
+   let str_val = del /\"/ "\"" . store /[^\"]*/ . del /\"/ "\""
+   let bool_val = store /0|1/
+   let int_val = store /[0-9]+/
+   let str_array_element = [ seq "el" . str_val ] . del /[ \t\n]*/ ""
+   let str_array_val = counter "el" . array_start . ( str_array_element . ( array_sep . str_array_element ) * ) ? . array_end
+
+   let str_entry       (kw:string) = [ key kw . value_sep . str_val ]
+   let bool_entry      (kw:string) = [ key kw . value_sep . bool_val ]
+   let int_entry       (kw:string) = [ key kw . value_sep . int_val ]
+   let str_array_entry (kw:string) = [ key kw . value_sep . str_array_val ]
+
+   (* Config entry grouped by function - same order as example config *)
+   let config_entry = bool_entry "placeholder"
+
+   (* Each entry in the config is one of the following three ... *)
+   let entry = config_entry
+   let comment = [ label "#comment" . del /#[ \t]*/ "# " .  store /([^ \t\n][^\n]*)?/ . del /\n/ "\n" ]
+   let empty = [ label "#empty" . eol ]
+
+   let record = indent . entry . eol
+
+   let lns = ( record | comment | empty ) *
+
+   let filter = incl "/etc/libvirt/ch.conf"
+              . Util.stdexcl
+
+   let xfm = transform lns filter