Home > > Writing a Linux Kernel Module under 2.6

Writing a Linux Kernel Module under 2.6

September 8th, 2004 esteve

Everytime I want to try something inside the kernel, I patch it’s code and recompile it whole, but this is not the nicest way when implementing new functionalities. The best way to do so is to implement it as a kernel module (LKM). The usual skeleton for a 2.6 kernel module is as follows:

hello.c:

#include <linux/module.h> // Needed by all modules
#include <linux/kernel.h> // Needed for KERN_ALERT
#include <linux/init.h> // Needed for the macros

static int hello_init(void)
{
        printk(KERN_ALERT “Hello, world \n”);
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT “Goodbye, world\n”);
}

module_init(hello_init);
module_exit(hello_exit);

Makefile:

ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
            $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
endif

References:
Questions about loading modules.
Writing your own (2.6 kernel) device driver 101.
The Linux Kernel Module Programming Guide (for 2.4 Kernels).

Categories: Tags:
  1. Manu
    June 16th, 2005 at 20:58 | #1

    Hi Steve: I will try this today and report my progress tomorrow :o) Manu

  2. June 16th, 2005 at 21:00 | #2

    Cool! Let me know if you have any problems.

Comments are closed.