pythonikun pythonikun - 1 year ago 156
Linux Question

How to code a Linux kernel module?

I have couple years c programming experience. Now I decided to working towards Linux kernel module development. However, I can't even get start. I have compiled this code in ubuntu.

#include <linux/module.h>
int init_module(void){ printk("<1> hellp"); return 0;}
void cleanup_module(void){ printk("<1> bye");}

however, the insmod is not working the error message is "Invalid module format". after googling i figured it may be some problem with version compatibility. And there is no good way to solve it. So Can some real kernel module developer give me some advice? what environment should I prepare before I start learning?


Answer Source

You are missing the module_init() and module_exit() macros and some crucial #defines. We need more information as well such as your make/gcc options. It may be reporting the "invalid module format" because you are compiling in 32bit when your kernel is 64bit, so ensure you are using the -64 compile and link flag.

A great hello world tutorial for Kernel Modules is located here: (the original link to the article is broken).

Welcome to writing kernel modules. They are a lot of fun compared to writing Windows drivers (I've done both). The linux kernel module interface is much simpler to use and there is a lot of base drivers you can delegate to and get the work done faster.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download