# How to build custom kernel modules

**URL:** <https://forums.balena.io/t/how-to-build-custom-kernel-modules/24098>\
**Category:** balenaOS\
**Created:** [August 13, 2019, 7:31am UTC](https://forums.balena.io/t/how-to-build-custom-kernel-modules/24098 "2019-08-13T07:31:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliasbits](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aliasbits](https://forums.balena.io/u/aliasbits)\
**Post date:** [August 13, 2019, 7:31am UTC](https://forums.balena.io/t/how-to-build-custom-kernel-modules/24098/1 "2019-08-13T07:31:55Z")

</div>

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](https://forums.balena.io/t/custom-resinos-and-os-update/3392/11)

```
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
```

---

<div class="post-metadata">

**Author:** ![spanceac](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@spanceac](https://forums.balena.io/u/spanceac)\
**Post date:** [August 13, 2019, 8:09am UTC](https://forums.balena.io/t/how-to-build-custom-kernel-modules/24098/2 "2019-08-13T08:09:15Z")

</div>

Hi @aliasbits,

This is great!

Thank you for your effort to document this!
