linux-kbuild.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] kmod /usr support
@ 2023-07-11 15:31 Michal Suchanek
  2023-07-11 15:31 ` [PATCH 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
                   ` (9 more replies)
  0 siblings, 10 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-11 15:31 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

with these patches it is possible to install kernel modules under an
arbitrary prefix - eg. moving the /lib/modules to /usr/lib/modules with
/usr prefix.

While the modprobe.d and depmod.d search which already includes multiple
paths is expanded to also include $(prefix) the module directory still
supports only one location, only a different one under $(module_prefix).

Having kmod search multiple module locations while only one is supported
now might break some assumption about relative modulke path
corresponding to a specific file, would require more invasive changes to
implement, and is not supportive of the goal of moving the modules away
from /lib.

Both kmod and the kernel need to be patched to make use of this feature.
Patched kernel is backwards compatible with older kmod.  Patched kmod
with empty $(module_prefix) is equivalent to unpatched kmod.

Thanks

Michal

Link: https://lore.kernel.org/linux-modules/20210112160211.5614-1-msuchanek@suse.de/

Michal Suchanek (4):
  man/depmod.d: Fix incorrect /usr/lib search path
  libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  kmod: Add config command to show compile time configuration as JSON
  libkmod, depmod, modprobe: Search for kernel modules under
    ${module_prefix}

 Makefile.am                          |   4 +-
 configure.ac                         |   7 ++
 libkmod/libkmod.c                    |   7 +-
 man/Makefile.am                      |  10 ++-
 man/depmod.d.xml                     |   9 ++-
 man/depmod.xml                       |   4 +-
 man/kmod.xml                         |   6 ++
 man/modinfo.xml                      |   2 +-
 man/modprobe.d.xml                   |   1 +
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   7 +-
 tools/kmod.c                         |  40 ++++++++++
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 20 files changed, 166 insertions(+), 88 deletions(-)

-- 
2.41.0


^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH 1/4] man/depmod.d: Fix incorrect /usr/lib search path
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
@ 2023-07-11 15:31 ` Michal Suchanek
  2023-07-11 15:31 ` [PATCH 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-11 15:31 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

depmod searches /lib/depmod.d but the man page says /usr/lib/depmod.d is
reached. Aling the documentation with the code.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 man/depmod.d.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 76548e92a312..8d3d821cddc8 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -39,7 +39,7 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/usr/lib/depmod.d/*.conf</filename></para>
+    <para><filename>/lib/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
  2023-07-11 15:31 ` [PATCH 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
@ 2023-07-11 15:31 ` Michal Suchanek
  2023-07-12  6:47   ` Jiri Slaby
  2023-07-11 15:31 ` [PATCH 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 83+ messages in thread
From: Michal Suchanek @ 2023-07-11 15:31 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

There is an ongoing effort to limit use of files outside of /usr (or
$prefix on general). Currently all modprobe.d paths are hardcoded to
outside of $prefix. Teach kmod to load modprobe.d from $prefix/lib.

On some distributions /usr/lib and /lib are the same directory because
of a compatibility symlink, and it is possible to craft configuration
files with sideeffects that would behave differently when loaded twice.
However, the override semantic ensures that one 'overrides' the other,
and only one configuration file of the same name is loaded from any of
the seach directories.

Cc: Takashi Iwai <tiwai@suse.com>
Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 Makefile.am        | 1 +
 libkmod/libkmod.c  | 3 ++-
 man/Makefile.am    | 9 +++++++--
 man/depmod.d.xml   | 1 +
 man/modprobe.d.xml | 1 +
 tools/depmod.c     | 1 +
 6 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8ba85c91a0f3..7aa5bfa5638d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ AM_CPPFLAGS = \
 	-include $(top_builddir)/config.h \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
+	-DPREFIX=\""$(prefix)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 2670f9a4611a..13c82b069e84 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -65,6 +65,7 @@ static const char *const default_config_paths[] = {
 	SYSCONFDIR "/modprobe.d",
 	"/run/modprobe.d",
 	"/usr/local/lib/modprobe.d",
+	PREFIX "/lib/modprobe.d",
 	"/lib/modprobe.d",
 	NULL
 };
@@ -237,7 +238,7 @@ static char *get_kernel_release(const char *dirname)
  *                to load from user-defined configuration parameters such as
  *                alias, blacklists, commands (install, remove). If NULL
  *                defaults to /etc/modprobe.d, /run/modprobe.d,
- *                /usr/local/lib/modprobe.d and /lib/modprobe.d. Give an empty
+ *                /usr/local/lib/modprobe.d and PREFIX/lib/modprobe.d. Give an empty
  *                vector if configuration should not be read. This array must
  *                be null terminated.
  *
diff --git a/man/Makefile.am b/man/Makefile.am
index 11514d52a190..ad07c30bbd24 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -17,9 +17,14 @@ EXTRA_DIST = $(MAN5:%.5=%.xml) $(MAN8:%.8=%.xml)
 CLEANFILES = $(dist_man_MANS)
 
 %.5 %.8: %.xml
-	$(AM_V_XSLT)$(XSLT) \
+	$(AM_V_XSLT)if [ -n '$(prefix)' ] ; then \
+		sed -e 's|@PREFIX@|$(prefix)|g' $< ; \
+	else \
+		sed -e '/@PREFIX@/d' $< ; \
+	fi | \
+	$(XSLT) \
 		-o $@ \
 		--nonet \
 		--stringparam man.output.quietly 1 \
 		--param funcsynopsis.style "'ansi'" \
-		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $<
+		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl -
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 8d3d821cddc8..431ebca6654b 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -40,6 +40,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/depmod.d/*.conf</filename></para>
+    <para><filename>@PREFIX@/lib/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
diff --git a/man/modprobe.d.xml b/man/modprobe.d.xml
index 0ab3e9110a7e..e8a91d7668af 100644
--- a/man/modprobe.d.xml
+++ b/man/modprobe.d.xml
@@ -41,6 +41,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/modprobe.d/*.conf</filename></para>
+    <para><filename>@PREFIX@/lib/modprobe.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/modprobe.d/*.conf</filename></para>
     <para><filename>/run/modprobe.d/*.conf</filename></para>
     <para><filename>/etc/modprobe.d/*.conf</filename></para>
diff --git a/tools/depmod.c b/tools/depmod.c
index 1d1d41db860f..7e9339923809 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -54,6 +54,7 @@ static const char *const default_cfg_paths[] = {
 	SYSCONFDIR "/depmod.d",
 	"/run/depmod.d",
 	"/usr/local/lib/depmod.d",
+	PREFIX "/lib/depmod.d",
 	"/lib/depmod.d",
 	NULL
 };
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
  2023-07-11 15:31 ` [PATCH 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
  2023-07-11 15:31 ` [PATCH 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
@ 2023-07-11 15:31 ` Michal Suchanek
  2023-07-11 15:31 ` [PATCH 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix} Michal Suchanek
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-11 15:31 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Show prefix (where configuration files are searched/to be installed)
and module compressions supported.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 man/kmod.xml |  6 ++++++
 tools/kmod.c | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/man/kmod.xml b/man/kmod.xml
index 0706ad58c2cc..f992a500f836 100644
--- a/man/kmod.xml
+++ b/man/kmod.xml
@@ -71,6 +71,12 @@
           <para>Show the help message.</para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term><command>config</command></term>
+        <listitem>
+          <para>Show compile time options in JSON.</para>
+        </listitem>
+      </varlistentry>
       <varlistentry>
         <term><command>list</command></term>
         <listitem>
diff --git a/tools/kmod.c b/tools/kmod.c
index 55689c075ab1..5a13716955c1 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -37,9 +37,11 @@ static const struct option options[] = {
 };
 
 static const struct kmod_cmd kmod_cmd_help;
+static const struct kmod_cmd kmod_cmd_config;
 
 static const struct kmod_cmd *kmod_cmds[] = {
 	&kmod_cmd_help,
+	&kmod_cmd_config,
 	&kmod_cmd_list,
 	&kmod_cmd_static_nodes,
 
@@ -95,6 +97,43 @@ static const struct kmod_cmd kmod_cmd_help = {
 	.help = "Show help message",
 };
 
+static const char *compressions[] = {
+#ifdef ENABLE_ZSTD
+			"zstd",
+#endif
+#ifdef ENABLE_XZ
+			"xz",
+#endif
+#ifdef ENABLE_ZLIB
+			"gz",
+#endif
+			NULL
+};
+
+static int kmod_config(int argc, char *argv[])
+{
+	unsigned i;
+	printf("{\"prefix\":\"" PREFIX "\""
+			",\"module_signature\":["
+#ifdef ENABLE_OPENSSL
+			"\"PKCS#7\","
+#endif
+			"\"legacy\"]"
+			",\"module_compression\":[");
+	for(i = 0; compressions[i]; i++) {
+		printf("%s\"%s\"", i ? "," : "", compressions[i]);
+	}
+	printf("]}\n");
+
+	return EXIT_SUCCESS;
+}
+
+static const struct kmod_cmd kmod_cmd_config = {
+	.name = "config",
+	.cmd = kmod_config,
+	.help = "Show compile time options in JSON",
+};
+
 static int handle_kmod_commands(int argc, char *argv[])
 {
 	const char *cmd;
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix}
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
                   ` (2 preceding siblings ...)
  2023-07-11 15:31 ` [PATCH 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
@ 2023-07-11 15:31 ` Michal Suchanek
  2023-07-11 15:34 ` [PATCH] depmod: Handle installing modules under a prefix Michal Suchanek
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-11 15:31 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

modprobe.d is now searched under ${prefix}/lib, add ${module_prefix} to
specify the directory where to search for kernel modules.

With this distributions that do not want to ship files in /lib can also
move kernel modules to /usr while others can keep them in /lib.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 Makefile.am                          |   3 +-
 configure.ac                         |   7 ++
 libkmod/libkmod.c                    |   6 +-
 man/Makefile.am                      |   1 +
 man/depmod.d.xml                     |   6 +-
 man/depmod.xml                       |   4 +-
 man/modinfo.xml                      |   2 +-
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   6 +-
 tools/kmod.c                         |   1 +
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 18 files changed, 108 insertions(+), 85 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 7aa5bfa5638d..96ae1edd7366 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
 	-DPREFIX=\""$(prefix)"\" \
+	-DMODULE_PREFIX=\""$(module_prefix)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
@@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
 MODULE_PLAYGROUND = testsuite/module-playground
 ROOTFS = testsuite/rootfs
 ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
-CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
+CREATE_ROOTFS = $(AM_V_GEN) MODULE_PREFIX=$(module_prefix) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
 
 build-module-playground:
 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
diff --git a/configure.ac b/configure.ac
index 6064dee77ae6..29d3ff8ae41d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -84,6 +84,12 @@ AC_ARG_WITH([rootlibdir],
         [], [with_rootlibdir=$libdir])
 AC_SUBST([rootlibdir], [$with_rootlibdir])
 
+# Ideally this would be $prefix but default to empty for compatibility with earlier versions
+AC_ARG_WITH([module_prefix],
+        AS_HELP_STRING([--with-module-prefix=DIR], [directory in which to look for /lib/modules directory with kernel modules - typically '' or ${prefix}]),
+        [], [with_module_prefix=])
+AC_SUBST([module_prefix], [$with_module_prefix])
+
 AC_ARG_WITH([zstd],
 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
 	[], [with_zstd=no])
@@ -305,6 +311,7 @@ AC_MSG_RESULT([
 	=======
 
 	prefix:			${prefix}
+	module_prefix:		${module_prefix}
 	sysconfdir:		${sysconfdir}
 	libdir:			${libdir}
 	rootlibdir:		${rootlibdir}
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 13c82b069e84..e41acae6f9fc 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -209,7 +209,7 @@ static int log_priority(const char *priority)
 	return 0;
 }
 
-static const char *dirname_default_prefix = "/lib/modules";
+static const char *dirname_default_prefix = MODULE_PREFIX "/lib/modules";
 
 static char *get_kernel_release(const char *dirname)
 {
@@ -231,14 +231,14 @@ static char *get_kernel_release(const char *dirname)
 /**
  * kmod_new:
  * @dirname: what to consider as linux module's directory, if NULL
- *           defaults to /lib/modules/`uname -r`. If it's relative,
+ *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
  *           it's treated as relative to the current working directory.
  *           Otherwise, give an absolute dirname.
  * @config_paths: ordered array of paths (directories or files) where
  *                to load from user-defined configuration parameters such as
  *                alias, blacklists, commands (install, remove). If NULL
  *                defaults to /etc/modprobe.d, /run/modprobe.d,
- *                /usr/local/lib/modprobe.d and PREFIX/lib/modprobe.d. Give an empty
+ *                /usr/local/lib/modprobe.d and ${module_prefix}/lib/modprobe.d. Give an empty
  *                vector if configuration should not be read. This array must
  *                be null terminated.
  *
diff --git a/man/Makefile.am b/man/Makefile.am
index ad07c30bbd24..1a9a92f9c224 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
 	else \
 		sed -e '/@PREFIX@/d' $< ; \
 	fi | \
+	sed -e 's|@MODULE_PREFIX@|$(module_prefix)|g' | \
 	$(XSLT) \
 		-o $@ \
 		--nonet \
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 431ebca6654b..2583a8d8a5fc 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -70,7 +70,7 @@
         </term>
         <listitem>
           <para>
-            This allows you to specify the order in which /lib/modules
+            This allows you to specify the order in which @MODULE_PREFIX@/lib/modules
             (or other configured module location) subdirectories will
             be processed by <command>depmod</command>. Directories are
             listed in order, with the highest priority given to the
@@ -101,7 +101,7 @@
             <command>depmod</command> command. It is possible to
             specify one kernel or all kernels using the * wildcard.
             <replaceable>modulesubdirectory</replaceable> is the
-            name of the subdirectory under /lib/modules (or other
+            name of the subdirectory under @MODULE_PREFIX@/lib/modules (or other
             module location) where the target module is installed.
           </para>
           <para>
@@ -110,7 +110,7 @@
             specifying the following command: "override kmod * extra".
             This will ensure that any matching module name installed
             under the <command>extra</command> subdirectory within
-            /lib/modules (or other module location) will take priority
+            @MODULE_PREFIX@/lib/modules (or other module location) will take priority
             over any likenamed module already provided by the kernel.
           </para>
         </listitem>
diff --git a/man/depmod.xml b/man/depmod.xml
index 3b0097184fd7..9d9cf195a355 100644
--- a/man/depmod.xml
+++ b/man/depmod.xml
@@ -80,7 +80,7 @@
     </para>
     <para> <command>depmod</command> creates a list of module dependencies by
       reading each module under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
+      <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable> and
       determining what symbols it exports and what symbols it needs.  By
       default, this list is written to <filename>modules.dep</filename>, and a
       binary hashed version named <filename>modules.dep.bin</filename>, in the
@@ -141,7 +141,7 @@
         <listitem>
           <para>
             If your modules are not currently in the (normal) directory
-            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
+            <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable>,
             but in a staging area, you can specify a
             <replaceable>basedir</replaceable> which is prepended to the
             directory name.  This <replaceable>basedir</replaceable> is
diff --git a/man/modinfo.xml b/man/modinfo.xml
index 9fe0324a2527..d48c64560e9d 100644
--- a/man/modinfo.xml
+++ b/man/modinfo.xml
@@ -54,7 +54,7 @@
       <command>modinfo</command> extracts information from the Linux Kernel
       modules given on the command line.  If the module name is not a filename,
       then the
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>
+      <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable>
       directory is searched, as is also done by
       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
       when loading kernel modules.
diff --git a/man/modprobe.xml b/man/modprobe.xml
index 91f9e27997cd..6e8bb8c9ce9e 100644
--- a/man/modprobe.xml
+++ b/man/modprobe.xml
@@ -78,7 +78,7 @@
       is no difference between _ and - in module names (automatic
       underscore conversion is performed).
       <command>modprobe</command> looks in the module directory
-      <filename>/lib/modules/`uname -r`</filename> for all
+      <filename>@MODULE_PREFIX@/lib/modules/`uname -r`</filename> for all
       the modules and other files, except for the optional
       configuration files in the
       <filename>/etc/modprobe.d</filename> directory
diff --git a/man/modules.dep.xml b/man/modules.dep.xml
index ed633694ec9e..9130ec392089 100644
--- a/man/modules.dep.xml
+++ b/man/modules.dep.xml
@@ -34,8 +34,8 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/lib/modules/modules.dep</filename></para>
-    <para><filename>/lib/modules/modules.dep.bin</filename></para>
+    <para><filename>@MODULE_PREFIX@/lib/modules/modules.dep</filename></para>
+    <para><filename>@MODULE_PREFIX@/lib/modules/modules.dep.bin</filename></para>
   </refsynopsisdiv>
 
   <refsect1><title>DESCRIPTION</title>
@@ -43,7 +43,7 @@
       <filename>modules.dep.bin</filename> is a binary file generated by
       <command>depmod</command> listing the dependencies for
       every module in the directories under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
+      <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable>.
       It is used by kmod tools such as <command>modprobe</command> and
       libkmod.
     </para>
diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
index e6045b0dd932..a7ab09bea2bf 100644
--- a/testsuite/module-playground/Makefile
+++ b/testsuite/module-playground/Makefile
@@ -47,7 +47,7 @@ endif
 
 else
 # normal makefile
-KDIR ?= /lib/modules/`uname -r`/build
+KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
 KVER ?= `uname -r`
 ifeq ($(FAKE_BUILD),)
     FAKE_BUILD=0
diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
index 4440ddcd6b4d..db41da36fedf 100755
--- a/testsuite/setup-rootfs.sh
+++ b/testsuite/setup-rootfs.sh
@@ -16,6 +16,19 @@ create_rootfs() {
 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
 	find "$ROOTFS" -type d -exec chmod +w {} \;
 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
+	if [ -n "$MODULE_PREFIX" ] ; then
+		sed -i -e "s|/lib/modules|$MODULE_PREFIX/lib/modules|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		sed -i -e "s|$MODULE_PREFIX/lib/modules/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
+			version=$(basename $i)
+			[ $version != 'external' ] || continue
+			i=$(dirname $i)
+			lib="$(dirname $i)"
+			up="$(dirname $lib)$MODULE_PREFIX"
+			mkdir -p "$up"
+			mv "$lib" "$up"
+		done
+	fi
 
 	if [ "$SYSCONFDIR" != "/etc" ]; then
 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
@@ -32,57 +45,57 @@ feature_enabled() {
 
 declare -A map
 map=(
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_PREFIX/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_PREFIX/lib/modules/4.4.4/updates/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_PREFIX/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
+    ["test-depmod/search-order-external-first$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-first$MODULE_PREFIX/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_PREFIX/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/override/"]="mod-simple.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
+    ["test-depmod/search-order-override$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-override$MODULE_PREFIX/lib/modules/4.4.4/override/"]="mod-simple.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
     ["test-init/"]="mod-simple.ko"
     ["test-remove/"]="mod-simple.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
+    ["test-modprobe/show-depends$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/show-depends$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/show-depends$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
     ["test-modprobe/show-exports/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/force/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel-force/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/softdep-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/softdep-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/force$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel$MODULE_PREFIX/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel-force$MODULE_PREFIX/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/alias-to-none$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/module-param-kcmdline$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
     ["test-modprobe/module-from-abspath/home/foo/"]="mod-simple.ko"
     ["test-modprobe/module-from-relpath/home/foo/"]="mod-simple.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-outdir$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-outdir$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-outdir$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
@@ -90,20 +103,20 @@ map=(
     ["test-modinfo/mod-simple-sha256.ko"]="mod-simple.ko"
     ["test-modinfo/mod-simple-pkcs7.ko"]="mod-simple.ko"
     ["test-modinfo/external/lib/modules/external/mod-simple.ko"]="mod-simple.ko"
-    ["test-tools/insert/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-tools/remove/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/insert$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/remove$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
 )
 
 gzip_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
+    "test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
     )
 
 xz_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
+    "test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
     )
 
 zstd_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
+    "test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
     )
 
 attach_sha256_array=(
diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
index 93606947f18a..870f2667862b 100644
--- a/testsuite/test-depmod.c
+++ b/testsuite/test-depmod.c
@@ -27,7 +27,7 @@
 
 #define MODULES_UNAME "4.4.4"
 #define MODULES_ORDER_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-order-compressed"
-#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_modules_order_for_compressed(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -55,8 +55,8 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
 	});
 
 #define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
-#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_UNAME
-#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir" MODULE_PREFIX "/lib/modules/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_modules_outdir(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -87,7 +87,7 @@ DEFINE_TEST(depmod_modules_outdir,
 	});
 
 #define SEARCH_ORDER_SIMPLE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-simple"
-#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_simple(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -114,7 +114,7 @@ DEFINE_TEST(depmod_search_order_simple,
 	});
 
 #define SEARCH_ORDER_SAME_PREFIX_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-same-prefix"
-#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_same_prefix(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -164,7 +164,7 @@ DEFINE_TEST(depmod_detect_loop,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-first"
-#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_external_first(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -191,7 +191,7 @@ DEFINE_TEST(depmod_search_order_external_first,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_LAST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-last"
-#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_external_last(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -218,7 +218,7 @@ DEFINE_TEST(depmod_search_order_external_last,
 	});
 
 #define SEARCH_ORDER_OVERRIDE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-override"
-#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_override(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c
index 56e73609f204..903a0102105e 100644
--- a/testsuite/test-testsuite.c
+++ b/testsuite/test-testsuite.c
@@ -64,7 +64,7 @@ static int testsuite_rootfs_fopen(const struct test *t)
 	char s[100];
 	int n;
 
-	fp = fopen("/lib/modules/a", "r");
+	fp = fopen(MODULE_PREFIX "/lib/modules/a", "r");
 	if (fp == NULL)
 		return EXIT_FAILURE;;
 
@@ -89,7 +89,7 @@ static int testsuite_rootfs_open(const struct test *t)
 	char buf[100];
 	int fd, done;
 
-	fd = open("/lib/modules/a", O_RDONLY);
+	fd = open(MODULE_PREFIX "/lib/modules/a", O_RDONLY);
 	if (fd < 0)
 		return EXIT_FAILURE;
 
@@ -121,12 +121,12 @@ static int testsuite_rootfs_stat_access(const struct test *t)
 {
 	struct stat st;
 
-	if (access("/lib/modules/a", F_OK) < 0) {
+	if (access(MODULE_PREFIX "/lib/modules/a", F_OK) < 0) {
 		ERR("access failed: %m\n");
 		return EXIT_FAILURE;
 	}
 
-	if (stat("/lib/modules/a", &st) < 0) {
+	if (stat(MODULE_PREFIX "/lib/modules/a", &st) < 0) {
 		ERR("stat failed: %m\n");
 		return EXIT_FAILURE;
 	}
diff --git a/tools/depmod.c b/tools/depmod.c
index 7e9339923809..686525b4adea 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -911,7 +911,7 @@ struct vertex;
 struct mod {
 	struct kmod_module *kmod;
 	char *path;
-	const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
+	const char *relpath; /* path relative to '$ROOT$MODULE_PREFIX/lib/modules/$VER/' */
 	char *uncrelpath; /* same as relpath but ending in .ko */
 	struct kmod_list *info_list;
 	struct kmod_list *dep_sym_list;
@@ -3024,11 +3024,11 @@ static int do_depmod(int argc, char *argv[])
 	}
 
 	cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
-				  "%s/lib/modules/%s",
+				  "%s" MODULE_PREFIX "/lib/modules/%s",
 				  root ?: "", cfg.kversion);
 
 	cfg.outdirnamelen = snprintf(cfg.outdirname, PATH_MAX,
-				     "%s/lib/modules/%s",
+				     "%s" MODULE_PREFIX "/lib/modules/%s",
 				     out_root ?: (root ?: ""), cfg.kversion);
 
 	if (optind == argc)
diff --git a/tools/kmod.c b/tools/kmod.c
index 5a13716955c1..4384752ab5bc 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -114,6 +114,7 @@ static int kmod_config(int argc, char *argv[])
 {
 	unsigned i;
 	printf("{\"prefix\":\"" PREFIX "\""
+			",\"module_prefix\":\"" MODULE_PREFIX "\""
 			",\"module_signature\":["
 #ifdef ENABLE_OPENSSL
 			"\"PKCS#7\","
diff --git a/tools/modinfo.c b/tools/modinfo.c
index d0aab200af4e..c10ce7ec5ef3 100644
--- a/tools/modinfo.c
+++ b/tools/modinfo.c
@@ -367,7 +367,7 @@ static void help(void)
 		"\t-m, --modname               Handle argument as module name instead of alias or filename\n"
 		"\t-F, --field=FIELD           Print only provided FIELD\n"
 		"\t-k, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
-		"\t-b, --basedir=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-b, --basedir=DIR           Use DIR as filesystem root for " MODULE_PREFIX "/lib/modules\n"
 		"\t-V, --version               Show version\n"
 		"\t-h, --help                  Show this help\n",
 		program_invocation_short_name);
@@ -462,7 +462,7 @@ static int do_modinfo(int argc, char *argv[])
 			}
 			kversion = u.release;
 		}
-		snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
+		snprintf(dirname_buf, sizeof(dirname_buf), "%s" MODULE_PREFIX "/lib/modules/%s",
 			 root, kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/modprobe.c b/tools/modprobe.c
index e891028349a8..f386c57275e0 100644
--- a/tools/modprobe.c
+++ b/tools/modprobe.c
@@ -142,7 +142,7 @@ static void help(void)
 		"\t-n, --show                  Same as --dry-run\n"
 
 		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
-		"\t-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-d, --dirname=DIR           Use DIR as filesystem root for " MODULE_PREFIX "/lib/modules\n"
 		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
 
 		"\t-s, --syslog                print to syslog, not stderr\n"
@@ -999,7 +999,7 @@ static int do_modprobe(int argc, char **orig_argv)
 			kversion = u.release;
 		}
 		snprintf(dirname_buf, sizeof(dirname_buf),
-				"%s/lib/modules/%s", root,
+				"%s" MODULE_PREFIX "/lib/modules/%s", root,
 				kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
index 8d2356da73f3..868af3b58ac7 100644
--- a/tools/static-nodes.c
+++ b/tools/static-nodes.c
@@ -212,15 +212,15 @@ static int do_static_nodes(int argc, char *argv[])
 		goto finish;
 	}
 
-	snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
+	snprintf(modules, sizeof(modules), MODULE_PREFIX "/lib/modules/%s/modules.devname", kernel.release);
 	in = fopen(modules, "re");
 	if (in == NULL) {
 		if (errno == ENOENT) {
-			fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
+			fprintf(stderr, "Warning: " MODULE_PREFIX "/lib/modules/%s/modules.devname not found - ignoring\n",
 				kernel.release);
 			ret = EXIT_SUCCESS;
 		} else {
-			fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
+			fprintf(stderr, "Error: could not open " MODULE_PREFIX "/lib/modules/%s/modules.devname - %m\n",
 				kernel.release);
 			ret = EXIT_FAILURE;
 		}
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH] depmod: Handle installing modules under a prefix
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
                   ` (3 preceding siblings ...)
  2023-07-11 15:31 ` [PATCH 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix} Michal Suchanek
@ 2023-07-11 15:34 ` Michal Suchanek
  2023-07-12  5:47   ` Jiri Slaby
  2023-07-12 14:00 ` [PATCH kmod v2 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 83+ messages in thread
From: Michal Suchanek @ 2023-07-11 15:34 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Some distributions aim at not shipping any files in / ustside of usr.

The path under which kernel modules are instaleld is hardcoded to /lib
which conflicts with this goal.

When kmod provides the config command use it to determine the correct
module installation prefix.

On kmod that does not provide the command / is used as before.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 Makefile          | 4 +++-
 scripts/depmod.sh | 8 ++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 47690c28456a..b05d696f06bd 100644
--- a/Makefile
+++ b/Makefile
@@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
 # makefile but the argument can be passed to make if needed.
 #
 
-MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
+export KERNEL_MODULE_PREFIX := $(shell kmod config | jq -r .module_prefix)
+
+MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
 export MODLIB
 
 PHONY += prepare0
diff --git a/scripts/depmod.sh b/scripts/depmod.sh
index 3643b4f896ed..88ac79056153 100755
--- a/scripts/depmod.sh
+++ b/scripts/depmod.sh
@@ -27,16 +27,16 @@ fi
 # numbers, so we cheat with a symlink here
 depmod_hack_needed=true
 tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
-mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
+mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
 if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
-	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
-		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
+	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
+		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
 		depmod_hack_needed=false
 	fi
 fi
 rm -rf "$tmp_dir"
 if $depmod_hack_needed; then
-	symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
+	symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_PREFIX/lib/modules/99.98.$KERNELRELEASE"
 	ln -s "$KERNELRELEASE" "$symlink"
 	KERNELRELEASE=99.98.$KERNELRELEASE
 fi
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH] depmod: Handle installing modules under a prefix
  2023-07-11 15:34 ` [PATCH] depmod: Handle installing modules under a prefix Michal Suchanek
@ 2023-07-12  5:47   ` Jiri Slaby
  2023-07-12  7:38     ` Michal Suchánek
  2023-07-12 13:45     ` [PATCH v2--to=linux-modules@vger.kernel.org] " Michal Suchanek
  0 siblings, 2 replies; 83+ messages in thread
From: Jiri Slaby @ 2023-07-12  5:47 UTC (permalink / raw)
  To: Michal Suchanek, linux-modules
  Cc: Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On 11. 07. 23, 17:34, Michal Suchanek wrote:
> Some distributions aim at not shipping any files in / ustside of usr.
> 
> The path under which kernel modules are instaleld is hardcoded to /lib
> which conflicts with this goal.
> 
> When kmod provides the config command use it to determine the correct
> module installation prefix.
> 
> On kmod that does not provide the command / is used as before.
> 
> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
>   Makefile          | 4 +++-
>   scripts/depmod.sh | 8 ++++----
>   2 files changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 47690c28456a..b05d696f06bd 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
>   # makefile but the argument can be passed to make if needed.
>   #
>   
> -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> +export KERNEL_MODULE_PREFIX := $(shell kmod config | jq -r .module_prefix)

echo -e 'KERNEL_MODULE_PREFIX := $(shell kmod config | jq -r 
.module_prefix)\nall:'|make -f -
invalid command 'config'
parse error: Invalid numeric literal at line 1, column 5

I think you should pipe kmod's 2> /dev/null to support older kmod. Ah, 
but you'd need 2> /dev/null for jq too. That would not be good as jq 
might not be installed and a user wouldn't see the error. So instead, I 
would do:

$(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)

thanks,
-- 
js
suse labs


^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  2023-07-11 15:31 ` [PATCH 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
@ 2023-07-12  6:47   ` Jiri Slaby
  2023-07-12  7:35     ` Michal Suchánek
  0 siblings, 1 reply; 83+ messages in thread
From: Jiri Slaby @ 2023-07-12  6:47 UTC (permalink / raw)
  To: Michal Suchanek, linux-modules
  Cc: Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On 11. 07. 23, 17:31, Michal Suchanek wrote:
> There is an ongoing effort to limit use of files outside of /usr (or
> $prefix on general). Currently all modprobe.d paths are hardcoded to
> outside of $prefix. Teach kmod to load modprobe.d from $prefix/lib.
> 
> On some distributions /usr/lib and /lib are the same directory because
> of a compatibility symlink, and it is possible to craft configuration
> files with sideeffects that would behave differently when loaded twice.
> However, the override semantic ensures that one 'overrides' the other,
> and only one configuration file of the same name is loaded from any of
> the seach directories.

search

...
> --- a/man/Makefile.am
> +++ b/man/Makefile.am
> @@ -17,9 +17,14 @@ EXTRA_DIST = $(MAN5:%.5=%.xml) $(MAN8:%.8=%.xml)
>   CLEANFILES = $(dist_man_MANS)
>   
>   %.5 %.8: %.xml
> -	$(AM_V_XSLT)$(XSLT) \
> +	$(AM_V_XSLT)if [ -n '$(prefix)' ] ; then \
> +		sed -e 's|@PREFIX@|$(prefix)|g' $< ; \

Hmm, if prefix is empty, this will remove @PREFIX@. So why you need this 
'if' at all?

> +	else \
> +		sed -e '/@PREFIX@/d' $< ; \
> +	fi | \


-- 
js
suse labs


^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  2023-07-12  6:47   ` Jiri Slaby
@ 2023-07-12  7:35     ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-12  7:35 UTC (permalink / raw)
  To: Jiri Slaby
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

On Wed, Jul 12, 2023 at 08:47:20AM +0200, Jiri Slaby wrote:
> On 11. 07. 23, 17:31, Michal Suchanek wrote:
> > There is an ongoing effort to limit use of files outside of /usr (or
> > $prefix on general). Currently all modprobe.d paths are hardcoded to
> > outside of $prefix. Teach kmod to load modprobe.d from $prefix/lib.
> > 
> > On some distributions /usr/lib and /lib are the same directory because
> > of a compatibility symlink, and it is possible to craft configuration
> > files with sideeffects that would behave differently when loaded twice.
> > However, the override semantic ensures that one 'overrides' the other,
> > and only one configuration file of the same name is loaded from any of
> > the seach directories.
> 
> search
> 
> ...
> > --- a/man/Makefile.am
> > +++ b/man/Makefile.am
> > @@ -17,9 +17,14 @@ EXTRA_DIST = $(MAN5:%.5=%.xml) $(MAN8:%.8=%.xml)
> >   CLEANFILES = $(dist_man_MANS)
> >   %.5 %.8: %.xml
> > -	$(AM_V_XSLT)$(XSLT) \
> > +	$(AM_V_XSLT)if [ -n '$(prefix)' ] ; then \
> > +		sed -e 's|@PREFIX@|$(prefix)|g' $< ; \
> 
> Hmm, if prefix is empty, this will remove @PREFIX@. So why you need this
> 'if' at all?

It removes the whole duplicate line.

Thanks

Michal

> 
> > +	else \
> > +		sed -e '/@PREFIX@/d' $< ; \
> > +	fi | \
> 
> 
> -- 
> js
> suse labs
> 

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH] depmod: Handle installing modules under a prefix
  2023-07-12  5:47   ` Jiri Slaby
@ 2023-07-12  7:38     ` Michal Suchánek
  2023-07-12 13:45     ` [PATCH v2--to=linux-modules@vger.kernel.org] " Michal Suchanek
  1 sibling, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-12  7:38 UTC (permalink / raw)
  To: Jiri Slaby
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Wed, Jul 12, 2023 at 07:47:13AM +0200, Jiri Slaby wrote:
> On 11. 07. 23, 17:34, Michal Suchanek wrote:
> > Some distributions aim at not shipping any files in / ustside of usr.
> > 
> > The path under which kernel modules are instaleld is hardcoded to /lib
> > which conflicts with this goal.
> > 
> > When kmod provides the config command use it to determine the correct
> > module installation prefix.
> > 
> > On kmod that does not provide the command / is used as before.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> >   Makefile          | 4 +++-
> >   scripts/depmod.sh | 8 ++++----
> >   2 files changed, 7 insertions(+), 5 deletions(-)
> > 
> > diff --git a/Makefile b/Makefile
> > index 47690c28456a..b05d696f06bd 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> >   # makefile but the argument can be passed to make if needed.
> >   #
> > -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > +export KERNEL_MODULE_PREFIX := $(shell kmod config | jq -r .module_prefix)
> 
> echo -e 'KERNEL_MODULE_PREFIX := $(shell kmod config | jq -r
> .module_prefix)\nall:'|make -f -
> invalid command 'config'
> parse error: Invalid numeric literal at line 1, column 5
> 
> I think you should pipe kmod's 2> /dev/null to support older kmod. Ah, but
> you'd need 2> /dev/null for jq too. That would not be good as jq might not
> be installed and a user wouldn't see the error. So instead, I would do:
> 
> $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)

Yes, that sounds reasonable. Also would cover the potential problem of
kmod changing the error output into something that is a valid JSON in
the future.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH v2--to=linux-modules@vger.kernel.org] depmod: Handle installing modules under a prefix
  2023-07-12  5:47   ` Jiri Slaby
  2023-07-12  7:38     ` Michal Suchánek
@ 2023-07-12 13:45     ` Michal Suchanek
  2023-07-12 14:14       ` Masahiro Yamada
  2023-07-14  6:25       ` [PATCH v2] " Jiri Slaby
  1 sibling, 2 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-12 13:45 UTC (permalink / raw)
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Some distributions aim at not shipping any files in / ustside of usr.

The path under which kernel modules are instaleld is hardcoded to /lib
which conflicts with this goal.

When kmod provides the config command use it to determine the correct
module installation prefix.

On kmod that does not provide the command / is used as before.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Avoid error on systems with kmod that does not support config
command
---
 Makefile          | 4 +++-
 scripts/depmod.sh | 8 ++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 47690c28456a..b1fea135bdec 100644
--- a/Makefile
+++ b/Makefile
@@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
 # makefile but the argument can be passed to make if needed.
 #
 
-MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
+export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
+
+MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
 export MODLIB
 
 PHONY += prepare0
diff --git a/scripts/depmod.sh b/scripts/depmod.sh
index 3643b4f896ed..88ac79056153 100755
--- a/scripts/depmod.sh
+++ b/scripts/depmod.sh
@@ -27,16 +27,16 @@ fi
 # numbers, so we cheat with a symlink here
 depmod_hack_needed=true
 tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
-mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
+mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
 if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
-	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
-		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
+	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
+		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
 		depmod_hack_needed=false
 	fi
 fi
 rm -rf "$tmp_dir"
 if $depmod_hack_needed; then
-	symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
+	symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_PREFIX/lib/modules/99.98.$KERNELRELEASE"
 	ln -s "$KERNELRELEASE" "$symlink"
 	KERNELRELEASE=99.98.$KERNELRELEASE
 fi
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v2 1/4] man/depmod.d: Fix incorrect /usr/lib search path
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
                   ` (4 preceding siblings ...)
  2023-07-11 15:34 ` [PATCH] depmod: Handle installing modules under a prefix Michal Suchanek
@ 2023-07-12 14:00 ` Michal Suchanek
  2023-07-12 14:00 ` [PATCH kmod v2 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-12 14:00 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

depmod searches /lib/depmod.d but the man page says /usr/lib/depmod.d is
searched. Align the documentation with the code.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Fix commit message typo
---
 man/depmod.d.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 76548e92a312..8d3d821cddc8 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -39,7 +39,7 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/usr/lib/depmod.d/*.conf</filename></para>
+    <para><filename>/lib/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v2 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
                   ` (5 preceding siblings ...)
  2023-07-12 14:00 ` [PATCH kmod v2 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
@ 2023-07-12 14:00 ` Michal Suchanek
  2023-07-14 14:16   ` Nicolas Schier
  2023-07-12 14:00 ` [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 83+ messages in thread
From: Michal Suchanek @ 2023-07-12 14:00 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

There is an ongoing effort to limit use of files outside of /usr (or
$prefix on general). Currently all modprobe.d paths are hardcoded to
outside of $prefix. Teach kmod to load modprobe.d from $prefix/lib.

On some distributions /usr/lib and /lib are the same directory because
of a compatibility symlink, and it is possible to craft configuration
files with sideeffects that would behave differently when loaded twice.
However, the override semantic ensures that one 'overrides' the other,
and only one configuration file of the same name is loaded from any of
the search directories.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Fix commit message typo
---
 Makefile.am        | 1 +
 libkmod/libkmod.c  | 3 ++-
 man/Makefile.am    | 9 +++++++--
 man/depmod.d.xml   | 1 +
 man/modprobe.d.xml | 1 +
 tools/depmod.c     | 1 +
 6 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8ba85c91a0f3..7aa5bfa5638d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ AM_CPPFLAGS = \
 	-include $(top_builddir)/config.h \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
+	-DPREFIX=\""$(prefix)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 2670f9a4611a..13c82b069e84 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -65,6 +65,7 @@ static const char *const default_config_paths[] = {
 	SYSCONFDIR "/modprobe.d",
 	"/run/modprobe.d",
 	"/usr/local/lib/modprobe.d",
+	PREFIX "/lib/modprobe.d",
 	"/lib/modprobe.d",
 	NULL
 };
@@ -237,7 +238,7 @@ static char *get_kernel_release(const char *dirname)
  *                to load from user-defined configuration parameters such as
  *                alias, blacklists, commands (install, remove). If NULL
  *                defaults to /etc/modprobe.d, /run/modprobe.d,
- *                /usr/local/lib/modprobe.d and /lib/modprobe.d. Give an empty
+ *                /usr/local/lib/modprobe.d and PREFIX/lib/modprobe.d. Give an empty
  *                vector if configuration should not be read. This array must
  *                be null terminated.
  *
diff --git a/man/Makefile.am b/man/Makefile.am
index 11514d52a190..ad07c30bbd24 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -17,9 +17,14 @@ EXTRA_DIST = $(MAN5:%.5=%.xml) $(MAN8:%.8=%.xml)
 CLEANFILES = $(dist_man_MANS)
 
 %.5 %.8: %.xml
-	$(AM_V_XSLT)$(XSLT) \
+	$(AM_V_XSLT)if [ -n '$(prefix)' ] ; then \
+		sed -e 's|@PREFIX@|$(prefix)|g' $< ; \
+	else \
+		sed -e '/@PREFIX@/d' $< ; \
+	fi | \
+	$(XSLT) \
 		-o $@ \
 		--nonet \
 		--stringparam man.output.quietly 1 \
 		--param funcsynopsis.style "'ansi'" \
-		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $<
+		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl -
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 8d3d821cddc8..431ebca6654b 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -40,6 +40,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/depmod.d/*.conf</filename></para>
+    <para><filename>@PREFIX@/lib/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
diff --git a/man/modprobe.d.xml b/man/modprobe.d.xml
index 0ab3e9110a7e..e8a91d7668af 100644
--- a/man/modprobe.d.xml
+++ b/man/modprobe.d.xml
@@ -41,6 +41,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/modprobe.d/*.conf</filename></para>
+    <para><filename>@PREFIX@/lib/modprobe.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/modprobe.d/*.conf</filename></para>
     <para><filename>/run/modprobe.d/*.conf</filename></para>
     <para><filename>/etc/modprobe.d/*.conf</filename></para>
diff --git a/tools/depmod.c b/tools/depmod.c
index 1d1d41db860f..7e9339923809 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -54,6 +54,7 @@ static const char *const default_cfg_paths[] = {
 	SYSCONFDIR "/depmod.d",
 	"/run/depmod.d",
 	"/usr/local/lib/depmod.d",
+	PREFIX "/lib/depmod.d",
 	"/lib/depmod.d",
 	NULL
 };
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
                   ` (6 preceding siblings ...)
  2023-07-12 14:00 ` [PATCH kmod v2 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
@ 2023-07-12 14:00 ` Michal Suchanek
  2023-07-14 13:52   ` Jan Engelhardt
  2023-07-14 15:26   ` Nicolas Schier
  2023-07-12 14:00 ` [PATCH kmod v2 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix} Michal Suchanek
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
  9 siblings, 2 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-12 14:00 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Show prefix (where configuration files are searched/to be installed),
module compressions, and module signatures supported.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: mention module signature in commit message
---
 man/kmod.xml |  6 ++++++
 tools/kmod.c | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/man/kmod.xml b/man/kmod.xml
index 0706ad58c2cc..f992a500f836 100644
--- a/man/kmod.xml
+++ b/man/kmod.xml
@@ -71,6 +71,12 @@
           <para>Show the help message.</para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term><command>config</command></term>
+        <listitem>
+          <para>Show compile time options in JSON.</para>
+        </listitem>
+      </varlistentry>
       <varlistentry>
         <term><command>list</command></term>
         <listitem>
diff --git a/tools/kmod.c b/tools/kmod.c
index 55689c075ab1..5a13716955c1 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -37,9 +37,11 @@ static const struct option options[] = {
 };
 
 static const struct kmod_cmd kmod_cmd_help;
+static const struct kmod_cmd kmod_cmd_config;
 
 static const struct kmod_cmd *kmod_cmds[] = {
 	&kmod_cmd_help,
+	&kmod_cmd_config,
 	&kmod_cmd_list,
 	&kmod_cmd_static_nodes,
 
@@ -95,6 +97,43 @@ static const struct kmod_cmd kmod_cmd_help = {
 	.help = "Show help message",
 };
 
+static const char *compressions[] = {
+#ifdef ENABLE_ZSTD
+			"zstd",
+#endif
+#ifdef ENABLE_XZ
+			"xz",
+#endif
+#ifdef ENABLE_ZLIB
+			"gz",
+#endif
+			NULL
+};
+
+static int kmod_config(int argc, char *argv[])
+{
+	unsigned i;
+	printf("{\"prefix\":\"" PREFIX "\""
+			",\"module_signature\":["
+#ifdef ENABLE_OPENSSL
+			"\"PKCS#7\","
+#endif
+			"\"legacy\"]"
+			",\"module_compression\":[");
+	for(i = 0; compressions[i]; i++) {
+		printf("%s\"%s\"", i ? "," : "", compressions[i]);
+	}
+	printf("]}\n");
+
+	return EXIT_SUCCESS;
+}
+
+static const struct kmod_cmd kmod_cmd_config = {
+	.name = "config",
+	.cmd = kmod_config,
+	.help = "Show compile time options in JSON",
+};
+
 static int handle_kmod_commands(int argc, char *argv[])
 {
 	const char *cmd;
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v2 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix}
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
                   ` (7 preceding siblings ...)
  2023-07-12 14:00 ` [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
@ 2023-07-12 14:00 ` Michal Suchanek
  2023-07-14 13:54   ` Jan Engelhardt
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
  9 siblings, 1 reply; 83+ messages in thread
From: Michal Suchanek @ 2023-07-12 14:00 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

modprobe.d is now searched under ${prefix}/lib, add ${module_prefix} to
specify the directory where to search for kernel modules.

With this distributions that do not want to ship files in /lib can also
move kernel modules to /usr while others can keep them in /lib.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 Makefile.am                          |   3 +-
 configure.ac                         |   7 ++
 libkmod/libkmod.c                    |   6 +-
 man/Makefile.am                      |   1 +
 man/depmod.d.xml                     |   6 +-
 man/depmod.xml                       |   4 +-
 man/modinfo.xml                      |   2 +-
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   6 +-
 tools/kmod.c                         |   1 +
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 18 files changed, 108 insertions(+), 85 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 7aa5bfa5638d..96ae1edd7366 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
 	-DPREFIX=\""$(prefix)"\" \
+	-DMODULE_PREFIX=\""$(module_prefix)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
@@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
 MODULE_PLAYGROUND = testsuite/module-playground
 ROOTFS = testsuite/rootfs
 ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
-CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
+CREATE_ROOTFS = $(AM_V_GEN) MODULE_PREFIX=$(module_prefix) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
 
 build-module-playground:
 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
diff --git a/configure.ac b/configure.ac
index 6064dee77ae6..29d3ff8ae41d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -84,6 +84,12 @@ AC_ARG_WITH([rootlibdir],
         [], [with_rootlibdir=$libdir])
 AC_SUBST([rootlibdir], [$with_rootlibdir])
 
+# Ideally this would be $prefix but default to empty for compatibility with earlier versions
+AC_ARG_WITH([module_prefix],
+        AS_HELP_STRING([--with-module-prefix=DIR], [directory in which to look for /lib/modules directory with kernel modules - typically '' or ${prefix}]),
+        [], [with_module_prefix=])
+AC_SUBST([module_prefix], [$with_module_prefix])
+
 AC_ARG_WITH([zstd],
 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
 	[], [with_zstd=no])
@@ -305,6 +311,7 @@ AC_MSG_RESULT([
 	=======
 
 	prefix:			${prefix}
+	module_prefix:		${module_prefix}
 	sysconfdir:		${sysconfdir}
 	libdir:			${libdir}
 	rootlibdir:		${rootlibdir}
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 13c82b069e84..e41acae6f9fc 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -209,7 +209,7 @@ static int log_priority(const char *priority)
 	return 0;
 }
 
-static const char *dirname_default_prefix = "/lib/modules";
+static const char *dirname_default_prefix = MODULE_PREFIX "/lib/modules";
 
 static char *get_kernel_release(const char *dirname)
 {
@@ -231,14 +231,14 @@ static char *get_kernel_release(const char *dirname)
 /**
  * kmod_new:
  * @dirname: what to consider as linux module's directory, if NULL
- *           defaults to /lib/modules/`uname -r`. If it's relative,
+ *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
  *           it's treated as relative to the current working directory.
  *           Otherwise, give an absolute dirname.
  * @config_paths: ordered array of paths (directories or files) where
  *                to load from user-defined configuration parameters such as
  *                alias, blacklists, commands (install, remove). If NULL
  *                defaults to /etc/modprobe.d, /run/modprobe.d,
- *                /usr/local/lib/modprobe.d and PREFIX/lib/modprobe.d. Give an empty
+ *                /usr/local/lib/modprobe.d and ${module_prefix}/lib/modprobe.d. Give an empty
  *                vector if configuration should not be read. This array must
  *                be null terminated.
  *
diff --git a/man/Makefile.am b/man/Makefile.am
index ad07c30bbd24..1a9a92f9c224 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
 	else \
 		sed -e '/@PREFIX@/d' $< ; \
 	fi | \
+	sed -e 's|@MODULE_PREFIX@|$(module_prefix)|g' | \
 	$(XSLT) \
 		-o $@ \
 		--nonet \
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 431ebca6654b..2583a8d8a5fc 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -70,7 +70,7 @@
         </term>
         <listitem>
           <para>
-            This allows you to specify the order in which /lib/modules
+            This allows you to specify the order in which @MODULE_PREFIX@/lib/modules
             (or other configured module location) subdirectories will
             be processed by <command>depmod</command>. Directories are
             listed in order, with the highest priority given to the
@@ -101,7 +101,7 @@
             <command>depmod</command> command. It is possible to
             specify one kernel or all kernels using the * wildcard.
             <replaceable>modulesubdirectory</replaceable> is the
-            name of the subdirectory under /lib/modules (or other
+            name of the subdirectory under @MODULE_PREFIX@/lib/modules (or other
             module location) where the target module is installed.
           </para>
           <para>
@@ -110,7 +110,7 @@
             specifying the following command: "override kmod * extra".
             This will ensure that any matching module name installed
             under the <command>extra</command> subdirectory within
-            /lib/modules (or other module location) will take priority
+            @MODULE_PREFIX@/lib/modules (or other module location) will take priority
             over any likenamed module already provided by the kernel.
           </para>
         </listitem>
diff --git a/man/depmod.xml b/man/depmod.xml
index 3b0097184fd7..9d9cf195a355 100644
--- a/man/depmod.xml
+++ b/man/depmod.xml
@@ -80,7 +80,7 @@
     </para>
     <para> <command>depmod</command> creates a list of module dependencies by
       reading each module under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
+      <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable> and
       determining what symbols it exports and what symbols it needs.  By
       default, this list is written to <filename>modules.dep</filename>, and a
       binary hashed version named <filename>modules.dep.bin</filename>, in the
@@ -141,7 +141,7 @@
         <listitem>
           <para>
             If your modules are not currently in the (normal) directory
-            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
+            <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable>,
             but in a staging area, you can specify a
             <replaceable>basedir</replaceable> which is prepended to the
             directory name.  This <replaceable>basedir</replaceable> is
diff --git a/man/modinfo.xml b/man/modinfo.xml
index 9fe0324a2527..d48c64560e9d 100644
--- a/man/modinfo.xml
+++ b/man/modinfo.xml
@@ -54,7 +54,7 @@
       <command>modinfo</command> extracts information from the Linux Kernel
       modules given on the command line.  If the module name is not a filename,
       then the
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>
+      <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable>
       directory is searched, as is also done by
       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
       when loading kernel modules.
diff --git a/man/modprobe.xml b/man/modprobe.xml
index 91f9e27997cd..6e8bb8c9ce9e 100644
--- a/man/modprobe.xml
+++ b/man/modprobe.xml
@@ -78,7 +78,7 @@
       is no difference between _ and - in module names (automatic
       underscore conversion is performed).
       <command>modprobe</command> looks in the module directory
-      <filename>/lib/modules/`uname -r`</filename> for all
+      <filename>@MODULE_PREFIX@/lib/modules/`uname -r`</filename> for all
       the modules and other files, except for the optional
       configuration files in the
       <filename>/etc/modprobe.d</filename> directory
diff --git a/man/modules.dep.xml b/man/modules.dep.xml
index ed633694ec9e..9130ec392089 100644
--- a/man/modules.dep.xml
+++ b/man/modules.dep.xml
@@ -34,8 +34,8 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/lib/modules/modules.dep</filename></para>
-    <para><filename>/lib/modules/modules.dep.bin</filename></para>
+    <para><filename>@MODULE_PREFIX@/lib/modules/modules.dep</filename></para>
+    <para><filename>@MODULE_PREFIX@/lib/modules/modules.dep.bin</filename></para>
   </refsynopsisdiv>
 
   <refsect1><title>DESCRIPTION</title>
@@ -43,7 +43,7 @@
       <filename>modules.dep.bin</filename> is a binary file generated by
       <command>depmod</command> listing the dependencies for
       every module in the directories under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
+      <filename>@MODULE_PREFIX@/lib/modules/</filename><replaceable>version</replaceable>.
       It is used by kmod tools such as <command>modprobe</command> and
       libkmod.
     </para>
diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
index e6045b0dd932..a7ab09bea2bf 100644
--- a/testsuite/module-playground/Makefile
+++ b/testsuite/module-playground/Makefile
@@ -47,7 +47,7 @@ endif
 
 else
 # normal makefile
-KDIR ?= /lib/modules/`uname -r`/build
+KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
 KVER ?= `uname -r`
 ifeq ($(FAKE_BUILD),)
     FAKE_BUILD=0
diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
index 4440ddcd6b4d..db41da36fedf 100755
--- a/testsuite/setup-rootfs.sh
+++ b/testsuite/setup-rootfs.sh
@@ -16,6 +16,19 @@ create_rootfs() {
 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
 	find "$ROOTFS" -type d -exec chmod +w {} \;
 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
+	if [ -n "$MODULE_PREFIX" ] ; then
+		sed -i -e "s|/lib/modules|$MODULE_PREFIX/lib/modules|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		sed -i -e "s|$MODULE_PREFIX/lib/modules/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
+			version=$(basename $i)
+			[ $version != 'external' ] || continue
+			i=$(dirname $i)
+			lib="$(dirname $i)"
+			up="$(dirname $lib)$MODULE_PREFIX"
+			mkdir -p "$up"
+			mv "$lib" "$up"
+		done
+	fi
 
 	if [ "$SYSCONFDIR" != "/etc" ]; then
 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
@@ -32,57 +45,57 @@ feature_enabled() {
 
 declare -A map
 map=(
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_PREFIX/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_PREFIX/lib/modules/4.4.4/updates/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_PREFIX/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
+    ["test-depmod/detect-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
+    ["test-depmod/search-order-external-first$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-first$MODULE_PREFIX/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_PREFIX/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/override/"]="mod-simple.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
+    ["test-depmod/search-order-override$MODULE_PREFIX/lib/modules/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-override$MODULE_PREFIX/lib/modules/4.4.4/override/"]="mod-simple.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
+    ["test-dependencies$MODULE_PREFIX/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
     ["test-init/"]="mod-simple.ko"
     ["test-remove/"]="mod-simple.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
+    ["test-modprobe/show-depends$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/show-depends$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/show-depends$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
     ["test-modprobe/show-exports/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/force/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel-force/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/softdep-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/softdep-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_PREFIX/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/force$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel$MODULE_PREFIX/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel-force$MODULE_PREFIX/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/alias-to-none$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/module-param-kcmdline$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
     ["test-modprobe/module-from-abspath/home/foo/"]="mod-simple.ko"
     ["test-modprobe/module-from-relpath/home/foo/"]="mod-simple.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-outdir$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-outdir$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-outdir$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
@@ -90,20 +103,20 @@ map=(
     ["test-modinfo/mod-simple-sha256.ko"]="mod-simple.ko"
     ["test-modinfo/mod-simple-pkcs7.ko"]="mod-simple.ko"
     ["test-modinfo/external/lib/modules/external/mod-simple.ko"]="mod-simple.ko"
-    ["test-tools/insert/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-tools/remove/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/insert$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/remove$MODULE_PREFIX/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
 )
 
 gzip_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
+    "test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
     )
 
 xz_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
+    "test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
     )
 
 zstd_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
+    "test-depmod/modules-order-compressed$MODULE_PREFIX/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
     )
 
 attach_sha256_array=(
diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
index 93606947f18a..870f2667862b 100644
--- a/testsuite/test-depmod.c
+++ b/testsuite/test-depmod.c
@@ -27,7 +27,7 @@
 
 #define MODULES_UNAME "4.4.4"
 #define MODULES_ORDER_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-order-compressed"
-#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_modules_order_for_compressed(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -55,8 +55,8 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
 	});
 
 #define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
-#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_UNAME
-#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir" MODULE_PREFIX "/lib/modules/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_modules_outdir(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -87,7 +87,7 @@ DEFINE_TEST(depmod_modules_outdir,
 	});
 
 #define SEARCH_ORDER_SIMPLE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-simple"
-#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_simple(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -114,7 +114,7 @@ DEFINE_TEST(depmod_search_order_simple,
 	});
 
 #define SEARCH_ORDER_SAME_PREFIX_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-same-prefix"
-#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_same_prefix(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -164,7 +164,7 @@ DEFINE_TEST(depmod_detect_loop,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-first"
-#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_external_first(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -191,7 +191,7 @@ DEFINE_TEST(depmod_search_order_external_first,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_LAST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-last"
-#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_external_last(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -218,7 +218,7 @@ DEFINE_TEST(depmod_search_order_external_last,
 	});
 
 #define SEARCH_ORDER_OVERRIDE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-override"
-#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS MODULE_PREFIX "/lib/modules/" MODULES_UNAME
 static noreturn int depmod_search_order_override(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c
index 56e73609f204..903a0102105e 100644
--- a/testsuite/test-testsuite.c
+++ b/testsuite/test-testsuite.c
@@ -64,7 +64,7 @@ static int testsuite_rootfs_fopen(const struct test *t)
 	char s[100];
 	int n;
 
-	fp = fopen("/lib/modules/a", "r");
+	fp = fopen(MODULE_PREFIX "/lib/modules/a", "r");
 	if (fp == NULL)
 		return EXIT_FAILURE;;
 
@@ -89,7 +89,7 @@ static int testsuite_rootfs_open(const struct test *t)
 	char buf[100];
 	int fd, done;
 
-	fd = open("/lib/modules/a", O_RDONLY);
+	fd = open(MODULE_PREFIX "/lib/modules/a", O_RDONLY);
 	if (fd < 0)
 		return EXIT_FAILURE;
 
@@ -121,12 +121,12 @@ static int testsuite_rootfs_stat_access(const struct test *t)
 {
 	struct stat st;
 
-	if (access("/lib/modules/a", F_OK) < 0) {
+	if (access(MODULE_PREFIX "/lib/modules/a", F_OK) < 0) {
 		ERR("access failed: %m\n");
 		return EXIT_FAILURE;
 	}
 
-	if (stat("/lib/modules/a", &st) < 0) {
+	if (stat(MODULE_PREFIX "/lib/modules/a", &st) < 0) {
 		ERR("stat failed: %m\n");
 		return EXIT_FAILURE;
 	}
diff --git a/tools/depmod.c b/tools/depmod.c
index 7e9339923809..686525b4adea 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -911,7 +911,7 @@ struct vertex;
 struct mod {
 	struct kmod_module *kmod;
 	char *path;
-	const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
+	const char *relpath; /* path relative to '$ROOT$MODULE_PREFIX/lib/modules/$VER/' */
 	char *uncrelpath; /* same as relpath but ending in .ko */
 	struct kmod_list *info_list;
 	struct kmod_list *dep_sym_list;
@@ -3024,11 +3024,11 @@ static int do_depmod(int argc, char *argv[])
 	}
 
 	cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
-				  "%s/lib/modules/%s",
+				  "%s" MODULE_PREFIX "/lib/modules/%s",
 				  root ?: "", cfg.kversion);
 
 	cfg.outdirnamelen = snprintf(cfg.outdirname, PATH_MAX,
-				     "%s/lib/modules/%s",
+				     "%s" MODULE_PREFIX "/lib/modules/%s",
 				     out_root ?: (root ?: ""), cfg.kversion);
 
 	if (optind == argc)
diff --git a/tools/kmod.c b/tools/kmod.c
index 5a13716955c1..4384752ab5bc 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -114,6 +114,7 @@ static int kmod_config(int argc, char *argv[])
 {
 	unsigned i;
 	printf("{\"prefix\":\"" PREFIX "\""
+			",\"module_prefix\":\"" MODULE_PREFIX "\""
 			",\"module_signature\":["
 #ifdef ENABLE_OPENSSL
 			"\"PKCS#7\","
diff --git a/tools/modinfo.c b/tools/modinfo.c
index d0aab200af4e..c10ce7ec5ef3 100644
--- a/tools/modinfo.c
+++ b/tools/modinfo.c
@@ -367,7 +367,7 @@ static void help(void)
 		"\t-m, --modname               Handle argument as module name instead of alias or filename\n"
 		"\t-F, --field=FIELD           Print only provided FIELD\n"
 		"\t-k, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
-		"\t-b, --basedir=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-b, --basedir=DIR           Use DIR as filesystem root for " MODULE_PREFIX "/lib/modules\n"
 		"\t-V, --version               Show version\n"
 		"\t-h, --help                  Show this help\n",
 		program_invocation_short_name);
@@ -462,7 +462,7 @@ static int do_modinfo(int argc, char *argv[])
 			}
 			kversion = u.release;
 		}
-		snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
+		snprintf(dirname_buf, sizeof(dirname_buf), "%s" MODULE_PREFIX "/lib/modules/%s",
 			 root, kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/modprobe.c b/tools/modprobe.c
index e891028349a8..f386c57275e0 100644
--- a/tools/modprobe.c
+++ b/tools/modprobe.c
@@ -142,7 +142,7 @@ static void help(void)
 		"\t-n, --show                  Same as --dry-run\n"
 
 		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
-		"\t-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-d, --dirname=DIR           Use DIR as filesystem root for " MODULE_PREFIX "/lib/modules\n"
 		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
 
 		"\t-s, --syslog                print to syslog, not stderr\n"
@@ -999,7 +999,7 @@ static int do_modprobe(int argc, char **orig_argv)
 			kversion = u.release;
 		}
 		snprintf(dirname_buf, sizeof(dirname_buf),
-				"%s/lib/modules/%s", root,
+				"%s" MODULE_PREFIX "/lib/modules/%s", root,
 				kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
index 8d2356da73f3..868af3b58ac7 100644
--- a/tools/static-nodes.c
+++ b/tools/static-nodes.c
@@ -212,15 +212,15 @@ static int do_static_nodes(int argc, char *argv[])
 		goto finish;
 	}
 
-	snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
+	snprintf(modules, sizeof(modules), MODULE_PREFIX "/lib/modules/%s/modules.devname", kernel.release);
 	in = fopen(modules, "re");
 	if (in == NULL) {
 		if (errno == ENOENT) {
-			fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
+			fprintf(stderr, "Warning: " MODULE_PREFIX "/lib/modules/%s/modules.devname not found - ignoring\n",
 				kernel.release);
 			ret = EXIT_SUCCESS;
 		} else {
-			fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
+			fprintf(stderr, "Error: could not open " MODULE_PREFIX "/lib/modules/%s/modules.devname - %m\n",
 				kernel.release);
 			ret = EXIT_FAILURE;
 		}
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH v2--to=linux-modules@vger.kernel.org] depmod: Handle installing modules under a prefix
  2023-07-12 13:45     ` [PATCH v2--to=linux-modules@vger.kernel.org] " Michal Suchanek
@ 2023-07-12 14:14       ` Masahiro Yamada
  2023-07-12 16:15         ` Michal Suchánek
  2023-07-14  6:25       ` [PATCH v2] " Jiri Slaby
  1 sibling, 1 reply; 83+ messages in thread
From: Masahiro Yamada @ 2023-07-12 14:14 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Wed, Jul 12, 2023 at 10:45 PM Michal Suchanek <msuchanek@suse.de> wrote:
>
> Some distributions aim at not shipping any files in / ustside of usr.
>
> The path under which kernel modules are instaleld is hardcoded to /lib
> which conflicts with this goal.
>
> When kmod provides the config command use it to determine the correct
> module installation prefix.
>
> On kmod that does not provide the command / is used as before.
>
> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
> v2: Avoid error on systems with kmod that does not support config
> command
> ---
>  Makefile          | 4 +++-
>  scripts/depmod.sh | 8 ++++----
>  2 files changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 47690c28456a..b1fea135bdec 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
>  # makefile but the argument can be passed to make if needed.
>  #
>
> -MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> +
> +MODLIB = $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)



You can do "make modules_install INSTALL_MOD_PATH=/usr/what/ever/prefix"


This patch is unneeded.












>  export MODLIB
>
>  PHONY += prepare0
> diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> index 3643b4f896ed..88ac79056153 100755
> --- a/scripts/depmod.sh
> +++ b/scripts/depmod.sh
> @@ -27,16 +27,16 @@ fi
>  # numbers, so we cheat with a symlink here
>  depmod_hack_needed=true
>  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
>  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> -       if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> -               -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> +       if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> +               -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
>                 depmod_hack_needed=false
>         fi
>  fi
>  rm -rf "$tmp_dir"
>  if $depmod_hack_needed; then
> -       symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> +       symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_PREFIX/lib/modules/99.98.$KERNELRELEASE"
>         ln -s "$KERNELRELEASE" "$symlink"
>         KERNELRELEASE=99.98.$KERNELRELEASE
>  fi
> --
> 2.41.0
>


-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v2--to=linux-modules@vger.kernel.org] depmod: Handle installing modules under a prefix
  2023-07-12 14:14       ` Masahiro Yamada
@ 2023-07-12 16:15         ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-12 16:15 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Wed, Jul 12, 2023 at 11:14:33PM +0900, Masahiro Yamada wrote:
> On Wed, Jul 12, 2023 at 10:45 PM Michal Suchanek <msuchanek@suse.de> wrote:
> >
> > Some distributions aim at not shipping any files in / ustside of usr.
> >
> > The path under which kernel modules are instaleld is hardcoded to /lib
> > which conflicts with this goal.
> >
> > When kmod provides the config command use it to determine the correct
> > module installation prefix.
> >
> > On kmod that does not provide the command / is used as before.
> >
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v2: Avoid error on systems with kmod that does not support config
> > command
> > ---
> >  Makefile          | 4 +++-
> >  scripts/depmod.sh | 8 ++++----
> >  2 files changed, 7 insertions(+), 5 deletions(-)
> >
> > diff --git a/Makefile b/Makefile
> > index 47690c28456a..b1fea135bdec 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> >  # makefile but the argument can be passed to make if needed.
> >  #
> >
> > -MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> > +
> > +MODLIB = $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> 
> You can do "make modules_install INSTALL_MOD_PATH=/usr/what/ever/prefix"
> 
> This patch is unneeded.

It's very much needed.

INSTALL_MOD_PATH is temporary staging location, KERNEL_MODULE_PREFIX is
permanent prefix under which modules are searched.

Note that depmod.sh does not use INSTALL_MOD_PATH but uses
KERNEL_MODULE_PREFIX.

Support for this is added by the corresponding kmod patchset.

Thanks

Michal

> >  export MODLIB
> >
> >  PHONY += prepare0
> > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > index 3643b4f896ed..88ac79056153 100755
> > --- a/scripts/depmod.sh
> > +++ b/scripts/depmod.sh
> > @@ -27,16 +27,16 @@ fi
> >  # numbers, so we cheat with a symlink here
> >  depmod_hack_needed=true
> >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > -       if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > -               -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > +       if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > +               -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> >                 depmod_hack_needed=false
> >         fi
> >  fi
> >  rm -rf "$tmp_dir"
> >  if $depmod_hack_needed; then
> > -       symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> > +       symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_PREFIX/lib/modules/99.98.$KERNELRELEASE"
> >         ln -s "$KERNELRELEASE" "$symlink"
> >         KERNELRELEASE=99.98.$KERNELRELEASE
> >  fi
> > --
> > 2.41.0
> >
> 
> 
> -- 
> Best Regards
> Masahiro Yamada

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v2] depmod: Handle installing modules under a prefix
  2023-07-12 13:45     ` [PATCH v2--to=linux-modules@vger.kernel.org] " Michal Suchanek
  2023-07-12 14:14       ` Masahiro Yamada
@ 2023-07-14  6:25       ` Jiri Slaby
  2023-07-14 12:21         ` [PATCH v3] " Michal Suchanek
  1 sibling, 1 reply; 83+ messages in thread
From: Jiri Slaby @ 2023-07-14  6:25 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel,
	linux-modules

On 12. 07. 23, 15:45, Michal Suchanek wrote:
> Some distributions aim at not shipping any files in / ustside of usr.

"outside".

> The path under which kernel modules are instaleld is hardcoded to /lib

"installed"

> which conflicts with this goal.
> 
> When kmod provides the config command use it to determine the correct
> module installation prefix.
> 
> On kmod that does not provide the command / is used as before.

Can you spice it with more commas? While the text is understandable 
after a while of staring, it's hard to parse.

Like:
When kmod provides the config command, use it to determine the correct 
module installation prefix.

On kmod that does not provide the command, / is used as before.





I would also argue here in the commit log on what Masahiro already 
pointed out. I.e. that INSTALL_MOD_PATH is useless in this case and why.

> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
> v2: Avoid error on systems with kmod that does not support config
> command
> ---
>   Makefile          | 4 +++-
>   scripts/depmod.sh | 8 ++++----
>   2 files changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 47690c28456a..b1fea135bdec 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
>   # makefile but the argument can be passed to make if needed.
>   #
>   
> -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> +
> +MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
>   export MODLIB
>   
>   PHONY += prepare0
> diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> index 3643b4f896ed..88ac79056153 100755
> --- a/scripts/depmod.sh
> +++ b/scripts/depmod.sh
> @@ -27,16 +27,16 @@ fi
>   # numbers, so we cheat with a symlink here
>   depmod_hack_needed=true
>   tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
>   if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> -	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> -		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> +	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> +		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
>   		depmod_hack_needed=false
>   	fi
>   fi
>   rm -rf "$tmp_dir"
>   if $depmod_hack_needed; then
> -	symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> +	symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_PREFIX/lib/modules/99.98.$KERNELRELEASE"
>   	ln -s "$KERNELRELEASE" "$symlink"
>   	KERNELRELEASE=99.98.$KERNELRELEASE
>   fi

-- 
js
suse labs


^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14  6:25       ` [PATCH v2] " Jiri Slaby
@ 2023-07-14 12:21         ` Michal Suchanek
  2023-07-14 13:38           ` Jan Engelhardt
  2023-07-14 14:05           ` Nicolas Schier
  0 siblings, 2 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-14 12:21 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Some distributions aim at not shipping any files in / outside of usr.

The path under which kernel modules are installed is hardcoded to /lib
which conflicts with this goal.

When kmod provides the config command, use it to determine the correct
module installation prefix.

This is a prefix under which the modules are searched by kmod on the
system, and is separate from the temporary staging location already
supported by INSTALL_MOD_PATH.

With kmod that does not provide the config command empty prefix is used
as before.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Avoid error on systems with kmod that does not support config
command
v3: More verbose commit message
---
 Makefile          | 4 +++-
 scripts/depmod.sh | 8 ++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 47690c28456a..b1fea135bdec 100644
--- a/Makefile
+++ b/Makefile
@@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
 # makefile but the argument can be passed to make if needed.
 #
 
-MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
+export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
+
+MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
 export MODLIB
 
 PHONY += prepare0
diff --git a/scripts/depmod.sh b/scripts/depmod.sh
index 3643b4f896ed..88ac79056153 100755
--- a/scripts/depmod.sh
+++ b/scripts/depmod.sh
@@ -27,16 +27,16 @@ fi
 # numbers, so we cheat with a symlink here
 depmod_hack_needed=true
 tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
-mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
+mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
 if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
-	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
-		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
+	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
+		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
 		depmod_hack_needed=false
 	fi
 fi
 rm -rf "$tmp_dir"
 if $depmod_hack_needed; then
-	symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
+	symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_PREFIX/lib/modules/99.98.$KERNELRELEASE"
 	ln -s "$KERNELRELEASE" "$symlink"
 	KERNELRELEASE=99.98.$KERNELRELEASE
 fi
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 12:21         ` [PATCH v3] " Michal Suchanek
@ 2023-07-14 13:38           ` Jan Engelhardt
  2023-07-14 13:57             ` Michal Suchánek
  2023-07-14 13:59             ` Michal Koutný
  2023-07-14 14:05           ` Nicolas Schier
  1 sibling, 2 replies; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-14 13:38 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Friday 2023-07-14 14:21, Michal Suchanek wrote:

>Some distributions aim at not shipping any files in / outside of usr.
>
>The path under which kernel modules are installed is hardcoded to /lib
>which conflicts with this goal.
>
>+MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)

Ok, so if the problem statement is that hardcoded paths are bad, then why
continue to hardcode the "/lib/modules" fragment? Just make it so that
KERNEL_MODULE_PREFIX can be set to the exact string "/usr/lib/modules" and not
just "/usr".

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-12 14:00 ` [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
@ 2023-07-14 13:52   ` Jan Engelhardt
  2023-07-14 14:02     ` Michal Suchánek
  2023-07-14 15:26   ` Nicolas Schier
  1 sibling, 1 reply; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-14 13:52 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Wednesday 2023-07-12 16:00, Michal Suchanek wrote:

>Show prefix (where configuration files are searched/to be installed),
>module compressions, and module signatures supported.

What about doing it like systemd and generate a .pc file instead 
that can then be queried like so, e.g.:

$ pkg-config kmod --variable=modulesdir
/usr/lib/modules

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix}
  2023-07-12 14:00 ` [PATCH kmod v2 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix} Michal Suchanek
@ 2023-07-14 13:54   ` Jan Engelhardt
  0 siblings, 0 replies; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-14 13:54 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Wednesday 2023-07-12 16:00, Michal Suchanek wrote:

>modprobe.d is now searched under ${prefix}/lib, add ${module_prefix} to
>specify the directory where to search for kernel modules.
>
>With this distributions that do not want to ship files in /lib can also
>move kernel modules to /usr while others can keep them in /lib.

>+# Ideally this would be $prefix but default to empty for compatibility with earlier versions
>+AC_ARG_WITH([module_prefix],
>+        AS_HELP_STRING([--with-module-prefix=DIR], [directory in which to look for /lib/modules directory with kernel modules - typically '' or ${prefix}]),
>+        [], [with_module_prefix=])
>+AC_SUBST([module_prefix], [$with_module_prefix])

Why stop there, let's make it fully configurable such that

 ./configure --with-module-prefix=/usr/lib/modules

works. Then one can specify arbitrary paths without fearing of getting a
/lib/modules tacked on anywhere down the line.

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 13:38           ` Jan Engelhardt
@ 2023-07-14 13:57             ` Michal Suchánek
  2023-07-14 13:59             ` Michal Koutný
  1 sibling, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-14 13:57 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

Hello,

On Fri, Jul 14, 2023 at 03:38:18PM +0200, Jan Engelhardt wrote:
> 
> On Friday 2023-07-14 14:21, Michal Suchanek wrote:
> 
> >Some distributions aim at not shipping any files in / outside of usr.
> >
> >The path under which kernel modules are installed is hardcoded to /lib
> >which conflicts with this goal.
> >
> >+MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> 
> Ok, so if the problem statement is that hardcoded paths are bad, then why
> continue to hardcode the "/lib/modules" fragment? Just make it so that
> KERNEL_MODULE_PREFIX can be set to the exact string "/usr/lib/modules" and not
> just "/usr".

That's certainly an option.

The feature is modelled after the installation prefix option that can
move the whole filesystem hierarchy of installed files under /usr/local,
/opt, or any other directory of choice. However, in that case the
subdirectories in the hierarchy can be configured as well while in this
case /lib/modules remains hardcoded.

Making it possible to set the whole path is generally more flexible
although there is no need to set the later part for this particular use
case.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 13:38           ` Jan Engelhardt
  2023-07-14 13:57             ` Michal Suchánek
@ 2023-07-14 13:59             ` Michal Koutný
  2023-07-14 14:05               ` Michal Suchánek
  1 sibling, 1 reply; 83+ messages in thread
From: Michal Koutný @ 2023-07-14 13:59 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: Michal Suchanek, linux-modules, Takashi Iwai, Lucas De Marchi,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 474 bytes --]

On Fri, Jul 14, 2023 at 03:38:18PM +0200, Jan Engelhardt <jengelh@inai.de> wrote:
> Ok, so if the problem statement is that hardcoded paths are bad, then why
> continue to hardcode the "/lib/modules" fragment? Just make it so that
> KERNEL_MODULE_PREFIX can be set to the exact string "/usr/lib/modules" and not
> just "/usr".

That sounds cleaner but I'm worried it would be a BC break in setups
that expect the existing layout under INSTALL_MOD_PATH, wouldn't it?

Michal

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-14 13:52   ` Jan Engelhardt
@ 2023-07-14 14:02     ` Michal Suchánek
  2023-07-14 14:12       ` Jan Engelhardt
  0 siblings, 1 reply; 83+ messages in thread
From: Michal Suchánek @ 2023-07-14 14:02 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

Hello,

On Fri, Jul 14, 2023 at 03:52:05PM +0200, Jan Engelhardt wrote:
> 
> On Wednesday 2023-07-12 16:00, Michal Suchanek wrote:
> 
> >Show prefix (where configuration files are searched/to be installed),
> >module compressions, and module signatures supported.
> 
> What about doing it like systemd and generate a .pc file instead 
> that can then be queried like so, e.g.:
> 
> $ pkg-config kmod --variable=modulesdir
> /usr/lib/modules

 - AFAICS tools packed with kernel generate but do not consume .pc files
   while JSON and jq are commonly used througout the kernel ecosystem
 - .pc files would be shipped with libkmod development package, not the
   kmod tool in a binary distribution

Other than that JSON and .pc files are roughly quivalent in usability.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 12:21         ` [PATCH v3] " Michal Suchanek
  2023-07-14 13:38           ` Jan Engelhardt
@ 2023-07-14 14:05           ` Nicolas Schier
  2023-07-14 14:30             ` Michal Suchánek
  1 sibling, 1 reply; 83+ messages in thread
From: Nicolas Schier @ 2023-07-14 14:05 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 3683 bytes --]

On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> Some distributions aim at not shipping any files in / outside of usr.

For me, preventing negation often makes things easier, e.g.: "... aim at
shipping files only below /usr".

> 
> The path under which kernel modules are installed is hardcoded to /lib
> which conflicts with this goal.
> 
> When kmod provides the config command, use it to determine the correct
> module installation prefix.
> 
> This is a prefix under which the modules are searched by kmod on the
> system, and is separate from the temporary staging location already
> supported by INSTALL_MOD_PATH.
> 
> With kmod that does not provide the config command empty prefix is used
> as before.
> 
> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
> v2: Avoid error on systems with kmod that does not support config
> command
> v3: More verbose commit message
> ---
>  Makefile          | 4 +++-
>  scripts/depmod.sh | 8 ++++----
>  2 files changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 47690c28456a..b1fea135bdec 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
>  # makefile but the argument can be passed to make if needed.
>  #
>  
> -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)

All other calls of `jq` that I could find are located at tools/; as this here
is evaluated on each invocation, this should probably be documented in
Documentation/process/changes.rst?

(Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)

> +
> +MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
>  export MODLIB
>  
>  PHONY += prepare0
> diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> index 3643b4f896ed..88ac79056153 100755
> --- a/scripts/depmod.sh
> +++ b/scripts/depmod.sh
> @@ -27,16 +27,16 @@ fi
>  # numbers, so we cheat with a symlink here
>  depmod_hack_needed=true
>  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
>  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> -	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> -		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> +	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> +		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
>  		depmod_hack_needed=false
>  	fi
>  fi

I'd like to come back to the statement from Masahiro: Is the check above,
against some very old versions of depmod [1], the only reason for this patch?  

If we could remove that, would

    make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install

be sufficient?

Kind regards,
Nicolas


[1]: https://lore.kernel.org/linux-kbuild/1307631448-29848-5-git-send-email-mmarek@suse.cz/

>  rm -rf "$tmp_dir"
>  if $depmod_hack_needed; then
> -	symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> +	symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_PREFIX/lib/modules/99.98.$KERNELRELEASE"
>  	ln -s "$KERNELRELEASE" "$symlink"
>  	KERNELRELEASE=99.98.$KERNELRELEASE
>  fi
> -- 
> 2.41.0

-- 
epost|xmpp: nicolas@fjasle.eu          irc://oftc.net/nsc
↳ gpg: 18ed 52db e34f 860e e9fb  c82b 7d97 0932 55a0 ce7f
     -- frykten for herren er opphav til kunnskap --

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 13:59             ` Michal Koutný
@ 2023-07-14 14:05               ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-14 14:05 UTC (permalink / raw)
  To: Michal Koutný
  Cc: Jan Engelhardt, linux-modules, Takashi Iwai, Lucas De Marchi,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

Hello,

On Fri, Jul 14, 2023 at 03:59:17PM +0200, Michal Koutný wrote:
> On Fri, Jul 14, 2023 at 03:38:18PM +0200, Jan Engelhardt <jengelh@inai.de> wrote:
> > Ok, so if the problem statement is that hardcoded paths are bad, then why
> > continue to hardcode the "/lib/modules" fragment? Just make it so that
> > KERNEL_MODULE_PREFIX can be set to the exact string "/usr/lib/modules" and not
> > just "/usr".
> 
> That sounds cleaner but I'm worried it would be a BC break in setups
> that expect the existing layout under INSTALL_MOD_PATH, wouldn't it?

It's a break either way, the expected directory righ now is exactly
/lib/modules. /usr/lib/modules works to some extent for some use cases
only when the compatibility symlink lib -> usr/lib is present.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-14 14:02     ` Michal Suchánek
@ 2023-07-14 14:12       ` Jan Engelhardt
  0 siblings, 0 replies; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-14 14:12 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Friday 2023-07-14 16:02, Michal Suchánek wrote:
>> 
>> What about doing it like systemd and generate a .pc file instead 
>> that can then be queried like so, e.g.:
>> 
>> $ pkg-config kmod --variable=modulesdir
>
> - .pc files would be shipped with libkmod development package, not the
>   kmod tool in a binary distribution

On that point: No, they would not.
Again, confer with systemd: systemd.rpm(SUSE) provides systemd.pc,
systemd-devel.rpm provides libsystemd.pc. kmod.rpm would provide
kmod.pc, kmod-devel.rpm would continue to provide libkmod.pc as
it does today.

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  2023-07-12 14:00 ` [PATCH kmod v2 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
@ 2023-07-14 14:16   ` Nicolas Schier
  2023-07-14 14:34     ` Michal Suchánek
  0 siblings, 1 reply; 83+ messages in thread
From: Nicolas Schier @ 2023-07-14 14:16 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

On Wed, Jul 12, 2023 at 04:00:46PM +0200 Michal Suchanek wrote:
> There is an ongoing effort to limit use of files outside of /usr (or
> $prefix on general). Currently all modprobe.d paths are hardcoded to
> outside of $prefix. Teach kmod to load modprobe.d from $prefix/lib.
> 
> On some distributions /usr/lib and /lib are the same directory because
> of a compatibility symlink, and it is possible to craft configuration
> files with sideeffects that would behave differently when loaded twice.
> However, the override semantic ensures that one 'overrides' the other,
> and only one configuration file of the same name is loaded from any of
> the search directories.
> 
> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
> v2: Fix commit message typo
> ---
>  Makefile.am        | 1 +
>  libkmod/libkmod.c  | 3 ++-
>  man/Makefile.am    | 9 +++++++--
>  man/depmod.d.xml   | 1 +
>  man/modprobe.d.xml | 1 +
>  tools/depmod.c     | 1 +
>  6 files changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/Makefile.am b/Makefile.am
> index 8ba85c91a0f3..7aa5bfa5638d 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -19,6 +19,7 @@ AM_CPPFLAGS = \
>  	-include $(top_builddir)/config.h \
>  	-I$(top_srcdir) \
>  	-DSYSCONFDIR=\""$(sysconfdir)"\" \
> +	-DPREFIX=\""$(prefix)"\" \
>  	${zlib_CFLAGS}
>  
>  AM_CFLAGS = $(OUR_CFLAGS)
> diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> index 2670f9a4611a..13c82b069e84 100644
> --- a/libkmod/libkmod.c
> +++ b/libkmod/libkmod.c
> @@ -65,6 +65,7 @@ static const char *const default_config_paths[] = {
>  	SYSCONFDIR "/modprobe.d",
>  	"/run/modprobe.d",
>  	"/usr/local/lib/modprobe.d",
> +	PREFIX "/lib/modprobe.d",
>  	"/lib/modprobe.d",
>  	NULL
>  };
> @@ -237,7 +238,7 @@ static char *get_kernel_release(const char *dirname)
>   *                to load from user-defined configuration parameters such as
>   *                alias, blacklists, commands (install, remove). If NULL
>   *                defaults to /etc/modprobe.d, /run/modprobe.d,
> - *                /usr/local/lib/modprobe.d and /lib/modprobe.d. Give an empty
> + *                /usr/local/lib/modprobe.d and PREFIX/lib/modprobe.d. Give an empty

In the chunk above, there still is /lib/modprobe.d included in the
default_config_paths array.

Kind regards,
Nicolas


-- 
epost|xmpp: nicolas@fjasle.eu          irc://oftc.net/nsc
↳ gpg: 18ed 52db e34f 860e e9fb  c82b 7d97 0932 55a0 ce7f
     -- frykten for herren er opphav til kunnskap --

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 14:05           ` Nicolas Schier
@ 2023-07-14 14:30             ` Michal Suchánek
  2023-07-14 14:42               ` Jan Engelhardt
  2023-07-14 14:54               ` Nicolas Schier
  0 siblings, 2 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-14 14:30 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

Hello,

On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > Some distributions aim at not shipping any files in / outside of usr.
> 
> For me, preventing negation often makes things easier, e.g.: "... aim at
> shipping files only below /usr".
> 
> > 
> > The path under which kernel modules are installed is hardcoded to /lib
> > which conflicts with this goal.
> > 
> > When kmod provides the config command, use it to determine the correct
> > module installation prefix.
> > 
> > This is a prefix under which the modules are searched by kmod on the
> > system, and is separate from the temporary staging location already
> > supported by INSTALL_MOD_PATH.
> > 
> > With kmod that does not provide the config command empty prefix is used
> > as before.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v2: Avoid error on systems with kmod that does not support config
> > command
> > v3: More verbose commit message
> > ---
> >  Makefile          | 4 +++-
> >  scripts/depmod.sh | 8 ++++----
> >  2 files changed, 7 insertions(+), 5 deletions(-)
> > 
> > diff --git a/Makefile b/Makefile
> > index 47690c28456a..b1fea135bdec 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> >  # makefile but the argument can be passed to make if needed.
> >  #
> >  
> > -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> 
> All other calls of `jq` that I could find are located at tools/; as this here
> is evaluated on each invocation, this should probably be documented in
> Documentation/process/changes.rst?
> 
> (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)

That's a good point.

> 
> > +
> > +MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> >  export MODLIB
> >  
> >  PHONY += prepare0
> > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > index 3643b4f896ed..88ac79056153 100755
> > --- a/scripts/depmod.sh
> > +++ b/scripts/depmod.sh
> > @@ -27,16 +27,16 @@ fi
> >  # numbers, so we cheat with a symlink here
> >  depmod_hack_needed=true
> >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > -	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > -		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > +	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > +		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> >  		depmod_hack_needed=false
> >  	fi
> >  fi
> 
> I'd like to come back to the statement from Masahiro: Is the check above,
> against some very old versions of depmod [1], the only reason for this patch?  
> 
> If we could remove that, would
> 
>     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> 
> be sufficient?

No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
the newly added part is not because it's integral part of where the
modules are installed on the system, and not the staging area path.

> 
> Kind regards,
> Nicolas
> 
> 
> [1]: https://lore.kernel.org/linux-kbuild/1307631448-29848-5-git-send-email-mmarek@suse.cz/

Was busybox ever fixed to not require the hack?

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  2023-07-14 14:16   ` Nicolas Schier
@ 2023-07-14 14:34     ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-14 14:34 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

On Fri, Jul 14, 2023 at 04:16:44PM +0200, Nicolas Schier wrote:
> On Wed, Jul 12, 2023 at 04:00:46PM +0200 Michal Suchanek wrote:
> > There is an ongoing effort to limit use of files outside of /usr (or
> > $prefix on general). Currently all modprobe.d paths are hardcoded to
> > outside of $prefix. Teach kmod to load modprobe.d from $prefix/lib.
> > 
> > On some distributions /usr/lib and /lib are the same directory because
> > of a compatibility symlink, and it is possible to craft configuration
> > files with sideeffects that would behave differently when loaded twice.
> > However, the override semantic ensures that one 'overrides' the other,
> > and only one configuration file of the same name is loaded from any of
> > the search directories.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v2: Fix commit message typo
> > ---
> >  Makefile.am        | 1 +
> >  libkmod/libkmod.c  | 3 ++-
> >  man/Makefile.am    | 9 +++++++--
> >  man/depmod.d.xml   | 1 +
> >  man/modprobe.d.xml | 1 +
> >  tools/depmod.c     | 1 +
> >  6 files changed, 13 insertions(+), 3 deletions(-)
> > 
> > diff --git a/Makefile.am b/Makefile.am
> > index 8ba85c91a0f3..7aa5bfa5638d 100644
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > @@ -19,6 +19,7 @@ AM_CPPFLAGS = \
> >  	-include $(top_builddir)/config.h \
> >  	-I$(top_srcdir) \
> >  	-DSYSCONFDIR=\""$(sysconfdir)"\" \
> > +	-DPREFIX=\""$(prefix)"\" \
> >  	${zlib_CFLAGS}
> >  
> >  AM_CFLAGS = $(OUR_CFLAGS)
> > diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> > index 2670f9a4611a..13c82b069e84 100644
> > --- a/libkmod/libkmod.c
> > +++ b/libkmod/libkmod.c
> > @@ -65,6 +65,7 @@ static const char *const default_config_paths[] = {
> >  	SYSCONFDIR "/modprobe.d",
> >  	"/run/modprobe.d",
> >  	"/usr/local/lib/modprobe.d",
> > +	PREFIX "/lib/modprobe.d",
> >  	"/lib/modprobe.d",
> >  	NULL
> >  };
> > @@ -237,7 +238,7 @@ static char *get_kernel_release(const char *dirname)
> >   *                to load from user-defined configuration parameters such as
> >   *                alias, blacklists, commands (install, remove). If NULL
> >   *                defaults to /etc/modprobe.d, /run/modprobe.d,
> > - *                /usr/local/lib/modprobe.d and /lib/modprobe.d. Give an empty
> > + *                /usr/local/lib/modprobe.d and PREFIX/lib/modprobe.d. Give an empty
> 
> In the chunk above, there still is /lib/modprobe.d included in the
> default_config_paths array.

Indeed, if this is rendered as documentation somewhere this would be
misleading.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 14:30             ` Michal Suchánek
@ 2023-07-14 14:42               ` Jan Engelhardt
  2023-07-14 14:54               ` Nicolas Schier
  1 sibling, 0 replies; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-14 14:42 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: Nicolas Schier, linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	linux-kbuild, linux-kernel


On Friday 2023-07-14 16:30, Michal Suchánek wrote:
>> >  
>> > -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
>> > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
>> 
>> All other calls of `jq` that I could find are located at tools/; as this here
>> is evaluated on each invocation, this should probably be documented in
>> Documentation/process/changes.rst?
>> 
>> (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
>
>That's a good point.

Speaking of which, "&>" is a bashism and probably should be replaced.

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 14:30             ` Michal Suchánek
  2023-07-14 14:42               ` Jan Engelhardt
@ 2023-07-14 14:54               ` Nicolas Schier
  2023-07-14 15:10                 ` Michal Suchánek
  2023-07-17 19:13                 ` [PATCH v3] depmod: Handle installing modules under a prefix Masahiro Yamada
  1 sibling, 2 replies; 83+ messages in thread
From: Nicolas Schier @ 2023-07-14 14:54 UTC (permalink / raw)
  To: Michal Such�nek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutn�,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 5213 bytes --]

On Fri, Jul 14, 2023 at 04:30:02PM +0200, Michal Such�nek wrote:
> Hello,
> 
> On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> > On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > > Some distributions aim at not shipping any files in / outside of usr.
> > 
> > For me, preventing negation often makes things easier, e.g.: "... aim at
> > shipping files only below /usr".
> > 
> > > 
> > > The path under which kernel modules are installed is hardcoded to /lib
> > > which conflicts with this goal.
> > > 
> > > When kmod provides the config command, use it to determine the correct
> > > module installation prefix.
> > > 
> > > This is a prefix under which the modules are searched by kmod on the
> > > system, and is separate from the temporary staging location already
> > > supported by INSTALL_MOD_PATH.
> > > 
> > > With kmod that does not provide the config command empty prefix is used
> > > as before.
> > > 
> > > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > > ---
> > > v2: Avoid error on systems with kmod that does not support config
> > > command
> > > v3: More verbose commit message
> > > ---
> > >  Makefile          | 4 +++-
> > >  scripts/depmod.sh | 8 ++++----
> > >  2 files changed, 7 insertions(+), 5 deletions(-)
> > > 
> > > diff --git a/Makefile b/Makefile
> > > index 47690c28456a..b1fea135bdec 100644
> > > --- a/Makefile
> > > +++ b/Makefile
> > > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> > >  # makefile but the argument can be passed to make if needed.
> > >  #
> > >  
> > > -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> > 
> > All other calls of `jq` that I could find are located at tools/; as this here
> > is evaluated on each invocation, this should probably be documented in
> > Documentation/process/changes.rst?
> > 
> > (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
> 
> That's a good point.
> 
> > 
> > > +
> > > +MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> > >  export MODLIB
> > >  
> > >  PHONY += prepare0
> > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > index 3643b4f896ed..88ac79056153 100755
> > > --- a/scripts/depmod.sh
> > > +++ b/scripts/depmod.sh
> > > @@ -27,16 +27,16 @@ fi
> > >  # numbers, so we cheat with a symlink here
> > >  depmod_hack_needed=true
> > >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> > >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > > -	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > -		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > +	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > +		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > >  		depmod_hack_needed=false
> > >  	fi
> > >  fi
> > 
> > I'd like to come back to the statement from Masahiro: Is the check above,
> > against some very old versions of depmod [1], the only reason for this patch?  
> > 
> > If we could remove that, would
> > 
> >     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> > 
> > be sufficient?
> 
> No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
> the newly added part is not because it's integral part of where the
> modules are installed on the system, and not the staging area path.

Ah, thanks.  So just for my understanding, could this be a (non-gentle)
alternative version of your patch, w/o modifying top-level Makefile?

diff --git a/scripts/depmod.sh b/scripts/depmod.sh
index 3643b4f896ed..72c819de0669 100755
--- a/scripts/depmod.sh
+++ b/scripts/depmod.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
 #
 # A depmod wrapper used by the toplevel Makefile
@@ -23,6 +23,8 @@ if [ -z $(command -v $DEPMOD) ]; then
        exit 0
 fi
 
+kmod_version=$(( $(kmod --version | sed -rne 's/^kmod version ([0-9]+).*$/\1/p') ))
+
 # older versions of depmod require the version string to start with three
 # numbers, so we cheat with a symlink here
 depmod_hack_needed=true
@@ -35,6 +37,13 @@ if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
        fi
 fi
 rm -rf "$tmp_dir"
+
+if [ "${kmod_version}" -gt 32 ]; then
+       kmod_prefix="$(kmod config | jq -r .module_prefix)"
+       INSTALL_MOD_PATH="${INSTALL_MOD_PATH#${kmod_prefix}"
+       depmod_hack_needed=false
+fi
+
 if $depmod_hack_needed; then
        symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
        ln -s "$KERNELRELEASE" "$symlink"

(untested, and assuming that kmod module prefix is in kmod >= 32)

Or are I am still missing something?

> Was busybox ever fixed to not require the hack?

I haven't checked that, yet.

Kind regards,
Nicolas

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 14:54               ` Nicolas Schier
@ 2023-07-14 15:10                 ` Michal Suchánek
  2023-07-14 19:37                   ` Nicolas Schier
  2023-07-17 19:14                   ` Masahiro Yamada
  2023-07-17 19:13                 ` [PATCH v3] depmod: Handle installing modules under a prefix Masahiro Yamada
  1 sibling, 2 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-14 15:10 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutn�,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

On Fri, Jul 14, 2023 at 04:54:49PM +0200, Nicolas Schier wrote:
> On Fri, Jul 14, 2023 at 04:30:02PM +0200, Michal Such�nek wrote:
> > Hello,
> > 
> > On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> > > On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > > > Some distributions aim at not shipping any files in / outside of usr.
> > > 
> > > For me, preventing negation often makes things easier, e.g.: "... aim at
> > > shipping files only below /usr".
> > > 
> > > > 
> > > > The path under which kernel modules are installed is hardcoded to /lib
> > > > which conflicts with this goal.
> > > > 
> > > > When kmod provides the config command, use it to determine the correct
> > > > module installation prefix.
> > > > 
> > > > This is a prefix under which the modules are searched by kmod on the
> > > > system, and is separate from the temporary staging location already
> > > > supported by INSTALL_MOD_PATH.
> > > > 
> > > > With kmod that does not provide the config command empty prefix is used
> > > > as before.
> > > > 
> > > > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > > > ---
> > > > v2: Avoid error on systems with kmod that does not support config
> > > > command
> > > > v3: More verbose commit message
> > > > ---
> > > >  Makefile          | 4 +++-
> > > >  scripts/depmod.sh | 8 ++++----
> > > >  2 files changed, 7 insertions(+), 5 deletions(-)
> > > > 
> > > > diff --git a/Makefile b/Makefile
> > > > index 47690c28456a..b1fea135bdec 100644
> > > > --- a/Makefile
> > > > +++ b/Makefile
> > > > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> > > >  # makefile but the argument can be passed to make if needed.
> > > >  #
> > > >  
> > > > -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > > > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> > > 
> > > All other calls of `jq` that I could find are located at tools/; as this here
> > > is evaluated on each invocation, this should probably be documented in
> > > Documentation/process/changes.rst?
> > > 
> > > (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
> > 
> > That's a good point.
> > 
> > > 
> > > > +
> > > > +MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> > > >  export MODLIB
> > > >  
> > > >  PHONY += prepare0
> > > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > > index 3643b4f896ed..88ac79056153 100755
> > > > --- a/scripts/depmod.sh
> > > > +++ b/scripts/depmod.sh
> > > > @@ -27,16 +27,16 @@ fi
> > > >  # numbers, so we cheat with a symlink here
> > > >  depmod_hack_needed=true
> > > >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > > > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > > > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> > > >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > > > -	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > -		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > +	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > +		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > >  		depmod_hack_needed=false
> > > >  	fi
> > > >  fi
> > > 
> > > I'd like to come back to the statement from Masahiro: Is the check above,
> > > against some very old versions of depmod [1], the only reason for this patch?  
> > > 
> > > If we could remove that, would
> > > 
> > >     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> > > 
> > > be sufficient?
> > 
> > No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
> > the newly added part is not because it's integral part of where the
> > modules are installed on the system, and not the staging area path.
> 
> Ah, thanks.  So just for my understanding, could this be a (non-gentle)
> alternative version of your patch, w/o modifying top-level Makefile?
> 
> diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> index 3643b4f896ed..72c819de0669 100755
> --- a/scripts/depmod.sh
> +++ b/scripts/depmod.sh
> @@ -1,4 +1,4 @@
> -#!/bin/sh
> +#!/bin/bash
>  # SPDX-License-Identifier: GPL-2.0
>  #
>  # A depmod wrapper used by the toplevel Makefile
> @@ -23,6 +23,8 @@ if [ -z $(command -v $DEPMOD) ]; then
>         exit 0
>  fi
>  
> +kmod_version=$(( $(kmod --version | sed -rne 's/^kmod version ([0-9]+).*$/\1/p') ))
> +
>  # older versions of depmod require the version string to start with three
>  # numbers, so we cheat with a symlink here
>  depmod_hack_needed=true
> @@ -35,6 +37,13 @@ if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
>         fi
>  fi
>  rm -rf "$tmp_dir"
> +
> +if [ "${kmod_version}" -gt 32 ]; then
> +       kmod_prefix="$(kmod config | jq -r .module_prefix)"
> +       INSTALL_MOD_PATH="${INSTALL_MOD_PATH#${kmod_prefix}"
> +       depmod_hack_needed=false
> +fi
> +
>  if $depmod_hack_needed; then
>         symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
>         ln -s "$KERNELRELEASE" "$symlink"
> 
> (untested, and assuming that kmod module prefix is in kmod >= 32)

It can be detected by running the 'kmod config' command first and
ignoring the output when it fails which the above patch already did.
The version check does not sound very reliable.

> Or are I am still missing something?

MODLIB still needs to include the extra prefix so that files are
installed in the correct location. And that's defined in the toplevel
Makefile.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-12 14:00 ` [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
  2023-07-14 13:52   ` Jan Engelhardt
@ 2023-07-14 15:26   ` Nicolas Schier
  2023-07-14 16:58     ` Michal Suchánek
  1 sibling, 1 reply; 83+ messages in thread
From: Nicolas Schier @ 2023-07-14 15:26 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2915 bytes --]

On Wed, Jul 12, 2023 at 04:00:47PM +0200 Michal Suchanek wrote:
> Show prefix (where configuration files are searched/to be installed),
> module compressions, and module signatures supported.
> 
> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
> v2: mention module signature in commit message
> ---
>  man/kmod.xml |  6 ++++++
>  tools/kmod.c | 39 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 45 insertions(+)
> 
> diff --git a/man/kmod.xml b/man/kmod.xml
> index 0706ad58c2cc..f992a500f836 100644
> --- a/man/kmod.xml
> +++ b/man/kmod.xml
> @@ -71,6 +71,12 @@
>            <para>Show the help message.</para>
>          </listitem>
>        </varlistentry>
> +      <varlistentry>
> +        <term><command>config</command></term>
> +        <listitem>
> +          <para>Show compile time options in JSON.</para>
> +        </listitem>
> +      </varlistentry>
>        <varlistentry>
>          <term><command>list</command></term>
>          <listitem>
> diff --git a/tools/kmod.c b/tools/kmod.c
> index 55689c075ab1..5a13716955c1 100644
> --- a/tools/kmod.c
> +++ b/tools/kmod.c
> @@ -37,9 +37,11 @@ static const struct option options[] = {
>  };
>  
>  static const struct kmod_cmd kmod_cmd_help;
> +static const struct kmod_cmd kmod_cmd_config;
>  
>  static const struct kmod_cmd *kmod_cmds[] = {
>  	&kmod_cmd_help,
> +	&kmod_cmd_config,
>  	&kmod_cmd_list,
>  	&kmod_cmd_static_nodes,
>  
> @@ -95,6 +97,43 @@ static const struct kmod_cmd kmod_cmd_help = {
>  	.help = "Show help message",
>  };
>  
> +static const char *compressions[] = {
> +#ifdef ENABLE_ZSTD
> +			"zstd",
> +#endif
> +#ifdef ENABLE_XZ
> +			"xz",
> +#endif
> +#ifdef ENABLE_ZLIB
> +			"gz",
> +#endif
> +			NULL
> +};
> +
> +static int kmod_config(int argc, char *argv[])
> +{
> +	unsigned i;
> +	printf("{\"prefix\":\"" PREFIX "\""
> +			",\"module_signature\":["
> +#ifdef ENABLE_OPENSSL
> +			"\"PKCS#7\","
> +#endif
> +			"\"legacy\"]"
> +			",\"module_compression\":[");
> +	for(i = 0; compressions[i]; i++) {
> +		printf("%s\"%s\"", i ? "," : "", compressions[i]);
> +	}
> +	printf("]}\n");
> +
> +	return EXIT_SUCCESS;
> +}
> +
> +static const struct kmod_cmd kmod_cmd_config = {
> +	.name = "config",
> +	.cmd = kmod_config,
> +	.help = "Show compile time options in JSON",
> +};
> +
>  static int handle_kmod_commands(int argc, char *argv[])
>  {
>  	const char *cmd;
> -- 
> 2.41.0

If kmod could show selected configs without some (JSON) syntax
around, it could simplify its proposed use in kbuild. E.g.:

    kmod config prefix 2>/dev/null

instead of

    kmod config &>/dev/null && kmod config | jq -r .prefix
.

-- 
epost|xmpp: nicolas@fjasle.eu          irc://oftc.net/nsc
↳ gpg: 18ed 52db e34f 860e e9fb  c82b 7d97 0932 55a0 ce7f
     -- frykten for herren er opphav til kunnskap --

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-14 15:26   ` Nicolas Schier
@ 2023-07-14 16:58     ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-14 16:58 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

On Fri, Jul 14, 2023 at 05:26:43PM +0200, Nicolas Schier wrote:
> On Wed, Jul 12, 2023 at 04:00:47PM +0200 Michal Suchanek wrote:
> > Show prefix (where configuration files are searched/to be installed),
> > module compressions, and module signatures supported.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v2: mention module signature in commit message
> > ---
> >  man/kmod.xml |  6 ++++++
> >  tools/kmod.c | 39 +++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 45 insertions(+)
> > 
> > diff --git a/man/kmod.xml b/man/kmod.xml
> > index 0706ad58c2cc..f992a500f836 100644
> > --- a/man/kmod.xml
> > +++ b/man/kmod.xml
> > @@ -71,6 +71,12 @@
> >            <para>Show the help message.</para>
> >          </listitem>
> >        </varlistentry>
> > +      <varlistentry>
> > +        <term><command>config</command></term>
> > +        <listitem>
> > +          <para>Show compile time options in JSON.</para>
> > +        </listitem>
> > +      </varlistentry>
> >        <varlistentry>
> >          <term><command>list</command></term>
> >          <listitem>
> > diff --git a/tools/kmod.c b/tools/kmod.c
> > index 55689c075ab1..5a13716955c1 100644
> > --- a/tools/kmod.c
> > +++ b/tools/kmod.c
> > @@ -37,9 +37,11 @@ static const struct option options[] = {
> >  };
> >  
> >  static const struct kmod_cmd kmod_cmd_help;
> > +static const struct kmod_cmd kmod_cmd_config;
> >  
> >  static const struct kmod_cmd *kmod_cmds[] = {
> >  	&kmod_cmd_help,
> > +	&kmod_cmd_config,
> >  	&kmod_cmd_list,
> >  	&kmod_cmd_static_nodes,
> >  
> > @@ -95,6 +97,43 @@ static const struct kmod_cmd kmod_cmd_help = {
> >  	.help = "Show help message",
> >  };
> >  
> > +static const char *compressions[] = {
> > +#ifdef ENABLE_ZSTD
> > +			"zstd",
> > +#endif
> > +#ifdef ENABLE_XZ
> > +			"xz",
> > +#endif
> > +#ifdef ENABLE_ZLIB
> > +			"gz",
> > +#endif
> > +			NULL
> > +};
> > +
> > +static int kmod_config(int argc, char *argv[])
> > +{
> > +	unsigned i;
> > +	printf("{\"prefix\":\"" PREFIX "\""
> > +			",\"module_signature\":["
> > +#ifdef ENABLE_OPENSSL
> > +			"\"PKCS#7\","
> > +#endif
> > +			"\"legacy\"]"
> > +			",\"module_compression\":[");
> > +	for(i = 0; compressions[i]; i++) {
> > +		printf("%s\"%s\"", i ? "," : "", compressions[i]);
> > +	}
> > +	printf("]}\n");
> > +
> > +	return EXIT_SUCCESS;
> > +}
> > +
> > +static const struct kmod_cmd kmod_cmd_config = {
> > +	.name = "config",
> > +	.cmd = kmod_config,
> > +	.help = "Show compile time options in JSON",
> > +};
> > +
> >  static int handle_kmod_commands(int argc, char *argv[])
> >  {
> >  	const char *cmd;
> > -- 
> > 2.41.0
> 
> If kmod could show selected configs without some (JSON) syntax
> around, it could simplify its proposed use in kbuild. E.g.:
> 
>     kmod config prefix 2>/dev/null
> 
> instead of
> 
>     kmod config &>/dev/null && kmod config | jq -r .prefix

Which would no longer hold for whole module directory:

      kmod config &>/dev/null && kmod config | jq -r .module_directory || echo /lib/modules

      vs

      kmod config module_directory &>/dev/null && kmod config module_directory || echo /lib/modules

Also JSON has standardized syntax for lists and users that can parse
JSON directly can load the whole configuration at once without several
calls to kmod config or pkg-config.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 15:10                 ` Michal Suchánek
@ 2023-07-14 19:37                   ` Nicolas Schier
  2023-07-17  9:55                     ` Michal Suchánek
  2023-07-17 19:14                   ` Masahiro Yamada
  1 sibling, 1 reply; 83+ messages in thread
From: Nicolas Schier @ 2023-07-14 19:37 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutn�,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 6969 bytes --]

On Fri, Jul 14, 2023 at 05:10:42PM +0200 Michal Suchánek wrote:
> On Fri, Jul 14, 2023 at 04:54:49PM +0200, Nicolas Schier wrote:
> > On Fri, Jul 14, 2023 at 04:30:02PM +0200, Michal Such�nek wrote:
> > > Hello,
> > > 
> > > On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> > > > On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > > > > Some distributions aim at not shipping any files in / outside of usr.
> > > > 
> > > > For me, preventing negation often makes things easier, e.g.: "... aim at
> > > > shipping files only below /usr".
> > > > 
> > > > > 
> > > > > The path under which kernel modules are installed is hardcoded to /lib
> > > > > which conflicts with this goal.
> > > > > 
> > > > > When kmod provides the config command, use it to determine the correct
> > > > > module installation prefix.
> > > > > 
> > > > > This is a prefix under which the modules are searched by kmod on the
> > > > > system, and is separate from the temporary staging location already
> > > > > supported by INSTALL_MOD_PATH.
> > > > > 
> > > > > With kmod that does not provide the config command empty prefix is used
> > > > > as before.
> > > > > 
> > > > > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > > > > ---
> > > > > v2: Avoid error on systems with kmod that does not support config
> > > > > command
> > > > > v3: More verbose commit message
> > > > > ---
> > > > >  Makefile          | 4 +++-
> > > > >  scripts/depmod.sh | 8 ++++----
> > > > >  2 files changed, 7 insertions(+), 5 deletions(-)
> > > > > 
> > > > > diff --git a/Makefile b/Makefile
> > > > > index 47690c28456a..b1fea135bdec 100644
> > > > > --- a/Makefile
> > > > > +++ b/Makefile
> > > > > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> > > > >  # makefile but the argument can be passed to make if needed.
> > > > >  #
> > > > >  
> > > > > -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > > > > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)

oh, should this be 'jq -r .prefix' (w/o ".module") to match your other patches?

> > > > 
> > > > All other calls of `jq` that I could find are located at tools/; as this here
> > > > is evaluated on each invocation, this should probably be documented in
> > > > Documentation/process/changes.rst?
> > > > 
> > > > (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
> > > 
> > > That's a good point.
> > > 
> > > > 
> > > > > +
> > > > > +MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> > > > >  export MODLIB
> > > > >  
> > > > >  PHONY += prepare0
> > > > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > > > index 3643b4f896ed..88ac79056153 100755
> > > > > --- a/scripts/depmod.sh
> > > > > +++ b/scripts/depmod.sh
> > > > > @@ -27,16 +27,16 @@ fi
> > > > >  # numbers, so we cheat with a symlink here
> > > > >  depmod_hack_needed=true
> > > > >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > > > > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > > > > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> > > > >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > > > > -	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > -		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > > +	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > +		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > >  		depmod_hack_needed=false
> > > > >  	fi
> > > > >  fi
> > > > 
> > > > I'd like to come back to the statement from Masahiro: Is the check above,
> > > > against some very old versions of depmod [1], the only reason for this patch?  
> > > > 
> > > > If we could remove that, would
> > > > 
> > > >     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> > > > 
> > > > be sufficient?
> > > 
> > > No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
> > > the newly added part is not because it's integral part of where the
> > > modules are installed on the system, and not the staging area path.
> > 
> > Ah, thanks.  So just for my understanding, could this be a (non-gentle)
> > alternative version of your patch, w/o modifying top-level Makefile?
> > 
> > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > index 3643b4f896ed..72c819de0669 100755
> > --- a/scripts/depmod.sh
> > +++ b/scripts/depmod.sh
> > @@ -1,4 +1,4 @@
> > -#!/bin/sh
> > +#!/bin/bash
> >  # SPDX-License-Identifier: GPL-2.0
> >  #
> >  # A depmod wrapper used by the toplevel Makefile
> > @@ -23,6 +23,8 @@ if [ -z $(command -v $DEPMOD) ]; then
> >         exit 0
> >  fi
> >  
> > +kmod_version=$(( $(kmod --version | sed -rne 's/^kmod version ([0-9]+).*$/\1/p') ))
> > +
> >  # older versions of depmod require the version string to start with three
> >  # numbers, so we cheat with a symlink here
> >  depmod_hack_needed=true
> > @@ -35,6 +37,13 @@ if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> >         fi
> >  fi
> >  rm -rf "$tmp_dir"
> > +
> > +if [ "${kmod_version}" -gt 32 ]; then
> > +       kmod_prefix="$(kmod config | jq -r .module_prefix)"
> > +       INSTALL_MOD_PATH="${INSTALL_MOD_PATH#${kmod_prefix}"
> > +       depmod_hack_needed=false
> > +fi
> > +
> >  if $depmod_hack_needed; then
> >         symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> >         ln -s "$KERNELRELEASE" "$symlink"
> > 
> > (untested, and assuming that kmod module prefix is in kmod >= 32)
> 
> It can be detected by running the 'kmod config' command first and
> ignoring the output when it fails which the above patch already did.
> The version check does not sound very reliable.
> 
> > Or are I am still missing something?
> 
> MODLIB still needs to include the extra prefix so that files are
> installed in the correct location. And that's defined in the toplevel
> Makefile.

Well, I think that depends.  Technically, you are right; and if we want
to support system with a non-empty kmod prefix fully transparently, then
patching top-level Makefile will probably be necessary.  

As for me, I am not convinced yet, that the fully transparent way to support
PREFIX/lib/modules/ is the best way forward.  I think it might be better to
first only make script/depmod.sh fit for a kmod prefix and require an adjusted
INSTALL_MOD_PATH for modules_install.

Which concrete distributions did you have in mind while composing the patches?

Kind regards,
Nicolas

-- 
epost|xmpp: nicolas@fjasle.eu          irc://oftc.net/nsc
↳ gpg: 18ed 52db e34f 860e e9fb  c82b 7d97 0932 55a0 ce7f
     -- frykten for herren er opphav til kunnskap --

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 19:37                   ` Nicolas Schier
@ 2023-07-17  9:55                     ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-17  9:55 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutn�,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, linux-kbuild, linux-kernel

On Fri, Jul 14, 2023 at 09:37:04PM +0200, Nicolas Schier wrote:
> On Fri, Jul 14, 2023 at 05:10:42PM +0200 Michal Suchánek wrote:
> > On Fri, Jul 14, 2023 at 04:54:49PM +0200, Nicolas Schier wrote:
> > > On Fri, Jul 14, 2023 at 04:30:02PM +0200, Michal Such�nek wrote:
> > > > Hello,
> > > > 
> > > > On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> > > > > On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > > > > > Some distributions aim at not shipping any files in / outside of usr.
> > > > > 
> > > > > For me, preventing negation often makes things easier, e.g.: "... aim at
> > > > > shipping files only below /usr".
> > > > > 
> > > > > > 
> > > > > > The path under which kernel modules are installed is hardcoded to /lib
> > > > > > which conflicts with this goal.
> > > > > > 
> > > > > > When kmod provides the config command, use it to determine the correct
> > > > > > module installation prefix.
> > > > > > 
> > > > > > This is a prefix under which the modules are searched by kmod on the
> > > > > > system, and is separate from the temporary staging location already
> > > > > > supported by INSTALL_MOD_PATH.
> > > > > > 
> > > > > > With kmod that does not provide the config command empty prefix is used
> > > > > > as before.
> > > > > > 
> > > > > > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > > > > > ---
> > > > > > v2: Avoid error on systems with kmod that does not support config
> > > > > > command
> > > > > > v3: More verbose commit message
> > > > > > ---
> > > > > >  Makefile          | 4 +++-
> > > > > >  scripts/depmod.sh | 8 ++++----
> > > > > >  2 files changed, 7 insertions(+), 5 deletions(-)
> > > > > > 
> > > > > > diff --git a/Makefile b/Makefile
> > > > > > index 47690c28456a..b1fea135bdec 100644
> > > > > > --- a/Makefile
> > > > > > +++ b/Makefile
> > > > > > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> > > > > >  # makefile but the argument can be passed to make if needed.
> > > > > >  #
> > > > > >  
> > > > > > -MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > > > > > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> 
> oh, should this be 'jq -r .prefix' (w/o ".module") to match your other patches?

No, this aligns perfectly fine, prefix is where kmod is installed.

> 
> > > > > 
> > > > > All other calls of `jq` that I could find are located at tools/; as this here
> > > > > is evaluated on each invocation, this should probably be documented in
> > > > > Documentation/process/changes.rst?
> > > > > 
> > > > > (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
> > > > 
> > > > That's a good point.
> > > > 
> > > > > 
> > > > > > +
> > > > > > +MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> > > > > >  export MODLIB
> > > > > >  
> > > > > >  PHONY += prepare0
> > > > > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > > > > index 3643b4f896ed..88ac79056153 100755
> > > > > > --- a/scripts/depmod.sh
> > > > > > +++ b/scripts/depmod.sh
> > > > > > @@ -27,16 +27,16 @@ fi
> > > > > >  # numbers, so we cheat with a symlink here
> > > > > >  depmod_hack_needed=true
> > > > > >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > > > > > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > > > > > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> > > > > >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > > > > > -	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > > -		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > > > +	if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > > +		-e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > > >  		depmod_hack_needed=false
> > > > > >  	fi
> > > > > >  fi
> > > > > 
> > > > > I'd like to come back to the statement from Masahiro: Is the check above,
> > > > > against some very old versions of depmod [1], the only reason for this patch?  
> > > > > 
> > > > > If we could remove that, would
> > > > > 
> > > > >     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> > > > > 
> > > > > be sufficient?
> > > > 
> > > > No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
> > > > the newly added part is not because it's integral part of where the
> > > > modules are installed on the system, and not the staging area path.
> > > 
> > > Ah, thanks.  So just for my understanding, could this be a (non-gentle)
> > > alternative version of your patch, w/o modifying top-level Makefile?
> > > 
> > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > index 3643b4f896ed..72c819de0669 100755
> > > --- a/scripts/depmod.sh
> > > +++ b/scripts/depmod.sh
> > > @@ -1,4 +1,4 @@
> > > -#!/bin/sh
> > > +#!/bin/bash
> > >  # SPDX-License-Identifier: GPL-2.0
> > >  #
> > >  # A depmod wrapper used by the toplevel Makefile
> > > @@ -23,6 +23,8 @@ if [ -z $(command -v $DEPMOD) ]; then
> > >         exit 0
> > >  fi
> > >  
> > > +kmod_version=$(( $(kmod --version | sed -rne 's/^kmod version ([0-9]+).*$/\1/p') ))
> > > +
> > >  # older versions of depmod require the version string to start with three
> > >  # numbers, so we cheat with a symlink here
> > >  depmod_hack_needed=true
> > > @@ -35,6 +37,13 @@ if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > >         fi
> > >  fi
> > >  rm -rf "$tmp_dir"
> > > +
> > > +if [ "${kmod_version}" -gt 32 ]; then
> > > +       kmod_prefix="$(kmod config | jq -r .module_prefix)"
> > > +       INSTALL_MOD_PATH="${INSTALL_MOD_PATH#${kmod_prefix}"
> > > +       depmod_hack_needed=false
> > > +fi
> > > +
> > >  if $depmod_hack_needed; then
> > >         symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> > >         ln -s "$KERNELRELEASE" "$symlink"
> > > 
> > > (untested, and assuming that kmod module prefix is in kmod >= 32)
> > 
> > It can be detected by running the 'kmod config' command first and
> > ignoring the output when it fails which the above patch already did.
> > The version check does not sound very reliable.
> > 
> > > Or are I am still missing something?
> > 
> > MODLIB still needs to include the extra prefix so that files are
> > installed in the correct location. And that's defined in the toplevel
> > Makefile.
> 
> Well, I think that depends.  Technically, you are right; and if we want
> to support system with a non-empty kmod prefix fully transparently, then
> patching top-level Makefile will probably be necessary.  
> 
> As for me, I am not convinced yet, that the fully transparent way to support
> PREFIX/lib/modules/ is the best way forward.  I think it might be better to
> first only make script/depmod.sh fit for a kmod prefix and require an adjusted
> INSTALL_MOD_PATH for modules_install.

Nevermind, I will update the patch to change the whole path. That will
make it crystal clear that no amount of fiddling with INSTALL_MOD_PATH
will work.

> Which concrete distributions did you have in mind while composing the patches?

If you STFW for usrmerge you can find that a number of distributions is
in different stages of experimenting with packing all shipped files into
/usr.

In the early experiment stages when the distribution is built with /lib
/bin, and /sbin in place and massaged after the fact to remove these
directories it does not matter much where files were installed
initially.

In later stages when the distribution is built without these extra
directories they are missing in the staging area for building packages,
and creating them is an error because files in these directories are not
to be shipped in packages. At this point the kernel insisting that
modules must be in /lib is sticking out like a sore thumb, it's on every
system.

Sure, there is a number of hacks that can be used to work around the
problem. Still it's a problem the needs to be addressed for usrmerge to
fully work.

openSUSE is maybe 75% there - it's nowhere near complete but the
packages that insist on using these directories outside of /usr are
becoming problematic.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH kmod v4 0/4] kmod /usr support
  2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
                   ` (8 preceding siblings ...)
  2023-07-12 14:00 ` [PATCH kmod v2 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix} Michal Suchanek
@ 2023-07-17 10:39 ` Michal Suchanek
  2023-07-17 10:39   ` [PATCH kmod v4 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
                     ` (5 more replies)
  9 siblings, 6 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-17 10:39 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

with these patches it is possible to install kernel modules in an arbitrary
directory - eg. moving the /lib/modules to /usr/lib/modules or /opt/linux.

While the modprobe.d and depmod.d search which already includes multiple
paths is expanded to also include $(prefix) the module directory still
supports only one location, only a different one under $(module_directory).

Having kmod search multiple module locations while only one is supported now
might break some assumption about relative module path corresponding to a
specific file, would require more invasive changes to implement, and is not
supportive of the goal of moving the modules away from /lib.

Both kmod and the kernel need to be patched to make use of this feature.
Patched kernel is backwards compatible with older kmod.  Patched kmod
with $(module_directory) set to /lib/modules is equivalent to unpatched kmod.

With this patch the kmod tool can spit out a JSON with the tool build-time
configuration, and jq is used for getting a single value out of that.

I opted for this because crating .pc files requires putting the logic into the
autohell files which is very clumsy. Also pkg-config insists on hiding
the actual .pc file data and only allowing clumsy queries through its
commandline interface.

jq has its downsides, too. So far it is not used by kernel build, only
by other tools shipped with the kernel. This adds another dependency for
kernel builds.

It turns out that the kernel already does use pkg-config. There are two
alternate spellings: pkg-config and pkgconfig. Searching for the latter
gives a few matches in the kernel giving off the impression that it's a
thing but not really used.

Thanks

Michal

Link: https://lore.kernel.org/linux-modules/20210112160211.5614-1-msuchanek@suse.de/


Michal Suchanek (4):
  man/depmod.d: Fix incorrect /usr/lib search path
  libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  kmod: Add config command to show compile time configuration as JSON
  libkmod, depmod, modprobe: Make directory for kernel modules
    configurable

 Makefile.am                          |   4 +-
 configure.ac                         |   7 ++
 libkmod/libkmod.c                    |  11 +--
 man/Makefile.am                      |  10 ++-
 man/depmod.d.xml                     |   9 ++-
 man/depmod.xml                       |   4 +-
 man/kmod.xml                         |   6 ++
 man/modinfo.xml                      |   2 +-
 man/modprobe.d.xml                   |   1 +
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   7 +-
 tools/kmod.c                         |  41 ++++++++++
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 20 files changed, 169 insertions(+), 90 deletions(-)

-- 
2.41.0


^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH kmod v4 1/4] man/depmod.d: Fix incorrect /usr/lib search path
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
@ 2023-07-17 10:39   ` Michal Suchanek
  2023-07-17 10:39   ` [PATCH kmod v4 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-17 10:39 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

depmod searches /lib/depmod.d but the man page says /usr/lib/depmod.d is
searched. Align the documentation with the code.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Fix commit message typo
---
 man/depmod.d.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 76548e92a312..8d3d821cddc8 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -39,7 +39,7 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/usr/lib/depmod.d/*.conf</filename></para>
+    <para><filename>/lib/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v4 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib.
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
  2023-07-17 10:39   ` [PATCH kmod v4 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
@ 2023-07-17 10:39   ` Michal Suchanek
  2023-07-17 10:39   ` [PATCH kmod v4 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-17 10:39 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

There is an ongoing effort to limit use of files outside of /usr (or
$prefix on general). Currently all modprobe.d paths are hardcoded to
outside of $prefix. Teach kmod to load modprobe.d from $prefix/lib.

On some distributions /usr/lib and /lib are the same directory because
of a compatibility symlink, and it is possible to craft configuration
files with sideeffects that would behave differently when loaded twice.
However, the override semantic ensures that one 'overrides' the other,
and only one configuration file of the same name is loaded from any of
the search directories.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Fix commit message typo
v3: Fix modprobe.d path list in code comment
---
 Makefile.am        | 1 +
 libkmod/libkmod.c  | 7 ++++---
 man/Makefile.am    | 9 +++++++--
 man/depmod.d.xml   | 1 +
 man/modprobe.d.xml | 1 +
 tools/depmod.c     | 1 +
 6 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8ba85c91a0f3..7aa5bfa5638d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ AM_CPPFLAGS = \
 	-include $(top_builddir)/config.h \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
+	-DPREFIX=\""$(prefix)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 2670f9a4611a..f3139c1cef97 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -65,6 +65,7 @@ static const char *const default_config_paths[] = {
 	SYSCONFDIR "/modprobe.d",
 	"/run/modprobe.d",
 	"/usr/local/lib/modprobe.d",
+	PREFIX "/lib/modprobe.d",
 	"/lib/modprobe.d",
 	NULL
 };
@@ -237,9 +238,9 @@ static char *get_kernel_release(const char *dirname)
  *                to load from user-defined configuration parameters such as
  *                alias, blacklists, commands (install, remove). If NULL
  *                defaults to /etc/modprobe.d, /run/modprobe.d,
- *                /usr/local/lib/modprobe.d and /lib/modprobe.d. Give an empty
- *                vector if configuration should not be read. This array must
- *                be null terminated.
+ *                /usr/local/lib/modprobe.d, PREFIX/lib/modprobe.d, and
+ *                /lib/modprobe.d. Give an empty vector if configuration should
+ *                not be read. This array must be null terminated.
  *
  * Create kmod library context. This reads the kmod configuration
  * and fills in the default values.
diff --git a/man/Makefile.am b/man/Makefile.am
index 11514d52a190..ad07c30bbd24 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -17,9 +17,14 @@ EXTRA_DIST = $(MAN5:%.5=%.xml) $(MAN8:%.8=%.xml)
 CLEANFILES = $(dist_man_MANS)
 
 %.5 %.8: %.xml
-	$(AM_V_XSLT)$(XSLT) \
+	$(AM_V_XSLT)if [ -n '$(prefix)' ] ; then \
+		sed -e 's|@PREFIX@|$(prefix)|g' $< ; \
+	else \
+		sed -e '/@PREFIX@/d' $< ; \
+	fi | \
+	$(XSLT) \
 		-o $@ \
 		--nonet \
 		--stringparam man.output.quietly 1 \
 		--param funcsynopsis.style "'ansi'" \
-		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $<
+		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl -
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 8d3d821cddc8..431ebca6654b 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -40,6 +40,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/depmod.d/*.conf</filename></para>
+    <para><filename>@PREFIX@/lib/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
diff --git a/man/modprobe.d.xml b/man/modprobe.d.xml
index 0ab3e9110a7e..e8a91d7668af 100644
--- a/man/modprobe.d.xml
+++ b/man/modprobe.d.xml
@@ -41,6 +41,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/modprobe.d/*.conf</filename></para>
+    <para><filename>@PREFIX@/lib/modprobe.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/modprobe.d/*.conf</filename></para>
     <para><filename>/run/modprobe.d/*.conf</filename></para>
     <para><filename>/etc/modprobe.d/*.conf</filename></para>
diff --git a/tools/depmod.c b/tools/depmod.c
index 1d1d41db860f..7e9339923809 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -54,6 +54,7 @@ static const char *const default_cfg_paths[] = {
 	SYSCONFDIR "/depmod.d",
 	"/run/depmod.d",
 	"/usr/local/lib/depmod.d",
+	PREFIX "/lib/depmod.d",
 	"/lib/depmod.d",
 	NULL
 };
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v4 3/4] kmod: Add config command to show compile time configuration as JSON
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
  2023-07-17 10:39   ` [PATCH kmod v4 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
  2023-07-17 10:39   ` [PATCH kmod v4 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
@ 2023-07-17 10:39   ` Michal Suchanek
  2023-07-17 10:39   ` [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-17 10:39 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Show prefix (where system configuration files are searched/to be
installed), sysconfdir (where user configuration files are searched),
module compressions, and module signatures supported.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: mention module signature in commit message
v3: add sysconfdir
---
 man/kmod.xml |  6 ++++++
 tools/kmod.c | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/man/kmod.xml b/man/kmod.xml
index 0706ad58c2cc..f992a500f836 100644
--- a/man/kmod.xml
+++ b/man/kmod.xml
@@ -71,6 +71,12 @@
           <para>Show the help message.</para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term><command>config</command></term>
+        <listitem>
+          <para>Show compile time options in JSON.</para>
+        </listitem>
+      </varlistentry>
       <varlistentry>
         <term><command>list</command></term>
         <listitem>
diff --git a/tools/kmod.c b/tools/kmod.c
index 55689c075ab1..a684c1d24284 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -37,9 +37,11 @@ static const struct option options[] = {
 };
 
 static const struct kmod_cmd kmod_cmd_help;
+static const struct kmod_cmd kmod_cmd_config;
 
 static const struct kmod_cmd *kmod_cmds[] = {
 	&kmod_cmd_help,
+	&kmod_cmd_config,
 	&kmod_cmd_list,
 	&kmod_cmd_static_nodes,
 
@@ -95,6 +97,44 @@ static const struct kmod_cmd kmod_cmd_help = {
 	.help = "Show help message",
 };
 
+static const char *compressions[] = {
+#ifdef ENABLE_ZSTD
+			"zstd",
+#endif
+#ifdef ENABLE_XZ
+			"xz",
+#endif
+#ifdef ENABLE_ZLIB
+			"gz",
+#endif
+			NULL
+};
+
+static int kmod_config(int argc, char *argv[])
+{
+	unsigned i;
+	printf("{\"prefix\":\"" PREFIX "\""
+			",\"sysconfdir\":\"" SYSCONFDIR "\""
+			",\"module_signature\":["
+#ifdef ENABLE_OPENSSL
+			"\"PKCS#7\","
+#endif
+			"\"legacy\"]"
+			",\"module_compression\":[");
+	for(i = 0; compressions[i]; i++) {
+		printf("%s\"%s\"", i ? "," : "", compressions[i]);
+	}
+	printf("]}\n");
+
+	return EXIT_SUCCESS;
+}
+
+static const struct kmod_cmd kmod_cmd_config = {
+	.name = "config",
+	.cmd = kmod_config,
+	.help = "Show compile time options in JSON",
+};
+
 static int handle_kmod_commands(int argc, char *argv[])
 {
 	const char *cmd;
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
                     ` (2 preceding siblings ...)
  2023-07-17 10:39   ` [PATCH kmod v4 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
@ 2023-07-17 10:39   ` Michal Suchanek
  2023-07-17 19:28     ` Jan Engelhardt
  2023-07-17 20:12     ` Lucas De Marchi
  2023-07-17 10:40   ` [PATCH v4] depmod: Handle installing modules under a prefix Michal Suchanek
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
  5 siblings, 2 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-17 10:39 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

modprobe.d is now searched under ${prefix}/lib, add ${module_directory} to
specify the directory where to search for kernel modules.

With this distributions that do not want to ship files in /lib can also
move kernel modules to /usr while others can keep them in /lib.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v4: Make the whole path configurable
---
 Makefile.am                          |   3 +-
 configure.ac                         |   7 ++
 libkmod/libkmod.c                    |   4 +-
 man/Makefile.am                      |   1 +
 man/depmod.d.xml                     |   6 +-
 man/depmod.xml                       |   4 +-
 man/modinfo.xml                      |   2 +-
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   6 +-
 tools/kmod.c                         |   1 +
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 18 files changed, 107 insertions(+), 84 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 7aa5bfa5638d..981574558c93 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
 	-DPREFIX=\""$(prefix)"\" \
+	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
@@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
 MODULE_PLAYGROUND = testsuite/module-playground
 ROOTFS = testsuite/rootfs
 ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
-CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
+CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
 
 build-module-playground:
 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
diff --git a/configure.ac b/configure.ac
index 6064dee77ae6..a195c8e6b554 100644
--- a/configure.ac
+++ b/configure.ac
@@ -84,6 +84,12 @@ AC_ARG_WITH([rootlibdir],
         [], [with_rootlibdir=$libdir])
 AC_SUBST([rootlibdir], [$with_rootlibdir])
 
+# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
+AC_ARG_WITH([module_directory],
+        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
+        [], [with_module_directory=/lib/modules])
+AC_SUBST([module_directory], [$with_module_directory])
+
 AC_ARG_WITH([zstd],
 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
 	[], [with_zstd=no])
@@ -304,6 +310,7 @@ AC_MSG_RESULT([
 	$PACKAGE $VERSION
 	=======
 
+	module_directory:	${module_directory}
 	prefix:			${prefix}
 	sysconfdir:		${sysconfdir}
 	libdir:			${libdir}
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index f3139c1cef97..d2ed8748c833 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -209,7 +209,7 @@ static int log_priority(const char *priority)
 	return 0;
 }
 
-static const char *dirname_default_prefix = "/lib/modules";
+static const char *dirname_default_prefix = MODULE_DIRECTORY;
 
 static char *get_kernel_release(const char *dirname)
 {
@@ -231,7 +231,7 @@ static char *get_kernel_release(const char *dirname)
 /**
  * kmod_new:
  * @dirname: what to consider as linux module's directory, if NULL
- *           defaults to /lib/modules/`uname -r`. If it's relative,
+ *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
  *           it's treated as relative to the current working directory.
  *           Otherwise, give an absolute dirname.
  * @config_paths: ordered array of paths (directories or files) where
diff --git a/man/Makefile.am b/man/Makefile.am
index ad07c30bbd24..25c2cc6fdf13 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
 	else \
 		sed -e '/@PREFIX@/d' $< ; \
 	fi | \
+	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
 	$(XSLT) \
 		-o $@ \
 		--nonet \
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 431ebca6654b..676977c69a4f 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -70,7 +70,7 @@
         </term>
         <listitem>
           <para>
-            This allows you to specify the order in which /lib/modules
+            This allows you to specify the order in which @MODULE_DIRECTORY@
             (or other configured module location) subdirectories will
             be processed by <command>depmod</command>. Directories are
             listed in order, with the highest priority given to the
@@ -101,7 +101,7 @@
             <command>depmod</command> command. It is possible to
             specify one kernel or all kernels using the * wildcard.
             <replaceable>modulesubdirectory</replaceable> is the
-            name of the subdirectory under /lib/modules (or other
+            name of the subdirectory under @MODULE_DIRECTORY@ (or other
             module location) where the target module is installed.
           </para>
           <para>
@@ -110,7 +110,7 @@
             specifying the following command: "override kmod * extra".
             This will ensure that any matching module name installed
             under the <command>extra</command> subdirectory within
-            /lib/modules (or other module location) will take priority
+            @MODULE_DIRECTORY@ (or other module location) will take priority
             over any likenamed module already provided by the kernel.
           </para>
         </listitem>
diff --git a/man/depmod.xml b/man/depmod.xml
index 3b0097184fd7..fce2a4a67a89 100644
--- a/man/depmod.xml
+++ b/man/depmod.xml
@@ -80,7 +80,7 @@
     </para>
     <para> <command>depmod</command> creates a list of module dependencies by
       reading each module under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
       determining what symbols it exports and what symbols it needs.  By
       default, this list is written to <filename>modules.dep</filename>, and a
       binary hashed version named <filename>modules.dep.bin</filename>, in the
@@ -141,7 +141,7 @@
         <listitem>
           <para>
             If your modules are not currently in the (normal) directory
-            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
+            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
             but in a staging area, you can specify a
             <replaceable>basedir</replaceable> which is prepended to the
             directory name.  This <replaceable>basedir</replaceable> is
diff --git a/man/modinfo.xml b/man/modinfo.xml
index 9fe0324a2527..b6c4d6045829 100644
--- a/man/modinfo.xml
+++ b/man/modinfo.xml
@@ -54,7 +54,7 @@
       <command>modinfo</command> extracts information from the Linux Kernel
       modules given on the command line.  If the module name is not a filename,
       then the
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
       directory is searched, as is also done by
       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
       when loading kernel modules.
diff --git a/man/modprobe.xml b/man/modprobe.xml
index 91f9e27997cd..4d1fd59c000b 100644
--- a/man/modprobe.xml
+++ b/man/modprobe.xml
@@ -78,7 +78,7 @@
       is no difference between _ and - in module names (automatic
       underscore conversion is performed).
       <command>modprobe</command> looks in the module directory
-      <filename>/lib/modules/`uname -r`</filename> for all
+      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
       the modules and other files, except for the optional
       configuration files in the
       <filename>/etc/modprobe.d</filename> directory
diff --git a/man/modules.dep.xml b/man/modules.dep.xml
index ed633694ec9e..8ef6d8b3536e 100644
--- a/man/modules.dep.xml
+++ b/man/modules.dep.xml
@@ -34,8 +34,8 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/lib/modules/modules.dep</filename></para>
-    <para><filename>/lib/modules/modules.dep.bin</filename></para>
+    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
+    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
   </refsynopsisdiv>
 
   <refsect1><title>DESCRIPTION</title>
@@ -43,7 +43,7 @@
       <filename>modules.dep.bin</filename> is a binary file generated by
       <command>depmod</command> listing the dependencies for
       every module in the directories under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
       It is used by kmod tools such as <command>modprobe</command> and
       libkmod.
     </para>
diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
index e6045b0dd932..a7ab09bea2bf 100644
--- a/testsuite/module-playground/Makefile
+++ b/testsuite/module-playground/Makefile
@@ -47,7 +47,7 @@ endif
 
 else
 # normal makefile
-KDIR ?= /lib/modules/`uname -r`/build
+KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
 KVER ?= `uname -r`
 ifeq ($(FAKE_BUILD),)
     FAKE_BUILD=0
diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
index 4440ddcd6b4d..a780f9381b3c 100755
--- a/testsuite/setup-rootfs.sh
+++ b/testsuite/setup-rootfs.sh
@@ -16,6 +16,19 @@ create_rootfs() {
 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
 	find "$ROOTFS" -type d -exec chmod +w {} \;
 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
+	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
+		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
+			version="$(basename $i)"
+			[ $version != 'external' ] || continue
+			mod="$(dirname $i)"
+			lib="$(dirname $mod)"
+			up="$(dirname $lib)$MODULE_DIRECTORY"
+			mkdir -p "$up"
+			mv "$i" "$up"
+		done
+	fi
 
 	if [ "$SYSCONFDIR" != "/etc" ]; then
 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
@@ -32,57 +45,57 @@ feature_enabled() {
 
 declare -A map
 map=(
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/kernel/crypto/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/updates/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/override/"]="mod-simple.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/override/"]="mod-simple.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
     ["test-init/"]="mod-simple.ko"
     ["test-remove/"]="mod-simple.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
     ["test-modprobe/show-exports/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/force/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel-force/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/force$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel-force$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/alias-to-none$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/module-param-kcmdline$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
     ["test-modprobe/module-from-abspath/home/foo/"]="mod-simple.ko"
     ["test-modprobe/module-from-relpath/home/foo/"]="mod-simple.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
@@ -90,20 +103,20 @@ map=(
     ["test-modinfo/mod-simple-sha256.ko"]="mod-simple.ko"
     ["test-modinfo/mod-simple-pkcs7.ko"]="mod-simple.ko"
     ["test-modinfo/external/lib/modules/external/mod-simple.ko"]="mod-simple.ko"
-    ["test-tools/insert/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-tools/remove/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/insert$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/remove$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
 )
 
 gzip_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"
     )
 
 xz_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
     )
 
 zstd_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"
     )
 
 attach_sha256_array=(
diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
index 93606947f18a..c96dbf0a62be 100644
--- a/testsuite/test-depmod.c
+++ b/testsuite/test-depmod.c
@@ -27,7 +27,7 @@
 
 #define MODULES_UNAME "4.4.4"
 #define MODULES_ORDER_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-order-compressed"
-#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_modules_order_for_compressed(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -55,8 +55,8 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
 	});
 
 #define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
-#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_UNAME
-#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir" MODULE_DIRECTORY "/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_modules_outdir(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -87,7 +87,7 @@ DEFINE_TEST(depmod_modules_outdir,
 	});
 
 #define SEARCH_ORDER_SIMPLE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-simple"
-#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_simple(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -114,7 +114,7 @@ DEFINE_TEST(depmod_search_order_simple,
 	});
 
 #define SEARCH_ORDER_SAME_PREFIX_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-same-prefix"
-#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_same_prefix(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -164,7 +164,7 @@ DEFINE_TEST(depmod_detect_loop,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-first"
-#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_external_first(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -191,7 +191,7 @@ DEFINE_TEST(depmod_search_order_external_first,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_LAST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-last"
-#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_external_last(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -218,7 +218,7 @@ DEFINE_TEST(depmod_search_order_external_last,
 	});
 
 #define SEARCH_ORDER_OVERRIDE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-override"
-#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_override(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c
index 56e73609f204..c77c4bbc04eb 100644
--- a/testsuite/test-testsuite.c
+++ b/testsuite/test-testsuite.c
@@ -64,7 +64,7 @@ static int testsuite_rootfs_fopen(const struct test *t)
 	char s[100];
 	int n;
 
-	fp = fopen("/lib/modules/a", "r");
+	fp = fopen(MODULE_DIRECTORY "/a", "r");
 	if (fp == NULL)
 		return EXIT_FAILURE;;
 
@@ -89,7 +89,7 @@ static int testsuite_rootfs_open(const struct test *t)
 	char buf[100];
 	int fd, done;
 
-	fd = open("/lib/modules/a", O_RDONLY);
+	fd = open(MODULE_DIRECTORY "/a", O_RDONLY);
 	if (fd < 0)
 		return EXIT_FAILURE;
 
@@ -121,12 +121,12 @@ static int testsuite_rootfs_stat_access(const struct test *t)
 {
 	struct stat st;
 
-	if (access("/lib/modules/a", F_OK) < 0) {
+	if (access(MODULE_DIRECTORY "/a", F_OK) < 0) {
 		ERR("access failed: %m\n");
 		return EXIT_FAILURE;
 	}
 
-	if (stat("/lib/modules/a", &st) < 0) {
+	if (stat(MODULE_DIRECTORY "/a", &st) < 0) {
 		ERR("stat failed: %m\n");
 		return EXIT_FAILURE;
 	}
diff --git a/tools/depmod.c b/tools/depmod.c
index 7e9339923809..22bc1d87c83e 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -911,7 +911,7 @@ struct vertex;
 struct mod {
 	struct kmod_module *kmod;
 	char *path;
-	const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
+	const char *relpath; /* path relative to '$ROOT$MODULE_DIRECTORY/$VER/' */
 	char *uncrelpath; /* same as relpath but ending in .ko */
 	struct kmod_list *info_list;
 	struct kmod_list *dep_sym_list;
@@ -3024,11 +3024,11 @@ static int do_depmod(int argc, char *argv[])
 	}
 
 	cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
-				  "%s/lib/modules/%s",
+				  "%s" MODULE_DIRECTORY "/%s",
 				  root ?: "", cfg.kversion);
 
 	cfg.outdirnamelen = snprintf(cfg.outdirname, PATH_MAX,
-				     "%s/lib/modules/%s",
+				     "%s" MODULE_DIRECTORY "/%s",
 				     out_root ?: (root ?: ""), cfg.kversion);
 
 	if (optind == argc)
diff --git a/tools/kmod.c b/tools/kmod.c
index a684c1d24284..3a2589c67447 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -115,6 +115,7 @@ static int kmod_config(int argc, char *argv[])
 	unsigned i;
 	printf("{\"prefix\":\"" PREFIX "\""
 			",\"sysconfdir\":\"" SYSCONFDIR "\""
+			",\"module_directory\":\"" MODULE_DIRECTORY "\""
 			",\"module_signature\":["
 #ifdef ENABLE_OPENSSL
 			"\"PKCS#7\","
diff --git a/tools/modinfo.c b/tools/modinfo.c
index d0aab200af4e..cacc32dc4c40 100644
--- a/tools/modinfo.c
+++ b/tools/modinfo.c
@@ -367,7 +367,7 @@ static void help(void)
 		"\t-m, --modname               Handle argument as module name instead of alias or filename\n"
 		"\t-F, --field=FIELD           Print only provided FIELD\n"
 		"\t-k, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
-		"\t-b, --basedir=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-b, --basedir=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
 		"\t-V, --version               Show version\n"
 		"\t-h, --help                  Show this help\n",
 		program_invocation_short_name);
@@ -462,7 +462,7 @@ static int do_modinfo(int argc, char *argv[])
 			}
 			kversion = u.release;
 		}
-		snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
+		snprintf(dirname_buf, sizeof(dirname_buf), "%s" MODULE_DIRECTORY "/%s",
 			 root, kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/modprobe.c b/tools/modprobe.c
index e891028349a8..5306bef250da 100644
--- a/tools/modprobe.c
+++ b/tools/modprobe.c
@@ -142,7 +142,7 @@ static void help(void)
 		"\t-n, --show                  Same as --dry-run\n"
 
 		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
-		"\t-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-d, --dirname=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
 		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
 
 		"\t-s, --syslog                print to syslog, not stderr\n"
@@ -999,7 +999,7 @@ static int do_modprobe(int argc, char **orig_argv)
 			kversion = u.release;
 		}
 		snprintf(dirname_buf, sizeof(dirname_buf),
-				"%s/lib/modules/%s", root,
+				"%s" MODULE_DIRECTORY "/%s", root,
 				kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
index 8d2356da73f3..5ef3743e967b 100644
--- a/tools/static-nodes.c
+++ b/tools/static-nodes.c
@@ -212,15 +212,15 @@ static int do_static_nodes(int argc, char *argv[])
 		goto finish;
 	}
 
-	snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
+	snprintf(modules, sizeof(modules), MODULE_DIRECTORY "/%s/modules.devname", kernel.release);
 	in = fopen(modules, "re");
 	if (in == NULL) {
 		if (errno == ENOENT) {
-			fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
+			fprintf(stderr, "Warning: " MODULE_DIRECTORY "/%s/modules.devname not found - ignoring\n",
 				kernel.release);
 			ret = EXIT_SUCCESS;
 		} else {
-			fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
+			fprintf(stderr, "Error: could not open " MODULE_DIRECTORY "/%s/modules.devname - %m\n",
 				kernel.release);
 			ret = EXIT_FAILURE;
 		}
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH v4] depmod: Handle installing modules under a prefix
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
                     ` (3 preceding siblings ...)
  2023-07-17 10:39   ` [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
@ 2023-07-17 10:40   ` Michal Suchanek
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
  5 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-17 10:40 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Some distributions aim at shipping all files in /usr.

The path under which kernel modules are installed is hardcoded to /lib
which conflicts with this goal.

When kmod provides the config command, use it to determine the correct
module installation path.

With kmod that does not provide the config command /lib/modules is used
as before.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Avoid error on systems with kmod that does not support config
command
v3: More verbose commit message
v4:
  - Document jq requirement
  - fix bashism
  - Update to getting full module path, not just additional prefix
---
 Documentation/process/changes.rst | 15 +++++++++++++++
 Makefile                          |  4 +++-
 scripts/depmod.sh                 |  8 ++++----
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/Documentation/process/changes.rst b/Documentation/process/changes.rst
index 5561dae94f85..f96a867ab85f 100644
--- a/Documentation/process/changes.rst
+++ b/Documentation/process/changes.rst
@@ -41,6 +41,7 @@ bison                  2.0              bison --version
 pahole                 1.16             pahole --version
 util-linux             2.10o            fdformat --version
 kmod                   13               depmod -V
+jq                     any              jq --version
 e2fsprogs              1.41.4           e2fsck -V
 jfsutils               1.1.3            fsck.jfs -V
 reiserfsprogs          3.6.3            reiserfsck -V
@@ -176,6 +177,15 @@ You will need openssl to build kernels 3.7 and higher if module signing is
 enabled.  You will also need openssl development packages to build kernels 4.3
 and higher.
 
+
+Kmod
+----
+
+``depmod`` is needed for building modular kernels. ``jq`` is additionally needed
+when support for installing modules outside of the default location
+``/lib/modules`` is required. Missing ``jq`` will result in an error message
+during build even if the tool is not needed.
+
 Tar
 ---
 
@@ -463,6 +473,11 @@ Kmod
 - <https://www.kernel.org/pub/linux/utils/kernel/kmod/>
 - <https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git>
 
+jq
+--
+
+- <https://github.com/jqlang/jq/tags>
+
 Ksymoops
 --------
 
diff --git a/Makefile b/Makefile
index 47690c28456a..5eb04baaac58 100644
--- a/Makefile
+++ b/Makefile
@@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
 # makefile but the argument can be passed to make if needed.
 #
 
-MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
+export KERNEL_MODULE_DIRECTORY := $(shell kmod config >/dev/null 2>&1 && kmod config | jq -r .module_directory || echo /lib/modules)
+
+MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_DIRECTORY)/$(KERNELRELEASE)
 export MODLIB
 
 PHONY += prepare0
diff --git a/scripts/depmod.sh b/scripts/depmod.sh
index 3643b4f896ed..06961f990fcb 100755
--- a/scripts/depmod.sh
+++ b/scripts/depmod.sh
@@ -27,16 +27,16 @@ fi
 # numbers, so we cheat with a symlink here
 depmod_hack_needed=true
 tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
-mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
+mkdir -p "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE"
 if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
-	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
-		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
+	if test -e "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE/modules.dep" -o \
+		-e "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE/modules.dep.bin"; then
 		depmod_hack_needed=false
 	fi
 fi
 rm -rf "$tmp_dir"
 if $depmod_hack_needed; then
-	symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
+	symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_DIRECTORY/99.98.$KERNELRELEASE"
 	ln -s "$KERNELRELEASE" "$symlink"
 	KERNELRELEASE=99.98.$KERNELRELEASE
 fi
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 14:54               ` Nicolas Schier
  2023-07-14 15:10                 ` Michal Suchánek
@ 2023-07-17 19:13                 ` Masahiro Yamada
  1 sibling, 0 replies; 83+ messages in thread
From: Masahiro Yamada @ 2023-07-17 19:13 UTC (permalink / raw)
  To: Nicolas Schier
  Cc: Michal Such�nek, linux-modules, Takashi Iwai,
	Lucas De Marchi, Michal Koutn�,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	linux-kbuild, linux-kernel

On Fri, Jul 14, 2023 at 11:55 PM Nicolas Schier <nicolas@fjasle.eu> wrote:
>
> On Fri, Jul 14, 2023 at 04:30:02PM +0200, Michal Such�nek wrote:
> > Hello,
> >
> > On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> > > On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > > > Some distributions aim at not shipping any files in / outside of usr.
> > >
> > > For me, preventing negation often makes things easier, e.g.: "... aim at
> > > shipping files only below /usr".
> > >
> > > >
> > > > The path under which kernel modules are installed is hardcoded to /lib
> > > > which conflicts with this goal.
> > > >
> > > > When kmod provides the config command, use it to determine the correct
> > > > module installation prefix.
> > > >
> > > > This is a prefix under which the modules are searched by kmod on the
> > > > system, and is separate from the temporary staging location already
> > > > supported by INSTALL_MOD_PATH.
> > > >
> > > > With kmod that does not provide the config command empty prefix is used
> > > > as before.
> > > >
> > > > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > > > ---
> > > > v2: Avoid error on systems with kmod that does not support config
> > > > command
> > > > v3: More verbose commit message
> > > > ---
> > > >  Makefile          | 4 +++-
> > > >  scripts/depmod.sh | 8 ++++----
> > > >  2 files changed, 7 insertions(+), 5 deletions(-)
> > > >
> > > > diff --git a/Makefile b/Makefile
> > > > index 47690c28456a..b1fea135bdec 100644
> > > > --- a/Makefile
> > > > +++ b/Makefile
> > > > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> > > >  # makefile but the argument can be passed to make if needed.
> > > >  #
> > > >
> > > > -MODLIB   = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > > > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> > >
> > > All other calls of `jq` that I could find are located at tools/; as this here
> > > is evaluated on each invocation, this should probably be documented in
> > > Documentation/process/changes.rst?
> > >
> > > (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
> >
> > That's a good point.
> >
> > >
> > > > +
> > > > +MODLIB   = $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> > > >  export MODLIB
> > > >
> > > >  PHONY += prepare0
> > > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > > index 3643b4f896ed..88ac79056153 100755
> > > > --- a/scripts/depmod.sh
> > > > +++ b/scripts/depmod.sh
> > > > @@ -27,16 +27,16 @@ fi
> > > >  # numbers, so we cheat with a symlink here
> > > >  depmod_hack_needed=true
> > > >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > > > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > > > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> > > >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > > > - if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > -         -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > + if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > +         -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > >           depmod_hack_needed=false
> > > >   fi
> > > >  fi
> > >
> > > I'd like to come back to the statement from Masahiro: Is the check above,
> > > against some very old versions of depmod [1], the only reason for this patch?
> > >
> > > If we could remove that, would
> > >
> > >     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> > >
> > > be sufficient?
> >
> > No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
> > the newly added part is not because it's integral part of where the
> > modules are installed on the system, and not the staging area path.
>
> Ah, thanks.  So just for my understanding, could this be a (non-gentle)
> alternative version of your patch, w/o modifying top-level Makefile?
>
> diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> index 3643b4f896ed..72c819de0669 100755
> --- a/scripts/depmod.sh
> +++ b/scripts/depmod.sh
> @@ -1,4 +1,4 @@
> -#!/bin/sh
> +#!/bin/bash
>  # SPDX-License-Identifier: GPL-2.0
>  #
>  # A depmod wrapper used by the toplevel Makefile
> @@ -23,6 +23,8 @@ if [ -z $(command -v $DEPMOD) ]; then
>         exit 0
>  fi
>
> +kmod_version=$(( $(kmod --version | sed -rne 's/^kmod version ([0-9]+).*$/\1/p') ))
> +
>  # older versions of depmod require the version string to start with three
>  # numbers, so we cheat with a symlink here
>  depmod_hack_needed=true
> @@ -35,6 +37,13 @@ if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
>         fi
>  fi
>  rm -rf "$tmp_dir"
> +
> +if [ "${kmod_version}" -gt 32 ]; then
> +       kmod_prefix="$(kmod config | jq -r .module_prefix)"
> +       INSTALL_MOD_PATH="${INSTALL_MOD_PATH#${kmod_prefix}"
> +       depmod_hack_needed=false
> +fi
> +
>  if $depmod_hack_needed; then
>         symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
>         ln -s "$KERNELRELEASE" "$symlink"
>
> (untested, and assuming that kmod module prefix is in kmod >= 32)
>
> Or are I am still missing something?
>
> > Was busybox ever fixed to not require the hack?
>
> I haven't checked that, yet.



I believe we can revert

8fc62e59425389a6d48429b9d146223122743435
bfe5424a8b31624e7a476f959d552999f931e7c7

There is no good reason to keep such old hacky code.


The old module-init-tools project was replaced by kmod.
I do not know whether busybox fixed the issue or not.
Anyway, Linux 3.0 was released 12 years ago.
There was plenty of time to fix the issue if we wanted.
If busybox is still not able to handle the X.Y version form,
it is just that nobody is caring about the busybox's depmod.




--
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-14 15:10                 ` Michal Suchánek
  2023-07-14 19:37                   ` Nicolas Schier
@ 2023-07-17 19:14                   ` Masahiro Yamada
  2023-07-18  8:52                     ` Michal Suchánek
  2023-09-11 19:56                     ` [PATCH] kbuild: rpm-pkg: Fix build with non-default MODLIB Michal Suchanek
  1 sibling, 2 replies; 83+ messages in thread
From: Masahiro Yamada @ 2023-07-17 19:14 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: Nicolas Schier, linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutn�,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	linux-kbuild, linux-kernel

On Sat, Jul 15, 2023 at 12:10 AM Michal Suchánek <msuchanek@suse.de> wrote:
>
> On Fri, Jul 14, 2023 at 04:54:49PM +0200, Nicolas Schier wrote:
> > On Fri, Jul 14, 2023 at 04:30:02PM +0200, Michal Such�nek wrote:
> > > Hello,
> > >
> > > On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> > > > On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > > > > Some distributions aim at not shipping any files in / outside of usr.
> > > >
> > > > For me, preventing negation often makes things easier, e.g.: "... aim at
> > > > shipping files only below /usr".
> > > >
> > > > >
> > > > > The path under which kernel modules are installed is hardcoded to /lib
> > > > > which conflicts with this goal.
> > > > >
> > > > > When kmod provides the config command, use it to determine the correct
> > > > > module installation prefix.
> > > > >
> > > > > This is a prefix under which the modules are searched by kmod on the
> > > > > system, and is separate from the temporary staging location already
> > > > > supported by INSTALL_MOD_PATH.
> > > > >
> > > > > With kmod that does not provide the config command empty prefix is used
> > > > > as before.
> > > > >
> > > > > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > > > > ---
> > > > > v2: Avoid error on systems with kmod that does not support config
> > > > > command
> > > > > v3: More verbose commit message
> > > > > ---
> > > > >  Makefile          | 4 +++-
> > > > >  scripts/depmod.sh | 8 ++++----
> > > > >  2 files changed, 7 insertions(+), 5 deletions(-)
> > > > >
> > > > > diff --git a/Makefile b/Makefile
> > > > > index 47690c28456a..b1fea135bdec 100644
> > > > > --- a/Makefile
> > > > > +++ b/Makefile
> > > > > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> > > > >  # makefile but the argument can be passed to make if needed.
> > > > >  #
> > > > >
> > > > > -MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > > > > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> > > >
> > > > All other calls of `jq` that I could find are located at tools/; as this here
> > > > is evaluated on each invocation, this should probably be documented in
> > > > Documentation/process/changes.rst?
> > > >
> > > > (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
> > >
> > > That's a good point.
> > >
> > > >
> > > > > +
> > > > > +MODLIB = $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> > > > >  export MODLIB
> > > > >
> > > > >  PHONY += prepare0
> > > > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > > > index 3643b4f896ed..88ac79056153 100755
> > > > > --- a/scripts/depmod.sh
> > > > > +++ b/scripts/depmod.sh
> > > > > @@ -27,16 +27,16 @@ fi
> > > > >  # numbers, so we cheat with a symlink here
> > > > >  depmod_hack_needed=true
> > > > >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > > > > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > > > > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> > > > >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > > > > -       if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > -               -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > > +       if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > +               -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > >                 depmod_hack_needed=false
> > > > >         fi
> > > > >  fi
> > > >
> > > > I'd like to come back to the statement from Masahiro: Is the check above,
> > > > against some very old versions of depmod [1], the only reason for this patch?
> > > >
> > > > If we could remove that, would
> > > >
> > > >     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> > > >
> > > > be sufficient?
> > >
> > > No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
> > > the newly added part is not because it's integral part of where the
> > > modules are installed on the system, and not the staging area path.
> >
> > Ah, thanks.  So just for my understanding, could this be a (non-gentle)
> > alternative version of your patch, w/o modifying top-level Makefile?
> >
> > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > index 3643b4f896ed..72c819de0669 100755
> > --- a/scripts/depmod.sh
> > +++ b/scripts/depmod.sh
> > @@ -1,4 +1,4 @@
> > -#!/bin/sh
> > +#!/bin/bash
> >  # SPDX-License-Identifier: GPL-2.0
> >  #
> >  # A depmod wrapper used by the toplevel Makefile
> > @@ -23,6 +23,8 @@ if [ -z $(command -v $DEPMOD) ]; then
> >         exit 0
> >  fi
> >
> > +kmod_version=$(( $(kmod --version | sed -rne 's/^kmod version ([0-9]+).*$/\1/p') ))
> > +
> >  # older versions of depmod require the version string to start with three
> >  # numbers, so we cheat with a symlink here
> >  depmod_hack_needed=true
> > @@ -35,6 +37,13 @@ if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> >         fi
> >  fi
> >  rm -rf "$tmp_dir"
> > +
> > +if [ "${kmod_version}" -gt 32 ]; then
> > +       kmod_prefix="$(kmod config | jq -r .module_prefix)"
> > +       INSTALL_MOD_PATH="${INSTALL_MOD_PATH#${kmod_prefix}"
> > +       depmod_hack_needed=false
> > +fi
> > +
> >  if $depmod_hack_needed; then
> >         symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> >         ln -s "$KERNELRELEASE" "$symlink"
> >
> > (untested, and assuming that kmod module prefix is in kmod >= 32)
>
> It can be detected by running the 'kmod config' command first and
> ignoring the output when it fails which the above patch already did.
> The version check does not sound very reliable.
>
> > Or are I am still missing something?
>
> MODLIB still needs to include the extra prefix so that files are
> installed in the correct location. And that's defined in the toplevel
> Makefile.
>
> Thanks
>
> Michal



As Documentation/kbuild/kbuild.rst mentions,
you can override MODLIB.

Kbuild already provides the maximum flexibility.


$ make modules_install \
  INSTALL_MOD_PATH=/path/to/whatever/staging/dir \
  MODLIB=/path/to/whatever/real/install/destination



-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-17 10:39   ` [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
@ 2023-07-17 19:28     ` Jan Engelhardt
  2023-07-18  8:43       ` Michal Suchánek
  2023-07-17 20:12     ` Lucas De Marchi
  1 sibling, 1 reply; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-17 19:28 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Monday 2023-07-17 12:39, Michal Suchanek wrote:

>modprobe.d is now searched under ${prefix}/lib, add ${module_directory} to
>specify the directory where to search for kernel modules.
>
>With this distributions that do not want to ship files in /lib can also
>move kernel modules to /usr while others can keep them in /lib.

This patch breaks kernel builds/installation.

 * assume $distro has given me a kmod that has your submission included,
   and such kmod was ./configure'd --with-module-directory=/usr/lib/modules

With such a kmod, the module installation of current and past kernels
is not possible, in other words, ** bisecting kernels ** is broken:


$ make modules_install INSTALL_MOD_PATH=$PWD/rt V=1
[...]
# INSTALL /tmp/linux/rt/lib/modules/6.4.3/kernel/virt/lib/irqbypass.ko
  mkdir -p /tmp/linux/rt/lib/modules/6.4.3/kernel/virt/lib/; cp virt/lib/irqbypass.ko /tmp/linux/rt/lib/modules/6.4.3/kernel/virt/lib/irqbypass.ko
[...]
# DEPMOD  /tmp/linux/rt/lib/modules/6.4.3
  sh ./scripts/depmod.sh depmod 6.4.3
depmod: ERROR: could not open directory /tmp/linux/rt/usr/lib/modules/99.98.6.4.3: No such file or directory
depmod: FATAL: could not search modules: No such file or directory
make: *** [Makefile:1956: modules_install] Error 1

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-17 10:39   ` [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
  2023-07-17 19:28     ` Jan Engelhardt
@ 2023-07-17 20:12     ` Lucas De Marchi
  2023-07-18  8:32       ` Michal Suchánek
  1 sibling, 1 reply; 83+ messages in thread
From: Lucas De Marchi @ 2023-07-17 20:12 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Mon, Jul 17, 2023 at 12:39:53PM +0200, Michal Suchanek wrote:
>modprobe.d is now searched under ${prefix}/lib, add ${module_directory} to
>specify the directory where to search for kernel modules.

I was failing to see the relation of modprobe.d with the module
directory. Maybe reword this slightly?

Now that modprobe.d is searched under ${prefix}/lib, allow a complete
transition to files only under ${prefix} by adding a ${module_directory}
configuration. This specifies the directory where to search for kernel
modules and should match the location where the kernel/distro installs
them.

>
>With this distributions that do not want to ship files in /lib can also
>move kernel modules to /usr while others can keep them in /lib.
>
>Signed-off-by: Michal Suchanek <msuchanek@suse.de>
>---
>v4: Make the whole path configurable
>---
> Makefile.am                          |   3 +-
> configure.ac                         |   7 ++
> libkmod/libkmod.c                    |   4 +-
> man/Makefile.am                      |   1 +
> man/depmod.d.xml                     |   6 +-
> man/depmod.xml                       |   4 +-
> man/modinfo.xml                      |   2 +-
> man/modprobe.xml                     |   2 +-
> man/modules.dep.xml                  |   6 +-
> testsuite/module-playground/Makefile |   2 +-
> testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
> testsuite/test-depmod.c              |  16 ++--
> testsuite/test-testsuite.c           |   8 +-
> tools/depmod.c                       |   6 +-
> tools/kmod.c                         |   1 +
> tools/modinfo.c                      |   4 +-
> tools/modprobe.c                     |   4 +-
> tools/static-nodes.c                 |   6 +-
> 18 files changed, 107 insertions(+), 84 deletions(-)
>
>diff --git a/Makefile.am b/Makefile.am
>index 7aa5bfa5638d..981574558c93 100644
>--- a/Makefile.am
>+++ b/Makefile.am
>@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
> 	-I$(top_srcdir) \
> 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
> 	-DPREFIX=\""$(prefix)"\" \
>+	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
> 	${zlib_CFLAGS}
>
> AM_CFLAGS = $(OUR_CFLAGS)
>@@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
> MODULE_PLAYGROUND = testsuite/module-playground
> ROOTFS = testsuite/rootfs
> ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
>-CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
>+CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
>
> build-module-playground:
> 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
>diff --git a/configure.ac b/configure.ac
>index 6064dee77ae6..a195c8e6b554 100644
>--- a/configure.ac
>+++ b/configure.ac
>@@ -84,6 +84,12 @@ AC_ARG_WITH([rootlibdir],
>         [], [with_rootlibdir=$libdir])
> AC_SUBST([rootlibdir], [$with_rootlibdir])
>
>+# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
>+AC_ARG_WITH([module_directory],
>+        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
>+        [], [with_module_directory=/lib/modules])
>+AC_SUBST([module_directory], [$with_module_directory])
>+
> AC_ARG_WITH([zstd],
> 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
> 	[], [with_zstd=no])
>@@ -304,6 +310,7 @@ AC_MSG_RESULT([
> 	$PACKAGE $VERSION
> 	=======
>
>+	module_directory:	${module_directory}
> 	prefix:			${prefix}
> 	sysconfdir:		${sysconfdir}
> 	libdir:			${libdir}
>diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
>index f3139c1cef97..d2ed8748c833 100644
>--- a/libkmod/libkmod.c
>+++ b/libkmod/libkmod.c
>@@ -209,7 +209,7 @@ static int log_priority(const char *priority)
> 	return 0;
> }
>
>-static const char *dirname_default_prefix = "/lib/modules";
>+static const char *dirname_default_prefix = MODULE_DIRECTORY;
>
> static char *get_kernel_release(const char *dirname)
> {
>@@ -231,7 +231,7 @@ static char *get_kernel_release(const char *dirname)
> /**
>  * kmod_new:
>  * @dirname: what to consider as linux module's directory, if NULL
>- *           defaults to /lib/modules/`uname -r`. If it's relative,
>+ *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
>  *           it's treated as relative to the current working directory.
>  *           Otherwise, give an absolute dirname.
>  * @config_paths: ordered array of paths (directories or files) where
>diff --git a/man/Makefile.am b/man/Makefile.am
>index ad07c30bbd24..25c2cc6fdf13 100644
>--- a/man/Makefile.am
>+++ b/man/Makefile.am
>@@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
> 	else \
> 		sed -e '/@PREFIX@/d' $< ; \
> 	fi | \
>+	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
> 	$(XSLT) \
> 		-o $@ \
> 		--nonet \
>diff --git a/man/depmod.d.xml b/man/depmod.d.xml
>index 431ebca6654b..676977c69a4f 100644
>--- a/man/depmod.d.xml
>+++ b/man/depmod.d.xml
>@@ -70,7 +70,7 @@
>         </term>
>         <listitem>
>           <para>
>-            This allows you to specify the order in which /lib/modules
>+            This allows you to specify the order in which @MODULE_DIRECTORY@
>             (or other configured module location) subdirectories will
>             be processed by <command>depmod</command>. Directories are
>             listed in order, with the highest priority given to the
>@@ -101,7 +101,7 @@
>             <command>depmod</command> command. It is possible to
>             specify one kernel or all kernels using the * wildcard.
>             <replaceable>modulesubdirectory</replaceable> is the
>-            name of the subdirectory under /lib/modules (or other
>+            name of the subdirectory under @MODULE_DIRECTORY@ (or other
>             module location) where the target module is installed.
>           </para>
>           <para>
>@@ -110,7 +110,7 @@
>             specifying the following command: "override kmod * extra".
>             This will ensure that any matching module name installed
>             under the <command>extra</command> subdirectory within
>-            /lib/modules (or other module location) will take priority
>+            @MODULE_DIRECTORY@ (or other module location) will take priority
>             over any likenamed module already provided by the kernel.
>           </para>
>         </listitem>
>diff --git a/man/depmod.xml b/man/depmod.xml
>index 3b0097184fd7..fce2a4a67a89 100644
>--- a/man/depmod.xml
>+++ b/man/depmod.xml
>@@ -80,7 +80,7 @@
>     </para>
>     <para> <command>depmod</command> creates a list of module dependencies by
>       reading each module under
>-      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
>+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
>       determining what symbols it exports and what symbols it needs.  By
>       default, this list is written to <filename>modules.dep</filename>, and a
>       binary hashed version named <filename>modules.dep.bin</filename>, in the
>@@ -141,7 +141,7 @@
>         <listitem>
>           <para>
>             If your modules are not currently in the (normal) directory
>-            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
>+            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
>             but in a staging area, you can specify a
>             <replaceable>basedir</replaceable> which is prepended to the
>             directory name.  This <replaceable>basedir</replaceable> is
>diff --git a/man/modinfo.xml b/man/modinfo.xml
>index 9fe0324a2527..b6c4d6045829 100644
>--- a/man/modinfo.xml
>+++ b/man/modinfo.xml
>@@ -54,7 +54,7 @@
>       <command>modinfo</command> extracts information from the Linux Kernel
>       modules given on the command line.  If the module name is not a filename,
>       then the
>-      <filename>/lib/modules/</filename><replaceable>version</replaceable>
>+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
>       directory is searched, as is also done by
>       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
>       when loading kernel modules.
>diff --git a/man/modprobe.xml b/man/modprobe.xml
>index 91f9e27997cd..4d1fd59c000b 100644
>--- a/man/modprobe.xml
>+++ b/man/modprobe.xml
>@@ -78,7 +78,7 @@
>       is no difference between _ and - in module names (automatic
>       underscore conversion is performed).
>       <command>modprobe</command> looks in the module directory
>-      <filename>/lib/modules/`uname -r`</filename> for all
>+      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
>       the modules and other files, except for the optional
>       configuration files in the
>       <filename>/etc/modprobe.d</filename> directory
>diff --git a/man/modules.dep.xml b/man/modules.dep.xml
>index ed633694ec9e..8ef6d8b3536e 100644
>--- a/man/modules.dep.xml
>+++ b/man/modules.dep.xml
>@@ -34,8 +34,8 @@
>   </refnamediv>
>
>   <refsynopsisdiv>
>-    <para><filename>/lib/modules/modules.dep</filename></para>
>-    <para><filename>/lib/modules/modules.dep.bin</filename></para>
>+    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
>+    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
>   </refsynopsisdiv>
>
>   <refsect1><title>DESCRIPTION</title>
>@@ -43,7 +43,7 @@
>       <filename>modules.dep.bin</filename> is a binary file generated by
>       <command>depmod</command> listing the dependencies for
>       every module in the directories under
>-      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
>+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
>       It is used by kmod tools such as <command>modprobe</command> and
>       libkmod.
>     </para>
>diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
>index e6045b0dd932..a7ab09bea2bf 100644
>--- a/testsuite/module-playground/Makefile
>+++ b/testsuite/module-playground/Makefile
>@@ -47,7 +47,7 @@ endif
>
> else
> # normal makefile
>-KDIR ?= /lib/modules/`uname -r`/build
>+KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
> KVER ?= `uname -r`
> ifeq ($(FAKE_BUILD),)
>     FAKE_BUILD=0
>diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
>index 4440ddcd6b4d..a780f9381b3c 100755
>--- a/testsuite/setup-rootfs.sh
>+++ b/testsuite/setup-rootfs.sh
>@@ -16,6 +16,19 @@ create_rootfs() {
> 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
> 	find "$ROOTFS" -type d -exec chmod +w {} \;
> 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
>+	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
>+		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
>+		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)

why this second sed?

Lucas De Marchi

>+		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
>+			version="$(basename $i)"
>+			[ $version != 'external' ] || continue
>+			mod="$(dirname $i)"
>+			lib="$(dirname $mod)"
>+			up="$(dirname $lib)$MODULE_DIRECTORY"
>+			mkdir -p "$up"
>+			mv "$i" "$up"
>+		done
>+	fi
>
> 	if [ "$SYSCONFDIR" != "/etc" ]; then
> 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
>@@ -32,57 +45,57 @@ feature_enabled() {
>
> declare -A map
> map=(
>-    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
>-    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
>-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
>-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
>-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
>-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
>+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/kernel/crypto/"]="mod-simple.ko"
>+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/updates/"]="mod-simple.ko"
>+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
>+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
>+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
>+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
>     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
>-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
>-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
>+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
>+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
>     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
>-    ["test-depmod/search-order-override/lib/modules/4.4.4/foo/"]="mod-simple.ko"
>-    ["test-depmod/search-order-override/lib/modules/4.4.4/override/"]="mod-simple.ko"
>-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
>-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
>-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
>-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
>+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
>+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/override/"]="mod-simple.ko"
>+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
>+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
>+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
>+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
>     ["test-init/"]="mod-simple.ko"
>     ["test-remove/"]="mod-simple.ko"
>-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
>+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
>     ["test-modprobe/show-exports/mod-loop-a.ko"]="mod-loop-a.ko"
>-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>-    ["test-modprobe/force/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
>-    ["test-modprobe/oldkernel/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
>-    ["test-modprobe/oldkernel-force/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
>-    ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
>-    ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
>+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>+    ["test-modprobe/force$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
>+    ["test-modprobe/oldkernel$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
>+    ["test-modprobe/oldkernel-force$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
>+    ["test-modprobe/alias-to-none$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
>+    ["test-modprobe/module-param-kcmdline$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
>     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
>     ["test-modprobe/module-from-abspath/home/foo/"]="mod-simple.ko"
>     ["test-modprobe/module-from-relpath/home/foo/"]="mod-simple.ko"
>-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
>-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
>-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
>-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
>-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
>-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
>+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
>+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
>+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
>+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
>+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
>+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
>     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
>     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
>     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
>@@ -90,20 +103,20 @@ map=(
>     ["test-modinfo/mod-simple-sha256.ko"]="mod-simple.ko"
>     ["test-modinfo/mod-simple-pkcs7.ko"]="mod-simple.ko"
>     ["test-modinfo/external/lib/modules/external/mod-simple.ko"]="mod-simple.ko"
>-    ["test-tools/insert/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
>-    ["test-tools/remove/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
>+    ["test-tools/insert$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
>+    ["test-tools/remove$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
> )
>
> gzip_array=(
>-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
>+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"
>     )
>
> xz_array=(
>-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
>+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
>     )
>
> zstd_array=(
>-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
>+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"
>     )
>
> attach_sha256_array=(
>diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
>index 93606947f18a..c96dbf0a62be 100644
>--- a/testsuite/test-depmod.c
>+++ b/testsuite/test-depmod.c
>@@ -27,7 +27,7 @@
>
> #define MODULES_UNAME "4.4.4"
> #define MODULES_ORDER_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-order-compressed"
>-#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS "/lib/modules/" MODULES_UNAME
>+#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
> static noreturn int depmod_modules_order_for_compressed(const struct test *t)
> {
> 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>@@ -55,8 +55,8 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
> 	});
>
> #define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
>-#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_UNAME
>-#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_UNAME
>+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir" MODULE_DIRECTORY "/" MODULES_UNAME
>+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
> static noreturn int depmod_modules_outdir(const struct test *t)
> {
> 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>@@ -87,7 +87,7 @@ DEFINE_TEST(depmod_modules_outdir,
> 	});
>
> #define SEARCH_ORDER_SIMPLE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-simple"
>-#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS "/lib/modules/" MODULES_UNAME
>+#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
> static noreturn int depmod_search_order_simple(const struct test *t)
> {
> 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>@@ -114,7 +114,7 @@ DEFINE_TEST(depmod_search_order_simple,
> 	});
>
> #define SEARCH_ORDER_SAME_PREFIX_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-same-prefix"
>-#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS "/lib/modules/" MODULES_UNAME
>+#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
> static noreturn int depmod_search_order_same_prefix(const struct test *t)
> {
> 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>@@ -164,7 +164,7 @@ DEFINE_TEST(depmod_detect_loop,
> 	});
>
> #define SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-first"
>-#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS "/lib/modules/" MODULES_UNAME
>+#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
> static noreturn int depmod_search_order_external_first(const struct test *t)
> {
> 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>@@ -191,7 +191,7 @@ DEFINE_TEST(depmod_search_order_external_first,
> 	});
>
> #define SEARCH_ORDER_EXTERNAL_LAST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-last"
>-#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS "/lib/modules/" MODULES_UNAME
>+#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
> static noreturn int depmod_search_order_external_last(const struct test *t)
> {
> 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>@@ -218,7 +218,7 @@ DEFINE_TEST(depmod_search_order_external_last,
> 	});
>
> #define SEARCH_ORDER_OVERRIDE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-override"
>-#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS "/lib/modules/" MODULES_UNAME
>+#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
> static noreturn int depmod_search_order_override(const struct test *t)
> {
> 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c
>index 56e73609f204..c77c4bbc04eb 100644
>--- a/testsuite/test-testsuite.c
>+++ b/testsuite/test-testsuite.c
>@@ -64,7 +64,7 @@ static int testsuite_rootfs_fopen(const struct test *t)
> 	char s[100];
> 	int n;
>
>-	fp = fopen("/lib/modules/a", "r");
>+	fp = fopen(MODULE_DIRECTORY "/a", "r");
> 	if (fp == NULL)
> 		return EXIT_FAILURE;;
>
>@@ -89,7 +89,7 @@ static int testsuite_rootfs_open(const struct test *t)
> 	char buf[100];
> 	int fd, done;
>
>-	fd = open("/lib/modules/a", O_RDONLY);
>+	fd = open(MODULE_DIRECTORY "/a", O_RDONLY);
> 	if (fd < 0)
> 		return EXIT_FAILURE;
>
>@@ -121,12 +121,12 @@ static int testsuite_rootfs_stat_access(const struct test *t)
> {
> 	struct stat st;
>
>-	if (access("/lib/modules/a", F_OK) < 0) {
>+	if (access(MODULE_DIRECTORY "/a", F_OK) < 0) {
> 		ERR("access failed: %m\n");
> 		return EXIT_FAILURE;
> 	}
>
>-	if (stat("/lib/modules/a", &st) < 0) {
>+	if (stat(MODULE_DIRECTORY "/a", &st) < 0) {
> 		ERR("stat failed: %m\n");
> 		return EXIT_FAILURE;
> 	}
>diff --git a/tools/depmod.c b/tools/depmod.c
>index 7e9339923809..22bc1d87c83e 100644
>--- a/tools/depmod.c
>+++ b/tools/depmod.c
>@@ -911,7 +911,7 @@ struct vertex;
> struct mod {
> 	struct kmod_module *kmod;
> 	char *path;
>-	const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
>+	const char *relpath; /* path relative to '$ROOT$MODULE_DIRECTORY/$VER/' */
> 	char *uncrelpath; /* same as relpath but ending in .ko */
> 	struct kmod_list *info_list;
> 	struct kmod_list *dep_sym_list;
>@@ -3024,11 +3024,11 @@ static int do_depmod(int argc, char *argv[])
> 	}
>
> 	cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
>-				  "%s/lib/modules/%s",
>+				  "%s" MODULE_DIRECTORY "/%s",
> 				  root ?: "", cfg.kversion);
>
> 	cfg.outdirnamelen = snprintf(cfg.outdirname, PATH_MAX,
>-				     "%s/lib/modules/%s",
>+				     "%s" MODULE_DIRECTORY "/%s",
> 				     out_root ?: (root ?: ""), cfg.kversion);
>
> 	if (optind == argc)
>diff --git a/tools/kmod.c b/tools/kmod.c
>index a684c1d24284..3a2589c67447 100644
>--- a/tools/kmod.c
>+++ b/tools/kmod.c
>@@ -115,6 +115,7 @@ static int kmod_config(int argc, char *argv[])
> 	unsigned i;
> 	printf("{\"prefix\":\"" PREFIX "\""
> 			",\"sysconfdir\":\"" SYSCONFDIR "\""
>+			",\"module_directory\":\"" MODULE_DIRECTORY "\""
> 			",\"module_signature\":["
> #ifdef ENABLE_OPENSSL
> 			"\"PKCS#7\","
>diff --git a/tools/modinfo.c b/tools/modinfo.c
>index d0aab200af4e..cacc32dc4c40 100644
>--- a/tools/modinfo.c
>+++ b/tools/modinfo.c
>@@ -367,7 +367,7 @@ static void help(void)
> 		"\t-m, --modname               Handle argument as module name instead of alias or filename\n"
> 		"\t-F, --field=FIELD           Print only provided FIELD\n"
> 		"\t-k, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
>-		"\t-b, --basedir=DIR           Use DIR as filesystem root for /lib/modules\n"
>+		"\t-b, --basedir=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
> 		"\t-V, --version               Show version\n"
> 		"\t-h, --help                  Show this help\n",
> 		program_invocation_short_name);
>@@ -462,7 +462,7 @@ static int do_modinfo(int argc, char *argv[])
> 			}
> 			kversion = u.release;
> 		}
>-		snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
>+		snprintf(dirname_buf, sizeof(dirname_buf), "%s" MODULE_DIRECTORY "/%s",
> 			 root, kversion);
> 		dirname = dirname_buf;
> 	}
>diff --git a/tools/modprobe.c b/tools/modprobe.c
>index e891028349a8..5306bef250da 100644
>--- a/tools/modprobe.c
>+++ b/tools/modprobe.c
>@@ -142,7 +142,7 @@ static void help(void)
> 		"\t-n, --show                  Same as --dry-run\n"
>
> 		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
>-		"\t-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules\n"
>+		"\t-d, --dirname=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
> 		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
>
> 		"\t-s, --syslog                print to syslog, not stderr\n"
>@@ -999,7 +999,7 @@ static int do_modprobe(int argc, char **orig_argv)
> 			kversion = u.release;
> 		}
> 		snprintf(dirname_buf, sizeof(dirname_buf),
>-				"%s/lib/modules/%s", root,
>+				"%s" MODULE_DIRECTORY "/%s", root,
> 				kversion);
> 		dirname = dirname_buf;
> 	}
>diff --git a/tools/static-nodes.c b/tools/static-nodes.c
>index 8d2356da73f3..5ef3743e967b 100644
>--- a/tools/static-nodes.c
>+++ b/tools/static-nodes.c
>@@ -212,15 +212,15 @@ static int do_static_nodes(int argc, char *argv[])
> 		goto finish;
> 	}
>
>-	snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
>+	snprintf(modules, sizeof(modules), MODULE_DIRECTORY "/%s/modules.devname", kernel.release);
> 	in = fopen(modules, "re");
> 	if (in == NULL) {
> 		if (errno == ENOENT) {
>-			fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
>+			fprintf(stderr, "Warning: " MODULE_DIRECTORY "/%s/modules.devname not found - ignoring\n",
> 				kernel.release);
> 			ret = EXIT_SUCCESS;
> 		} else {
>-			fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
>+			fprintf(stderr, "Error: could not open " MODULE_DIRECTORY "/%s/modules.devname - %m\n",
> 				kernel.release);
> 			ret = EXIT_FAILURE;
> 		}
>-- 
>2.41.0
>

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-17 20:12     ` Lucas De Marchi
@ 2023-07-18  8:32       ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-18  8:32 UTC (permalink / raw)
  To: Lucas De Marchi
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

On Mon, Jul 17, 2023 at 05:12:59PM -0300, Lucas De Marchi wrote:
> On Mon, Jul 17, 2023 at 12:39:53PM +0200, Michal Suchanek wrote:
> > modprobe.d is now searched under ${prefix}/lib, add ${module_directory} to
> > specify the directory where to search for kernel modules.
> 
> I was failing to see the relation of modprobe.d with the module
> directory. Maybe reword this slightly?
> 
> Now that modprobe.d is searched under ${prefix}/lib, allow a complete
> transition to files only under ${prefix} by adding a ${module_directory}
> configuration. This specifies the directory where to search for kernel
> modules and should match the location where the kernel/distro installs
> them.
> 
> > 
> > With this distributions that do not want to ship files in /lib can also
> > move kernel modules to /usr while others can keep them in /lib.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v4: Make the whole path configurable
> > ---
> > Makefile.am                          |   3 +-
> > configure.ac                         |   7 ++
> > libkmod/libkmod.c                    |   4 +-
> > man/Makefile.am                      |   1 +
> > man/depmod.d.xml                     |   6 +-
> > man/depmod.xml                       |   4 +-
> > man/modinfo.xml                      |   2 +-
> > man/modprobe.xml                     |   2 +-
> > man/modules.dep.xml                  |   6 +-
> > testsuite/module-playground/Makefile |   2 +-
> > testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
> > testsuite/test-depmod.c              |  16 ++--
> > testsuite/test-testsuite.c           |   8 +-
> > tools/depmod.c                       |   6 +-
> > tools/kmod.c                         |   1 +
> > tools/modinfo.c                      |   4 +-
> > tools/modprobe.c                     |   4 +-
> > tools/static-nodes.c                 |   6 +-
> > 18 files changed, 107 insertions(+), 84 deletions(-)
> > 
> > diff --git a/Makefile.am b/Makefile.am
> > index 7aa5bfa5638d..981574558c93 100644
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > @@ -20,6 +20,7 @@ AM_CPPFLAGS = \
> > 	-I$(top_srcdir) \
> > 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
> > 	-DPREFIX=\""$(prefix)"\" \
> > +	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
> > 	${zlib_CFLAGS}
> > 
> > AM_CFLAGS = $(OUR_CFLAGS)
> > @@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
> > MODULE_PLAYGROUND = testsuite/module-playground
> > ROOTFS = testsuite/rootfs
> > ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
> > -CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
> > +CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
> > 
> > build-module-playground:
> > 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
> > diff --git a/configure.ac b/configure.ac
> > index 6064dee77ae6..a195c8e6b554 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -84,6 +84,12 @@ AC_ARG_WITH([rootlibdir],
> >         [], [with_rootlibdir=$libdir])
> > AC_SUBST([rootlibdir], [$with_rootlibdir])
> > 
> > +# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
> > +AC_ARG_WITH([module_directory],
> > +        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
> > +        [], [with_module_directory=/lib/modules])
> > +AC_SUBST([module_directory], [$with_module_directory])
> > +
> > AC_ARG_WITH([zstd],
> > 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
> > 	[], [with_zstd=no])
> > @@ -304,6 +310,7 @@ AC_MSG_RESULT([
> > 	$PACKAGE $VERSION
> > 	=======
> > 
> > +	module_directory:	${module_directory}
> > 	prefix:			${prefix}
> > 	sysconfdir:		${sysconfdir}
> > 	libdir:			${libdir}
> > diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> > index f3139c1cef97..d2ed8748c833 100644
> > --- a/libkmod/libkmod.c
> > +++ b/libkmod/libkmod.c
> > @@ -209,7 +209,7 @@ static int log_priority(const char *priority)
> > 	return 0;
> > }
> > 
> > -static const char *dirname_default_prefix = "/lib/modules";
> > +static const char *dirname_default_prefix = MODULE_DIRECTORY;
> > 
> > static char *get_kernel_release(const char *dirname)
> > {
> > @@ -231,7 +231,7 @@ static char *get_kernel_release(const char *dirname)
> > /**
> >  * kmod_new:
> >  * @dirname: what to consider as linux module's directory, if NULL
> > - *           defaults to /lib/modules/`uname -r`. If it's relative,
> > + *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
> >  *           it's treated as relative to the current working directory.
> >  *           Otherwise, give an absolute dirname.
> >  * @config_paths: ordered array of paths (directories or files) where
> > diff --git a/man/Makefile.am b/man/Makefile.am
> > index ad07c30bbd24..25c2cc6fdf13 100644
> > --- a/man/Makefile.am
> > +++ b/man/Makefile.am
> > @@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
> > 	else \
> > 		sed -e '/@PREFIX@/d' $< ; \
> > 	fi | \
> > +	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
> > 	$(XSLT) \
> > 		-o $@ \
> > 		--nonet \
> > diff --git a/man/depmod.d.xml b/man/depmod.d.xml
> > index 431ebca6654b..676977c69a4f 100644
> > --- a/man/depmod.d.xml
> > +++ b/man/depmod.d.xml
> > @@ -70,7 +70,7 @@
> >         </term>
> >         <listitem>
> >           <para>
> > -            This allows you to specify the order in which /lib/modules
> > +            This allows you to specify the order in which @MODULE_DIRECTORY@
> >             (or other configured module location) subdirectories will
> >             be processed by <command>depmod</command>. Directories are
> >             listed in order, with the highest priority given to the
> > @@ -101,7 +101,7 @@
> >             <command>depmod</command> command. It is possible to
> >             specify one kernel or all kernels using the * wildcard.
> >             <replaceable>modulesubdirectory</replaceable> is the
> > -            name of the subdirectory under /lib/modules (or other
> > +            name of the subdirectory under @MODULE_DIRECTORY@ (or other
> >             module location) where the target module is installed.
> >           </para>
> >           <para>
> > @@ -110,7 +110,7 @@
> >             specifying the following command: "override kmod * extra".
> >             This will ensure that any matching module name installed
> >             under the <command>extra</command> subdirectory within
> > -            /lib/modules (or other module location) will take priority
> > +            @MODULE_DIRECTORY@ (or other module location) will take priority
> >             over any likenamed module already provided by the kernel.
> >           </para>
> >         </listitem>
> > diff --git a/man/depmod.xml b/man/depmod.xml
> > index 3b0097184fd7..fce2a4a67a89 100644
> > --- a/man/depmod.xml
> > +++ b/man/depmod.xml
> > @@ -80,7 +80,7 @@
> >     </para>
> >     <para> <command>depmod</command> creates a list of module dependencies by
> >       reading each module under
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
> >       determining what symbols it exports and what symbols it needs.  By
> >       default, this list is written to <filename>modules.dep</filename>, and a
> >       binary hashed version named <filename>modules.dep.bin</filename>, in the
> > @@ -141,7 +141,7 @@
> >         <listitem>
> >           <para>
> >             If your modules are not currently in the (normal) directory
> > -            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
> > +            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
> >             but in a staging area, you can specify a
> >             <replaceable>basedir</replaceable> which is prepended to the
> >             directory name.  This <replaceable>basedir</replaceable> is
> > diff --git a/man/modinfo.xml b/man/modinfo.xml
> > index 9fe0324a2527..b6c4d6045829 100644
> > --- a/man/modinfo.xml
> > +++ b/man/modinfo.xml
> > @@ -54,7 +54,7 @@
> >       <command>modinfo</command> extracts information from the Linux Kernel
> >       modules given on the command line.  If the module name is not a filename,
> >       then the
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable>
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
> >       directory is searched, as is also done by
> >       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
> >       when loading kernel modules.
> > diff --git a/man/modprobe.xml b/man/modprobe.xml
> > index 91f9e27997cd..4d1fd59c000b 100644
> > --- a/man/modprobe.xml
> > +++ b/man/modprobe.xml
> > @@ -78,7 +78,7 @@
> >       is no difference between _ and - in module names (automatic
> >       underscore conversion is performed).
> >       <command>modprobe</command> looks in the module directory
> > -      <filename>/lib/modules/`uname -r`</filename> for all
> > +      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
> >       the modules and other files, except for the optional
> >       configuration files in the
> >       <filename>/etc/modprobe.d</filename> directory
> > diff --git a/man/modules.dep.xml b/man/modules.dep.xml
> > index ed633694ec9e..8ef6d8b3536e 100644
> > --- a/man/modules.dep.xml
> > +++ b/man/modules.dep.xml
> > @@ -34,8 +34,8 @@
> >   </refnamediv>
> > 
> >   <refsynopsisdiv>
> > -    <para><filename>/lib/modules/modules.dep</filename></para>
> > -    <para><filename>/lib/modules/modules.dep.bin</filename></para>
> > +    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
> > +    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
> >   </refsynopsisdiv>
> > 
> >   <refsect1><title>DESCRIPTION</title>
> > @@ -43,7 +43,7 @@
> >       <filename>modules.dep.bin</filename> is a binary file generated by
> >       <command>depmod</command> listing the dependencies for
> >       every module in the directories under
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
> >       It is used by kmod tools such as <command>modprobe</command> and
> >       libkmod.
> >     </para>
> > diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
> > index e6045b0dd932..a7ab09bea2bf 100644
> > --- a/testsuite/module-playground/Makefile
> > +++ b/testsuite/module-playground/Makefile
> > @@ -47,7 +47,7 @@ endif
> > 
> > else
> > # normal makefile
> > -KDIR ?= /lib/modules/`uname -r`/build
> > +KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
> > KVER ?= `uname -r`
> > ifeq ($(FAKE_BUILD),)
> >     FAKE_BUILD=0
> > diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
> > index 4440ddcd6b4d..a780f9381b3c 100755
> > --- a/testsuite/setup-rootfs.sh
> > +++ b/testsuite/setup-rootfs.sh
> > @@ -16,6 +16,19 @@ create_rootfs() {
> > 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
> > 	find "$ROOTFS" -type d -exec chmod +w {} \;
> > 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
> > +	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
> > +		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
> > +		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
> 
> why this second sed?

These are 'external' modules not part of a kernel, the absolute path is
used in modules.dep and reported in outputs.

Moving these breaks the tests, and is beside the point.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-17 19:28     ` Jan Engelhardt
@ 2023-07-18  8:43       ` Michal Suchánek
  2023-07-18  9:41         ` Jan Engelhardt
  0 siblings, 1 reply; 83+ messages in thread
From: Michal Suchánek @ 2023-07-18  8:43 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

Hello,

On Mon, Jul 17, 2023 at 09:28:30PM +0200, Jan Engelhardt wrote:
> 
> On Monday 2023-07-17 12:39, Michal Suchanek wrote:
> 
> >modprobe.d is now searched under ${prefix}/lib, add ${module_directory} to
> >specify the directory where to search for kernel modules.
> >
> >With this distributions that do not want to ship files in /lib can also
> >move kernel modules to /usr while others can keep them in /lib.
> 
> This patch breaks kernel builds/installation.
> 
>  * assume $distro has given me a kmod that has your submission included,
>    and such kmod was ./configure'd --with-module-directory=/usr/lib/modules
> 
> With such a kmod, the module installation of current and past kernels
> is not possible, in other words, ** bisecting kernels ** is broken:

It might be nice to provide backwads compatibility with earlier
configurations.

However, if it comes at the cost of making the implementation more
complex and future maintenance more difficult it might not be such a
great idea. So far I have not seen a proposal how to do it nicely.

You can use any of the number of workarounds that have been used for
installing kernels on usrmerged distributions up until now.

sed -i -e s,/lib/modules,/usr/lib/modules, Makefile scripts/depmod.sh is
one.

Failing depmod is not critical, you can fix it up after the fact once
the modules are deployed on the test system.

You can also build a differently configured kmod for the purpose and
pass it with DEPMOD= to the kernel build.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH v3] depmod: Handle installing modules under a prefix
  2023-07-17 19:14                   ` Masahiro Yamada
@ 2023-07-18  8:52                     ` Michal Suchánek
  2023-09-11 19:56                     ` [PATCH] kbuild: rpm-pkg: Fix build with non-default MODLIB Michal Suchanek
  1 sibling, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-18  8:52 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Nicolas Schier, linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutn�,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	linux-kbuild, linux-kernel

On Tue, Jul 18, 2023 at 04:14:11AM +0900, Masahiro Yamada wrote:
> On Sat, Jul 15, 2023 at 12:10 AM Michal Suchánek <msuchanek@suse.de> wrote:
> >
> > On Fri, Jul 14, 2023 at 04:54:49PM +0200, Nicolas Schier wrote:
> > > On Fri, Jul 14, 2023 at 04:30:02PM +0200, Michal Such�nek wrote:
> > > > Hello,
> > > >
> > > > On Fri, Jul 14, 2023 at 04:05:10PM +0200, Nicolas Schier wrote:
> > > > > On Fri, Jul 14, 2023 at 02:21:08PM +0200 Michal Suchanek wrote:
> > > > > > Some distributions aim at not shipping any files in / outside of usr.
> > > > >
> > > > > For me, preventing negation often makes things easier, e.g.: "... aim at
> > > > > shipping files only below /usr".
> > > > >
> > > > > >
> > > > > > The path under which kernel modules are installed is hardcoded to /lib
> > > > > > which conflicts with this goal.
> > > > > >
> > > > > > When kmod provides the config command, use it to determine the correct
> > > > > > module installation prefix.
> > > > > >
> > > > > > This is a prefix under which the modules are searched by kmod on the
> > > > > > system, and is separate from the temporary staging location already
> > > > > > supported by INSTALL_MOD_PATH.
> > > > > >
> > > > > > With kmod that does not provide the config command empty prefix is used
> > > > > > as before.
> > > > > >
> > > > > > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > > > > > ---
> > > > > > v2: Avoid error on systems with kmod that does not support config
> > > > > > command
> > > > > > v3: More verbose commit message
> > > > > > ---
> > > > > >  Makefile          | 4 +++-
> > > > > >  scripts/depmod.sh | 8 ++++----
> > > > > >  2 files changed, 7 insertions(+), 5 deletions(-)
> > > > > >
> > > > > > diff --git a/Makefile b/Makefile
> > > > > > index 47690c28456a..b1fea135bdec 100644
> > > > > > --- a/Makefile
> > > > > > +++ b/Makefile
> > > > > > @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
> > > > > >  # makefile but the argument can be passed to make if needed.
> > > > > >  #
> > > > > >
> > > > > > -MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> > > > > > +export KERNEL_MODULE_PREFIX := $(shell kmod config &> /dev/null && kmod config | jq -r .module_prefix)
> > > > >
> > > > > All other calls of `jq` that I could find are located at tools/; as this here
> > > > > is evaluated on each invocation, this should probably be documented in
> > > > > Documentation/process/changes.rst?
> > > > >
> > > > > (Absence of `jq` will cause error messages, even with CONFIG_MODULES=n.)
> > > >
> > > > That's a good point.
> > > >
> > > > >
> > > > > > +
> > > > > > +MODLIB = $(INSTALL_MOD_PATH)$(KERNEL_MODULE_PREFIX)/lib/modules/$(KERNELRELEASE)
> > > > > >  export MODLIB
> > > > > >
> > > > > >  PHONY += prepare0
> > > > > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > > > > index 3643b4f896ed..88ac79056153 100755
> > > > > > --- a/scripts/depmod.sh
> > > > > > +++ b/scripts/depmod.sh
> > > > > > @@ -27,16 +27,16 @@ fi
> > > > > >  # numbers, so we cheat with a symlink here
> > > > > >  depmod_hack_needed=true
> > > > > >  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> > > > > > -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> > > > > > +mkdir -p "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE"
> > > > > >  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > > > > > -       if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > > -               -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > > > +       if test -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep" -o \
> > > > > > +               -e "$tmp_dir$KERNEL_MODULE_PREFIX/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> > > > > >                 depmod_hack_needed=false
> > > > > >         fi
> > > > > >  fi
> > > > >
> > > > > I'd like to come back to the statement from Masahiro: Is the check above,
> > > > > against some very old versions of depmod [1], the only reason for this patch?
> > > > >
> > > > > If we could remove that, would
> > > > >
> > > > >     make INSTALL_MOD_PATH="$(kmod config | jq -r .module_prefix)" modules_install
> > > > >
> > > > > be sufficient?
> > > >
> > > > No, the INSTALL_MOD_PATH is passed as the -b argument to depmod while
> > > > the newly added part is not because it's integral part of where the
> > > > modules are installed on the system, and not the staging area path.
> > >
> > > Ah, thanks.  So just for my understanding, could this be a (non-gentle)
> > > alternative version of your patch, w/o modifying top-level Makefile?
> > >
> > > diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> > > index 3643b4f896ed..72c819de0669 100755
> > > --- a/scripts/depmod.sh
> > > +++ b/scripts/depmod.sh
> > > @@ -1,4 +1,4 @@
> > > -#!/bin/sh
> > > +#!/bin/bash
> > >  # SPDX-License-Identifier: GPL-2.0
> > >  #
> > >  # A depmod wrapper used by the toplevel Makefile
> > > @@ -23,6 +23,8 @@ if [ -z $(command -v $DEPMOD) ]; then
> > >         exit 0
> > >  fi
> > >
> > > +kmod_version=$(( $(kmod --version | sed -rne 's/^kmod version ([0-9]+).*$/\1/p') ))
> > > +
> > >  # older versions of depmod require the version string to start with three
> > >  # numbers, so we cheat with a symlink here
> > >  depmod_hack_needed=true
> > > @@ -35,6 +37,13 @@ if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> > >         fi
> > >  fi
> > >  rm -rf "$tmp_dir"
> > > +
> > > +if [ "${kmod_version}" -gt 32 ]; then
> > > +       kmod_prefix="$(kmod config | jq -r .module_prefix)"
> > > +       INSTALL_MOD_PATH="${INSTALL_MOD_PATH#${kmod_prefix}"
> > > +       depmod_hack_needed=false
> > > +fi
> > > +
> > >  if $depmod_hack_needed; then
> > >         symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> > >         ln -s "$KERNELRELEASE" "$symlink"
> > >
> > > (untested, and assuming that kmod module prefix is in kmod >= 32)
> >
> > It can be detected by running the 'kmod config' command first and
> > ignoring the output when it fails which the above patch already did.
> > The version check does not sound very reliable.
> >
> > > Or are I am still missing something?
> >
> > MODLIB still needs to include the extra prefix so that files are
> > installed in the correct location. And that's defined in the toplevel
> > Makefile.
> >
> > Thanks
> >
> > Michal
> 
> 
> 
> As Documentation/kbuild/kbuild.rst mentions,
> you can override MODLIB.
> 
> Kbuild already provides the maximum flexibility.
> 
> 
> $ make modules_install \
>   INSTALL_MOD_PATH=/path/to/whatever/staging/dir \
>   MODLIB=/path/to/whatever/real/install/destination

That's great that we have another workaround for the case when the
kernel build system and kmod do not agree on the location of the kernel
modules.

However, they should by default agree, and if this feature is merged
into kmod the kernel should also adapt to make sure the modules are
installed where kmod expects them.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18  8:43       ` Michal Suchánek
@ 2023-07-18  9:41         ` Jan Engelhardt
  2023-07-18 10:29           ` Michal Suchánek
  0 siblings, 1 reply; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-18  9:41 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Tuesday 2023-07-18 10:43, Michal Suchánek wrote:
>> >With this distributions that do not want to ship files in /lib can also
>> >move kernel modules to /usr while others can keep them in /lib.
>> 
>> This patch breaks kernel builds/installation / bisecting [when the
>> system has a kmod ./configure'd --with-module-directory=/usr/lib/modules]
>
>It might be nice to provide backwads compatibility with earlier
>configurations.
>
>However, if it comes at the cost of making the implementation more
>complex and future maintenance more difficult it might not be such a
>great idea. So far I have not seen a proposal how to do it nicely.



diff --git a/configure.ac b/configure.ac
index a195c8e..7fde927 100644
--- a/configure.ac
+++ b/configure.ac
@@ -84,11 +84,7 @@ AC_ARG_WITH([rootlibdir],
         [], [with_rootlibdir=$libdir])
 AC_SUBST([rootlibdir], [$with_rootlibdir])
 
-# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
-AC_ARG_WITH([module_directory],
-        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
-        [], [with_module_directory=/lib/modules])
-AC_SUBST([module_directory], [$with_module_directory])
+AC_SUBST([module_directory], [/lib/modules])
 
 AC_ARG_WITH([zstd],
 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h
index 4a4af58..e2f9c95 100644
--- a/libkmod/libkmod-internal.h
+++ b/libkmod/libkmod-internal.h
@@ -199,3 +199,5 @@ void kmod_module_signature_info_free(struct kmod_signature_info *sig_info) __att
 
 /* libkmod-builtin.c */
 ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname, char ***modinfo) __attribute__((nonnull(1, 2, 3)));
+
+extern const char *dirname_default_prefix;
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index d2ed874..b426cde 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -209,7 +209,7 @@ static int log_priority(const char *priority)
 	return 0;
 }
 
-static const char *dirname_default_prefix = MODULE_DIRECTORY;
+const char *dirname_default_prefix = MODULE_DIRECTORY;
 
 static char *get_kernel_release(const char *dirname)
 {
diff --git a/tools/depmod.c b/tools/depmod.c
index 22bc1d8..929060f 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -65,6 +65,7 @@ static const struct option cmdopts[] = {
 	{ "quick", no_argument, 0, 'A' },
 	{ "basedir", required_argument, 0, 'b' },
 	{ "outdir", required_argument, 0, 'o' },
+	{ "modulesdir", required_argument, 0, 'M' },
 	{ "config", required_argument, 0, 'C' },
 	{ "symvers", required_argument, 0, 'E' },
 	{ "filesyms", required_argument, 0, 'F' },
@@ -2943,6 +2944,9 @@ static int do_depmod(int argc, char *argv[])
 				free(out_root);
 			out_root = path_make_absolute_cwd(optarg);
 			break;
+		case 'M':
+			dirname_default_prefix = optarg;
+			break;
 		case 'C': {
 			size_t bytes = sizeof(char *) * (n_config_paths + 2);
 			void *tmp = realloc(config_paths, bytes);



^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18  9:41         ` Jan Engelhardt
@ 2023-07-18 10:29           ` Michal Suchánek
  2023-07-18 12:17             ` Jan Engelhardt
  0 siblings, 1 reply; 83+ messages in thread
From: Michal Suchánek @ 2023-07-18 10:29 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Jul 18, 2023 at 11:41:32AM +0200, Jan Engelhardt wrote:
> 
> On Tuesday 2023-07-18 10:43, Michal Suchánek wrote:
> >> >With this distributions that do not want to ship files in /lib can also
> >> >move kernel modules to /usr while others can keep them in /lib.
> >> 
> >> This patch breaks kernel builds/installation / bisecting [when the
> >> system has a kmod ./configure'd --with-module-directory=/usr/lib/modules]
> >
> >It might be nice to provide backwads compatibility with earlier
> >configurations.
> >
> >However, if it comes at the cost of making the implementation more
> >complex and future maintenance more difficult it might not be such a
> >great idea. So far I have not seen a proposal how to do it nicely.
> 
> 
> 
> diff --git a/configure.ac b/configure.ac
> index a195c8e..7fde927 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -84,11 +84,7 @@ AC_ARG_WITH([rootlibdir],
>          [], [with_rootlibdir=$libdir])
>  AC_SUBST([rootlibdir], [$with_rootlibdir])
>  
> -# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
> -AC_ARG_WITH([module_directory],
> -        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
> -        [], [with_module_directory=/lib/modules])
> -AC_SUBST([module_directory], [$with_module_directory])
> +AC_SUBST([module_directory], [/lib/modules])
>  
>  AC_ARG_WITH([zstd],
>  	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
> diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h
> index 4a4af58..e2f9c95 100644
> --- a/libkmod/libkmod-internal.h
> +++ b/libkmod/libkmod-internal.h
> @@ -199,3 +199,5 @@ void kmod_module_signature_info_free(struct kmod_signature_info *sig_info) __att
>  
>  /* libkmod-builtin.c */
>  ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname, char ***modinfo) __attribute__((nonnull(1, 2, 3)));
> +
> +extern const char *dirname_default_prefix;
> diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> index d2ed874..b426cde 100644
> --- a/libkmod/libkmod.c
> +++ b/libkmod/libkmod.c
> @@ -209,7 +209,7 @@ static int log_priority(const char *priority)
>  	return 0;
>  }
>  
> -static const char *dirname_default_prefix = MODULE_DIRECTORY;
> +const char *dirname_default_prefix = MODULE_DIRECTORY;
>  
>  static char *get_kernel_release(const char *dirname)
>  {
> diff --git a/tools/depmod.c b/tools/depmod.c
> index 22bc1d8..929060f 100644
> --- a/tools/depmod.c
> +++ b/tools/depmod.c
> @@ -65,6 +65,7 @@ static const struct option cmdopts[] = {
>  	{ "quick", no_argument, 0, 'A' },
>  	{ "basedir", required_argument, 0, 'b' },
>  	{ "outdir", required_argument, 0, 'o' },
> +	{ "modulesdir", required_argument, 0, 'M' },
>  	{ "config", required_argument, 0, 'C' },
>  	{ "symvers", required_argument, 0, 'E' },
>  	{ "filesyms", required_argument, 0, 'F' },
> @@ -2943,6 +2944,9 @@ static int do_depmod(int argc, char *argv[])
>  				free(out_root);
>  			out_root = path_make_absolute_cwd(optarg);
>  			break;
> +		case 'M':
> +			dirname_default_prefix = optarg;
> +			break;
>  		case 'C': {
>  			size_t bytes = sizeof(char *) * (n_config_paths + 2);
>  			void *tmp = realloc(config_paths, bytes);

That breaks kmod for the usrmerged distributions, though.

It might be fine to provide an option to override the build-time
default. Still the build-time default has to match where the modules are
placed in the distribution for things to work correctly out of the box.

Note: it will work either way at the times the module directory can be
accessed through the lib -> usr/lib compatibility symlink. The default
needs to be correct for the cases when the symlink is not provided.

So you could do

make DEPMOD='depmod -M /lib/modules'

to build an old kernel but you could equally do

make DEPMOD='/path/to/special/depmod'

If you needed

make DEPMOD='depmod -M /usr/lib/modules'

to build an usrmerged kernel on usrmerged distribution then this whole
exercise is pointless. kmod cannot find the usrmerged modules then.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH kmod v5 0/5] kmod /usr support
  2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
                     ` (4 preceding siblings ...)
  2023-07-17 10:40   ` [PATCH v4] depmod: Handle installing modules under a prefix Michal Suchanek
@ 2023-07-18 12:01   ` Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 1/5] configure: Detect openssl sm3 support Michal Suchanek
                       ` (7 more replies)
  5 siblings, 8 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-18 12:01 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

with these patches it is possible to install kernel modules in an arbitrary
directory - eg. moving the /lib/modules to /usr/lib/modules or /opt/linux.

While the modprobe.d and depmod.d search which already includes multiple
paths is expanded to also include $(prefix) the module directory still
supports only one location, only a different one under $(module_directory).

Having kmod search multiple module locations while only one is supported now
might break some assumption about relative module path corresponding to a
specific file, would require more invasive changes to implement, and is not
supportive of the goal of moving the modules away from /lib.

Both kmod and the kernel need to be patched to make use of this feature.
Patched kernel is backwards compatible with older kmod.  Patched kmod
with $(module_directory) set to /lib/modules is equivalent to unpatched kmod.

Thanks

Michal

Link: https://lore.kernel.org/linux-modules/20210112160211.5614-1-msuchanek@suse.de/

v4: set whole path to module directory instead of adding prefix
v5: use pkg-config instead of jq, fix build on openssl without sm3 support


Michal Suchanek (5):
  configure: Detect openssl sm3 support
  man/depmod.d: Fix incorrect /usr/lib search path
  libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib.
  kmod: Add pkgconfig file with kmod compile time configuration
  libkmod, depmod, modprobe: Make directory for kernel modules
    configurable

 Makefile.am                          |   6 +-
 configure.ac                         |  30 ++++++++
 libkmod/libkmod.c                    |  11 +--
 man/Makefile.am                      |  10 ++-
 man/depmod.d.xml                     |   9 ++-
 man/depmod.xml                       |   4 +-
 man/modinfo.xml                      |   2 +-
 man/modprobe.d.xml                   |   1 +
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   7 +-
 tools/kmod.pc.in                     |  10 +++
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 19 files changed, 156 insertions(+), 91 deletions(-)
 create mode 100644 tools/kmod.pc.in

-- 
2.41.0


^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH kmod v5 1/5] configure: Detect openssl sm3 support
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
@ 2023-07-18 12:01     ` Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 2/5] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
                       ` (6 subsequent siblings)
  7 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-18 12:01 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Older openssl versions do not support sm3. The code has an option to
disable the sm3 hash but the lack of openssl support is not detected
automatically.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 configure.ac | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/configure.ac b/configure.ac
index 6064dee77ae6..331cc8a1ffd5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -123,6 +123,13 @@ AC_ARG_WITH([openssl],
 AS_IF([test "x$with_openssl" != "xno"], [
 	PKG_CHECK_MODULES([libcrypto], [libcrypto >= 1.1.0], [LIBS="$LIBS $libcrypto_LIBS"])
 	AC_DEFINE([ENABLE_OPENSSL], [1], [Enable openssl for modinfo.])
+	AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#include <openssl/ssl.h>
+		int nid = NID_sm3;]])], [
+		AC_MSG_NOTICE([openssl supports sm3])
+	], [
+		AC_MSG_NOTICE([openssl sm3 support not detected])
+		CPPFLAGS="$CPPFLAGS -DOPENSSL_NO_SM3"
+	])
 ], [
 	AC_MSG_NOTICE([openssl support not requested])
 ])
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v5 2/5] man/depmod.d: Fix incorrect /usr/lib search path
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 1/5] configure: Detect openssl sm3 support Michal Suchanek
@ 2023-07-18 12:01     ` Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 3/5] libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib Michal Suchanek
                       ` (5 subsequent siblings)
  7 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-18 12:01 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

depmod searches /lib/depmod.d but the man page says /usr/lib/depmod.d is
searched. Align the documentation with the code.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Fix commit message typo
---
 man/depmod.d.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 76548e92a312..8d3d821cddc8 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -39,7 +39,7 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/usr/lib/depmod.d/*.conf</filename></para>
+    <para><filename>/lib/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v5 3/5] libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib.
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 1/5] configure: Detect openssl sm3 support Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 2/5] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
@ 2023-07-18 12:01     ` Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 4/5] kmod: Add pkgconfig file with kmod compile time configuration Michal Suchanek
                       ` (4 subsequent siblings)
  7 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-18 12:01 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

There is an ongoing effort to limit use of files outside of /usr (or
${prefix} on general). Currently all modprobe.d paths are hardcoded to
outside of $prefix. Teach kmod to load modprobe.d from ${prefix}/lib.

On some distributions /usr/lib and /lib are the same directory because
of a compatibility symlink, and it is possible to craft configuration
files with sideeffects that would behave differently when loaded twice.
However, the override semantic ensures that one 'overrides' the other,
and only one configuration file of the same name is loaded from any of
the search directories.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Fix commit message typo
v3: Fix modprobe.d path list in code comment
v5: Add distconfdir
---
 Makefile.am        | 1 +
 configure.ac       | 5 +++++
 libkmod/libkmod.c  | 7 ++++---
 man/Makefile.am    | 9 +++++++--
 man/depmod.d.xml   | 1 +
 man/modprobe.d.xml | 1 +
 tools/depmod.c     | 1 +
 7 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8ba85c91a0f3..14eb07e63cea 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,7 @@ AM_CPPFLAGS = \
 	-include $(top_builddir)/config.h \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
+	-DDISTCONFDIR=\""$(distconfdir)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
diff --git a/configure.ac b/configure.ac
index 331cc8a1ffd5..728f88a56704 100644
--- a/configure.ac
+++ b/configure.ac
@@ -79,6 +79,10 @@ AC_COMPILE_IFELSE(
 # --with-
 #####################################################################
 
+AC_ARG_WITH([distconfdir], AS_HELP_STRING([--with-distconfdir=DIR], [directory to search for distribution configuration files]),
+        [], [with_distconfdir='${prefix}/lib'])
+AC_SUBST([distconfdir], [$with_distconfdir])
+
 AC_ARG_WITH([rootlibdir],
         AS_HELP_STRING([--with-rootlibdir=DIR], [rootfs directory to install shared libraries]),
         [], [with_rootlibdir=$libdir])
@@ -313,6 +317,7 @@ AC_MSG_RESULT([
 
 	prefix:			${prefix}
 	sysconfdir:		${sysconfdir}
+	distconfdir:		${distconfdir}
 	libdir:			${libdir}
 	rootlibdir:		${rootlibdir}
 	includedir:		${includedir}
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 2670f9a4611a..09e6041461b0 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -65,6 +65,7 @@ static const char *const default_config_paths[] = {
 	SYSCONFDIR "/modprobe.d",
 	"/run/modprobe.d",
 	"/usr/local/lib/modprobe.d",
+	DISTCONFDIR "/modprobe.d",
 	"/lib/modprobe.d",
 	NULL
 };
@@ -237,9 +238,9 @@ static char *get_kernel_release(const char *dirname)
  *                to load from user-defined configuration parameters such as
  *                alias, blacklists, commands (install, remove). If NULL
  *                defaults to /etc/modprobe.d, /run/modprobe.d,
- *                /usr/local/lib/modprobe.d and /lib/modprobe.d. Give an empty
- *                vector if configuration should not be read. This array must
- *                be null terminated.
+ *                /usr/local/lib/modprobe.d, DISTCONFDIR/modprobe.d, and
+ *                /lib/modprobe.d. Give an empty vector if configuration should
+ *                not be read. This array must be null terminated.
  *
  * Create kmod library context. This reads the kmod configuration
  * and fills in the default values.
diff --git a/man/Makefile.am b/man/Makefile.am
index 11514d52a190..2fea8e46bf2f 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -17,9 +17,14 @@ EXTRA_DIST = $(MAN5:%.5=%.xml) $(MAN8:%.8=%.xml)
 CLEANFILES = $(dist_man_MANS)
 
 %.5 %.8: %.xml
-	$(AM_V_XSLT)$(XSLT) \
+	$(AM_V_XSLT)if [ '$(distconfdir)' != '/lib' ] ; then \
+		sed -e 's|@DISTCONFDIR@|$(distconfdir)|g' $< ; \
+	else \
+		sed -e '/@DISTCONFDIR@/d' $< ; \
+	fi | \
+	$(XSLT) \
 		-o $@ \
 		--nonet \
 		--stringparam man.output.quietly 1 \
 		--param funcsynopsis.style "'ansi'" \
-		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $<
+		http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl -
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index 8d3d821cddc8..f282a39cc840 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -40,6 +40,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/depmod.d/*.conf</filename></para>
+    <para><filename>@DISTCONFDIR@/depmod.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/depmod.d/*.conf</filename></para>
     <para><filename>/run/depmod.d/*.conf</filename></para>
     <para><filename>/etc/depmod.d/*.conf</filename></para>
diff --git a/man/modprobe.d.xml b/man/modprobe.d.xml
index 0ab3e9110a7e..2bf6537f07e6 100644
--- a/man/modprobe.d.xml
+++ b/man/modprobe.d.xml
@@ -41,6 +41,7 @@
 
   <refsynopsisdiv>
     <para><filename>/lib/modprobe.d/*.conf</filename></para>
+    <para><filename>@DISTCONFDIR@/modprobe.d/*.conf</filename></para>
     <para><filename>/usr/local/lib/modprobe.d/*.conf</filename></para>
     <para><filename>/run/modprobe.d/*.conf</filename></para>
     <para><filename>/etc/modprobe.d/*.conf</filename></para>
diff --git a/tools/depmod.c b/tools/depmod.c
index 1d1d41db860f..630fef9c8fb0 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -54,6 +54,7 @@ static const char *const default_cfg_paths[] = {
 	SYSCONFDIR "/depmod.d",
 	"/run/depmod.d",
 	"/usr/local/lib/depmod.d",
+	DISTCONFDIR "/depmod.d",
 	"/lib/depmod.d",
 	NULL
 };
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v5 4/5] kmod: Add pkgconfig file with kmod compile time configuration
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
                       ` (2 preceding siblings ...)
  2023-07-18 12:01     ` [PATCH kmod v5 3/5] libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib Michal Suchanek
@ 2023-07-18 12:01     ` Michal Suchanek
  2023-07-18 12:01     ` [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
                       ` (3 subsequent siblings)
  7 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-07-18 12:01 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Show distconfdir (where system configuration files are searched/to be
installed), sysconfdir (where user configuration files are searched),
module compressions, and module signatures supported.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: mention module signature in commit message
v3: add sysconfdir
v5: add distconfdir, switch to pkgconfig
---
 Makefile.am      |  2 +-
 configure.ac     | 11 +++++++++++
 tools/kmod.pc.in |  9 +++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 tools/kmod.pc.in

diff --git a/Makefile.am b/Makefile.am
index 14eb07e63cea..6d0b2decfef3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -96,7 +96,7 @@ libkmod_libkmod_internal_la_DEPENDENCIES  = $(libkmod_libkmod_la_DEPENDENCIES)
 libkmod_libkmod_internal_la_LIBADD = $(libkmod_libkmod_la_LIBADD)
 
 pkgconfigdir = $(libdir)/pkgconfig
-pkgconfig_DATA = libkmod/libkmod.pc
+pkgconfig_DATA = libkmod/libkmod.pc tools/kmod.pc
 
 bashcompletiondir=@bashcompletiondir@
 dist_bashcompletion_DATA = \
diff --git a/configure.ac b/configure.ac
index 728f88a56704..b4584d6cdc67 100644
--- a/configure.ac
+++ b/configure.ac
@@ -21,6 +21,9 @@ LT_INIT([disable-static pic-only])
 AS_IF([test "x$enable_static" = "xyes"], [AC_MSG_ERROR([--enable-static is not supported by kmod])])
 AS_IF([test "x$enable_largefile" = "xno"], [AC_MSG_ERROR([--disable-largefile is not supported by kmod])])
 
+module_compressions=""
+module_signatures="legacy"
+
 #####################################################################
 # Program checks and configurations
 #####################################################################
@@ -94,6 +97,7 @@ AC_ARG_WITH([zstd],
 AS_IF([test "x$with_zstd" != "xno"], [
 	PKG_CHECK_MODULES([libzstd], [libzstd >= 1.4.4], [LIBS="$LIBS $libzstd_LIBS"])
 	AC_DEFINE([ENABLE_ZSTD], [1], [Enable Zstandard for modules.])
+	module_compressions="zstd $module_compressions"
 ], [
 	AC_MSG_NOTICE([Zstandard support not requested])
 ])
@@ -105,6 +109,7 @@ AC_ARG_WITH([xz],
 AS_IF([test "x$with_xz" != "xno"], [
 	PKG_CHECK_MODULES([liblzma], [liblzma >= 4.99], [LIBS="$LIBS $liblzma_LIBS"])
 	AC_DEFINE([ENABLE_XZ], [1], [Enable Xz for modules.])
+	module_compressions="xz $module_compressions"
 ], [
 	AC_MSG_NOTICE([Xz support not requested])
 ])
@@ -116,6 +121,7 @@ AC_ARG_WITH([zlib],
 AS_IF([test "x$with_zlib" != "xno"], [
 	PKG_CHECK_MODULES([zlib], [zlib], [LIBS="$LIBS $zlib_LIBS"])
 	AC_DEFINE([ENABLE_ZLIB], [1], [Enable zlib for modules.])
+	module_compressions="gzip $module_compressions"
 ], [
 	AC_MSG_NOTICE([zlib support not requested])
 ])
@@ -134,6 +140,7 @@ AS_IF([test "x$with_openssl" != "xno"], [
 		AC_MSG_NOTICE([openssl sm3 support not detected])
 		CPPFLAGS="$CPPFLAGS -DOPENSSL_NO_SM3"
 	])
+	module_signatures="PKCS7 $module_signatures"
 ], [
 	AC_MSG_NOTICE([openssl support not requested])
 ])
@@ -298,6 +305,9 @@ AC_DEFINE_UNQUOTED(KMOD_FEATURES, ["$with_features"], [Features in this build])
 # Generate files from *.in
 #####################################################################
 
+AC_SUBST([module_compressions], $module_compressions)
+AC_SUBST([module_signatures], $module_signatures)
+
 AC_CONFIG_FILES([
 	Makefile
 	man/Makefile
@@ -305,6 +315,7 @@ AC_CONFIG_FILES([
 	libkmod/docs/version.xml
 	libkmod/libkmod.pc
 	libkmod/python/kmod/version.py
+	tools/kmod.pc
 ])
 
 
diff --git a/tools/kmod.pc.in b/tools/kmod.pc.in
new file mode 100644
index 000000000000..2595980a6b35
--- /dev/null
+++ b/tools/kmod.pc.in
@@ -0,0 +1,9 @@
+prefix=@prefix@
+sysconfdir=@sysconfdir@
+distconfdir=@distconfdir@
+module_compressions=@module_compressions@
+module_signatures=@module_signatures@
+
+Name: kmod
+Description: Tools to deal with kernel modules
+Version: @VERSION@
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
                       ` (3 preceding siblings ...)
  2023-07-18 12:01     ` [PATCH kmod v5 4/5] kmod: Add pkgconfig file with kmod compile time configuration Michal Suchanek
@ 2023-07-18 12:01     ` Michal Suchanek
  2023-10-17 17:50       ` Lucas De Marchi
  2023-07-18 12:03     ` [PATCH v5] depmod: Handle installing modules under a prefix Michal Suchanek
                       ` (2 subsequent siblings)
  7 siblings, 1 reply; 83+ messages in thread
From: Michal Suchanek @ 2023-07-18 12:01 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Now that modprobe.d is searched under ${prefix}/lib, allow a complete
transition to files only under ${prefix} by adding a ${module_directory}
configuration. This specifies the directory where to search for kernel
modules and should match the location where the kernel/distro installs
them.

With this distributions that do not want to ship files in /lib can also
move kernel modules to /usr while others can keep them in /lib.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v4: Make the whole path configurable
v5: More verbose commit message
---
 Makefile.am                          |   3 +-
 configure.ac                         |   7 ++
 libkmod/libkmod.c                    |   4 +-
 man/Makefile.am                      |   1 +
 man/depmod.d.xml                     |   6 +-
 man/depmod.xml                       |   4 +-
 man/modinfo.xml                      |   2 +-
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   6 +-
 tools/kmod.pc.in                     |   1 +
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 18 files changed, 107 insertions(+), 84 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 6d0b2decfef3..019aa749fdf1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
 	-DDISTCONFDIR=\""$(distconfdir)"\" \
+	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
@@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
 MODULE_PLAYGROUND = testsuite/module-playground
 ROOTFS = testsuite/rootfs
 ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
-CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
+CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
 
 build-module-playground:
 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
diff --git a/configure.ac b/configure.ac
index b4584d6cdc67..4051dc9249e2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -91,6 +91,12 @@ AC_ARG_WITH([rootlibdir],
         [], [with_rootlibdir=$libdir])
 AC_SUBST([rootlibdir], [$with_rootlibdir])
 
+# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
+AC_ARG_WITH([module_directory],
+        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
+        [], [with_module_directory=/lib/modules])
+AC_SUBST([module_directory], [$with_module_directory])
+
 AC_ARG_WITH([zstd],
 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
 	[], [with_zstd=no])
@@ -326,6 +332,7 @@ AC_MSG_RESULT([
 	$PACKAGE $VERSION
 	=======
 
+	module_directory:	${module_directory}
 	prefix:			${prefix}
 	sysconfdir:		${sysconfdir}
 	distconfdir:		${distconfdir}
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 09e6041461b0..63719e886de8 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -209,7 +209,7 @@ static int log_priority(const char *priority)
 	return 0;
 }
 
-static const char *dirname_default_prefix = "/lib/modules";
+static const char *dirname_default_prefix = MODULE_DIRECTORY;
 
 static char *get_kernel_release(const char *dirname)
 {
@@ -231,7 +231,7 @@ static char *get_kernel_release(const char *dirname)
 /**
  * kmod_new:
  * @dirname: what to consider as linux module's directory, if NULL
- *           defaults to /lib/modules/`uname -r`. If it's relative,
+ *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
  *           it's treated as relative to the current working directory.
  *           Otherwise, give an absolute dirname.
  * @config_paths: ordered array of paths (directories or files) where
diff --git a/man/Makefile.am b/man/Makefile.am
index 2fea8e46bf2f..f550091a216a 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
 	else \
 		sed -e '/@DISTCONFDIR@/d' $< ; \
 	fi | \
+	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
 	$(XSLT) \
 		-o $@ \
 		--nonet \
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index f282a39cc840..b07e6a2bd4fe 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -70,7 +70,7 @@
         </term>
         <listitem>
           <para>
-            This allows you to specify the order in which /lib/modules
+            This allows you to specify the order in which @MODULE_DIRECTORY@
             (or other configured module location) subdirectories will
             be processed by <command>depmod</command>. Directories are
             listed in order, with the highest priority given to the
@@ -101,7 +101,7 @@
             <command>depmod</command> command. It is possible to
             specify one kernel or all kernels using the * wildcard.
             <replaceable>modulesubdirectory</replaceable> is the
-            name of the subdirectory under /lib/modules (or other
+            name of the subdirectory under @MODULE_DIRECTORY@ (or other
             module location) where the target module is installed.
           </para>
           <para>
@@ -110,7 +110,7 @@
             specifying the following command: "override kmod * extra".
             This will ensure that any matching module name installed
             under the <command>extra</command> subdirectory within
-            /lib/modules (or other module location) will take priority
+            @MODULE_DIRECTORY@ (or other module location) will take priority
             over any likenamed module already provided by the kernel.
           </para>
         </listitem>
diff --git a/man/depmod.xml b/man/depmod.xml
index 3b0097184fd7..fce2a4a67a89 100644
--- a/man/depmod.xml
+++ b/man/depmod.xml
@@ -80,7 +80,7 @@
     </para>
     <para> <command>depmod</command> creates a list of module dependencies by
       reading each module under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
       determining what symbols it exports and what symbols it needs.  By
       default, this list is written to <filename>modules.dep</filename>, and a
       binary hashed version named <filename>modules.dep.bin</filename>, in the
@@ -141,7 +141,7 @@
         <listitem>
           <para>
             If your modules are not currently in the (normal) directory
-            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
+            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
             but in a staging area, you can specify a
             <replaceable>basedir</replaceable> which is prepended to the
             directory name.  This <replaceable>basedir</replaceable> is
diff --git a/man/modinfo.xml b/man/modinfo.xml
index 9fe0324a2527..b6c4d6045829 100644
--- a/man/modinfo.xml
+++ b/man/modinfo.xml
@@ -54,7 +54,7 @@
       <command>modinfo</command> extracts information from the Linux Kernel
       modules given on the command line.  If the module name is not a filename,
       then the
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
       directory is searched, as is also done by
       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
       when loading kernel modules.
diff --git a/man/modprobe.xml b/man/modprobe.xml
index 91f9e27997cd..4d1fd59c000b 100644
--- a/man/modprobe.xml
+++ b/man/modprobe.xml
@@ -78,7 +78,7 @@
       is no difference between _ and - in module names (automatic
       underscore conversion is performed).
       <command>modprobe</command> looks in the module directory
-      <filename>/lib/modules/`uname -r`</filename> for all
+      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
       the modules and other files, except for the optional
       configuration files in the
       <filename>/etc/modprobe.d</filename> directory
diff --git a/man/modules.dep.xml b/man/modules.dep.xml
index ed633694ec9e..8ef6d8b3536e 100644
--- a/man/modules.dep.xml
+++ b/man/modules.dep.xml
@@ -34,8 +34,8 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/lib/modules/modules.dep</filename></para>
-    <para><filename>/lib/modules/modules.dep.bin</filename></para>
+    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
+    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
   </refsynopsisdiv>
 
   <refsect1><title>DESCRIPTION</title>
@@ -43,7 +43,7 @@
       <filename>modules.dep.bin</filename> is a binary file generated by
       <command>depmod</command> listing the dependencies for
       every module in the directories under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
       It is used by kmod tools such as <command>modprobe</command> and
       libkmod.
     </para>
diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
index e6045b0dd932..a7ab09bea2bf 100644
--- a/testsuite/module-playground/Makefile
+++ b/testsuite/module-playground/Makefile
@@ -47,7 +47,7 @@ endif
 
 else
 # normal makefile
-KDIR ?= /lib/modules/`uname -r`/build
+KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
 KVER ?= `uname -r`
 ifeq ($(FAKE_BUILD),)
     FAKE_BUILD=0
diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
index 4440ddcd6b4d..a780f9381b3c 100755
--- a/testsuite/setup-rootfs.sh
+++ b/testsuite/setup-rootfs.sh
@@ -16,6 +16,19 @@ create_rootfs() {
 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
 	find "$ROOTFS" -type d -exec chmod +w {} \;
 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
+	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
+		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
+			version="$(basename $i)"
+			[ $version != 'external' ] || continue
+			mod="$(dirname $i)"
+			lib="$(dirname $mod)"
+			up="$(dirname $lib)$MODULE_DIRECTORY"
+			mkdir -p "$up"
+			mv "$i" "$up"
+		done
+	fi
 
 	if [ "$SYSCONFDIR" != "/etc" ]; then
 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
@@ -32,57 +45,57 @@ feature_enabled() {
 
 declare -A map
 map=(
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/kernel/crypto/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/updates/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/override/"]="mod-simple.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/override/"]="mod-simple.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
     ["test-init/"]="mod-simple.ko"
     ["test-remove/"]="mod-simple.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
     ["test-modprobe/show-exports/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/force/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel-force/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/force$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel-force$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/alias-to-none$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/module-param-kcmdline$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
     ["test-modprobe/module-from-abspath/home/foo/"]="mod-simple.ko"
     ["test-modprobe/module-from-relpath/home/foo/"]="mod-simple.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
@@ -90,20 +103,20 @@ map=(
     ["test-modinfo/mod-simple-sha256.ko"]="mod-simple.ko"
     ["test-modinfo/mod-simple-pkcs7.ko"]="mod-simple.ko"
     ["test-modinfo/external/lib/modules/external/mod-simple.ko"]="mod-simple.ko"
-    ["test-tools/insert/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-tools/remove/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/insert$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/remove$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
 )
 
 gzip_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"
     )
 
 xz_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
     )
 
 zstd_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"
     )
 
 attach_sha256_array=(
diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
index 93606947f18a..c96dbf0a62be 100644
--- a/testsuite/test-depmod.c
+++ b/testsuite/test-depmod.c
@@ -27,7 +27,7 @@
 
 #define MODULES_UNAME "4.4.4"
 #define MODULES_ORDER_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-order-compressed"
-#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_modules_order_for_compressed(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -55,8 +55,8 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
 	});
 
 #define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
-#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_UNAME
-#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir" MODULE_DIRECTORY "/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_modules_outdir(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -87,7 +87,7 @@ DEFINE_TEST(depmod_modules_outdir,
 	});
 
 #define SEARCH_ORDER_SIMPLE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-simple"
-#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_simple(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -114,7 +114,7 @@ DEFINE_TEST(depmod_search_order_simple,
 	});
 
 #define SEARCH_ORDER_SAME_PREFIX_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-same-prefix"
-#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_same_prefix(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -164,7 +164,7 @@ DEFINE_TEST(depmod_detect_loop,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-first"
-#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_external_first(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -191,7 +191,7 @@ DEFINE_TEST(depmod_search_order_external_first,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_LAST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-last"
-#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_external_last(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -218,7 +218,7 @@ DEFINE_TEST(depmod_search_order_external_last,
 	});
 
 #define SEARCH_ORDER_OVERRIDE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-override"
-#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_override(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c
index 56e73609f204..c77c4bbc04eb 100644
--- a/testsuite/test-testsuite.c
+++ b/testsuite/test-testsuite.c
@@ -64,7 +64,7 @@ static int testsuite_rootfs_fopen(const struct test *t)
 	char s[100];
 	int n;
 
-	fp = fopen("/lib/modules/a", "r");
+	fp = fopen(MODULE_DIRECTORY "/a", "r");
 	if (fp == NULL)
 		return EXIT_FAILURE;;
 
@@ -89,7 +89,7 @@ static int testsuite_rootfs_open(const struct test *t)
 	char buf[100];
 	int fd, done;
 
-	fd = open("/lib/modules/a", O_RDONLY);
+	fd = open(MODULE_DIRECTORY "/a", O_RDONLY);
 	if (fd < 0)
 		return EXIT_FAILURE;
 
@@ -121,12 +121,12 @@ static int testsuite_rootfs_stat_access(const struct test *t)
 {
 	struct stat st;
 
-	if (access("/lib/modules/a", F_OK) < 0) {
+	if (access(MODULE_DIRECTORY "/a", F_OK) < 0) {
 		ERR("access failed: %m\n");
 		return EXIT_FAILURE;
 	}
 
-	if (stat("/lib/modules/a", &st) < 0) {
+	if (stat(MODULE_DIRECTORY "/a", &st) < 0) {
 		ERR("stat failed: %m\n");
 		return EXIT_FAILURE;
 	}
diff --git a/tools/depmod.c b/tools/depmod.c
index 630fef9c8fb0..7f7c4b74d216 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -911,7 +911,7 @@ struct vertex;
 struct mod {
 	struct kmod_module *kmod;
 	char *path;
-	const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
+	const char *relpath; /* path relative to '$ROOT$MODULE_DIRECTORY/$VER/' */
 	char *uncrelpath; /* same as relpath but ending in .ko */
 	struct kmod_list *info_list;
 	struct kmod_list *dep_sym_list;
@@ -3024,11 +3024,11 @@ static int do_depmod(int argc, char *argv[])
 	}
 
 	cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
-				  "%s/lib/modules/%s",
+				  "%s" MODULE_DIRECTORY "/%s",
 				  root ?: "", cfg.kversion);
 
 	cfg.outdirnamelen = snprintf(cfg.outdirname, PATH_MAX,
-				     "%s/lib/modules/%s",
+				     "%s" MODULE_DIRECTORY "/%s",
 				     out_root ?: (root ?: ""), cfg.kversion);
 
 	if (optind == argc)
diff --git a/tools/kmod.pc.in b/tools/kmod.pc.in
index 2595980a6b35..97215c8ed48f 100644
--- a/tools/kmod.pc.in
+++ b/tools/kmod.pc.in
@@ -1,6 +1,7 @@
 prefix=@prefix@
 sysconfdir=@sysconfdir@
 distconfdir=@distconfdir@
+module_directory=@module_directory@
 module_compressions=@module_compressions@
 module_signatures=@module_signatures@
 
diff --git a/tools/modinfo.c b/tools/modinfo.c
index d0aab200af4e..cacc32dc4c40 100644
--- a/tools/modinfo.c
+++ b/tools/modinfo.c
@@ -367,7 +367,7 @@ static void help(void)
 		"\t-m, --modname               Handle argument as module name instead of alias or filename\n"
 		"\t-F, --field=FIELD           Print only provided FIELD\n"
 		"\t-k, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
-		"\t-b, --basedir=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-b, --basedir=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
 		"\t-V, --version               Show version\n"
 		"\t-h, --help                  Show this help\n",
 		program_invocation_short_name);
@@ -462,7 +462,7 @@ static int do_modinfo(int argc, char *argv[])
 			}
 			kversion = u.release;
 		}
-		snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
+		snprintf(dirname_buf, sizeof(dirname_buf), "%s" MODULE_DIRECTORY "/%s",
 			 root, kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/modprobe.c b/tools/modprobe.c
index e891028349a8..5306bef250da 100644
--- a/tools/modprobe.c
+++ b/tools/modprobe.c
@@ -142,7 +142,7 @@ static void help(void)
 		"\t-n, --show                  Same as --dry-run\n"
 
 		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
-		"\t-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-d, --dirname=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
 		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
 
 		"\t-s, --syslog                print to syslog, not stderr\n"
@@ -999,7 +999,7 @@ static int do_modprobe(int argc, char **orig_argv)
 			kversion = u.release;
 		}
 		snprintf(dirname_buf, sizeof(dirname_buf),
-				"%s/lib/modules/%s", root,
+				"%s" MODULE_DIRECTORY "/%s", root,
 				kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
index 8d2356da73f3..5ef3743e967b 100644
--- a/tools/static-nodes.c
+++ b/tools/static-nodes.c
@@ -212,15 +212,15 @@ static int do_static_nodes(int argc, char *argv[])
 		goto finish;
 	}
 
-	snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
+	snprintf(modules, sizeof(modules), MODULE_DIRECTORY "/%s/modules.devname", kernel.release);
 	in = fopen(modules, "re");
 	if (in == NULL) {
 		if (errno == ENOENT) {
-			fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
+			fprintf(stderr, "Warning: " MODULE_DIRECTORY "/%s/modules.devname not found - ignoring\n",
 				kernel.release);
 			ret = EXIT_SUCCESS;
 		} else {
-			fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
+			fprintf(stderr, "Error: could not open " MODULE_DIRECTORY "/%s/modules.devname - %m\n",
 				kernel.release);
 			ret = EXIT_FAILURE;
 		}
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH v5] depmod: Handle installing modules under a prefix
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
                       ` (4 preceding siblings ...)
  2023-07-18 12:01     ` [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
@ 2023-07-18 12:03     ` Michal Suchanek
  2023-07-18 12:14       ` Masahiro Yamada
  2023-08-17 16:37     ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchánek
  2023-10-17 15:45     ` Michal Suchánek
  7 siblings, 1 reply; 83+ messages in thread
From: Michal Suchanek @ 2023-07-18 12:03 UTC (permalink / raw)
  To: linux-modules
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Some distributions aim at shipping all files in /usr.

The path under which kernel modules are installed is hardcoded to /lib
which conflicts with this goal.

When kmod provides the config command, use it to determine the correct
module installation path.

With kmod that does not provide the config command /lib/modules is used
as before.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: Avoid error on systems with kmod that does not support config
command
v3: More verbose commit message
v4:
  - Document jq requirement
  - fix bashism
  - Update to getting full module path, not just additional prefix
v5: switch to pkgconfig
---
 Makefile          | 4 +++-
 scripts/depmod.sh | 8 ++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile
index 47690c28456a..1620d224ad9c 100644
--- a/Makefile
+++ b/Makefile
@@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
 # makefile but the argument can be passed to make if needed.
 #
 
-MODLIB	= $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
+export KERNEL_MODULE_DIRECTORY := $(shell pkg-config --print-variables kmod 2>/dev/null | grep '^module_directory$$' >/dev/null && pkg-config --variable=module_directory kmod || echo /lib/modules)
+
+MODLIB	= $(INSTALL_MOD_PATH)$(KERNEL_MODULE_DIRECTORY)/$(KERNELRELEASE)
 export MODLIB
 
 PHONY += prepare0
diff --git a/scripts/depmod.sh b/scripts/depmod.sh
index 3643b4f896ed..06961f990fcb 100755
--- a/scripts/depmod.sh
+++ b/scripts/depmod.sh
@@ -27,16 +27,16 @@ fi
 # numbers, so we cheat with a symlink here
 depmod_hack_needed=true
 tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
-mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
+mkdir -p "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE"
 if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
-	if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
-		-e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
+	if test -e "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE/modules.dep" -o \
+		-e "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE/modules.dep.bin"; then
 		depmod_hack_needed=false
 	fi
 fi
 rm -rf "$tmp_dir"
 if $depmod_hack_needed; then
-	symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
+	symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_DIRECTORY/99.98.$KERNELRELEASE"
 	ln -s "$KERNELRELEASE" "$symlink"
 	KERNELRELEASE=99.98.$KERNELRELEASE
 fi
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH v5] depmod: Handle installing modules under a prefix
  2023-07-18 12:03     ` [PATCH v5] depmod: Handle installing modules under a prefix Michal Suchanek
@ 2023-07-18 12:14       ` Masahiro Yamada
  0 siblings, 0 replies; 83+ messages in thread
From: Masahiro Yamada @ 2023-07-18 12:14 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Jul 18, 2023 at 9:03 PM Michal Suchanek <msuchanek@suse.de> wrote:
>
> Some distributions aim at shipping all files in /usr.
>
> The path under which kernel modules are installed is hardcoded to /lib
> which conflicts with this goal.
>
> When kmod provides the config command, use it to determine the correct
> module installation path.
>
> With kmod that does not provide the config command /lib/modules is used
> as before.
>
> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
> v2: Avoid error on systems with kmod that does not support config
> command
> v3: More verbose commit message
> v4:
>   - Document jq requirement
>   - fix bashism
>   - Update to getting full module path, not just additional prefix
> v5: switch to pkgconfig
> ---
>  Makefile          | 4 +++-
>  scripts/depmod.sh | 8 ++++----
>  2 files changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 47690c28456a..1620d224ad9c 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1165,7 +1165,9 @@ export INSTALL_DTBS_PATH ?= $(INSTALL_PATH)/dtbs/$(KERNELRELEASE)
>  # makefile but the argument can be passed to make if needed.
>  #
>
> -MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)
> +export KERNEL_MODULE_DIRECTORY := $(shell pkg-config --print-variables kmod 2>/dev/null | grep '^module_directory$$' >/dev/null && pkg-config --variable=module_directory kmod || echo /lib/modules)

As I replied in another thread, Kbuild already provides
the max flexibility.

Please do not send this any more. NACK.









> +
> +MODLIB = $(INSTALL_MOD_PATH)$(KERNEL_MODULE_DIRECTORY)/$(KERNELRELEASE)
>  export MODLIB
>
>  PHONY += prepare0
> diff --git a/scripts/depmod.sh b/scripts/depmod.sh
> index 3643b4f896ed..06961f990fcb 100755
> --- a/scripts/depmod.sh
> +++ b/scripts/depmod.sh
> @@ -27,16 +27,16 @@ fi
>  # numbers, so we cheat with a symlink here
>  depmod_hack_needed=true
>  tmp_dir=$(mktemp -d ${TMPDIR:-/tmp}/depmod.XXXXXX)
> -mkdir -p "$tmp_dir/lib/modules/$KERNELRELEASE"
> +mkdir -p "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE"
>  if "$DEPMOD" -b "$tmp_dir" $KERNELRELEASE 2>/dev/null; then
> -       if test -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep" -o \
> -               -e "$tmp_dir/lib/modules/$KERNELRELEASE/modules.dep.bin"; then
> +       if test -e "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE/modules.dep" -o \
> +               -e "$tmp_dir$KERNEL_MODULE_DIRECTORY/$KERNELRELEASE/modules.dep.bin"; then
>                 depmod_hack_needed=false
>         fi
>  fi
>  rm -rf "$tmp_dir"
>  if $depmod_hack_needed; then
> -       symlink="$INSTALL_MOD_PATH/lib/modules/99.98.$KERNELRELEASE"
> +       symlink="$INSTALL_MOD_PATH$KERNEL_MODULE_DIRECTORY/99.98.$KERNELRELEASE"
>         ln -s "$KERNELRELEASE" "$symlink"
>         KERNELRELEASE=99.98.$KERNELRELEASE
>  fi
> --
> 2.41.0
>


-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18 10:29           ` Michal Suchánek
@ 2023-07-18 12:17             ` Jan Engelhardt
  2023-07-18 12:27               ` Michal Suchánek
  0 siblings, 1 reply; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-18 12:17 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Tuesday 2023-07-18 12:29, Michal Suchánek wrote:
>> -static const char *dirname_default_prefix = MODULE_DIRECTORY;
>> +const char *dirname_default_prefix = MODULE_DIRECTORY;
>>  
>> @@ -2943,6 +2944,9 @@ static int do_depmod(int argc, char *argv[])
>>  				free(out_root);
>>  			out_root = path_make_absolute_cwd(optarg);
>>  			break;
>> +		case 'M':
>> +			dirname_default_prefix = optarg;
>> +			break;
>>  		case 'C': {
>>  			size_t bytes = sizeof(char *) * (n_config_paths + 2);
>>  			void *tmp = realloc(config_paths, bytes);
>
>That breaks kmod for the usrmerged distributions, though.

Does it though?
The *distro* has /lib -> /usr/lib,
the *staging area* for installation does not.

>[...]
>Note: it will work either way at the times the module directory can be
>accessed through the lib -> usr/lib compatibility symlink. The default
>needs to be correct for the cases when the symlink is not provided.

>So you could do
>
>make DEPMOD='depmod -M /lib/modules'

That would be a workable situation.

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18 12:17             ` Jan Engelhardt
@ 2023-07-18 12:27               ` Michal Suchánek
  2023-07-18 12:42                 ` Jan Engelhardt
  0 siblings, 1 reply; 83+ messages in thread
From: Michal Suchánek @ 2023-07-18 12:27 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Jul 18, 2023 at 02:17:08PM +0200, Jan Engelhardt wrote:
> 
> On Tuesday 2023-07-18 12:29, Michal Suchánek wrote:
> >> -static const char *dirname_default_prefix = MODULE_DIRECTORY;
> >> +const char *dirname_default_prefix = MODULE_DIRECTORY;
> >>  
> >> @@ -2943,6 +2944,9 @@ static int do_depmod(int argc, char *argv[])
> >>  				free(out_root);
> >>  			out_root = path_make_absolute_cwd(optarg);
> >>  			break;
> >> +		case 'M':
> >> +			dirname_default_prefix = optarg;
> >> +			break;
> >>  		case 'C': {
> >>  			size_t bytes = sizeof(char *) * (n_config_paths + 2);
> >>  			void *tmp = realloc(config_paths, bytes);
> >
> >That breaks kmod for the usrmerged distributions, though.
> 
> Does it though?
> The *distro* has /lib -> /usr/lib,
> the *staging area* for installation does not.

Yes, currently the fully installed distribution has the symlink, at
least openSUSE does.

However, there is a goal to remove it eventually, and even now it is not
present at all times.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18 12:27               ` Michal Suchánek
@ 2023-07-18 12:42                 ` Jan Engelhardt
  2023-07-18 13:45                   ` Michal Suchánek
  0 siblings, 1 reply; 83+ messages in thread
From: Jan Engelhardt @ 2023-07-18 12:42 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel


On Tuesday 2023-07-18 14:27, Michal Suchánek wrote:
>> >
>> >That breaks kmod for the usrmerged distributions, though.
>> 
>> Does it though?
>> The *distro* has /lib -> /usr/lib,
>> the *staging area* for installation does not.
>
>Yes, currently the fully installed distribution has the symlink, at
>least openSUSE does.
>
>However, there is a goal to remove it eventually, and even now it is not
>present at all times.

The ELF interpreter /lib/ld-linux.so.2 is hardcoded everywhere.

Making /lib a directory again and bindmounting/symlinking
all the ELF interpreters, that seems to entail a bit more work
than just leaving /lib as a symlink.

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18 12:42                 ` Jan Engelhardt
@ 2023-07-18 13:45                   ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-07-18 13:45 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Jul 18, 2023 at 02:42:42PM +0200, Jan Engelhardt wrote:
> 
> On Tuesday 2023-07-18 14:27, Michal Suchánek wrote:
> >> >
> >> >That breaks kmod for the usrmerged distributions, though.
> >> 
> >> Does it though?
> >> The *distro* has /lib -> /usr/lib,
> >> the *staging area* for installation does not.
> >
> >Yes, currently the fully installed distribution has the symlink, at
> >least openSUSE does.
> >
> >However, there is a goal to remove it eventually, and even now it is not
> >present at all times.
> 
> The ELF interpreter /lib/ld-linux.so.2 is hardcoded everywhere.

Sure, there are other problems.

I will leave that for others that are familiar with that part of the
system.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 0/5] kmod /usr support
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
                       ` (5 preceding siblings ...)
  2023-07-18 12:03     ` [PATCH v5] depmod: Handle installing modules under a prefix Michal Suchanek
@ 2023-08-17 16:37     ` Michal Suchánek
  2023-08-19 11:25       ` Masahiro Yamada
  2023-10-17 15:45     ` Michal Suchánek
  7 siblings, 1 reply; 83+ messages in thread
From: Michal Suchánek @ 2023-08-17 16:37 UTC (permalink / raw)
  To: linux-modules
  Cc: Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

On Tue, Jul 18, 2023 at 02:01:51PM +0200, Michal Suchanek wrote:
> Hello,
> 
> with these patches it is possible to install kernel modules in an arbitrary
> directory - eg. moving the /lib/modules to /usr/lib/modules or /opt/linux.
> 
> While the modprobe.d and depmod.d search which already includes multiple
> paths is expanded to also include $(prefix) the module directory still
> supports only one location, only a different one under $(module_directory).
> 
> Having kmod search multiple module locations while only one is supported now
> might break some assumption about relative module path corresponding to a
> specific file, would require more invasive changes to implement, and is not
> supportive of the goal of moving the modules away from /lib.
> 
> Both kmod and the kernel need to be patched to make use of this feature.
> Patched kernel is backwards compatible with older kmod.  Patched kmod
> with $(module_directory) set to /lib/modules is equivalent to unpatched kmod.

The patch to kernel to support autodetection of module directory is
rejected. However, a workaround like

make MODLIB='$(INSTALL_MOD_PATH)/usr/lib/modules/$(KERNELRELEASE)'

is suggested.

Can you consider inluding the kmod changes?

Thanks

Michal

> 
> Thanks
> 
> Michal
> 
> Link: https://lore.kernel.org/linux-modules/20210112160211.5614-1-msuchanek@suse.de/
> 
> v4: set whole path to module directory instead of adding prefix
> v5: use pkg-config instead of jq, fix build on openssl without sm3 support
> 
> 
> Michal Suchanek (5):
>   configure: Detect openssl sm3 support
>   man/depmod.d: Fix incorrect /usr/lib search path
>   libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib.
>   kmod: Add pkgconfig file with kmod compile time configuration
>   libkmod, depmod, modprobe: Make directory for kernel modules
>     configurable
> 
>  Makefile.am                          |   6 +-
>  configure.ac                         |  30 ++++++++
>  libkmod/libkmod.c                    |  11 +--
>  man/Makefile.am                      |  10 ++-
>  man/depmod.d.xml                     |   9 ++-
>  man/depmod.xml                       |   4 +-
>  man/modinfo.xml                      |   2 +-
>  man/modprobe.d.xml                   |   1 +
>  man/modprobe.xml                     |   2 +-
>  man/modules.dep.xml                  |   6 +-
>  testsuite/module-playground/Makefile |   2 +-
>  testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
>  testsuite/test-depmod.c              |  16 ++--
>  testsuite/test-testsuite.c           |   8 +-
>  tools/depmod.c                       |   7 +-
>  tools/kmod.pc.in                     |  10 +++
>  tools/modinfo.c                      |   4 +-
>  tools/modprobe.c                     |   4 +-
>  tools/static-nodes.c                 |   6 +-
>  19 files changed, 156 insertions(+), 91 deletions(-)
>  create mode 100644 tools/kmod.pc.in
> 
> -- 
> 2.41.0
> 

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 0/5] kmod /usr support
  2023-08-17 16:37     ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchánek
@ 2023-08-19 11:25       ` Masahiro Yamada
  2023-08-21  6:22         ` Michal Suchánek
  2023-09-11 19:50         ` Michal Suchánek
  0 siblings, 2 replies; 83+ messages in thread
From: Masahiro Yamada @ 2023-08-19 11:25 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Fri, Aug 18, 2023 at 12:15 PM Michal Suchánek <msuchanek@suse.de> wrote:
>
> Hello,
>
> On Tue, Jul 18, 2023 at 02:01:51PM +0200, Michal Suchanek wrote:
> > Hello,
> >
> > with these patches it is possible to install kernel modules in an arbitrary
> > directory - eg. moving the /lib/modules to /usr/lib/modules or /opt/linux.
> >
> > While the modprobe.d and depmod.d search which already includes multiple
> > paths is expanded to also include $(prefix) the module directory still
> > supports only one location, only a different one under $(module_directory).
> >
> > Having kmod search multiple module locations while only one is supported now
> > might break some assumption about relative module path corresponding to a
> > specific file, would require more invasive changes to implement, and is not
> > supportive of the goal of moving the modules away from /lib.
> >
> > Both kmod and the kernel need to be patched to make use of this feature.
> > Patched kernel is backwards compatible with older kmod.  Patched kmod
> > with $(module_directory) set to /lib/modules is equivalent to unpatched kmod.
>
> The patch to kernel to support autodetection of module directory is
> rejected. However, a workaround like
>
> make MODLIB='$(INSTALL_MOD_PATH)/usr/lib/modules/$(KERNELRELEASE)'
>
> is suggested.
>
> Can you consider inluding the kmod changes?


Hi.

I have a question about your original patch
for the Kbuild change.

In your patch, Kbuild runs 'kmod config' or
'pkg-config --variable=module_directory kmod',
then sets the returned string to MODLIB.


If kmod is configured to use /usr/lib/modules,
/opt/modules, or whatever,
should we change the installation path of the debug
vdso accordingly?

Currently, the debug vdso is always installed
to /lib/modules/$(KERNELRELEASE)/vdso/.

However, modules and vdso are unrelated to each other.
kmod does not care about vdso.


The following commits started to install debug vdso.

Commit 8150caad0226 ("[POWERPC] powerpc vDSO: install unstripped
copies on disk")
Commit f79eb83b3af4 ("x86: Install unstripped copy of 64bit vdso to disk")


I do not know why they chose $(MODLIB)/vdso as the install destination.


I am thinking of split the variable into two:
MODLIB -  installation path for modules
VDSOLIB - installation path for debug vdso (not affected by kmod config)

I think that is the way to do this correctly.









>
> Thanks
>
> Michal
>
> >
> > Thanks
> >
> > Michal
> >
> > Link: https://lore.kernel.org/linux-modules/20210112160211.5614-1-msuchanek@suse.de/
> >
> > v4: set whole path to module directory instead of adding prefix
> > v5: use pkg-config instead of jq, fix build on openssl without sm3 support
> >
> >
> > Michal Suchanek (5):
> >   configure: Detect openssl sm3 support
> >   man/depmod.d: Fix incorrect /usr/lib search path
> >   libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib.
> >   kmod: Add pkgconfig file with kmod compile time configuration
> >   libkmod, depmod, modprobe: Make directory for kernel modules
> >     configurable
> >
> >  Makefile.am                          |   6 +-
> >  configure.ac                         |  30 ++++++++
> >  libkmod/libkmod.c                    |  11 +--
> >  man/Makefile.am                      |  10 ++-
> >  man/depmod.d.xml                     |   9 ++-
> >  man/depmod.xml                       |   4 +-
> >  man/modinfo.xml                      |   2 +-
> >  man/modprobe.d.xml                   |   1 +
> >  man/modprobe.xml                     |   2 +-
> >  man/modules.dep.xml                  |   6 +-
> >  testsuite/module-playground/Makefile |   2 +-
> >  testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
> >  testsuite/test-depmod.c              |  16 ++--
> >  testsuite/test-testsuite.c           |   8 +-
> >  tools/depmod.c                       |   7 +-
> >  tools/kmod.pc.in                     |  10 +++
> >  tools/modinfo.c                      |   4 +-
> >  tools/modprobe.c                     |   4 +-
> >  tools/static-nodes.c                 |   6 +-
> >  19 files changed, 156 insertions(+), 91 deletions(-)
> >  create mode 100644 tools/kmod.pc.in
> >
> > --
> > 2.41.0
> >



--
Best Regards


Masahiro Yamada

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 0/5] kmod /usr support
  2023-08-19 11:25       ` Masahiro Yamada
@ 2023-08-21  6:22         ` Michal Suchánek
  2023-09-11 19:50         ` Michal Suchánek
  1 sibling, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-08-21  6:22 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Sat, Aug 19, 2023 at 08:25:52PM +0900, Masahiro Yamada wrote:
> On Fri, Aug 18, 2023 at 12:15 PM Michal Suchánek <msuchanek@suse.de> wrote:
> >
> > Hello,
> >
> > On Tue, Jul 18, 2023 at 02:01:51PM +0200, Michal Suchanek wrote:
> > > Hello,
> > >
> > > with these patches it is possible to install kernel modules in an arbitrary
> > > directory - eg. moving the /lib/modules to /usr/lib/modules or /opt/linux.
> > >
> > > While the modprobe.d and depmod.d search which already includes multiple
> > > paths is expanded to also include $(prefix) the module directory still
> > > supports only one location, only a different one under $(module_directory).
> > >
> > > Having kmod search multiple module locations while only one is supported now
> > > might break some assumption about relative module path corresponding to a
> > > specific file, would require more invasive changes to implement, and is not
> > > supportive of the goal of moving the modules away from /lib.
> > >
> > > Both kmod and the kernel need to be patched to make use of this feature.
> > > Patched kernel is backwards compatible with older kmod.  Patched kmod
> > > with $(module_directory) set to /lib/modules is equivalent to unpatched kmod.
> >
> > The patch to kernel to support autodetection of module directory is
> > rejected. However, a workaround like
> >
> > make MODLIB='$(INSTALL_MOD_PATH)/usr/lib/modules/$(KERNELRELEASE)'
> >
> > is suggested.
> >
> > Can you consider inluding the kmod changes?
> 
> 
> Hi.
> 
> I have a question about your original patch
> for the Kbuild change.
> 
> In your patch, Kbuild runs 'kmod config' or
> 'pkg-config --variable=module_directory kmod',
> then sets the returned string to MODLIB.
> 
> 
> If kmod is configured to use /usr/lib/modules,
> /opt/modules, or whatever,
> should we change the installation path of the debug
> vdso accordingly?
> 
> Currently, the debug vdso is always installed
> to /lib/modules/$(KERNELRELEASE)/vdso/.
> 
> However, modules and vdso are unrelated to each other.
> kmod does not care about vdso.
> 
> 
> The following commits started to install debug vdso.
> 
> Commit 8150caad0226 ("[POWERPC] powerpc vDSO: install unstripped
> copies on disk")
> Commit f79eb83b3af4 ("x86: Install unstripped copy of 64bit vdso to disk")
> 
> 
> I do not know why they chose $(MODLIB)/vdso as the install destination.

It's the only directory that exists per-kernel-release.

While technically vdso has nothing to do with modules there is no other
place where it can be stored where it would not conflict between kernel
releases so far.

In fact the distro followed the pattern and started dumping other things
not related to kernel modules into $(MODLIB) for this very reason. Also
from the distribution point of view $(MODLIB) is listed as owned by the
kernel package, and additional files that might be there sometimes need
not be specially handled.

Finally any tools that can find vdso today will search for it in
$(MODLIB), and consequently moving it elsewhere is not practical.

If the makefiles grow a VDSOLIB it should default to MODLIB. It may be
useful for somebody to have this flexibility in the future but I am not
interested for my use case.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 0/5] kmod /usr support
  2023-08-19 11:25       ` Masahiro Yamada
  2023-08-21  6:22         ` Michal Suchánek
@ 2023-09-11 19:50         ` Michal Suchánek
  1 sibling, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-09-11 19:50 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Sat, Aug 19, 2023 at 08:25:52PM +0900, Masahiro Yamada wrote:
> On Fri, Aug 18, 2023 at 12:15 PM Michal Suchánek <msuchanek@suse.de> wrote:
> >
> > Hello,
> >
> > On Tue, Jul 18, 2023 at 02:01:51PM +0200, Michal Suchanek wrote:
> > > Hello,
> > >
> > > with these patches it is possible to install kernel modules in an arbitrary
> > > directory - eg. moving the /lib/modules to /usr/lib/modules or /opt/linux.
> > >
> > > While the modprobe.d and depmod.d search which already includes multiple
> > > paths is expanded to also include $(prefix) the module directory still
> > > supports only one location, only a different one under $(module_directory).
> > >
> > > Having kmod search multiple module locations while only one is supported now
> > > might break some assumption about relative module path corresponding to a
> > > specific file, would require more invasive changes to implement, and is not
> > > supportive of the goal of moving the modules away from /lib.
> > >
> > > Both kmod and the kernel need to be patched to make use of this feature.
> > > Patched kernel is backwards compatible with older kmod.  Patched kmod
> > > with $(module_directory) set to /lib/modules is equivalent to unpatched kmod.
> >
> > The patch to kernel to support autodetection of module directory is
> > rejected. However, a workaround like
> >
> > make MODLIB='$(INSTALL_MOD_PATH)/usr/lib/modules/$(KERNELRELEASE)'
> >
> > is suggested.
> >
> > Can you consider inluding the kmod changes?
> 
> 
> Hi.
> 
> I have a question about your original patch
> for the Kbuild change.
> 
> In your patch, Kbuild runs 'kmod config' or
> 'pkg-config --variable=module_directory kmod',
> then sets the returned string to MODLIB.
> 
> 
> If kmod is configured to use /usr/lib/modules,
> /opt/modules, or whatever,
> should we change the installation path of the debug
> vdso accordingly?
> 
> Currently, the debug vdso is always installed
> to /lib/modules/$(KERNELRELEASE)/vdso/.
> 
> However, modules and vdso are unrelated to each other.
> kmod does not care about vdso.
> 
> 
> The following commits started to install debug vdso.
> 
> Commit 8150caad0226 ("[POWERPC] powerpc vDSO: install unstripped
> copies on disk")
> Commit f79eb83b3af4 ("x86: Install unstripped copy of 64bit vdso to disk")
> 
> 
> I do not know why they chose $(MODLIB)/vdso as the install destination.
> 
> 
> I am thinking of split the variable into two:
> MODLIB -  installation path for modules
> VDSOLIB - installation path for debug vdso (not affected by kmod config)
> 
> I think that is the way to do this correctly.

The source and build symlinks that point at the kernel tree are not
modules either yet they are in the module directory as well.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH] kbuild: rpm-pkg: Fix build with non-default MODLIB
  2023-07-17 19:14                   ` Masahiro Yamada
  2023-07-18  8:52                     ` Michal Suchánek
@ 2023-09-11 19:56                     ` Michal Suchanek
  1 sibling, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-09-11 19:56 UTC (permalink / raw)
  To: linux-kbuild, linux-kernel
  Cc: Michal Suchanek, Nicolas Schier, linux-modules, Takashi Iwai,
	Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers

The default MODLIB value is composed of two variables and the hardcoded
string '/lib/modules/'.

MODLIB = $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE)

Defining this middle part as a variable was rejected on the basis that
users can pass the whole MODLIB to make, such as

make 'MODLIB=$(INSTALL_MOD_PATH)/usr/lib/modules/$(KERNELRELEASE)'

However, this middle part of MODLIB is independently hardcoded by
rpm-pkg, and when the user alters MODLIB this is not reflected when
building the package.

Given that $(INSTALL_MOD_PATH) is overridden during the rpm package build
it is likely going to be empty. Then MODLIB can be passed to the rpm
package, and used in place of the whole
/usr/lib/modules/$(KERNELRELEASE) part.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
 scripts/package/kernel.spec | 14 +++++++-------
 scripts/package/mkspec      |  1 +
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/scripts/package/kernel.spec b/scripts/package/kernel.spec
index ac3f2ee6d7a0..1a727f636f67 100644
--- a/scripts/package/kernel.spec
+++ b/scripts/package/kernel.spec
@@ -67,8 +67,8 @@ cp $(%{make} %{makeflags} -s image_name) %{buildroot}/boot/vmlinuz-%{KERNELRELEA
 %{make} %{makeflags} INSTALL_HDR_PATH=%{buildroot}/usr headers_install
 cp System.map %{buildroot}/boot/System.map-%{KERNELRELEASE}
 cp .config %{buildroot}/boot/config-%{KERNELRELEASE}
-ln -fns /usr/src/kernels/%{KERNELRELEASE} %{buildroot}/lib/modules/%{KERNELRELEASE}/build
-ln -fns /usr/src/kernels/%{KERNELRELEASE} %{buildroot}/lib/modules/%{KERNELRELEASE}/source
+ln -fns /usr/src/kernels/%{KERNELRELEASE} %{buildroot}%{MODLIB}/build
+ln -fns /usr/src/kernels/%{KERNELRELEASE} %{buildroot}%{MODLIB}/source
 %if %{with_devel}
 %{make} %{makeflags} run-command KBUILD_RUN_COMMAND='${srctree}/scripts/package/install-extmod-build %{buildroot}/usr/src/kernels/%{KERNELRELEASE}'
 %endif
@@ -99,9 +99,9 @@ fi
 
 %files
 %defattr (-, root, root)
-/lib/modules/%{KERNELRELEASE}
-%exclude /lib/modules/%{KERNELRELEASE}/build
-%exclude /lib/modules/%{KERNELRELEASE}/source
+%{MODLIB}
+%exclude %{MODLIB}/build
+%exclude %{MODLIB}/source
 /boot/*
 
 %files headers
@@ -112,6 +112,6 @@ fi
 %files devel
 %defattr (-, root, root)
 /usr/src/kernels/%{KERNELRELEASE}
-/lib/modules/%{KERNELRELEASE}/build
-/lib/modules/%{KERNELRELEASE}/source
+%{MODLIB}/build
+%{MODLIB}/source
 %endif
diff --git a/scripts/package/mkspec b/scripts/package/mkspec
index d41608efb747..d41b2e5304ac 100755
--- a/scripts/package/mkspec
+++ b/scripts/package/mkspec
@@ -18,6 +18,7 @@ fi
 cat<<EOF
 %define ARCH ${ARCH}
 %define KERNELRELEASE ${KERNELRELEASE}
+%define MODLIB ${MODLIB}
 %define pkg_release $("${srctree}/init/build-version")
 EOF
 
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 0/5] kmod /usr support
  2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
                       ` (6 preceding siblings ...)
  2023-08-17 16:37     ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchánek
@ 2023-10-17 15:45     ` Michal Suchánek
  2023-10-17 16:18       ` Lucas De Marchi
  7 siblings, 1 reply; 83+ messages in thread
From: Michal Suchánek @ 2023-10-17 15:45 UTC (permalink / raw)
  To: linux-modules
  Cc: Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

it has been a few months since these kmod patches have been posted, and
a new kmod versio has been released since.

Is there any interest in adding this to kmod?

Thanks

Michal

On Tue, Jul 18, 2023 at 02:01:51PM +0200, Michal Suchanek wrote:
> Hello,
> 
> with these patches it is possible to install kernel modules in an arbitrary
> directory - eg. moving the /lib/modules to /usr/lib/modules or /opt/linux.
> 
> While the modprobe.d and depmod.d search which already includes multiple
> paths is expanded to also include $(prefix) the module directory still
> supports only one location, only a different one under $(module_directory).
> 
> Having kmod search multiple module locations while only one is supported now
> might break some assumption about relative module path corresponding to a
> specific file, would require more invasive changes to implement, and is not
> supportive of the goal of moving the modules away from /lib.
> 
> Both kmod and the kernel need to be patched to make use of this feature.
> Patched kernel is backwards compatible with older kmod.  Patched kmod
> with $(module_directory) set to /lib/modules is equivalent to unpatched kmod.
> 
> Thanks
> 
> Michal
> 
> Link: https://lore.kernel.org/linux-modules/20210112160211.5614-1-msuchanek@suse.de/
> 
> v4: set whole path to module directory instead of adding prefix
> v5: use pkg-config instead of jq, fix build on openssl without sm3 support
> 
> 
> Michal Suchanek (5):
>   configure: Detect openssl sm3 support
>   man/depmod.d: Fix incorrect /usr/lib search path
>   libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib.
>   kmod: Add pkgconfig file with kmod compile time configuration
>   libkmod, depmod, modprobe: Make directory for kernel modules
>     configurable
> 
>  Makefile.am                          |   6 +-
>  configure.ac                         |  30 ++++++++
>  libkmod/libkmod.c                    |  11 +--
>  man/Makefile.am                      |  10 ++-
>  man/depmod.d.xml                     |   9 ++-
>  man/depmod.xml                       |   4 +-
>  man/modinfo.xml                      |   2 +-
>  man/modprobe.d.xml                   |   1 +
>  man/modprobe.xml                     |   2 +-
>  man/modules.dep.xml                  |   6 +-
>  testsuite/module-playground/Makefile |   2 +-
>  testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
>  testsuite/test-depmod.c              |  16 ++--
>  testsuite/test-testsuite.c           |   8 +-
>  tools/depmod.c                       |   7 +-
>  tools/kmod.pc.in                     |  10 +++
>  tools/modinfo.c                      |   4 +-
>  tools/modprobe.c                     |   4 +-
>  tools/static-nodes.c                 |   6 +-
>  19 files changed, 156 insertions(+), 91 deletions(-)
>  create mode 100644 tools/kmod.pc.in
> 
> -- 
> 2.41.0
> 

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 0/5] kmod /usr support
  2023-10-17 15:45     ` Michal Suchánek
@ 2023-10-17 16:18       ` Lucas De Marchi
  2023-10-17 16:40         ` Michal Suchánek
  0 siblings, 1 reply; 83+ messages in thread
From: Lucas De Marchi @ 2023-10-17 16:18 UTC (permalink / raw)
  To: Michal Suchánek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Oct 17, 2023 at 05:45:39PM +0200, Michal Suchánek wrote:
>Hello,
>
>it has been a few months since these kmod patches have been posted, and
>a new kmod versio has been released since.
>
>Is there any interest in adding this to kmod?

yes, but I think the main drag is deciding with the kernel build system
maintainers what they are willing to accept as an interface. There isn't
much point in exporting a json config if from the kernel side they would
rather use something else. Or to use pkg-config.

I confess I lost track of that discussion. Did that settle with
pkg-config being the preferred solution?

Lucas De Marchi

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 0/5] kmod /usr support
  2023-10-17 16:18       ` Lucas De Marchi
@ 2023-10-17 16:40         ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-10-17 16:40 UTC (permalink / raw)
  To: Lucas De Marchi
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Oct 17, 2023 at 11:18:23AM -0500, Lucas De Marchi wrote:
> On Tue, Oct 17, 2023 at 05:45:39PM +0200, Michal Suchánek wrote:
> > Hello,
> > 
> > it has been a few months since these kmod patches have been posted, and
> > a new kmod versio has been released since.
> > 
> > Is there any interest in adding this to kmod?
> 
> yes, but I think the main drag is deciding with the kernel build system
> maintainers what they are willing to accept as an interface. There isn't
> much point in exporting a json config if from the kernel side they would
> rather use something else. Or to use pkg-config.
> 
> I confess I lost track of that discussion. Did that settle with
> pkg-config being the preferred solution?

The current discussion about the kernel side can be found here:

https://lore.kernel.org/linux-kbuild/20231017151050.GJ6241@kitsune.suse.cz/T/#t

My impression is that pkg-config is accepted as an interface on the
basis that it's already required for building the kernel while jq is
currently required only for some additional optional tools.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-07-18 12:01     ` [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
@ 2023-10-17 17:50       ` Lucas De Marchi
  2023-10-18  1:25         ` Jan Engelhardt
                           ` (3 more replies)
  0 siblings, 4 replies; 83+ messages in thread
From: Lucas De Marchi @ 2023-10-17 17:50 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Jul 18, 2023 at 02:01:56PM +0200, Michal Suchanek wrote:
>Now that modprobe.d is searched under ${prefix}/lib, allow a complete
>transition to files only under ${prefix} by adding a ${module_directory}
>configuration. This specifies the directory where to search for kernel
>modules and should match the location where the kernel/distro installs
>them.
>
>With this distributions that do not want to ship files in /lib can also
>move kernel modules to /usr while others can keep them in /lib.
>
>Signed-off-by: Michal Suchanek <msuchanek@suse.de>
>---
>v4: Make the whole path configurable
>v5: More verbose commit message
>---
> Makefile.am                          |   3 +-
> configure.ac                         |   7 ++
> libkmod/libkmod.c                    |   4 +-
> man/Makefile.am                      |   1 +
> man/depmod.d.xml                     |   6 +-
> man/depmod.xml                       |   4 +-
> man/modinfo.xml                      |   2 +-
> man/modprobe.xml                     |   2 +-
> man/modules.dep.xml                  |   6 +-
> testsuite/module-playground/Makefile |   2 +-
> testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
> testsuite/test-depmod.c              |  16 ++--
> testsuite/test-testsuite.c           |   8 +-
> tools/depmod.c                       |   6 +-
> tools/kmod.pc.in                     |   1 +
> tools/modinfo.c                      |   4 +-
> tools/modprobe.c                     |   4 +-
> tools/static-nodes.c                 |   6 +-
> 18 files changed, 107 insertions(+), 84 deletions(-)
>
>diff --git a/Makefile.am b/Makefile.am
>index 6d0b2decfef3..019aa749fdf1 100644
>--- a/Makefile.am
>+++ b/Makefile.am
>@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
> 	-I$(top_srcdir) \
> 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
> 	-DDISTCONFDIR=\""$(distconfdir)"\" \
>+	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
> 	${zlib_CFLAGS}
>
> AM_CFLAGS = $(OUR_CFLAGS)
>@@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
> MODULE_PLAYGROUND = testsuite/module-playground
> ROOTFS = testsuite/rootfs
> ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
>-CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
>+CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
>
> build-module-playground:
> 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
>diff --git a/configure.ac b/configure.ac
>index b4584d6cdc67..4051dc9249e2 100644
>--- a/configure.ac
>+++ b/configure.ac
>@@ -91,6 +91,12 @@ AC_ARG_WITH([rootlibdir],
>         [], [with_rootlibdir=$libdir])
> AC_SUBST([rootlibdir], [$with_rootlibdir])
>
>+# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
>+AC_ARG_WITH([module_directory],
>+        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
>+        [], [with_module_directory=/lib/modules])
>+AC_SUBST([module_directory], [$with_module_directory])

we will probably have "fun" results if we accept a relative path here.

>+
> AC_ARG_WITH([zstd],
> 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
> 	[], [with_zstd=no])
>@@ -326,6 +332,7 @@ AC_MSG_RESULT([
> 	$PACKAGE $VERSION
> 	=======
>
>+	module_directory:	${module_directory}
> 	prefix:			${prefix}
> 	sysconfdir:		${sysconfdir}
> 	distconfdir:		${distconfdir}
>diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
>index 09e6041461b0..63719e886de8 100644
>--- a/libkmod/libkmod.c
>+++ b/libkmod/libkmod.c
>@@ -209,7 +209,7 @@ static int log_priority(const char *priority)
> 	return 0;
> }
>
>-static const char *dirname_default_prefix = "/lib/modules";
>+static const char *dirname_default_prefix = MODULE_DIRECTORY;
>
> static char *get_kernel_release(const char *dirname)
> {
>@@ -231,7 +231,7 @@ static char *get_kernel_release(const char *dirname)
> /**
>  * kmod_new:
>  * @dirname: what to consider as linux module's directory, if NULL
>- *           defaults to /lib/modules/`uname -r`. If it's relative,
>+ *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,

module_prefix?  did you mean to use $MODULE_DIRECTORY/`uname -r`?

>  *           it's treated as relative to the current working directory.
>  *           Otherwise, give an absolute dirname.
>  * @config_paths: ordered array of paths (directories or files) where
>diff --git a/man/Makefile.am b/man/Makefile.am
>index 2fea8e46bf2f..f550091a216a 100644
>--- a/man/Makefile.am
>+++ b/man/Makefile.am
>@@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
> 	else \
> 		sed -e '/@DISTCONFDIR@/d' $< ; \
> 	fi | \
>+	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
> 	$(XSLT) \
> 		-o $@ \
> 		--nonet \
>diff --git a/man/depmod.d.xml b/man/depmod.d.xml
>index f282a39cc840..b07e6a2bd4fe 100644
>--- a/man/depmod.d.xml
>+++ b/man/depmod.d.xml
>@@ -70,7 +70,7 @@
>         </term>
>         <listitem>
>           <para>
>-            This allows you to specify the order in which /lib/modules
>+            This allows you to specify the order in which @MODULE_DIRECTORY@
>             (or other configured module location) subdirectories will
>             be processed by <command>depmod</command>. Directories are
>             listed in order, with the highest priority given to the
>@@ -101,7 +101,7 @@
>             <command>depmod</command> command. It is possible to
>             specify one kernel or all kernels using the * wildcard.
>             <replaceable>modulesubdirectory</replaceable> is the
>-            name of the subdirectory under /lib/modules (or other
>+            name of the subdirectory under @MODULE_DIRECTORY@ (or other
>             module location) where the target module is installed.
>           </para>
>           <para>
>@@ -110,7 +110,7 @@
>             specifying the following command: "override kmod * extra".
>             This will ensure that any matching module name installed
>             under the <command>extra</command> subdirectory within
>-            /lib/modules (or other module location) will take priority
>+            @MODULE_DIRECTORY@ (or other module location) will take priority
>             over any likenamed module already provided by the kernel.
>           </para>
>         </listitem>
>diff --git a/man/depmod.xml b/man/depmod.xml
>index 3b0097184fd7..fce2a4a67a89 100644
>--- a/man/depmod.xml
>+++ b/man/depmod.xml
>@@ -80,7 +80,7 @@
>     </para>
>     <para> <command>depmod</command> creates a list of module dependencies by
>       reading each module under
>-      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
>+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
>       determining what symbols it exports and what symbols it needs.  By
>       default, this list is written to <filename>modules.dep</filename>, and a
>       binary hashed version named <filename>modules.dep.bin</filename>, in the
>@@ -141,7 +141,7 @@
>         <listitem>
>           <para>
>             If your modules are not currently in the (normal) directory
>-            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
>+            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
>             but in a staging area, you can specify a
>             <replaceable>basedir</replaceable> which is prepended to the
>             directory name.  This <replaceable>basedir</replaceable> is
>diff --git a/man/modinfo.xml b/man/modinfo.xml
>index 9fe0324a2527..b6c4d6045829 100644
>--- a/man/modinfo.xml
>+++ b/man/modinfo.xml
>@@ -54,7 +54,7 @@
>       <command>modinfo</command> extracts information from the Linux Kernel
>       modules given on the command line.  If the module name is not a filename,
>       then the
>-      <filename>/lib/modules/</filename><replaceable>version</replaceable>
>+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
>       directory is searched, as is also done by
>       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
>       when loading kernel modules.
>diff --git a/man/modprobe.xml b/man/modprobe.xml
>index 91f9e27997cd..4d1fd59c000b 100644
>--- a/man/modprobe.xml
>+++ b/man/modprobe.xml
>@@ -78,7 +78,7 @@
>       is no difference between _ and - in module names (automatic
>       underscore conversion is performed).
>       <command>modprobe</command> looks in the module directory
>-      <filename>/lib/modules/`uname -r`</filename> for all
>+      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
>       the modules and other files, except for the optional
>       configuration files in the
>       <filename>/etc/modprobe.d</filename> directory
>diff --git a/man/modules.dep.xml b/man/modules.dep.xml
>index ed633694ec9e..8ef6d8b3536e 100644
>--- a/man/modules.dep.xml
>+++ b/man/modules.dep.xml
>@@ -34,8 +34,8 @@
>   </refnamediv>
>
>   <refsynopsisdiv>
>-    <para><filename>/lib/modules/modules.dep</filename></para>
>-    <para><filename>/lib/modules/modules.dep.bin</filename></para>
>+    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
>+    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
>   </refsynopsisdiv>
>
>   <refsect1><title>DESCRIPTION</title>
>@@ -43,7 +43,7 @@
>       <filename>modules.dep.bin</filename> is a binary file generated by
>       <command>depmod</command> listing the dependencies for
>       every module in the directories under
>-      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
>+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
>       It is used by kmod tools such as <command>modprobe</command> and
>       libkmod.
>     </para>
>diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
>index e6045b0dd932..a7ab09bea2bf 100644
>--- a/testsuite/module-playground/Makefile
>+++ b/testsuite/module-playground/Makefile
>@@ -47,7 +47,7 @@ endif
>
> else
> # normal makefile
>-KDIR ?= /lib/modules/`uname -r`/build
>+KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
> KVER ?= `uname -r`
> ifeq ($(FAKE_BUILD),)
>     FAKE_BUILD=0
>diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
>index 4440ddcd6b4d..a780f9381b3c 100755
>--- a/testsuite/setup-rootfs.sh
>+++ b/testsuite/setup-rootfs.sh
>@@ -16,6 +16,19 @@ create_rootfs() {
> 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
> 	find "$ROOTFS" -type d -exec chmod +w {} \;
> 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
>+	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
>+		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
>+		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
>+		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
>+			version="$(basename $i)"
>+			[ $version != 'external' ] || continue
>+			mod="$(dirname $i)"
>+			lib="$(dirname $mod)"
>+			up="$(dirname $lib)$MODULE_DIRECTORY"
>+			mkdir -p "$up"
>+			mv "$i" "$up"
>+		done
>+	fi
>
> 	if [ "$SYSCONFDIR" != "/etc" ]; then
> 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
>@@ -32,57 +45,57 @@ feature_enabled() {
>
> declare -A map
> map=(
>-    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
>-    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
>-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
>-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
>-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
>-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
>-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
>+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/kernel/crypto/"]="mod-simple.ko"
>+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/updates/"]="mod-simple.ko"
>+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
>+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
>+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
>+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
>+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
>     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"

why didn't you change it here?

>-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
>-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
>+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
>+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
>     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"

and here...


Otherwise looks good to me.

Pushed the first 4 patches.

thanks
Lucas De Marchi

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-10-17 17:50       ` Lucas De Marchi
@ 2023-10-18  1:25         ` Jan Engelhardt
  2023-11-09 17:40           ` Michal Suchánek
  2023-11-09 17:44         ` Michal Suchánek
                           ` (2 subsequent siblings)
  3 siblings, 1 reply; 83+ messages in thread
From: Jan Engelhardt @ 2023-10-18  1:25 UTC (permalink / raw)
  To: Lucas De Marchi
  Cc: Michal Suchanek, linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Tuesday 2023-10-17 19:50, Lucas De Marchi wrote:
>> +AC_ARG_WITH([module_directory],
>> +        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to
>> look for kernel modules - typically '/lib/modules' or
>> '${prefix}/lib/modules']),
>> +        [], [with_module_directory=/lib/modules])
>> +AC_SUBST([module_directory], [$with_module_directory])
>
> we will probably have "fun" results if we accept a relative path here.

$ ./configure --prefix=/usr --bindir=../bin
configure: error: expected an absolute directory name for --bindir: ../bin

While such check does not exist for --with-module-directory, everyone
has likely been well-trained not to use relative paths. Even if, say,
cmake/meson *could* do it, it just would have never occurred to me to
actually *utilize* it, because it is just too ambiguous and
potentially dangerous.

Just think of all the fun you could have with LD_LIBRARY_PATH=".."


^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-10-18  1:25         ` Jan Engelhardt
@ 2023-11-09 17:40           ` Michal Suchánek
  0 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-11-09 17:40 UTC (permalink / raw)
  To: Jan Engelhardt
  Cc: Lucas De Marchi, linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Masahiro Yamada, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Wed, Oct 18, 2023 at 03:25:19AM +0200, Jan Engelhardt wrote:
> On Tuesday 2023-10-17 19:50, Lucas De Marchi wrote:
> >> +AC_ARG_WITH([module_directory],
> >> +        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to
> >> look for kernel modules - typically '/lib/modules' or
> >> '${prefix}/lib/modules']),
> >> +        [], [with_module_directory=/lib/modules])
> >> +AC_SUBST([module_directory], [$with_module_directory])
> >
> > we will probably have "fun" results if we accept a relative path here.
> 
> $ ./configure --prefix=/usr --bindir=../bin
> configure: error: expected an absolute directory name for --bindir: ../bin
> 
> While such check does not exist for --with-module-directory, everyone
> has likely been well-trained not to use relative paths. Even if, say,
> cmake/meson *could* do it, it just would have never occurred to me to
> actually *utilize* it, because it is just too ambiguous and
> potentially dangerous.
> 
> Just think of all the fun you could have with LD_LIBRARY_PATH=".."

Is there an example of how this guarding against relative paths is done
in other options that accept directories?

There may be something for the built-in options but the existing custom
rootlibdir option does not seem to have anything either.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-10-17 17:50       ` Lucas De Marchi
  2023-10-18  1:25         ` Jan Engelhardt
@ 2023-11-09 17:44         ` Michal Suchánek
  2023-11-10 12:13         ` [PATCH 0/2] kmod /usr support Michal Suchanek
  2023-11-13  9:27         ` [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchánek
  3 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-11-09 17:44 UTC (permalink / raw)
  To: Lucas De Marchi
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Oct 17, 2023 at 12:50:15PM -0500, Lucas De Marchi wrote:
> On Tue, Jul 18, 2023 at 02:01:56PM +0200, Michal Suchanek wrote:
> > Now that modprobe.d is searched under ${prefix}/lib, allow a complete
> > transition to files only under ${prefix} by adding a ${module_directory}
> > configuration. This specifies the directory where to search for kernel
> > modules and should match the location where the kernel/distro installs
> > them.
> > 
> > With this distributions that do not want to ship files in /lib can also
> > move kernel modules to /usr while others can keep them in /lib.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v4: Make the whole path configurable
> > v5: More verbose commit message
> > ---
> > Makefile.am                          |   3 +-
> > configure.ac                         |   7 ++
> > libkmod/libkmod.c                    |   4 +-
> > man/Makefile.am                      |   1 +
> > man/depmod.d.xml                     |   6 +-
> > man/depmod.xml                       |   4 +-
> > man/modinfo.xml                      |   2 +-
> > man/modprobe.xml                     |   2 +-
> > man/modules.dep.xml                  |   6 +-
> > testsuite/module-playground/Makefile |   2 +-
> > testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
> > testsuite/test-depmod.c              |  16 ++--
> > testsuite/test-testsuite.c           |   8 +-
> > tools/depmod.c                       |   6 +-
> > tools/kmod.pc.in                     |   1 +
> > tools/modinfo.c                      |   4 +-
> > tools/modprobe.c                     |   4 +-
> > tools/static-nodes.c                 |   6 +-
> > 18 files changed, 107 insertions(+), 84 deletions(-)
> > 
> > diff --git a/Makefile.am b/Makefile.am
> > index 6d0b2decfef3..019aa749fdf1 100644
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > @@ -20,6 +20,7 @@ AM_CPPFLAGS = \
> > 	-I$(top_srcdir) \
> > 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
> > 	-DDISTCONFDIR=\""$(distconfdir)"\" \
> > +	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
> > 	${zlib_CFLAGS}
> > 
> > AM_CFLAGS = $(OUR_CFLAGS)
> > @@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
> > MODULE_PLAYGROUND = testsuite/module-playground
> > ROOTFS = testsuite/rootfs
> > ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
> > -CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
> > +CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
> > 
> > build-module-playground:
> > 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
> > diff --git a/configure.ac b/configure.ac
> > index b4584d6cdc67..4051dc9249e2 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -91,6 +91,12 @@ AC_ARG_WITH([rootlibdir],
> >         [], [with_rootlibdir=$libdir])
> > AC_SUBST([rootlibdir], [$with_rootlibdir])
> > 
> > +# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
> > +AC_ARG_WITH([module_directory],
> > +        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
> > +        [], [with_module_directory=/lib/modules])
> > +AC_SUBST([module_directory], [$with_module_directory])
> 
> we will probably have "fun" results if we accept a relative path here.
> 
> > +
> > AC_ARG_WITH([zstd],
> > 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
> > 	[], [with_zstd=no])
> > @@ -326,6 +332,7 @@ AC_MSG_RESULT([
> > 	$PACKAGE $VERSION
> > 	=======
> > 
> > +	module_directory:	${module_directory}
> > 	prefix:			${prefix}
> > 	sysconfdir:		${sysconfdir}
> > 	distconfdir:		${distconfdir}
> > diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> > index 09e6041461b0..63719e886de8 100644
> > --- a/libkmod/libkmod.c
> > +++ b/libkmod/libkmod.c
> > @@ -209,7 +209,7 @@ static int log_priority(const char *priority)
> > 	return 0;
> > }
> > 
> > -static const char *dirname_default_prefix = "/lib/modules";
> > +static const char *dirname_default_prefix = MODULE_DIRECTORY;
> > 
> > static char *get_kernel_release(const char *dirname)
> > {
> > @@ -231,7 +231,7 @@ static char *get_kernel_release(const char *dirname)
> > /**
> >  * kmod_new:
> >  * @dirname: what to consider as linux module's directory, if NULL
> > - *           defaults to /lib/modules/`uname -r`. If it's relative,
> > + *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
> 
> module_prefix?  did you mean to use $MODULE_DIRECTORY/`uname -r`?

Yes, that's leftover from previous revision, will fix up.

> 
> >  *           it's treated as relative to the current working directory.
> >  *           Otherwise, give an absolute dirname.
> >  * @config_paths: ordered array of paths (directories or files) where
> > diff --git a/man/Makefile.am b/man/Makefile.am
> > index 2fea8e46bf2f..f550091a216a 100644
> > --- a/man/Makefile.am
> > +++ b/man/Makefile.am
> > @@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
> > 	else \
> > 		sed -e '/@DISTCONFDIR@/d' $< ; \
> > 	fi | \
> > +	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
> > 	$(XSLT) \
> > 		-o $@ \
> > 		--nonet \
> > diff --git a/man/depmod.d.xml b/man/depmod.d.xml
> > index f282a39cc840..b07e6a2bd4fe 100644
> > --- a/man/depmod.d.xml
> > +++ b/man/depmod.d.xml
> > @@ -70,7 +70,7 @@
> >         </term>
> >         <listitem>
> >           <para>
> > -            This allows you to specify the order in which /lib/modules
> > +            This allows you to specify the order in which @MODULE_DIRECTORY@
> >             (or other configured module location) subdirectories will
> >             be processed by <command>depmod</command>. Directories are
> >             listed in order, with the highest priority given to the
> > @@ -101,7 +101,7 @@
> >             <command>depmod</command> command. It is possible to
> >             specify one kernel or all kernels using the * wildcard.
> >             <replaceable>modulesubdirectory</replaceable> is the
> > -            name of the subdirectory under /lib/modules (or other
> > +            name of the subdirectory under @MODULE_DIRECTORY@ (or other
> >             module location) where the target module is installed.
> >           </para>
> >           <para>
> > @@ -110,7 +110,7 @@
> >             specifying the following command: "override kmod * extra".
> >             This will ensure that any matching module name installed
> >             under the <command>extra</command> subdirectory within
> > -            /lib/modules (or other module location) will take priority
> > +            @MODULE_DIRECTORY@ (or other module location) will take priority
> >             over any likenamed module already provided by the kernel.
> >           </para>
> >         </listitem>
> > diff --git a/man/depmod.xml b/man/depmod.xml
> > index 3b0097184fd7..fce2a4a67a89 100644
> > --- a/man/depmod.xml
> > +++ b/man/depmod.xml
> > @@ -80,7 +80,7 @@
> >     </para>
> >     <para> <command>depmod</command> creates a list of module dependencies by
> >       reading each module under
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
> >       determining what symbols it exports and what symbols it needs.  By
> >       default, this list is written to <filename>modules.dep</filename>, and a
> >       binary hashed version named <filename>modules.dep.bin</filename>, in the
> > @@ -141,7 +141,7 @@
> >         <listitem>
> >           <para>
> >             If your modules are not currently in the (normal) directory
> > -            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
> > +            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
> >             but in a staging area, you can specify a
> >             <replaceable>basedir</replaceable> which is prepended to the
> >             directory name.  This <replaceable>basedir</replaceable> is
> > diff --git a/man/modinfo.xml b/man/modinfo.xml
> > index 9fe0324a2527..b6c4d6045829 100644
> > --- a/man/modinfo.xml
> > +++ b/man/modinfo.xml
> > @@ -54,7 +54,7 @@
> >       <command>modinfo</command> extracts information from the Linux Kernel
> >       modules given on the command line.  If the module name is not a filename,
> >       then the
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable>
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
> >       directory is searched, as is also done by
> >       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
> >       when loading kernel modules.
> > diff --git a/man/modprobe.xml b/man/modprobe.xml
> > index 91f9e27997cd..4d1fd59c000b 100644
> > --- a/man/modprobe.xml
> > +++ b/man/modprobe.xml
> > @@ -78,7 +78,7 @@
> >       is no difference between _ and - in module names (automatic
> >       underscore conversion is performed).
> >       <command>modprobe</command> looks in the module directory
> > -      <filename>/lib/modules/`uname -r`</filename> for all
> > +      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
> >       the modules and other files, except for the optional
> >       configuration files in the
> >       <filename>/etc/modprobe.d</filename> directory
> > diff --git a/man/modules.dep.xml b/man/modules.dep.xml
> > index ed633694ec9e..8ef6d8b3536e 100644
> > --- a/man/modules.dep.xml
> > +++ b/man/modules.dep.xml
> > @@ -34,8 +34,8 @@
> >   </refnamediv>
> > 
> >   <refsynopsisdiv>
> > -    <para><filename>/lib/modules/modules.dep</filename></para>
> > -    <para><filename>/lib/modules/modules.dep.bin</filename></para>
> > +    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
> > +    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
> >   </refsynopsisdiv>
> > 
> >   <refsect1><title>DESCRIPTION</title>
> > @@ -43,7 +43,7 @@
> >       <filename>modules.dep.bin</filename> is a binary file generated by
> >       <command>depmod</command> listing the dependencies for
> >       every module in the directories under
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
> >       It is used by kmod tools such as <command>modprobe</command> and
> >       libkmod.
> >     </para>
> > diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
> > index e6045b0dd932..a7ab09bea2bf 100644
> > --- a/testsuite/module-playground/Makefile
> > +++ b/testsuite/module-playground/Makefile
> > @@ -47,7 +47,7 @@ endif
> > 
> > else
> > # normal makefile
> > -KDIR ?= /lib/modules/`uname -r`/build
> > +KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
> > KVER ?= `uname -r`
> > ifeq ($(FAKE_BUILD),)
> >     FAKE_BUILD=0
> > diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
> > index 4440ddcd6b4d..a780f9381b3c 100755
> > --- a/testsuite/setup-rootfs.sh
> > +++ b/testsuite/setup-rootfs.sh
> > @@ -16,6 +16,19 @@ create_rootfs() {
> > 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
> > 	find "$ROOTFS" -type d -exec chmod +w {} \;
> > 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
> > +	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
> > +		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
> > +		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
> > +		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
> > +			version="$(basename $i)"
> > +			[ $version != 'external' ] || continue
> > +			mod="$(dirname $i)"
> > +			lib="$(dirname $mod)"
> > +			up="$(dirname $lib)$MODULE_DIRECTORY"
> > +			mkdir -p "$up"
> > +			mv "$i" "$up"
> > +		done
> > +	fi
> > 
> > 	if [ "$SYSCONFDIR" != "/etc" ]; then
> > 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
> > @@ -32,57 +45,57 @@ feature_enabled() {
> > 
> > declare -A map
> > map=(
> > -    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
> > -    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/kernel/crypto/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/updates/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
> > +    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
> >     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
> 
> why didn't you change it here?

That's a module that's not installed into the directory of the current
kernel -> does not relally matter where it is so long as it is not in
that one directory corresponding to the tested kernel version AFAICT.

> 
> > -    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
> >     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
> 
> and here...

Same.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH 0/2] kmod /usr support
  2023-10-17 17:50       ` Lucas De Marchi
  2023-10-18  1:25         ` Jan Engelhardt
  2023-11-09 17:44         ` Michal Suchánek
@ 2023-11-10 12:13         ` Michal Suchanek
  2023-11-10 12:13           ` [PATCH 1/2] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
                             ` (2 more replies)
  2023-11-13  9:27         ` [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchánek
  3 siblings, 3 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-11-10 12:13 UTC (permalink / raw)
  To: linux-modules, Lucas De Marchi
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Hello,

This is resend of the last patch in the series that adds prefix support
to kernel module location together with additional patch for validating
the user supplied input to options that are interpreted as directories.

Thanks

Michal

Michal Suchanek (2):
  libkmod, depmod, modprobe: Make directory for kernel modules
    configurable
  configure: Check that provided paths are absolute

 Makefile.am                          |   3 +-
 configure.ac                         |  24 ++++++
 libkmod/libkmod.c                    |   4 +-
 man/Makefile.am                      |   1 +
 man/depmod.d.xml                     |   6 +-
 man/depmod.xml                       |   4 +-
 man/modinfo.xml                      |   2 +-
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   6 +-
 tools/kmod.pc.in                     |   1 +
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 18 files changed, 124 insertions(+), 84 deletions(-)

-- 
2.42.0



^ permalink raw reply	[flat|nested] 83+ messages in thread

* [PATCH 1/2] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-11-10 12:13         ` [PATCH 0/2] kmod /usr support Michal Suchanek
@ 2023-11-10 12:13           ` Michal Suchanek
  2023-11-10 12:13           ` [PATCH 2/2] configure: Check that provided paths are absolute Michal Suchanek
  2023-12-06 18:36           ` [PATCH 0/2] kmod /usr support Lucas De Marchi
  2 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-11-10 12:13 UTC (permalink / raw)
  To: linux-modules, Lucas De Marchi
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

Now that modprobe.d is searched under ${prefix}/lib, allow a complete
transition to files only under ${prefix} by adding a ${module_directory}
configuration. This specifies the directory where to search for kernel
modules and should match the location where the kernel/distro installs
them.

With this distributions that do not want to ship files in /lib can also
move kernel modules to /usr while others can keep them in /lib.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v4: Make the whole path configurable
v5: More verbose commit message
v6: fix docstring still containing module_prefix
---
 Makefile.am                          |   3 +-
 configure.ac                         |   7 ++
 libkmod/libkmod.c                    |   4 +-
 man/Makefile.am                      |   1 +
 man/depmod.d.xml                     |   6 +-
 man/depmod.xml                       |   4 +-
 man/modinfo.xml                      |   2 +-
 man/modprobe.xml                     |   2 +-
 man/modules.dep.xml                  |   6 +-
 testsuite/module-playground/Makefile |   2 +-
 testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
 testsuite/test-depmod.c              |  16 ++--
 testsuite/test-testsuite.c           |   8 +-
 tools/depmod.c                       |   6 +-
 tools/kmod.pc.in                     |   1 +
 tools/modinfo.c                      |   4 +-
 tools/modprobe.c                     |   4 +-
 tools/static-nodes.c                 |   6 +-
 18 files changed, 107 insertions(+), 84 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 2a54c25bd631..4062d81227df 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir) \
 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
 	-DDISTCONFDIR=\""$(distconfdir)"\" \
+	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
 	${zlib_CFLAGS}
 
 AM_CFLAGS = $(OUR_CFLAGS)
@@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
 MODULE_PLAYGROUND = testsuite/module-playground
 ROOTFS = testsuite/rootfs
 ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
-CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
+CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
 
 build-module-playground:
 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
diff --git a/configure.ac b/configure.ac
index a6b8fa0308b6..d6da5ee9ae9a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -91,6 +91,12 @@ AC_ARG_WITH([rootlibdir],
         [], [with_rootlibdir=$libdir])
 AC_SUBST([rootlibdir], [$with_rootlibdir])
 
+# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
+AC_ARG_WITH([module_directory],
+        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
+        [], [with_module_directory=/lib/modules])
+AC_SUBST([module_directory], [$with_module_directory])
+
 AC_ARG_WITH([zstd],
 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
 	[], [with_zstd=no])
@@ -319,6 +325,7 @@ AC_MSG_RESULT([
 	$PACKAGE $VERSION
 	=======
 
+	module_directory:	${module_directory}
 	prefix:			${prefix}
 	sysconfdir:		${sysconfdir}
 	distconfdir:		${distconfdir}
diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
index 57fac1cb9f7b..213b42482fb6 100644
--- a/libkmod/libkmod.c
+++ b/libkmod/libkmod.c
@@ -210,7 +210,7 @@ static int log_priority(const char *priority)
 	return 0;
 }
 
-static const char *dirname_default_prefix = "/lib/modules";
+static const char *dirname_default_prefix = MODULE_DIRECTORY;
 
 static char *get_kernel_release(const char *dirname)
 {
@@ -266,7 +266,7 @@ static enum kmod_file_compression_type get_kernel_compression(struct kmod_ctx *c
 /**
  * kmod_new:
  * @dirname: what to consider as linux module's directory, if NULL
- *           defaults to /lib/modules/`uname -r`. If it's relative,
+ *           defaults to $MODULE_DIRECTORY/`uname -r`. If it's relative,
  *           it's treated as relative to the current working directory.
  *           Otherwise, give an absolute dirname.
  * @config_paths: ordered array of paths (directories or files) where
diff --git a/man/Makefile.am b/man/Makefile.am
index 2fea8e46bf2f..f550091a216a 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
 	else \
 		sed -e '/@DISTCONFDIR@/d' $< ; \
 	fi | \
+	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
 	$(XSLT) \
 		-o $@ \
 		--nonet \
diff --git a/man/depmod.d.xml b/man/depmod.d.xml
index f282a39cc840..b07e6a2bd4fe 100644
--- a/man/depmod.d.xml
+++ b/man/depmod.d.xml
@@ -70,7 +70,7 @@
         </term>
         <listitem>
           <para>
-            This allows you to specify the order in which /lib/modules
+            This allows you to specify the order in which @MODULE_DIRECTORY@
             (or other configured module location) subdirectories will
             be processed by <command>depmod</command>. Directories are
             listed in order, with the highest priority given to the
@@ -101,7 +101,7 @@
             <command>depmod</command> command. It is possible to
             specify one kernel or all kernels using the * wildcard.
             <replaceable>modulesubdirectory</replaceable> is the
-            name of the subdirectory under /lib/modules (or other
+            name of the subdirectory under @MODULE_DIRECTORY@ (or other
             module location) where the target module is installed.
           </para>
           <para>
@@ -110,7 +110,7 @@
             specifying the following command: "override kmod * extra".
             This will ensure that any matching module name installed
             under the <command>extra</command> subdirectory within
-            /lib/modules (or other module location) will take priority
+            @MODULE_DIRECTORY@ (or other module location) will take priority
             over any likenamed module already provided by the kernel.
           </para>
         </listitem>
diff --git a/man/depmod.xml b/man/depmod.xml
index 3b0097184fd7..fce2a4a67a89 100644
--- a/man/depmod.xml
+++ b/man/depmod.xml
@@ -80,7 +80,7 @@
     </para>
     <para> <command>depmod</command> creates a list of module dependencies by
       reading each module under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
       determining what symbols it exports and what symbols it needs.  By
       default, this list is written to <filename>modules.dep</filename>, and a
       binary hashed version named <filename>modules.dep.bin</filename>, in the
@@ -141,7 +141,7 @@
         <listitem>
           <para>
             If your modules are not currently in the (normal) directory
-            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
+            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
             but in a staging area, you can specify a
             <replaceable>basedir</replaceable> which is prepended to the
             directory name.  This <replaceable>basedir</replaceable> is
diff --git a/man/modinfo.xml b/man/modinfo.xml
index 9fe0324a2527..b6c4d6045829 100644
--- a/man/modinfo.xml
+++ b/man/modinfo.xml
@@ -54,7 +54,7 @@
       <command>modinfo</command> extracts information from the Linux Kernel
       modules given on the command line.  If the module name is not a filename,
       then the
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
       directory is searched, as is also done by
       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
       when loading kernel modules.
diff --git a/man/modprobe.xml b/man/modprobe.xml
index 91f9e27997cd..4d1fd59c000b 100644
--- a/man/modprobe.xml
+++ b/man/modprobe.xml
@@ -78,7 +78,7 @@
       is no difference between _ and - in module names (automatic
       underscore conversion is performed).
       <command>modprobe</command> looks in the module directory
-      <filename>/lib/modules/`uname -r`</filename> for all
+      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
       the modules and other files, except for the optional
       configuration files in the
       <filename>/etc/modprobe.d</filename> directory
diff --git a/man/modules.dep.xml b/man/modules.dep.xml
index ed633694ec9e..8ef6d8b3536e 100644
--- a/man/modules.dep.xml
+++ b/man/modules.dep.xml
@@ -34,8 +34,8 @@
   </refnamediv>
 
   <refsynopsisdiv>
-    <para><filename>/lib/modules/modules.dep</filename></para>
-    <para><filename>/lib/modules/modules.dep.bin</filename></para>
+    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
+    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
   </refsynopsisdiv>
 
   <refsect1><title>DESCRIPTION</title>
@@ -43,7 +43,7 @@
       <filename>modules.dep.bin</filename> is a binary file generated by
       <command>depmod</command> listing the dependencies for
       every module in the directories under
-      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
+      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
       It is used by kmod tools such as <command>modprobe</command> and
       libkmod.
     </para>
diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
index e6045b0dd932..a7ab09bea2bf 100644
--- a/testsuite/module-playground/Makefile
+++ b/testsuite/module-playground/Makefile
@@ -47,7 +47,7 @@ endif
 
 else
 # normal makefile
-KDIR ?= /lib/modules/`uname -r`/build
+KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
 KVER ?= `uname -r`
 ifeq ($(FAKE_BUILD),)
     FAKE_BUILD=0
diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
index 4440ddcd6b4d..a780f9381b3c 100755
--- a/testsuite/setup-rootfs.sh
+++ b/testsuite/setup-rootfs.sh
@@ -16,6 +16,19 @@ create_rootfs() {
 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
 	find "$ROOTFS" -type d -exec chmod +w {} \;
 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
+	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
+		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
+		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
+			version="$(basename $i)"
+			[ $version != 'external' ] || continue
+			mod="$(dirname $i)"
+			lib="$(dirname $mod)"
+			up="$(dirname $lib)$MODULE_DIRECTORY"
+			mkdir -p "$up"
+			mv "$i" "$up"
+		done
+	fi
 
 	if [ "$SYSCONFDIR" != "/etc" ]; then
 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
@@ -32,57 +45,57 @@ feature_enabled() {
 
 declare -A map
 map=(
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
-    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
-    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/kernel/crypto/"]="mod-simple.ko"
+    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/updates/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
+    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/foo/"]="mod-simple.ko"
-    ["test-depmod/search-order-override/lib/modules/4.4.4/override/"]="mod-simple.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
-    ["test-dependencies/lib/modules/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
+    ["test-depmod/search-order-override$MODULE_DIRECTORY/4.4.4/override/"]="mod-simple.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/foo/"]="mod-foo-b.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/"]="mod-foo-c.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/lib/"]="mod-foo-a.ko"
+    ["test-dependencies$MODULE_DIRECTORY/4.0.20-kmod/kernel/fs/"]="mod-foo.ko"
     ["test-init/"]="mod-simple.ko"
     ["test-remove/"]="mod-simple.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/show-depends/lib/modules/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/show-depends$MODULE_DIRECTORY/4.4.4/kernel/mod-simple.ko"]="mod-simple.ko"
     ["test-modprobe/show-exports/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/softdep-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
-    ["test-modprobe/install-cmd-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
-    ["test-modprobe/force/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/oldkernel-force/lib/modules/3.3.3/kernel/"]="mod-simple.ko"
-    ["test-modprobe/alias-to-none/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-modprobe/module-param-kcmdline/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/softdep-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
+    ["test-modprobe/install-cmd-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
+    ["test-modprobe/force$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/oldkernel-force$MODULE_DIRECTORY/3.3.3/kernel/"]="mod-simple.ko"
+    ["test-modprobe/alias-to-none$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-modprobe/module-param-kcmdline$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
     ["test-modprobe/external/lib/modules/external/"]="mod-simple.ko"
     ["test-modprobe/module-from-abspath/home/foo/"]="mod-simple.ko"
     ["test-modprobe/module-from-relpath/home/foo/"]="mod-simple.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
-    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-outdir$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
@@ -90,20 +103,20 @@ map=(
     ["test-modinfo/mod-simple-sha256.ko"]="mod-simple.ko"
     ["test-modinfo/mod-simple-pkcs7.ko"]="mod-simple.ko"
     ["test-modinfo/external/lib/modules/external/mod-simple.ko"]="mod-simple.ko"
-    ["test-tools/insert/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
-    ["test-tools/remove/lib/modules/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/insert$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
+    ["test-tools/remove$MODULE_DIRECTORY/4.4.4/kernel/"]="mod-simple.ko"
 )
 
 gzip_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/block/cciss.ko"
     )
 
 xz_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/scsi_mod.ko"
     )
 
 zstd_array=(
-    "test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"
+    "test-depmod/modules-order-compressed$MODULE_DIRECTORY/4.4.4/kernel/drivers/scsi/hpsa.ko"
     )
 
 attach_sha256_array=(
diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
index 93606947f18a..c96dbf0a62be 100644
--- a/testsuite/test-depmod.c
+++ b/testsuite/test-depmod.c
@@ -27,7 +27,7 @@
 
 #define MODULES_UNAME "4.4.4"
 #define MODULES_ORDER_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-order-compressed"
-#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_ORDER_LIB_MODULES MODULES_ORDER_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_modules_order_for_compressed(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -55,8 +55,8 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
 	});
 
 #define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
-#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_UNAME
-#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir" MODULE_DIRECTORY "/" MODULES_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_modules_outdir(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -87,7 +87,7 @@ DEFINE_TEST(depmod_modules_outdir,
 	});
 
 #define SEARCH_ORDER_SIMPLE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-simple"
-#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SIMPLE_LIB_MODULES SEARCH_ORDER_SIMPLE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_simple(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -114,7 +114,7 @@ DEFINE_TEST(depmod_search_order_simple,
 	});
 
 #define SEARCH_ORDER_SAME_PREFIX_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-same-prefix"
-#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_SAME_PREFIX_LIB_MODULES SEARCH_ORDER_SAME_PREFIX_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_same_prefix(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -164,7 +164,7 @@ DEFINE_TEST(depmod_detect_loop,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-first"
-#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_FIRST_LIB_MODULES SEARCH_ORDER_EXTERNAL_FIRST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_external_first(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -191,7 +191,7 @@ DEFINE_TEST(depmod_search_order_external_first,
 	});
 
 #define SEARCH_ORDER_EXTERNAL_LAST_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-external-last"
-#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_EXTERNAL_LAST_LIB_MODULES SEARCH_ORDER_EXTERNAL_LAST_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_external_last(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
@@ -218,7 +218,7 @@ DEFINE_TEST(depmod_search_order_external_last,
 	});
 
 #define SEARCH_ORDER_OVERRIDE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-override"
-#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS "/lib/modules/" MODULES_UNAME
+#define SEARCH_ORDER_OVERRIDE_LIB_MODULES SEARCH_ORDER_OVERRIDE_ROOTFS MODULE_DIRECTORY "/" MODULES_UNAME
 static noreturn int depmod_search_order_override(const struct test *t)
 {
 	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c
index 56e73609f204..c77c4bbc04eb 100644
--- a/testsuite/test-testsuite.c
+++ b/testsuite/test-testsuite.c
@@ -64,7 +64,7 @@ static int testsuite_rootfs_fopen(const struct test *t)
 	char s[100];
 	int n;
 
-	fp = fopen("/lib/modules/a", "r");
+	fp = fopen(MODULE_DIRECTORY "/a", "r");
 	if (fp == NULL)
 		return EXIT_FAILURE;;
 
@@ -89,7 +89,7 @@ static int testsuite_rootfs_open(const struct test *t)
 	char buf[100];
 	int fd, done;
 
-	fd = open("/lib/modules/a", O_RDONLY);
+	fd = open(MODULE_DIRECTORY "/a", O_RDONLY);
 	if (fd < 0)
 		return EXIT_FAILURE;
 
@@ -121,12 +121,12 @@ static int testsuite_rootfs_stat_access(const struct test *t)
 {
 	struct stat st;
 
-	if (access("/lib/modules/a", F_OK) < 0) {
+	if (access(MODULE_DIRECTORY "/a", F_OK) < 0) {
 		ERR("access failed: %m\n");
 		return EXIT_FAILURE;
 	}
 
-	if (stat("/lib/modules/a", &st) < 0) {
+	if (stat(MODULE_DIRECTORY "/a", &st) < 0) {
 		ERR("stat failed: %m\n");
 		return EXIT_FAILURE;
 	}
diff --git a/tools/depmod.c b/tools/depmod.c
index ab8513b21526..43fc354afb09 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -911,7 +911,7 @@ struct vertex;
 struct mod {
 	struct kmod_module *kmod;
 	char *path;
-	const char *relpath; /* path relative to '$ROOT/lib/modules/$VER/' */
+	const char *relpath; /* path relative to '$ROOT$MODULE_DIRECTORY/$VER/' */
 	char *uncrelpath; /* same as relpath but ending in .ko */
 	struct kmod_list *info_list;
 	struct kmod_list *dep_sym_list;
@@ -3024,11 +3024,11 @@ static int do_depmod(int argc, char *argv[])
 	}
 
 	cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
-				  "%s/lib/modules/%s",
+				  "%s" MODULE_DIRECTORY "/%s",
 				  root ?: "", cfg.kversion);
 
 	cfg.outdirnamelen = snprintf(cfg.outdirname, PATH_MAX,
-				     "%s/lib/modules/%s",
+				     "%s" MODULE_DIRECTORY "/%s",
 				     out_root ?: (root ?: ""), cfg.kversion);
 
 	if (optind == argc)
diff --git a/tools/kmod.pc.in b/tools/kmod.pc.in
index 2595980a6b35..97215c8ed48f 100644
--- a/tools/kmod.pc.in
+++ b/tools/kmod.pc.in
@@ -1,6 +1,7 @@
 prefix=@prefix@
 sysconfdir=@sysconfdir@
 distconfdir=@distconfdir@
+module_directory=@module_directory@
 module_compressions=@module_compressions@
 module_signatures=@module_signatures@
 
diff --git a/tools/modinfo.c b/tools/modinfo.c
index d0aab200af4e..cacc32dc4c40 100644
--- a/tools/modinfo.c
+++ b/tools/modinfo.c
@@ -367,7 +367,7 @@ static void help(void)
 		"\t-m, --modname               Handle argument as module name instead of alias or filename\n"
 		"\t-F, --field=FIELD           Print only provided FIELD\n"
 		"\t-k, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
-		"\t-b, --basedir=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-b, --basedir=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
 		"\t-V, --version               Show version\n"
 		"\t-h, --help                  Show this help\n",
 		program_invocation_short_name);
@@ -462,7 +462,7 @@ static int do_modinfo(int argc, char *argv[])
 			}
 			kversion = u.release;
 		}
-		snprintf(dirname_buf, sizeof(dirname_buf), "%s/lib/modules/%s",
+		snprintf(dirname_buf, sizeof(dirname_buf), "%s" MODULE_DIRECTORY "/%s",
 			 root, kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/modprobe.c b/tools/modprobe.c
index e891028349a8..5306bef250da 100644
--- a/tools/modprobe.c
+++ b/tools/modprobe.c
@@ -142,7 +142,7 @@ static void help(void)
 		"\t-n, --show                  Same as --dry-run\n"
 
 		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
-		"\t-d, --dirname=DIR           Use DIR as filesystem root for /lib/modules\n"
+		"\t-d, --dirname=DIR           Use DIR as filesystem root for " MODULE_DIRECTORY "\n"
 		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"
 
 		"\t-s, --syslog                print to syslog, not stderr\n"
@@ -999,7 +999,7 @@ static int do_modprobe(int argc, char **orig_argv)
 			kversion = u.release;
 		}
 		snprintf(dirname_buf, sizeof(dirname_buf),
-				"%s/lib/modules/%s", root,
+				"%s" MODULE_DIRECTORY "/%s", root,
 				kversion);
 		dirname = dirname_buf;
 	}
diff --git a/tools/static-nodes.c b/tools/static-nodes.c
index 8d2356da73f3..5ef3743e967b 100644
--- a/tools/static-nodes.c
+++ b/tools/static-nodes.c
@@ -212,15 +212,15 @@ static int do_static_nodes(int argc, char *argv[])
 		goto finish;
 	}
 
-	snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
+	snprintf(modules, sizeof(modules), MODULE_DIRECTORY "/%s/modules.devname", kernel.release);
 	in = fopen(modules, "re");
 	if (in == NULL) {
 		if (errno == ENOENT) {
-			fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
+			fprintf(stderr, "Warning: " MODULE_DIRECTORY "/%s/modules.devname not found - ignoring\n",
 				kernel.release);
 			ret = EXIT_SUCCESS;
 		} else {
-			fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
+			fprintf(stderr, "Error: could not open " MODULE_DIRECTORY "/%s/modules.devname - %m\n",
 				kernel.release);
 			ret = EXIT_FAILURE;
 		}
-- 
2.42.0



^ permalink raw reply related	[flat|nested] 83+ messages in thread

* [PATCH 2/2] configure: Check that provided paths are absolute
  2023-11-10 12:13         ` [PATCH 0/2] kmod /usr support Michal Suchanek
  2023-11-10 12:13           ` [PATCH 1/2] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
@ 2023-11-10 12:13           ` Michal Suchanek
  2023-12-06 18:36           ` [PATCH 0/2] kmod /usr support Lucas De Marchi
  2 siblings, 0 replies; 83+ messages in thread
From: Michal Suchanek @ 2023-11-10 12:13 UTC (permalink / raw)
  To: linux-modules, Lucas De Marchi
  Cc: Michal Suchanek, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

configure checks that its built-in directory options get an absolute
path. Copy the check for custom options.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v6: new patch
---
 configure.ac | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/configure.ac b/configure.ac
index d6da5ee9ae9a..de01e08cf2e8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -97,6 +97,23 @@ AC_ARG_WITH([module_directory],
         [], [with_module_directory=/lib/modules])
 AC_SUBST([module_directory], [$with_module_directory])
 
+# Check all directory arguments for consistency.
+for ac_var in	distconfdir rootlibdir module_directory
+do
+  eval ac_val=\$$ac_var
+  # Remove trailing slashes.
+  case $ac_val in
+    */ )
+      ac_val=`expr "X$ac_val" : 'X\(.*@<:@^/@:>@\)' \| "X$ac_val" : 'X\(.*\)'`
+      eval $ac_var=\$ac_val;;
+  esac
+  # Be sure to have absolute directory names.
+  case $ac_val in
+    @<:@\\/$@:>@* | ?:@<:@\\/@:>@* )  continue;;
+  esac
+  as_fn_error $? "expected an absolute directory name for --$ac_var: $ac_val"
+done
+
 AC_ARG_WITH([zstd],
 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
 	[], [with_zstd=no])
-- 
2.42.0



^ permalink raw reply related	[flat|nested] 83+ messages in thread

* Re: [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable
  2023-10-17 17:50       ` Lucas De Marchi
                           ` (2 preceding siblings ...)
  2023-11-10 12:13         ` [PATCH 0/2] kmod /usr support Michal Suchanek
@ 2023-11-13  9:27         ` Michal Suchánek
  3 siblings, 0 replies; 83+ messages in thread
From: Michal Suchánek @ 2023-11-13  9:27 UTC (permalink / raw)
  To: Lucas De Marchi
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Tue, Oct 17, 2023 at 12:50:15PM -0500, Lucas De Marchi wrote:
> On Tue, Jul 18, 2023 at 02:01:56PM +0200, Michal Suchanek wrote:
> > Now that modprobe.d is searched under ${prefix}/lib, allow a complete
> > transition to files only under ${prefix} by adding a ${module_directory}
> > configuration. This specifies the directory where to search for kernel
> > modules and should match the location where the kernel/distro installs
> > them.
> > 
> > With this distributions that do not want to ship files in /lib can also
> > move kernel modules to /usr while others can keep them in /lib.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v4: Make the whole path configurable
> > v5: More verbose commit message
> > ---
> > Makefile.am                          |   3 +-
> > configure.ac                         |   7 ++
> > libkmod/libkmod.c                    |   4 +-
> > man/Makefile.am                      |   1 +
> > man/depmod.d.xml                     |   6 +-
> > man/depmod.xml                       |   4 +-
> > man/modinfo.xml                      |   2 +-
> > man/modprobe.xml                     |   2 +-
> > man/modules.dep.xml                  |   6 +-
> > testsuite/module-playground/Makefile |   2 +-
> > testsuite/setup-rootfs.sh            | 109 +++++++++++++++------------
> > testsuite/test-depmod.c              |  16 ++--
> > testsuite/test-testsuite.c           |   8 +-
> > tools/depmod.c                       |   6 +-
> > tools/kmod.pc.in                     |   1 +
> > tools/modinfo.c                      |   4 +-
> > tools/modprobe.c                     |   4 +-
> > tools/static-nodes.c                 |   6 +-
> > 18 files changed, 107 insertions(+), 84 deletions(-)
> > 
> > diff --git a/Makefile.am b/Makefile.am
> > index 6d0b2decfef3..019aa749fdf1 100644
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > @@ -20,6 +20,7 @@ AM_CPPFLAGS = \
> > 	-I$(top_srcdir) \
> > 	-DSYSCONFDIR=\""$(sysconfdir)"\" \
> > 	-DDISTCONFDIR=\""$(distconfdir)"\" \
> > +	-DMODULE_DIRECTORY=\""$(module_directory)"\" \
> > 	${zlib_CFLAGS}
> > 
> > AM_CFLAGS = $(OUR_CFLAGS)
> > @@ -220,7 +221,7 @@ EXTRA_DIST += testsuite/setup-rootfs.sh
> > MODULE_PLAYGROUND = testsuite/module-playground
> > ROOTFS = testsuite/rootfs
> > ROOTFS_PRISTINE = $(top_srcdir)/testsuite/rootfs-pristine
> > -CREATE_ROOTFS = $(AM_V_GEN) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
> > +CREATE_ROOTFS = $(AM_V_GEN) MODULE_DIRECTORY=$(module_directory) $(top_srcdir)/testsuite/setup-rootfs.sh $(ROOTFS_PRISTINE) $(ROOTFS) $(MODULE_PLAYGROUND) $(top_builddir)/config.h $(sysconfdir)
> > 
> > build-module-playground:
> > 	$(AM_V_GEN)if test "$(top_srcdir)" != "$(top_builddir)"; then \
> > diff --git a/configure.ac b/configure.ac
> > index b4584d6cdc67..4051dc9249e2 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -91,6 +91,12 @@ AC_ARG_WITH([rootlibdir],
> >         [], [with_rootlibdir=$libdir])
> > AC_SUBST([rootlibdir], [$with_rootlibdir])
> > 
> > +# Ideally this would be $prefix/lib/modules but default to /lib/modules for compatibility with earlier versions
> > +AC_ARG_WITH([module_directory],
> > +        AS_HELP_STRING([--with-module-directory=DIR], [directory in which to look for kernel modules - typically '/lib/modules' or '${prefix}/lib/modules']),
> > +        [], [with_module_directory=/lib/modules])
> > +AC_SUBST([module_directory], [$with_module_directory])
> 
> we will probably have "fun" results if we accept a relative path here.
> 
> > +
> > AC_ARG_WITH([zstd],
> > 	AS_HELP_STRING([--with-zstd], [handle Zstandard-compressed modules @<:@default=disabled@:>@]),
> > 	[], [with_zstd=no])
> > @@ -326,6 +332,7 @@ AC_MSG_RESULT([
> > 	$PACKAGE $VERSION
> > 	=======
> > 
> > +	module_directory:	${module_directory}
> > 	prefix:			${prefix}
> > 	sysconfdir:		${sysconfdir}
> > 	distconfdir:		${distconfdir}
> > diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
> > index 09e6041461b0..63719e886de8 100644
> > --- a/libkmod/libkmod.c
> > +++ b/libkmod/libkmod.c
> > @@ -209,7 +209,7 @@ static int log_priority(const char *priority)
> > 	return 0;
> > }
> > 
> > -static const char *dirname_default_prefix = "/lib/modules";
> > +static const char *dirname_default_prefix = MODULE_DIRECTORY;
> > 
> > static char *get_kernel_release(const char *dirname)
> > {
> > @@ -231,7 +231,7 @@ static char *get_kernel_release(const char *dirname)
> > /**
> >  * kmod_new:
> >  * @dirname: what to consider as linux module's directory, if NULL
> > - *           defaults to /lib/modules/`uname -r`. If it's relative,
> > + *           defaults to ${module_prefix}/lib/modules/`uname -r`. If it's relative,
> 
> module_prefix?  did you mean to use $MODULE_DIRECTORY/`uname -r`?
> 
> >  *           it's treated as relative to the current working directory.
> >  *           Otherwise, give an absolute dirname.
> >  * @config_paths: ordered array of paths (directories or files) where
> > diff --git a/man/Makefile.am b/man/Makefile.am
> > index 2fea8e46bf2f..f550091a216a 100644
> > --- a/man/Makefile.am
> > +++ b/man/Makefile.am
> > @@ -22,6 +22,7 @@ CLEANFILES = $(dist_man_MANS)
> > 	else \
> > 		sed -e '/@DISTCONFDIR@/d' $< ; \
> > 	fi | \
> > +	sed -e 's|@MODULE_DIRECTORY@|$(module_directory)|g' | \
> > 	$(XSLT) \
> > 		-o $@ \
> > 		--nonet \
> > diff --git a/man/depmod.d.xml b/man/depmod.d.xml
> > index f282a39cc840..b07e6a2bd4fe 100644
> > --- a/man/depmod.d.xml
> > +++ b/man/depmod.d.xml
> > @@ -70,7 +70,7 @@
> >         </term>
> >         <listitem>
> >           <para>
> > -            This allows you to specify the order in which /lib/modules
> > +            This allows you to specify the order in which @MODULE_DIRECTORY@
> >             (or other configured module location) subdirectories will
> >             be processed by <command>depmod</command>. Directories are
> >             listed in order, with the highest priority given to the
> > @@ -101,7 +101,7 @@
> >             <command>depmod</command> command. It is possible to
> >             specify one kernel or all kernels using the * wildcard.
> >             <replaceable>modulesubdirectory</replaceable> is the
> > -            name of the subdirectory under /lib/modules (or other
> > +            name of the subdirectory under @MODULE_DIRECTORY@ (or other
> >             module location) where the target module is installed.
> >           </para>
> >           <para>
> > @@ -110,7 +110,7 @@
> >             specifying the following command: "override kmod * extra".
> >             This will ensure that any matching module name installed
> >             under the <command>extra</command> subdirectory within
> > -            /lib/modules (or other module location) will take priority
> > +            @MODULE_DIRECTORY@ (or other module location) will take priority
> >             over any likenamed module already provided by the kernel.
> >           </para>
> >         </listitem>
> > diff --git a/man/depmod.xml b/man/depmod.xml
> > index 3b0097184fd7..fce2a4a67a89 100644
> > --- a/man/depmod.xml
> > +++ b/man/depmod.xml
> > @@ -80,7 +80,7 @@
> >     </para>
> >     <para> <command>depmod</command> creates a list of module dependencies by
> >       reading each module under
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable> and
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable> and
> >       determining what symbols it exports and what symbols it needs.  By
> >       default, this list is written to <filename>modules.dep</filename>, and a
> >       binary hashed version named <filename>modules.dep.bin</filename>, in the
> > @@ -141,7 +141,7 @@
> >         <listitem>
> >           <para>
> >             If your modules are not currently in the (normal) directory
> > -            <filename>/lib/modules/</filename><replaceable>version</replaceable>,
> > +            <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>,
> >             but in a staging area, you can specify a
> >             <replaceable>basedir</replaceable> which is prepended to the
> >             directory name.  This <replaceable>basedir</replaceable> is
> > diff --git a/man/modinfo.xml b/man/modinfo.xml
> > index 9fe0324a2527..b6c4d6045829 100644
> > --- a/man/modinfo.xml
> > +++ b/man/modinfo.xml
> > @@ -54,7 +54,7 @@
> >       <command>modinfo</command> extracts information from the Linux Kernel
> >       modules given on the command line.  If the module name is not a filename,
> >       then the
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable>
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>
> >       directory is searched, as is also done by
> >       <citerefentry><refentrytitle>modprobe</refentrytitle><manvolnum>8</manvolnum></citerefentry>
> >       when loading kernel modules.
> > diff --git a/man/modprobe.xml b/man/modprobe.xml
> > index 91f9e27997cd..4d1fd59c000b 100644
> > --- a/man/modprobe.xml
> > +++ b/man/modprobe.xml
> > @@ -78,7 +78,7 @@
> >       is no difference between _ and - in module names (automatic
> >       underscore conversion is performed).
> >       <command>modprobe</command> looks in the module directory
> > -      <filename>/lib/modules/`uname -r`</filename> for all
> > +      <filename>@MODULE_DIRECTORY@/`uname -r`</filename> for all
> >       the modules and other files, except for the optional
> >       configuration files in the
> >       <filename>/etc/modprobe.d</filename> directory
> > diff --git a/man/modules.dep.xml b/man/modules.dep.xml
> > index ed633694ec9e..8ef6d8b3536e 100644
> > --- a/man/modules.dep.xml
> > +++ b/man/modules.dep.xml
> > @@ -34,8 +34,8 @@
> >   </refnamediv>
> > 
> >   <refsynopsisdiv>
> > -    <para><filename>/lib/modules/modules.dep</filename></para>
> > -    <para><filename>/lib/modules/modules.dep.bin</filename></para>
> > +    <para><filename>@MODULE_DIRECTORY@/modules.dep</filename></para>
> > +    <para><filename>@MODULE_DIRECTORY@/modules.dep.bin</filename></para>
> >   </refsynopsisdiv>
> > 
> >   <refsect1><title>DESCRIPTION</title>
> > @@ -43,7 +43,7 @@
> >       <filename>modules.dep.bin</filename> is a binary file generated by
> >       <command>depmod</command> listing the dependencies for
> >       every module in the directories under
> > -      <filename>/lib/modules/</filename><replaceable>version</replaceable>.
> > +      <filename>@MODULE_DIRECTORY@/</filename><replaceable>version</replaceable>.
> >       It is used by kmod tools such as <command>modprobe</command> and
> >       libkmod.
> >     </para>
> > diff --git a/testsuite/module-playground/Makefile b/testsuite/module-playground/Makefile
> > index e6045b0dd932..a7ab09bea2bf 100644
> > --- a/testsuite/module-playground/Makefile
> > +++ b/testsuite/module-playground/Makefile
> > @@ -47,7 +47,7 @@ endif
> > 
> > else
> > # normal makefile
> > -KDIR ?= /lib/modules/`uname -r`/build
> > +KDIR ?= $(module_prefix)/lib/modules/`uname -r`/build
> > KVER ?= `uname -r`
> > ifeq ($(FAKE_BUILD),)
> >     FAKE_BUILD=0
> > diff --git a/testsuite/setup-rootfs.sh b/testsuite/setup-rootfs.sh
> > index 4440ddcd6b4d..a780f9381b3c 100755
> > --- a/testsuite/setup-rootfs.sh
> > +++ b/testsuite/setup-rootfs.sh
> > @@ -16,6 +16,19 @@ create_rootfs() {
> > 	cp -r "$ROOTFS_PRISTINE" "$ROOTFS"
> > 	find "$ROOTFS" -type d -exec chmod +w {} \;
> > 	find "$ROOTFS" -type f -name .gitignore -exec rm -f {} \;
> > +	if [ "$MODULE_DIRECTORY" != "/lib/modules" ] ; then
> > +		sed -i -e "s|/lib/modules|$MODULE_DIRECTORY|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
> > +		sed -i -e "s|$MODULE_DIRECTORY/external|/lib/modules/external|g" $(find "$ROOTFS" -name \*.txt -o -name \*.conf -o -name \*.dep)
> > +		for i in "$ROOTFS"/*/lib/modules/* "$ROOTFS"/*/*/lib/modules/* ; do
> > +			version="$(basename $i)"
> > +			[ $version != 'external' ] || continue
> > +			mod="$(dirname $i)"
> > +			lib="$(dirname $mod)"
> > +			up="$(dirname $lib)$MODULE_DIRECTORY"
> > +			mkdir -p "$up"
> > +			mv "$i" "$up"
> > +		done
> > +	fi
> > 
> > 	if [ "$SYSCONFDIR" != "/etc" ]; then
> > 		find "$ROOTFS" -type d -name etc -printf "%h\n" | while read -r e; do
> > @@ -32,57 +45,57 @@ feature_enabled() {
> > 
> > declare -A map
> > map=(
> > -    ["test-depmod/search-order-simple/lib/modules/4.4.4/kernel/crypto/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-simple/lib/modules/4.4.4/updates/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foo/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-same-prefix/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
> > -    ["test-depmod/detect-loop/lib/modules/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
> > -    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foo/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-external-first/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/kernel/crypto/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-simple$MODULE_DIRECTORY/4.4.4/updates/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-same-prefix$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-a.ko"]="mod-loop-a.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-b.ko"]="mod-loop-b.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-c.ko"]="mod-loop-c.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-d.ko"]="mod-loop-d.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-e.ko"]="mod-loop-e.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-f.ko"]="mod-loop-f.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-g.ko"]="mod-loop-g.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-h.ko"]="mod-loop-h.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-i.ko"]="mod-loop-i.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-j.ko"]="mod-loop-j.ko"
> > +    ["test-depmod/detect-loop$MODULE_DIRECTORY/4.4.4/kernel/mod-loop-k.ko"]="mod-loop-k.ko"
> > +    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-external-first$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
> >     ["test-depmod/search-order-external-first/lib/modules/external/"]="mod-simple.ko"
> 
> why didn't you change it here?
> 
> > -    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foo/"]="mod-simple.ko"
> > -    ["test-depmod/search-order-external-last/lib/modules/4.4.4/foobar/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foo/"]="mod-simple.ko"
> > +    ["test-depmod/search-order-external-last$MODULE_DIRECTORY/4.4.4/foobar/"]="mod-simple.ko"
> >     ["test-depmod/search-order-external-last/lib/modules/external/"]="mod-simple.ko"
> 
> and here...

The path is embedded in binary files:

t grep '/lib/modules' | grep Binary
Binary file testsuite/rootfs-pristine/test-modinfo/external/lib/modules/4.4.4/modules.dep.bin matches
Binary file testsuite/rootfs-pristine/test-modprobe/external/lib/modules/4.4.4/modules.dep.bin matches
Binary file testsuite/rootfs-pristine/test-modprobe/module-from-abspath/lib/modules/4.4.4/modules.dep.bin matches
Binary file testsuite/rootfs-pristine/test-modprobe/module-from-relpath/lib/modules/4.4.4/modules.dep.bin matches

The reason is that path to 'external' modules that are not
in $MODULE_DIRECTORY/$(KERNELRELEASE) is recorded as absolute path.

The way these tests are designed the binary files cannot be changed.

To get the same file the non-'external' modules have to be moved to
match the new location of $MODULE_DIRECTORY which results in the same
path relative to $MODULE_DIRECTORY while the 'external' ones are not
moved getting the same absolute path regardless of $MODULE_DIRECTORY.

Thanks

Michal

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH 0/2] kmod /usr support
  2023-11-10 12:13         ` [PATCH 0/2] kmod /usr support Michal Suchanek
  2023-11-10 12:13           ` [PATCH 1/2] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
  2023-11-10 12:13           ` [PATCH 2/2] configure: Check that provided paths are absolute Michal Suchanek
@ 2023-12-06 18:36           ` Lucas De Marchi
  2023-12-19  8:37             ` Masahiro Yamada
  2 siblings, 1 reply; 83+ messages in thread
From: Lucas De Marchi @ 2023-12-06 18:36 UTC (permalink / raw)
  To: Michal Suchanek
  Cc: linux-modules, Takashi Iwai, Lucas De Marchi, Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Masahiro Yamada, Nathan Chancellor,
	Nick Desaulniers, Nicolas Schier, linux-kbuild, linux-kernel

On Fri, Nov 10, 2023 at 01:13:53PM +0100, Michal Suchanek wrote:
>Hello,
>
>This is resend of the last patch in the series that adds prefix support
>to kernel module location together with additional patch for validating
>the user supplied input to options that are interpreted as directories.
>
>Thanks

applied, thanks

Lucas De Marchi

^ permalink raw reply	[flat|nested] 83+ messages in thread

* Re: [PATCH 0/2] kmod /usr support
  2023-12-06 18:36           ` [PATCH 0/2] kmod /usr support Lucas De Marchi
@ 2023-12-19  8:37             ` Masahiro Yamada
  0 siblings, 0 replies; 83+ messages in thread
From: Masahiro Yamada @ 2023-12-19  8:37 UTC (permalink / raw)
  To: Lucas De Marchi
  Cc: Michal Suchanek, linux-modules, Takashi Iwai, Lucas De Marchi,
	Michal Koutný,
	Jiri Slaby, Jan Engelhardt, Nathan Chancellor, Nick Desaulniers,
	Nicolas Schier, linux-kbuild, linux-kernel

On Thu, Dec 7, 2023 at 3:37 AM Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>
> On Fri, Nov 10, 2023 at 01:13:53PM +0100, Michal Suchanek wrote:
> >Hello,
> >
> >This is resend of the last patch in the series that adds prefix support
> >to kernel module location together with additional patch for validating
> >the user supplied input to options that are interpreted as directories.
> >
> >Thanks
>
> applied, thanks
>
> Lucas De Marchi



If I understood this correctly, MODULE_DIRECTORY is determined
by "configure --with-module-directory=...", and there is no
way to change it after that.


If so, how to work with cross-building?

Cross-building is typical when building embedded Linux systems.


Consider this scenario:

- Your build machine adopts
    MODULE_DIRECTORY=/usr/lib/modules
- The target embedded system adopts
    MODULE_DIRECTORY=/lib/modules

(or vice a versa)




depmod is used also for cross-building because
it is executed as a part of "make module_install".


The counterpart patch set for Kbuild provides
KERNEL_MODULE_DIRECTORY, which only changes
the destination directory to which *.ko are copied.

You cannot change the directory where the
depmod searches for modules, as it is fixed
at the compile-time of kmod.




In this case, what we can do is to build another
instance of kmod configured for the target system,
and use it for modules_install:

1. In the kmod source directory
    ./configure --with=module-directory=/lib/modules
    make

2. make modules_install INSTALL_MOD_PATH=<staging-dir>
     KERNEL_MODULE_DIRECTORY=/lib/modules
     DEPMOD=<new-depmod-you-has-just-built>



If you use OpenEmbedded etc., this is what you do
because host tools are built from sources.

But, should it be required all the time?
Even when the target embedded system uses
busybox-based modprobe instead of kmod?



depmod provides --basedir option, which changes
the prefix part, but there is no way to override
the stem part, MODULE_DIRECTRY.

In the review of the counter patch set,
I am suggesting an option to override MODULE_DIRECTRY
(let's say --moduledir) at least for depmod.

(Perhaps modinfo too, as it also supports --basedir)



Then, we can change scripts/depmod.sh so that
Kbuild can propagate KERNEL_MODULE_DIRECTORY
to depmod.


if  <depmod supports --moduledir>; then
    set -- "$@"  --moduledir "${KERNEL_MODULE_DIRECTORY}"
fi



Does it make sense?




-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 83+ messages in thread

end of thread, other threads:[~2023-12-19  8:38 UTC | newest]

Thread overview: 83+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-11 15:31 [PATCH 0/4] kmod /usr support Michal Suchanek
2023-07-11 15:31 ` [PATCH 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
2023-07-11 15:31 ` [PATCH 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
2023-07-12  6:47   ` Jiri Slaby
2023-07-12  7:35     ` Michal Suchánek
2023-07-11 15:31 ` [PATCH 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
2023-07-11 15:31 ` [PATCH 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix} Michal Suchanek
2023-07-11 15:34 ` [PATCH] depmod: Handle installing modules under a prefix Michal Suchanek
2023-07-12  5:47   ` Jiri Slaby
2023-07-12  7:38     ` Michal Suchánek
2023-07-12 13:45     ` [PATCH v2--to=linux-modules@vger.kernel.org] " Michal Suchanek
2023-07-12 14:14       ` Masahiro Yamada
2023-07-12 16:15         ` Michal Suchánek
2023-07-14  6:25       ` [PATCH v2] " Jiri Slaby
2023-07-14 12:21         ` [PATCH v3] " Michal Suchanek
2023-07-14 13:38           ` Jan Engelhardt
2023-07-14 13:57             ` Michal Suchánek
2023-07-14 13:59             ` Michal Koutný
2023-07-14 14:05               ` Michal Suchánek
2023-07-14 14:05           ` Nicolas Schier
2023-07-14 14:30             ` Michal Suchánek
2023-07-14 14:42               ` Jan Engelhardt
2023-07-14 14:54               ` Nicolas Schier
2023-07-14 15:10                 ` Michal Suchánek
2023-07-14 19:37                   ` Nicolas Schier
2023-07-17  9:55                     ` Michal Suchánek
2023-07-17 19:14                   ` Masahiro Yamada
2023-07-18  8:52                     ` Michal Suchánek
2023-09-11 19:56                     ` [PATCH] kbuild: rpm-pkg: Fix build with non-default MODLIB Michal Suchanek
2023-07-17 19:13                 ` [PATCH v3] depmod: Handle installing modules under a prefix Masahiro Yamada
2023-07-12 14:00 ` [PATCH kmod v2 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
2023-07-12 14:00 ` [PATCH kmod v2 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
2023-07-14 14:16   ` Nicolas Schier
2023-07-14 14:34     ` Michal Suchánek
2023-07-12 14:00 ` [PATCH kmod v2 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
2023-07-14 13:52   ` Jan Engelhardt
2023-07-14 14:02     ` Michal Suchánek
2023-07-14 14:12       ` Jan Engelhardt
2023-07-14 15:26   ` Nicolas Schier
2023-07-14 16:58     ` Michal Suchánek
2023-07-12 14:00 ` [PATCH kmod v2 4/4] libkmod, depmod, modprobe: Search for kernel modules under ${module_prefix} Michal Suchanek
2023-07-14 13:54   ` Jan Engelhardt
2023-07-17 10:39 ` [PATCH kmod v4 0/4] kmod /usr support Michal Suchanek
2023-07-17 10:39   ` [PATCH kmod v4 1/4] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
2023-07-17 10:39   ` [PATCH kmod v4 2/4] libkmod, depmod: Load modprobe.d, depmod.d from $prefix/lib Michal Suchanek
2023-07-17 10:39   ` [PATCH kmod v4 3/4] kmod: Add config command to show compile time configuration as JSON Michal Suchanek
2023-07-17 10:39   ` [PATCH kmod v4 4/4] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
2023-07-17 19:28     ` Jan Engelhardt
2023-07-18  8:43       ` Michal Suchánek
2023-07-18  9:41         ` Jan Engelhardt
2023-07-18 10:29           ` Michal Suchánek
2023-07-18 12:17             ` Jan Engelhardt
2023-07-18 12:27               ` Michal Suchánek
2023-07-18 12:42                 ` Jan Engelhardt
2023-07-18 13:45                   ` Michal Suchánek
2023-07-17 20:12     ` Lucas De Marchi
2023-07-18  8:32       ` Michal Suchánek
2023-07-17 10:40   ` [PATCH v4] depmod: Handle installing modules under a prefix Michal Suchanek
2023-07-18 12:01   ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchanek
2023-07-18 12:01     ` [PATCH kmod v5 1/5] configure: Detect openssl sm3 support Michal Suchanek
2023-07-18 12:01     ` [PATCH kmod v5 2/5] man/depmod.d: Fix incorrect /usr/lib search path Michal Suchanek
2023-07-18 12:01     ` [PATCH kmod v5 3/5] libkmod, depmod: Load modprobe.d, depmod.d from ${prefix}/lib Michal Suchanek
2023-07-18 12:01     ` [PATCH kmod v5 4/5] kmod: Add pkgconfig file with kmod compile time configuration Michal Suchanek
2023-07-18 12:01     ` [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
2023-10-17 17:50       ` Lucas De Marchi
2023-10-18  1:25         ` Jan Engelhardt
2023-11-09 17:40           ` Michal Suchánek
2023-11-09 17:44         ` Michal Suchánek
2023-11-10 12:13         ` [PATCH 0/2] kmod /usr support Michal Suchanek
2023-11-10 12:13           ` [PATCH 1/2] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchanek
2023-11-10 12:13           ` [PATCH 2/2] configure: Check that provided paths are absolute Michal Suchanek
2023-12-06 18:36           ` [PATCH 0/2] kmod /usr support Lucas De Marchi
2023-12-19  8:37             ` Masahiro Yamada
2023-11-13  9:27         ` [PATCH kmod v5 5/5] libkmod, depmod, modprobe: Make directory for kernel modules configurable Michal Suchánek
2023-07-18 12:03     ` [PATCH v5] depmod: Handle installing modules under a prefix Michal Suchanek
2023-07-18 12:14       ` Masahiro Yamada
2023-08-17 16:37     ` [PATCH kmod v5 0/5] kmod /usr support Michal Suchánek
2023-08-19 11:25       ` Masahiro Yamada
2023-08-21  6:22         ` Michal Suchánek
2023-09-11 19:50         ` Michal Suchánek
2023-10-17 15:45     ` Michal Suchánek
2023-10-17 16:18       ` Lucas De Marchi
2023-10-17 16:40         ` Michal Suchánek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).