[v4] depmod: Handle installing modules under a prefix

Message ID 20230717104100.6146-1-msuchanek@suse.de
State New
Headers
Series [v4] depmod: Handle installing modules under a prefix |

Commit Message

Michal Suchánek July 17, 2023, 10:40 a.m. UTC
  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(-)
  

Patch

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