How to build custom kernel modules

One-to-many kernels modules needs to be build in order support multiple balenaOS versions.
The below example is for usb, but can be changed to support other kernel modules.
Note: Not all modules can be loaded dynamically, see Custom ResinOS and OS update

FROM resin/intel-nuc-buildpack-deps:stretch AS kernelbuilder1

RUN apt-get update && apt-get install -y --no-install-recommends \
	    bc \
	&& rm -rf /var/lib/apt/lists/*

ENV KERNEL_MAJOR_VERSION=v4.x \
    KERNEL_VERSION=4.12.12 \
    RESIN_OS_VERSION=2.13.6%2Brev1.dev

# tutorial from here: https://wiki.archlinux.org/index.php/Compile_kernel_module
# ideas from here: https://github.com/resin-io-playground/kernel-module-build

RUN cd /usr/src \
    && wget https://mirrors.edge.kernel.org/pub/linux/kernel/${KERNEL_MAJOR_VERSION}/linux-${KERNEL_VERSION}.tar.gz \
    && tar xzf linux-${KERNEL_VERSION}.tar.gz \
    && wget https://files.resin.io/images/intel-nuc/${RESIN_OS_VERSION}/kernel_modules_headers.tar.gz \
    && tar xzf kernel_modules_headers.tar.gz

RUN cd /usr/src \
    && cd linux-${KERNEL_VERSION} \
    && cp ../kernel_modules_headers/.config ../kernel_modules_headers/Module.symvers . \
    && sed -i 's/# CONFIG_USB_XXX is not set/CONFIG_USB_XXX=m/g' .config \
    && make modules_prepare \
    && make M=drivers/usb \
    && make M=drivers/usb modules_install
1 Like

Hi @aliasbits,

This is great!

Thank you for your effort to document this!