1

I need to prepare an apt-package for stuff to enable building kernel modules for a custom Linux. I have cross-built on a different machine the kernel headers and modules using headers_install and modules_install make-targets. After copying the generated directories I'm still not able to build kernel modules on the target machine since /lib/modules/$(shell uname -r)/build is missing.

Here is my question. What are the minimal dependencies I need to include to my package in order to enable module builds (alongside with the generated kernel headers and modules mentioned above)?

Thanks in advance.

ababo
  • 1,490
  • 1
  • 10
  • 24
  • For long time `dkms` exists. https://en.wikipedia.org/wiki/Dynamic_Kernel_Module_Support – 0andriy Feb 11 '20 at 21:48
  • Yep, the question is how to prepare the minimal dependencies for building modules, in particular the `build` directory. – ababo Feb 12 '20 at 08:23
  • Perhaps this is helpful: https://stackoverflow.com/questions/19805244/while-building-kernel-modules-why-do-we-need-lib-modules/19808044#19808044 – dragosht Feb 13 '20 at 06:12
  • Distributions usually provide a package for that. – 0andriy Feb 14 '20 at 16:28

1 Answers1

1

After some experimenting I've got to a working solution:

#!/bin/bash

ARCH=arm

SRC_DIR=$1
MOD_DIR=$2

BUILD_DIR=$MOD_DIR/build

set -ex

cd $SRC_DIR
make modules_install INSTALL_HDR_PATH=$MOD_DIR
rm $MOD_DIR/{build,source}

mkdir $BUILD_DIR
cp $SRC_DIR/{.config,Makefile,System.map,Module.symvers} $BUILD_DIR

mkdir -p $BUILD_DIR/arch/$ARCH
cp $SRC_DIR/arch/$ARCH/Makefile $BUILD_DIR/arch/$ARCH/

cp -r $SRC_DIR/scripts $BUILD_DIR/

# Build a headers tree manually, because
# `make headers_install` doesn't put everything needed.
cp -r $SRC_DIR/include $BUILD_DIR/
cp -r $SRC_DIR/arch/$ARCH/include/* $BUILD_DIR/include/
cp -r $SRC_DIR/include/generated/* $BUILD_DIR/include/
cp -r $SRC_DIR/arch/$ARCH/include/generated/* $BUILD_DIR/include/
cp $SRC_DIR/include/linux/kconfig.h $BUILD_DIR/include/linux/

This script is fed with a path to a kernel source tree after the latter was built natively (not cross-platform).

ababo
  • 1,490
  • 1
  • 10
  • 24