æ¬æè®°å½æç第ä¸ä¸ªLinux设å¤é©±å¨ç¨åºçç¼è¯è¿ç¨ãéå°é®é¢ç解å³æ¹æ³ã
ç¯å¢ï¼2.4.18-14çå
æ ¸ï¼Linuxå
æ ¸æºç ï¼2.4.18ã
Linuxå
æ ¸æºç è·¯å¾:/usr/src/linux(è¿ä¸ªæºç æ¯ä»kernel.orgç½ç«downloadç2.4.18çæ¬ï¼
æç
§ãlinux设å¤é©±å¨å¼å详解ãä¸ä¹¦ä¸çæ¥éª¤å®ç°ç»å
¸ä¾å"helloï¼world!"çä¾åã
å
·ä½æ¥éª¤å¦ä¸ï¼
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
1.æºç å¦ä¸ï¼
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the module ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
module_init(hello_init); /* load the module */
module_exit(hello_exit); /* unload the module */
è¿å
¥ç®å½ï¼
[root@Alex_linux /]#cd /work/jiakun_test/moduletest
[root@Alex_linux moduletest]# vi hello.c
ç¶åæ·å
¥ä¸é¢ä¹¦ä¸çæºç ã
2.ç¼è¯ä»£ç ï¼
1>.é¦å
æå¨2.4å
æ ¸çèææºä¸è¿è¡ç¼è¯ï¼ç¼è¯è¿ç¨å¦ä¸ï¼
[root@Alex_linux moduletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
å
¶ä¸-Ié项æå®å
æ²³æºç ï¼ä¹å°±æ¯å
æ ¸æºç æ è·¯å¾ãç¼è¯ç»æï¼
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `module_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `module_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
å¨ç½ä¸æ¥è¯¢æç½åæ示没æå¼å
¥kernel.h
解å³ï¼vi hello.c
å¨ç¬¬ä¸è¡å å
¥ï¼#include <linux/kernel.h>
å次ç¼è¯ä»ç¶æ¥KERN_ALERT没æ声æ
ä¿®æ¹ç¼è¯æ¡ä»¶-Iï¼å次ç¼è¯ï¼
[root@Alex_linux moduletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moduletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moduletest]#
2>.æ¥çæå°è¯å¨2.6å
æ ¸çèææºä¸è¿è¡ç¼è¯
ç¼è¯è¿ç¨å¦ä¸ï¼
[root@JiaKun moduletest]# ls
hello.c makefile
[root@JiaKun moduletest]# vi hello.c
[root@JiaKun moduletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moduletest modules
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [modules] Error 2
[root@JiaKun moduletest]# vi makefile
[root@JiaKun moduletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moduletest modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moduletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moduletest/Makefile'. Stop.
make[1]: *** [_module_/home/alex/test/moduletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [modules] Error 2
[root@JiaKun moduletest]# mv makefile Makefile
[root@JiaKun moduletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moduletest modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moduletest/hello.o
Building modules, stage 2.
MODPOST
CC /home/alex/test/moduletest/hello.mod.o
LD [M] /home/alex/test/moduletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moduletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Module.symvers
3.æ§è¡ä»£ç ï¼å 载驱å¨æ¨¡åï¼
2.4å
æ ¸å 载模åï¼
insmod ./hello.o
ä½æ¯æ¤æ¶å¹¶æ²¡æè¾åºprintkæå°çä¿¡æ¯ãä½æ¯å¯ä»¥å¨/var/log/messages ä¸çå°æå°çä¿¡æ¯ï¼è¿æ¯ç±äºKERN_ALERTä¼å
级ä¸å¤é«ãè¿é
éè¦ä¿®æ¹ä¸ºï¼KERN_EMERGãå次ç¼è¯ï¼å 载模åå³å¯ä»¥çå°ç»æ
2.6å
æ ¸å 载模åï¼
[root@JiaKun moduletest]# insmod hello.ko
[root@JiaKun moduletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
æçæåå¯è½ä¼åºç°insmodå½ä»¤æ¾ä¸å°çé误ï¼è¿å¯è½æä¸é¢å 个åå ï¼
<1> ä½ çç³»ç»æ²¡æå®è£
module-init-toolså·¥å
·ï¼å
³äºæ¤é®é¢ï¼åªéå®è£
å³å¯ï¼ä½æ¯ä¸è¬è£
å®ç³»ç»æ¯æè¿ä¸ªå½ä»¤çã
<2> ç¯å¢åé没ææ·»å 导è´ä¸è½ä½¿ç¨è¯¥å½ä»¤ã使ç¨echo $PATHå³å¯æ¥çPATHç¯å¢åéï¼åç°æ²¡æ/sbinè¿ä¸ªè·¯å¾ï¼æä»¥ä½ å½ç¶ä¸è½ä½¿ç¨insmodè¿ä¸ªå½ä»¤äºã解å³çæ¹æ³å¾ç®åï¼åªéå¨å½ä»¤è¡è¾å
¥ï¼
PATH = "$PATH:/sbin"å³å¯æ·»å ãï¼insmodå¨/sbinè¿ä¸ªç®å½ä¸ï¼ä½ å¯ä»¥ä½¿ç¨whereis insmodæ¥çï¼ã
<3> insmodè¿ä¸ªå½ä»¤éè¦å¨rootæéä¸æè½ä½¿ç¨ã
å è½½å®æåä½ å¯ä»¥è¾å
¥lsmodæ¥çhelloè¿ä¸ªæ¨¡åå¦ã
4.å¸è½½é©±å¨æ¨¡åï¼rmmod hello.
å 载模ååå°±å¯å¨å±å¹ä¸çå°å¦ä¸ä¿¡æ¯ï¼Hello world enter.
å¸è½½æ¶å°±å¯å¨å±å¹ä¸çå°å¦ä¸ä¿¡æ¯ï¼hello world exit.
[root@JiaKun moduletest]# rmmod hello.ko
[root@JiaKun moduletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
å¦å¤ï¼å¦ææå¤ä¸ªæ件ï¼åæä¸åæ¹å¼ç¼åMakefileæ件ï¼file1.cãfile2.cï¼ï¼
obj -m := modulename.o
module-objs := file1.o file2.o
温馨提示:答案为网友推荐,仅供参考