Hostname: linuxdba
Kernel version: 2.6.18-274.3.1.el5
1. Hello.c
linuxdba --> cat hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello world.\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, Hello world.\n");
}
module_init(hello_init);
module_exit(hello_exit);
2. Build.(Download and install kernel source code)
2.1 Embed this code to some drivers, such as "uio" driver.
linuxdba --> pwd
/usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio
linuxdba --> ll
total 32
-rw-r--r-- 1 root root 301 Oct 12 20:21 hello.c
-rw-r--r-- 1 root root 444 Oct 12 20:02 Kconfig
-rw-r--r-- 1 root root 44 Oct 12 20:25 Makefile
-rw-r--r-- 1 root root 0 Oct 12 20:23 Module.markers
-rw-r--r-- 1 root root 358 Oct 12 20:23 Module.symvers
-rw-r--r-- 1 root root 15344 Oct 12 20:02 uio.c
linuxdba --> cat Makefile
obj-$(CONFIG_UIO) += uio.o
obj-m += hello.o
linuxdba --> cd ../../
linuxdba --> make modules SUBDIRS=$PWD/drivers/uio
WARNING: Symbol version dump /usr/src/redhat/BUILD/kernel-2.6.18/linux-
2.6.18.i386/Module.symvers
is missing; modules will have no dependencies and modversions.
CC [M] /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/uio.o
CC [M] /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/hello.o
Building modules, stage 2.
MODPOST
CC /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/hello.mod.o
LD [M] /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/hello.ko
CC /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/uio.mod.o
LD [M] /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/uio.ko
2.2 Create one source tree as one module.
linuxdba --> mkdir drivers/hello
linuxdba --> ll drivers/hello/
total 8
-rw-r--r-- 1 root root 301 Oct 12 21:01 hello.c
-rw-r--r-- 1 root root 17 Oct 12 21:01 Makefile
linuxdba --> cat drivers/hello/Makefile
obj-m += hello.o
linuxdba --> grep hello drivers/Makefile
obj-$(CONFIG_HELLO) += hello/ // Add this line to this file.
linuxdba --> make modules SUBDIRS=$PWD/drivers/hello
WARNING: Symbol version dump /usr/src/redhat/BUILD/kernel-2.6.18/linux-
2.6.18.i386/Module.symvers
is missing; modules will have no dependencies and modversions.
CC [M] /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/hello/hello.o
Building modules, stage 2.
MODPOST
CC /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/hello/hello.mod.o
LD [M] /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/hello/hello.ko
3. Install the driver.
linuxdba --> insmod drivers/hello/hello.ko
or
linuxdba --> insmod drivers/uio/hello.ko
4. Uninstall the driver.
linuxdba --> rmmod drivers/hello/hello.ko
or
linuxdba --> rmmod drivers/uio/hello.ko
You will get the desired informations from "/var/log/messages".