Re: [sandbox][PATCH] Add module directory prefix selection feature

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add module directory selection feature

This is useful when running as a non-privileged user if we want to
boot a custom compiled kernel: we might not have rights to install in
/lib/modules/<kernel release> so when compiling the kernel we can use
"make modules_install INSTALL_MOD_PATH=/path" which installs in
/path/lib/modules/<kernel release>. By setting with
gvir_sandbox_config_set_kmodpath(cfg, "/path/lib/modules") we can
now achieve just that.
diff --git a/libvirt-sandbox/libvirt-sandbox-builder-initrd.c b/libvirt-sandbox/libvirt-sandbox-builder-initrd.c
index dd01c7a..1e5563e 100644
--- a/libvirt-sandbox/libvirt-sandbox-builder-initrd.c
+++ b/libvirt-sandbox/libvirt-sandbox-builder-initrd.c
@@ -331,8 +331,8 @@ static GList *gvir_sandbox_builder_initrd_find_modules(GList *modnames,
                                                        GVirSandboxConfigInitrd *config,
                                                        GError **error)
 {
-    gchar *moddirpath = g_strdup_printf("/lib/modules/%s/kernel",
-                                        gvir_sandbox_config_initrd_get_kver(config));
+    gchar *moddirpath = g_strdup_printf("%s",
+                                        gvir_sandbox_config_initrd_get_kmoddir(config));
     GFile *moddir = g_file_new_for_path(moddirpath);
 
     GList *modfiles = gvir_sandbox_builder_initrd_find_files(modnames,
@@ -340,6 +340,10 @@ static GList *gvir_sandbox_builder_initrd_find_modules(GList *modnames,
                                                              error);
 
     GList *tmp1 = modnames;
+
+    if (!modfiles)
+        goto cleanup;
+
     while (tmp1) {
         gboolean found = FALSE;
         GList *tmp2 = modfiles;
diff --git a/libvirt-sandbox/libvirt-sandbox-builder-machine.c b/libvirt-sandbox/libvirt-sandbox-builder-machine.c
index 5f39679..6add320 100644
--- a/libvirt-sandbox/libvirt-sandbox-builder-machine.c
+++ b/libvirt-sandbox/libvirt-sandbox-builder-machine.c
@@ -123,6 +123,10 @@ static gchar *gvir_sandbox_builder_machine_mkinitrd(GVirSandboxConfig *config,
     int fd = -1;
 
     gvir_sandbox_config_initrd_set_kver(initrd, gvir_sandbox_config_get_kernrelease(config));
+    gchar *kmoddir = g_strdup_printf("%s/%s/kernel", gvir_sandbox_config_get_kmodpath(config),
+                    gvir_sandbox_config_get_kernrelease(config));
+    gvir_sandbox_config_initrd_set_kmoddir(initrd, kmoddir);
+
     gvir_sandbox_config_initrd_set_init(initrd, LIBEXECDIR "/libvirt-sandbox-init-qemu");
 
     gvir_sandbox_config_initrd_add_module(initrd, "fscache.ko");
@@ -153,6 +157,7 @@ cleanup:
         g_free(targetfile);
         targetfile = NULL;
     }
+    g_free(kmoddir);
     g_object_unref(initrd);
     g_object_unref(builder);
     return targetfile;
diff --git a/libvirt-sandbox/libvirt-sandbox-config-initrd.c b/libvirt-sandbox/libvirt-sandbox-config-initrd.c
index 5c75fce..cc70352 100644
--- a/libvirt-sandbox/libvirt-sandbox-config-initrd.c
+++ b/libvirt-sandbox/libvirt-sandbox-config-initrd.c
@@ -44,6 +44,7 @@ struct _GVirSandboxConfigInitrdPrivate
 {
     gchar *kver;
     gchar *init;
+    gchar *kmoddir;
     GList *modules;
 };
 
@@ -53,6 +54,7 @@ G_DEFINE_TYPE(GVirSandboxConfigInitrd, gvir_sandbox_config_initrd, G_TYPE_OBJECT
 enum {
     PROP_0,
     PROP_KVER,
+    PROP_KMODDIR,
     PROP_INIT,
 };
 
@@ -76,6 +78,10 @@ static void gvir_sandbox_config_initrd_get_property(GObject *object,
         g_value_set_string(value, priv->kver);
         break;
 
+    case PROP_KMODDIR:
+        g_value_set_string(value, priv->kmoddir);
+        break;
+
     case PROP_INIT:
         g_value_set_string(value, priv->init);
         break;
@@ -100,6 +106,11 @@ static void gvir_sandbox_config_initrd_set_property(GObject *object,
         priv->kver = g_value_dup_string(value);
         break;
 
+    case PROP_KMODDIR:
+        g_free(priv->kmoddir);
+        priv->kmoddir = g_value_dup_string(value);
+        break;
+
     case PROP_INIT:
         g_free(priv->init);
         priv->init = g_value_dup_string(value);
@@ -142,6 +153,17 @@ static void gvir_sandbox_config_initrd_class_init(GVirSandboxConfigInitrdClass *
                                                         G_PARAM_STATIC_NICK |
                                                         G_PARAM_STATIC_BLURB));
     g_object_class_install_property(object_class,
+                                    PROP_KMODDIR,
+                                    g_param_spec_string("kmoddir",
+                                                        "Kmoddir",
+                                                        "Kernel modules directory",
+                                                        NULL,
+                                                        G_PARAM_READABLE |
+                                                        G_PARAM_WRITABLE |
+                                                        G_PARAM_STATIC_NAME |
+                                                        G_PARAM_STATIC_NICK |
+                                                        G_PARAM_STATIC_BLURB));
+    g_object_class_install_property(object_class,
                                     PROP_INIT,
                                     g_param_spec_string("init",
                                                         "Init",
@@ -209,6 +231,34 @@ const gchar *gvir_sandbox_config_initrd_get_kver(GVirSandboxConfigInitrd *config
     return priv->kver;
 }
 
+/**
+ * gvir_sandbox_config_initrd_set_kmoddir:
+ * @config: (transfer none): the sandbox initrd config
+ * @kmoddir: (transfer none): the full path to the kernel modules directory
+ *
+ * Sets the full path to where the kernel modules will be looked up
+ */
+void gvir_sandbox_config_initrd_set_kmoddir(GVirSandboxConfigInitrd *config, const gchar *kmoddir)
+{
+    GVirSandboxConfigInitrdPrivate *priv = config->priv;
+    g_free(priv->kmoddir);
+    priv->kmoddir = g_strdup(kmoddir);
+}
+
+/**
+ * gvir_sandbox_config_initrd_get_kmoddir:
+ * @config: (transfer none): the full path to the kernel modules directory
+ *
+ * Retrieves the current kernel modules directory
+ *
+ * Returns: (transfer none): the full path to the kernel modules directory
+ */
+const gchar *gvir_sandbox_config_initrd_get_kmoddir(GVirSandboxConfigInitrd *config)
+{
+    GVirSandboxConfigInitrdPrivate *priv = config->priv;
+    return priv->kmoddir;
+}
+
 
 /**
  * gvir_sandbox_config_initrd_set_init:
diff --git a/libvirt-sandbox/libvirt-sandbox-config-initrd.h b/libvirt-sandbox/libvirt-sandbox-config-initrd.h
index 6e09e62..cb96080 100644
--- a/libvirt-sandbox/libvirt-sandbox-config-initrd.h
+++ b/libvirt-sandbox/libvirt-sandbox-config-initrd.h
@@ -64,6 +64,9 @@ GVirSandboxConfigInitrd *gvir_sandbox_config_initrd_new(void);
 void gvir_sandbox_config_initrd_set_kver(GVirSandboxConfigInitrd *config, const gchar *version);
 const gchar *gvir_sandbox_config_initrd_get_kver(GVirSandboxConfigInitrd *config);
 
+void gvir_sandbox_config_initrd_set_kmoddir(GVirSandboxConfigInitrd *config, const gchar *kmoddir);
+const gchar *gvir_sandbox_config_initrd_get_kmoddir(GVirSandboxConfigInitrd *config);
+
 void gvir_sandbox_config_initrd_set_init(GVirSandboxConfigInitrd *config, const gchar *hostpath);
 const gchar *gvir_sandbox_config_initrd_get_init(GVirSandboxConfigInitrd *config);
 
diff --git a/libvirt-sandbox/libvirt-sandbox-config.c b/libvirt-sandbox/libvirt-sandbox-config.c
index 7f6efea..41d8946 100644
--- a/libvirt-sandbox/libvirt-sandbox-config.c
+++ b/libvirt-sandbox/libvirt-sandbox-config.c
@@ -48,6 +48,7 @@ struct _GVirSandboxConfigPrivate
     gchar *arch;
     gchar *kernrelease;
     gchar *kernpath;
+    gchar *kmodpath;
     gboolean shell;
 
     guint uid;
@@ -76,6 +77,7 @@ enum {
     PROP_SHELL,
     PROP_KERNRELEASE,
     PROP_KERNPATH,
+    PROP_KMODPATH,
 
     PROP_UID,
     PROP_GID,
@@ -135,6 +137,10 @@ static void gvir_sandbox_config_get_property(GObject *object,
         g_value_set_string(value, priv->kernpath);
         break;
 
+    case PROP_KMODPATH:
+        g_value_set_string(value, priv->kmodpath);
+        break;
+
     case PROP_SHELL:
         g_value_set_boolean(value, priv->shell);
         break;
@@ -203,6 +209,11 @@ static void gvir_sandbox_config_set_property(GObject *object,
         priv->kernpath = g_value_dup_string(value);
         break;
 
+    case PROP_KMODPATH:
+        g_free(priv->kmodpath);
+        priv->kmodpath = g_value_dup_string(value);
+        break;
+
     case PROP_SHELL:
         priv->shell = g_value_get_boolean(value);
         break;
@@ -263,6 +274,7 @@ static void gvir_sandbox_config_finalize(GObject *object)
     g_free(priv->arch);
     g_free(priv->kernrelease);
     g_free(priv->kernpath);
+    g_free(priv->kmodpath);
     g_free(priv->secLabel);
 
     G_OBJECT_CLASS(gvir_sandbox_config_parent_class)->finalize(object);
@@ -337,6 +349,17 @@ static void gvir_sandbox_config_class_init(GVirSandboxConfigClass *klass)
                                                         G_PARAM_STATIC_NICK |
                                                         G_PARAM_STATIC_BLURB));
     g_object_class_install_property(object_class,
+                                    PROP_KMODPATH,
+                                    g_param_spec_string("kmodpath",
+                                                        "Kmodpath",
+                                                        "Kernel modules path",
+                                                        NULL,
+                                                        G_PARAM_READABLE |
+                                                        G_PARAM_WRITABLE |
+                                                        G_PARAM_STATIC_NAME |
+                                                        G_PARAM_STATIC_NICK |
+                                                        G_PARAM_STATIC_BLURB));
+    g_object_class_install_property(object_class,
                                     PROP_SHELL,
                                     g_param_spec_string("shell",
                                                         "SHELL",
@@ -436,6 +459,7 @@ static void gvir_sandbox_config_init(GVirSandboxConfig *config)
     priv->arch = g_strdup(uts.machine);
     priv->kernrelease = g_strdup(uts.release);
     priv->kernpath = g_strdup_printf("/boot/vmlinuz-%s", priv->kernrelease);
+    priv->kmodpath = g_strdup_printf("/lib/modules");
     priv->secLabel = g_strdup("system_u:system_r:svirt_t:s0:c0.c1023");
 
     priv->uid = geteuid();
@@ -553,6 +577,7 @@ const gchar *gvir_sandbox_config_get_kernrelease(GVirSandboxConfig *config)
     GVirSandboxConfigPrivate *priv = config->priv;
     return priv->kernrelease;
 }
+
 /**
  * gvir_sandbox_config_set_kernpath:
  * @config: (transfer none): the sandbox config
@@ -584,6 +609,39 @@ const gchar *gvir_sandbox_config_get_kernpath(GVirSandboxConfig *config)
     return priv->kernpath;
 }
 
+/**
+ * gvir_sandbox_config_set_kmodpath:
+ * @config: (transfer none): the sandbox config
+ * @kmodpath: (transfer none): the kernel modules path
+ *
+ * Sets the generic path to the kernel modules directory.
+ * It will default to "/lib/modules", modules being searched in
+ * /lib/modules/<kernel release>. If "/path" is given as argument
+ * modules will be searched in /path/<kernel release>
+ */
+
+void gvir_sandbox_config_set_kmodpath(GVirSandboxConfig *config, const gchar *kmodpath)
+{
+    GVirSandboxConfigPrivate *priv = config->priv;
+    g_free(priv->kmodpath);
+    priv->kmodpath = g_strdup(kmodpath);
+}
+
+
+/**
+ * gvir_sandbox_config_get_kmodpath:
+ * @config: (transfer none): the sandbox config
+ *
+ * Retrieves the sandbox kernel modules path
+ *
+ * Returns: (transfer none): the current kernel modules path
+ */
+const gchar *gvir_sandbox_config_get_kmodpath(GVirSandboxConfig *config)
+{
+    GVirSandboxConfigPrivate *priv = config->priv;
+    return priv->kmodpath;
+}
+
 
 /**
  * gvir_sandbox_config_set_shell:
@@ -1651,6 +1709,10 @@ static gboolean gvir_sandbox_config_load_config(GVirSandboxConfig *config,
         g_free(priv->kernpath);
         priv->kernpath = str;
     }
+    if ((str = g_key_file_get_string(file, "core", "kmodpath", NULL)) != NULL) {
+        g_free(priv->kmodpath);
+        priv->kmodpath = str;
+    }
     b = g_key_file_get_boolean(file, "core", "shell", &e);
     if (e) {
         g_error_free(e);
@@ -1870,6 +1932,7 @@ static void gvir_sandbox_config_save_config(GVirSandboxConfig *config,
     g_key_file_set_string(file, "core", "arch", priv->arch);
     g_key_file_set_string(file, "core", "kernrelease", priv->kernrelease);
     g_key_file_set_string(file, "core", "kernpath", priv->kernpath);
+    g_key_file_set_string(file, "core", "kmodpath", priv->kmodpath);
     g_key_file_set_boolean(file, "core", "shell", priv->shell);
 
     g_key_file_set_uint64(file, "identity", "uid", priv->uid);
diff --git a/libvirt-sandbox/libvirt-sandbox-config.h b/libvirt-sandbox/libvirt-sandbox-config.h
index a6c8ae8..17b9001 100644
--- a/libvirt-sandbox/libvirt-sandbox-config.h
+++ b/libvirt-sandbox/libvirt-sandbox-config.h
@@ -82,6 +82,9 @@ const gchar *gvir_sandbox_config_get_kernrelease(GVirSandboxConfig *config);
 void gvir_sandbox_config_set_kernpath(GVirSandboxConfig *config, const gchar *kernpath);
 const gchar *gvir_sandbox_config_get_kernpath(GVirSandboxConfig *config);
 
+void gvir_sandbox_config_set_kmodpath(GVirSandboxConfig *config, const gchar *kmodpath);
+const gchar *gvir_sandbox_config_get_kmodpath(GVirSandboxConfig *config);
+
 void gvir_sandbox_config_set_shell(GVirSandboxConfig *config, gboolean shell);
 gboolean gvir_sandbox_config_get_shell(GVirSandboxConfig *config);
 
diff --git a/libvirt-sandbox/libvirt-sandbox.sym b/libvirt-sandbox/libvirt-sandbox.sym
index 424fd98..87dd526 100644
--- a/libvirt-sandbox/libvirt-sandbox.sym
+++ b/libvirt-sandbox/libvirt-sandbox.sym
@@ -96,6 +96,7 @@ LIBVIRT_SANDBOX_0.0.1 {
 	gvir_sandbox_config_set_arch;
 	gvir_sandbox_config_set_kernrelease;
 	gvir_sandbox_config_set_kernpath;
+	gvir_sandbox_config_set_kmodpath;
 	gvir_sandbox_config_set_userid;
 	gvir_sandbox_config_set_groupid;
 	gvir_sandbox_config_set_username;
@@ -107,11 +108,13 @@ LIBVIRT_SANDBOX_0.0.1 {
 	gvir_sandbox_config_initrd_add_module;
 	gvir_sandbox_config_initrd_get_init;
 	gvir_sandbox_config_initrd_get_kver;
+	gvir_sandbox_config_initrd_get_kmoddir;
 	gvir_sandbox_config_initrd_get_modules;
 	gvir_sandbox_config_initrd_get_type;
 	gvir_sandbox_config_initrd_new;
 	gvir_sandbox_config_initrd_set_init;
 	gvir_sandbox_config_initrd_set_kver;
+	gvir_sandbox_config_initrd_set_kmoddir;
 
 	gvir_sandbox_console_get_type;
 	gvir_sandbox_console_attach;
--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]