How to get started writing a kernel module

I'm very new in Openwrt-based development. I need to write a custom kernel module that will use Netfilter. I've previously written kernel modules for ubuntu. But I don't even know how to start for OpenWrt. I just wrote a basic Hello World-type kernel module and tried to compile it. But I couldn't do that, as build-essential files ain't there. And, so I got:

root@OpenWrt:~/nf# make
make -C /lib/modules/5.10.146/build M=/root/nf modules
make[1]: *** /lib/modules/5.10.146/build: No such file or directory.  Stop.
make: *** [Makefile:4: all] Error 2
root@OpenWrt:~/nf#

Is there any command equivalent to this command:

apt-get install build-essential linux-headers-`uname -r`

I'd really appreciate it if anyone helps me by saying what things I really need to study/try first to be able to write my kernel module for OpenWrt.
FYI, I using RPi 3 Model B.

Writing a linux kernel is the same for any distro as long as you consider compiling from source. The only difference might be the "build system" or especially for OpenWrt, the fact that most of the time there is cross-compiling involved.

Have a look at one of the most simple kernel drivers (in terms of Makefile and Source files) that is included in OpenWrt:https://github.com/openwrt/openwrt/tree/master/package/kernel/mt7621-qtn-rgmii

Or: patch whatever Kconfig and Makefile in the respective subsystem you want to have your kernel module and add all the source files to "files" in you target folder.

Sorry. Can you please tell me a few words on how can I set up the environment for kernel module development?

Start reading here: Install Buildsystem

Create your own working OpenWrt image for your target and take it from there. For debugging your code it’s almost a necessity to have serial acccess to your device and be familiar how to recover from a soft brick.

Since you said you were developing kernel modules before, but didn’t find the wiki linked, maybe familiarize yourself with the basics I just mentioned before attending to write (or port) other modules to “OpenWrt”.

Openwrt is not self hosting, all dev and compiling is done on a separate machine.

The wiki has detailed instructions on how to set up the buildroot cross compiling system on a Linux system

Thank you. I've set up the build system. And tried to make the kernel driver that you linked here but got this:

root@vision:~/openwrt/package/kernel/mt7621-qtn-rgmii# make
Makefile:17: /package.mk: No such file or directory
make: *** No rule to make target '/package.mk'.  Stop.
root@vision:~/openwrt/package/kernel/mt7621-qtn-rgmii#

Seems like an unlikely path...

What should fix it?

root@vision:# find ~/openwrt/ -name package.mk
/root/openwrt/include/package.mk


No!

You use the OpenWrt root directory and call:
make package/kernel/mt7621-qtn-rgmii/compile

But that wont work as that one depends on MT7621 arch

As I have set up the build system, I want to cross-compile a sample kernel module and load it into my physical device as an experiment. Is there any source like that?

You need to have a kernel built for at least that device arch first, what is the device you are using?

Raspberry Pi 3 Model B

Then it's easy, basically the same as building an image.
Just after that, you add a package and can compile whatever you want.
You can use the mt7621-qtn-rgmii as an example

Is every kernel module a package? Can't I write a kernel module and generate the .ko by cross-compilation and take the .ko file and load it to the real device?
FYI, my RPi device is running an image alredy.

In OpenWrt yes, the easiest way is to make it a package and then you can take its .ko as output or just install it as a package
Otherwise you would have to patch the kernel

1 Like

// You can use the mt7621-qtn-rgmii as an example//
Can you please tell why whenever I want to make it, it says:

root@vision:~/openwrt# make ./package/kernel/mt7621-qtn-rgmii
make: 'package/kernel/mt7621-qtn-rgmii' is up to date.
root@vision:~/openwrt#

Again, wrong!!

make package/kernel/mt7621-qtn-rgmii/compile V=s

1 Like

My bad. Thank you. But this time it says make[2]: Nothing to be done for 'compile'. the .ko file is not generated.

root@vision:~/openwrt# make package/kernel/mt7621-qtn-rgmii/compile V=s
make[2]: Entering directory '/root/openwrt/scripts/config'
make[2]: 'conf' is up to date.
make[2]: Leaving directory '/root/openwrt/scripts/config'
make[1]: Entering directory '/root/openwrt'
make[2]: Entering directory '/root/openwrt/package/kernel/mt7621-qtn-rgmii'
make[2]: Nothing to be done for 'compile'.
make[2]: Leaving directory '/root/openwrt/package/kernel/mt7621-qtn-rgmii'
time: package/kernel/mt7621-qtn-rgmii/compile#0.24#0.04#0.27
make[1]: Leaving directory '/root/openwrt'

Because it depends on MT7621 being the architecture, just use the package to copy/paste it and edit to suit your needs

1 Like

I've actually changed the the source code (the .c file) keeping the Makefile unchanged.