# How to build an additional kernel module?

**URL:** <https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575>\
**Category:** For Developers\
**Created:** [February 29, 2020, 6:53pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575 "2020-02-29T18:53:34Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [February 29, 2020, 6:53pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/1 "2020-02-29T18:53:34Z")

</div>

I keep working with banana pi bpi-r2. Successfully launched a openwrt snapshot of it. I want to add a hw-nat kernel module. I have a patch that I put in target/linux/mediatek/patches-4.19 . I also created a file in package / kernel / linux / modules of the following content  
`
define KernelPackage/nat-hw-mtk
TITLE:= MediaTek's hardware NAT module
DEPENDS:=@TARGET_mediatek_mt7623
KCONFIG:= 
CONFIG_NET_MEDIATEK_HNAT=y
FILES:=$(LINUX_DIR)/drivers/net/ethernet/mediatek/mtk_hnat/mtkhnat.ko
AUTOLOAD:=$(call AutoLoad,90,mtkhnat)
endef`

define KernelPackage/nat-hw-mtk/description  
MediaTek's hardware NAT driver.  
endef

$(eval $(call KernelPackage,nat-hw-mtk))  
  
This added a menu item "kmod-nat-hw-mtk" to the kernel module configuration.

 ![Screenshot_20200229_204359](https://forum.openwrt.org/uploads/default/original/3X/e/7/e77babe9e7d8dc587a4fa1d4a989902c23262fca.png)  
run  
$ make target/linux/compile  
The patch is applied successfully. Everything is built without errors, but there is no file mkthnat.ko .  
What did I not do?

---

<div class="post-metadata">

**Author:** ![Pilot6](https://forum.openwrt.org/user_avatar/forum.openwrt.org/pilot6/32/4125_2.png) [@Pilot6](https://forum.openwrt.org/u/Pilot6)\
**Post date:** [February 29, 2020, 7:09pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/2 "2020-02-29T19:09:32Z")

</div>

> [@aquaforum](#):
>
> but there is no file mkthnat.ko

Where are you looking for this file? You should have `kmod-nat-hw-mtk.ipk` with that file in your `bin/packages`. The `mkthnat.ko` is inside.

It is actually `bin/target/xxxxxx/packages`

---

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [February 29, 2020, 7:22pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/3 "2020-02-29T19:22:51Z")

</div>

I'm looking for it in build\_dir/target-arm\_cortex-a7+neon-vfpv4\_musl\_eabi/linux-mediatek\_mt7623/linux-4.19.106/drivers/net/ethernet/mediatek/mtk\_hnat.  
I watched it work make target/linux/compile V=s  
There is no activity in this folder

---

<div class="post-metadata">

**Author:** ![Pilot6](https://forum.openwrt.org/user_avatar/forum.openwrt.org/pilot6/32/4125_2.png) [@Pilot6](https://forum.openwrt.org/u/Pilot6)\
**Post date:** [February 29, 2020, 7:28pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/5 "2020-02-29T19:28:02Z")

</div>

So did the ipk appear?

---

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [February 29, 2020, 7:29pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/6 "2020-02-29T19:29:29Z")

</div>

And so  
`
$ make package/kernel/linux/compile V=s /
make[1]: Entering directory '/var/opt/vanessa/router/bpi-r2/openwrt'
make[2]: Entering directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/libs/toolchain'
echo "libc" >> /var/opt/vanessa/router/bpi-r2/openwrt/staging_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/pkginfo/toolchain.default.install
echo "libgcc" >> /var/opt/vanessa/router/bpi-r2/openwrt/staging_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/pkginfo/toolchain.default.install
echo "libpthread" >> /var/opt/vanessa/router/bpi-r2/openwrt/staging_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/pkginfo/toolchain.default.install
make[2]: Leaving directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/libs/toolchain'
time: package/libs/toolchain/compile#0.13#0.03#0.16
make[2]: Entering directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/firmware/linux-firmware'
make[2]: Leaving directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/firmware/linux-firmware'
time: package/firmware/linux-firmware/compile#0.24#0.03#0.25
make[2]: Entering directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/firmware/prism54-firmware'
make[2]: Leaving directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/firmware/prism54-firmware'
time: package/firmware/prism54-firmware/compile#0.17#0.04#0.19
make[2]: Entering directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/kernel/linux'
ERROR: module '/var/opt/vanessa/router/bpi-r2/openwrt/build_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/linux-mediatek_mt7623/linux-4.19.106/drivers/net/ethernet/mediatek/mtk_hnat/mtkh
nat.ko' is missing.
make[2]: *** [modules/hwnat.mk:15: /var/opt/vanessa/router/bpi-r2/openwrt/bin/targets/mediatek/mt7623/packages/kmod-nat-hw-mtk_4.19.106-1_arm_cortex-a7_neon-vfpv4.ipk] Error 1
make[2]: Leaving directory '/var/opt/vanessa/router/bpi-r2/openwrt/package/kernel/linux'
time: package/kernel/linux/compile#2.26#0.16#2.41
make[1]: *** [package/Makefile:113: package/kernel/linux/compile] Error 2
make[1]: Leaving directory '/var/opt/vanessa/router/bpi-r2/openwrt'
make: *** [/var/opt/vanessa/router/bpi-r2/openwrt/include/toplevel.mk:227: package/kernel/linux/compile] Error 2
`  
The compiler bypasses it

---

<div class="post-metadata">

**Author:** ![anomeome](https://forum.openwrt.org/user_avatar/forum.openwrt.org/anomeome/32/15002_2.png) [@anomeome](https://forum.openwrt.org/u/anomeome)\
**Post date:** [February 29, 2020, 7:37pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/7 "2020-02-29T19:37:09Z")

</div>

Maybe try a clean first (check dependency issue)

```
make target/linux/{clean,compile} V=s

```

maybe add a _grep_ to look for something of interest.

---

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [February 29, 2020, 7:57pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/8 "2020-02-29T19:57:26Z")

</div>

So I did too, the make doesn't go in drivers/net/ethernet/mediatek/mtk\_hnat folder

---

<div class="post-metadata">

**Author:** ![dl12345](https://forum.openwrt.org/letter_avatar/dl12345/32/5_5575768a8748004e209b776fc1b2916d.png) [@dl12345](https://forum.openwrt.org/u/dl12345)\
**Post date:** [February 29, 2020, 10:08pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/9 "2020-02-29T22:08:56Z")

</div>

Doing a quick google, this driver seems to depend on a number of additional kernel symbols. Look at the KConfig in the patch you created to see which.

```
config NET_MEDIATEK_HNAT
	tristate "MediaTek MT7623 hardware NAT support"
	depends on NET_MEDIATEK_SOC && NF_CONNTRACK && NF_CONNTRACK_IPV4 && IP_NF_NAT && IP_NF_TARGET_MASQUERADE

```

These need to be added to your Makefile KCONFIG line or defined indirectly through the DEPENDS:= line if the relevant kmod- is part of the Openwrt packages so that it compiles the relevant dependent modules and defines the kernel symbols. So, if none of the dependent symbols are already packaged by other kmod- packages (which is not the case for the netfilter ones), the line would look thus:

```
  KCONFIG:=CONFIG_NET_MEDIATEK_HNAT=y \
     CONFIG_NET_MEDIATEK_SOC=y \
     CONFIG_NF_CONNTRACK=y \
     CONFIG_NF_CONNTRACK_IPV4=y
     CONFIG_IP_NF_NAT=y \
     CONFIG_IP_NF_TARGET_MASQUERADE=y

```

But, as I mentioned, some of those kernel symbols above are set by other kmod- packages. Most of the netfilter ones are set in openwrt/include/netfilter.mk, so you'd need to check yourself which ones and then put the associated kmods in the DEPENDS line (for example DEPENDS=+kmod\_nf\_conntrack) instead of explicitly putting those symbols in the KCONFIG line, as the kmod- packages will then build the required dependent kmods and define the kernel symbols.

---

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [February 29, 2020, 10:52pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/10 "2020-02-29T22:52:37Z")

</div>

I'm trying to move this patch from here [https://github.com/eliasmagn/bpir2wrt](https://github.com/eliasmagn/bpir2wrt)  
Had to modify it for work with the kernel 4.19. But the compiler bypasses it. In the kernel .config file by eliasmagn is present a line  
`
CONFIG_NET_MEDIATEK_HNAT=m
`  
what i am doing does not add such a line in .config file

---

<div class="post-metadata">

**Author:** ![dl12345](https://forum.openwrt.org/letter_avatar/dl12345/32/5_5575768a8748004e209b776fc1b2916d.png) [@dl12345](https://forum.openwrt.org/u/dl12345)\
**Post date:** [February 29, 2020, 11:46pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/11 "2020-02-29T23:46:01Z")

</div>

You're compiling it as a module. Go back and read what I wrote again. Have you actually tried this?

---

<div class="post-metadata">

**Author:** ![dl12345](https://forum.openwrt.org/letter_avatar/dl12345/32/5_5575768a8748004e209b776fc1b2916d.png) [@dl12345](https://forum.openwrt.org/u/dl12345)\
**Post date:** [March 1, 2020, 12:04am UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/12 "2020-03-01T00:04:46Z")

</div>

If you just want to get it to work, don't try and create a kmod package for it if it's causing you trouble. Just compile it directly into the kernel.

```
make kernel_menuconfig CONFIG_TARGET=subtarget

```

Toggle the option for that driver on, then remove your package Makefile and it should compile the patched driver directly into the kernel.

But frankly, just try what I suggested in the previous post before you do that.

---

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [March 1, 2020, 8:57am UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/13 "2020-03-01T08:57:19Z")

</div>

Yes, I tried - it didn't help

---

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [March 1, 2020, 9:48am UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/14 "2020-03-01T09:48:15Z")

</div>

This driver is not there ...  
`
make kernel_menuconfig CONFIG_TARGET=subtarget
`

 ![Screenshot_20200301_114554](https://forum.openwrt.org/uploads/default/original/3X/d/b/db88d1fc71098aa94d196e9f8944221d3bb98a13.png)

---

<div class="post-metadata">

**Author:** ![aquaforum](https://forum.openwrt.org/user_avatar/forum.openwrt.org/aquaforum/32/18347_2.png) [@aquaforum](https://forum.openwrt.org/u/aquaforum)\
**Post date:** [March 16, 2020, 6:10pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/15 "2020-03-16T18:10:21Z")

</div>

I worked on the problem again.  
Found the following: The scripts/kconfig/conf program removes a CONFIG\_NET\_MEDIATEK\_HNAT=m parameter from a .config file

---

<div class="post-metadata">

**Author:** ![user674574](https://forum.openwrt.org/user_avatar/forum.openwrt.org/user674574/32/24055_2.png) [@user674574](https://forum.openwrt.org/u/user674574)\
**Post date:** [August 24, 2020, 6:40pm UTC](https://forum.openwrt.org/t/how-to-build-an-additional-kernel-module/56575/16 "2020-08-24T18:40:46Z")

</div>

> [@dl12345](#):
>
> If you just want to get it to work, don't try and create a kmod package for it if it's causing you trouble. Just compile it directly into the kernel.
> 
> ```plaintext
> make kernel_menuconfig CONFIG_TARGET=subtarget
> 
> ```
> 
> Toggle the option for that driver on, then remove your package Makefile and it should compile the patched driver directly into the kernel.
> 
> But frankly, just try what I suggested in the previous post before you do that.

Im trying to learn how to do this but im not having success. Im new to both linux and openwrt but ive learned some over the past week.  
Im not sure how to do this though.

I want to add  
[https://www.realtek.com/en/component/zoo/category/network-interface-controllers-10-100-1000m-gigabit-ethernet-usb-3-0-software](https://www.realtek.com/en/component/zoo/category/network-interface-controllers-10-100-1000m-gigabit-ethernet-usb-3-0-software)  
into kernel. I have no clue if that works? Any pointers appriciated!  
Trying to get 2.5gbit to work

Thanks

EDIT: Would adding kmod-usb-net-rtl8152 with an asterisk, instead of a module make a difference when making the firmware?  
Installing kmod-usb-net-rtl8152 on 19.07.3 x86\_64 after the fact does not work with my 2.5gbit usb ethernet
