Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Thanks!

like image 384
pythoniku Avatar asked Oct 07 '12 04:10

pythoniku


2 Answers

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: http://archive.is/KNkEE (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.

like image 178
guru_florida Avatar answered Sep 30 '22 04:09

guru_florida


A complete and simplified Blog about Linux Kernels, Module Programming and writing simple device drivers for embedded devices.

xploredevicedrivers.blogspot.in

like image 39
Komal Thakkar Avatar answered Sep 30 '22 06:09

Komal Thakkar