制作简单的linux内核模块

我试图升级我的守护程序代码,而不是一个可执行文件,它将成为一个内核模块,但即使使用一个简单的例子,我仍然有困难。

这是我尝试的代码:

#include <linux/module.h> #include <linux/kernel.h> int init_module(void){ printk("Hello world 1.\n"); return 0; } void cleanup_module(void){ printk(KERN_ALERT "Goodbye world 1.\n"); } 

而且因为我明白我不能直接编译代码,所以我试图复制并稍微修改makefile,如下所示:

 WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes INCLUDE := -isystem /lib/modules/`uname -r`/build/include CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE} CC := gcc OBJS := ${patsubst %.c, %.o, ${wildcard *.c}} all: ${OBJS} 

然后在命令行上执行“make”,期望安装程序:

这是我的输出:

  gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include -c -o do dc In file included from /lib/modules/2.6.21.5-smp/build/include/asm/thread_info.h:16, from /lib/modules/2.6.21.5-smp/build/include/linux/thread_info.h:21, from /lib/modules/2.6.21.5-smp/build/include/linux/preempt.h:9, from /lib/modules/2.6.21.5-smp/build/include/linux/spinlock.h:49, from /lib/modules/2.6.21.5-smp/build/include/linux/module.h:9, from dc:1: /lib/modules/2.6.21.5-smp/build/include/asm/processor.h:82: error: 'CONFIG_X86_L1_CACHE_SHIFT' undeclared here (not in a function) /lib/modules/2.6.21.5-smp/build/include/asm/processor.h:82: error: requested alignment is not a constant In file included from /lib/modules/2.6.21.5-smp/build/include/linux/sched.h:51, from /lib/modules/2.6.21.5-smp/build/include/linux/utsname.h:35, from /lib/modules/2.6.21.5-smp/build/include/asm/elf.h:12, from /lib/modules/2.6.21.5-smp/build/include/linux/elf.h:7, from /lib/modules/2.6.21.5-smp/build/include/linux/module.h:15, from dc:1: /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:33:3: error: #error You lose. /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if /lib/modules/2.6.21.5-smp/build/include/linux/jiffies.h:225:31: error: division by zero in #if In file included from /lib/modules/2.6.21.5-smp/build/include/linux/module.h:21, from dc:1: /lib/modules/2.6.21.5-smp/build/include/asm/module.h:62:2: error: #error unknown processor family make: *** [do] Error 1 

为什么当我只处理两个有据可查的function时,我应该得到所有这些错误? 我的makefileclosures了吗? 请指教?

没关系。 我发现自己的答案是:

https://www.kernel.org/doc/Documentation/kbuild/modules.txt

它指出:

 To build against the running kernel use: $ make -C /lib/modules/`uname -r`/build M=$PWD Then to install the module(s) just built, add the target "modules_install" to the command: $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install