gaochang2008
关于模块开发的问题 大虾指教
insmod hello.ko insmod:
error inserting 'hello.ko': -1 Invalid module format
下面是代码:
#if defined(MODVERSIONS)
#include <linux/modversions.h>
#endif
#include <linux/module.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
# include <linux/netfilter/x_tables.h>
# define ipt_register_match xt_register_match
# define ipt_unregister_match xt_unregister_match
# define ipt_match xt_match
#else
# include <linux/netfilter_ipv4/ip_tables.h>
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21) */
#include <net/tcp.h>
#include <net/udp.h>
MODULE_LICENSE("GPL");
static int __init init(void)
{
printk(KERN_INFO "nihao /n");
return 0;
}
static void __exit fini(void)
{
printk(KERN_INFO "exit hello/n");
return 0;
}
module_init(init);
module_exit(fini);
下面是makefile文件:
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KERNEL_SRC = /usr/src/linux
#KERNEL_SRC ?= $(firstword $(wildcard /lib/modules/$(shell uname -r)/build /usr/src/linux)) 注: 2种make寻找内核版本的方式 都测试过,依然出现 insmod出现错误
ifeq ($(KERNEL_SRC),)
$(error You need to define KERNEL_SRC)
endif
ifneq ($wildcard $(KERNEL_SRC)/include/linux/modversions.h),)
MODVERSIONS = -DMODVERSIONS
endif
#CC = gcc
#CFLAGS = -O3 -Wall
hello.ko: hello.c
$(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules
clean:
-rm -rf *.o *.so *.ko .*.cmd *.mod.c .tmp_versions
endif
版本是
2.6.22.142.6.22.14 #1 SMP Sat Feb 23 23:09:44 CST 2008 i686 GNU/Linux
在另一个同样内核版本的机器上 没有 可以插入模块,,没有出现任何问题,
大虾 急救呀,,
gaochang2008
[quote]原帖由 [i]Godbach[/i] 于 2008-6-30 16:34 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8704182&ptid=1184668][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
#KERNEL_SRC ?= $(firstword $(wildcard /lib/modules/$(shell uname -r)/build /usr/src/linux))
你把这个地方$(shell uname -r)直接写成对应的内核版本,试试 [/quote]
那个内核版本是别人安装的,,一般都放在 /usr/src 下面(我在那个目录下就看到一处源代码),,,我已经测试过了,还是有这个问题,
Godbach
你的测试代码很多是没用的,看一下LDD3的第一个例子:
[table=95%][tr][td][font=FixedSys][color=#000000][color=#ff9900]/*
* $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $
*/[/color]
[color=#0000cc]#[/color][color=#ff0000]include[/color] [color=#0000cc]<[/color]linux[color=#0000cc]/[/color]init[color=#0000cc].[/color]h[color=#0000cc]>[/color]
[color=#0000cc]#[/color][color=#ff0000]include[/color] [color=#0000cc]<[/color]linux[color=#0000cc]/[/color]module[color=#0000cc].[/color]h[color=#0000cc]>[/color]
MODULE_LICENSE[color=#0000cc]([/color][color=#ff00ff]"Dual BSD/GPL"[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]static[/color] [color=#0000ff]int[/color] hello_init[color=#0000cc]([/color][color=#0000ff]void[/color][color=#0000cc])[/color]
[color=#0000cc]{[/color]
printk[color=#0000cc]([/color]KERN_ALERT [color=#ff00ff]"Hello, world/n"[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]return[/color] 0[color=#0000cc];[/color]
[color=#0000cc]}[/color]
[color=#0000ff]static[/color] [color=#0000ff]void[/color] hello_exit[color=#0000cc]([/color][color=#0000ff]void[/color][color=#0000cc])[/color]
[color=#0000cc]{[/color]
printk[color=#0000cc]([/color]KERN_ALERT [color=#ff00ff]"Goodbye, cruel world/n"[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000cc]}[/color]
module_init[color=#0000cc]([/color]hello_init[color=#0000cc])[/color][color=#0000cc];[/color]
module_exit[color=#0000cc]([/color]hello_exit[color=#0000cc])[/color][color=#0000cc];[/color]
[/color][/font][/td][/tr][/table]
Godbach
以及对应的Makefile,我这里用的是2.6.20的内核。
[table=95%][tr][td][font=FixedSys]# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/2.6.20/build
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
[/font][/td][/tr][/table]
gaochang2008
[quote]原帖由 [i]Godbach[/i] 于 2008-6-30 16:40 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8704227&ptid=1184668][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
你用uname -r的结果和/lib/modules下面对应的目录看是否是一样的 [/quote]
开始一样的 显示uname -a 显示2.6.22.142.6.22.14 lib/modules显示2.6.22.142.6.22.14 (这种情况测试的时候也模块也不可以安装)
但是应该显示2.6.22.14 的后来我把lib/modules 下面的文件修改2.6.22.14(原来显示 2.6.22.142.6.22.14 ) uname -a 显示2.6.22.142.6.22.14(模块也不可以安装)
gaochang2008
[quote]原帖由 [i]Godbach[/i] 于 2008-6-30 16:55 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8704367&ptid=1184668][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
以及对应的Makefile,我这里用的是2.6.20的内核。
# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once a ... [/quote]
按照你的代码 和make文件 我也测试过了,,还是有问题