我的环境:
[root@localhost hello_world]# uname -a
Linux localhost.localdomain 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost hello_world]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m
和内核源代码树版本是:
[root@localhost lab]# ls /home/bruce/rpmbuild/BUILD/kernel-2.6.32-504.el6/
linux-2.6.32-504.el6.x86_64
[root@localhost lab]# rpm -qa kernel\* | sort
kernel-2.6.32-504.el6.x86_64
kernel-firmware-2.6.32-504.el6.noarch
kernel-headers-2.6.32-504.el6.x86_64
生成后跟wiki:I Need the Kernel Source
一个简单的内核" hello world":
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_ALERT */
int init_module(void)
{
printk(KERN_INFO "Hello world.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world.\n");
}
和Makefile:
KERNEL_SOURCE_VERSION ?= $(shell uname -r)
KERNEL_TREE ?= /lib/modules/$(KERNEL_SOURCE_VERSION)/build
obj-m += hello_world.o
all:
make -C $(KERNEL_TREE) M=$(PWD) modules
clean:
make -C $(KERNEL_TREE) M=$(PWD) clean
它成功了。
[root@localhost hello_world]# modinfo hello_world.ko
filename: hello_world.ko
srcversion: 67A7C9765BA14A0A1C8B6CF
depends:
vermagic: 2.6.32 SMP mod_unload modversions
但是当加载hello_world.ko时,它会报告错误:
[root@localhost hello_world]# insmod hello_world.ko
insmod: error inserting 'hello_world.ko': -1 Invalid module format
并显示日志消息:
[root@localhost hello_world]# tail -n 2 /var/log/messages
Feb 2 22:09:47 localhost kernel: hello_world: no symbol version for module_layout
Feb 2 22:10:12 localhost kernel: hello_world: no symbol version for module_layout
这是我的第一个内核程序,我认为内核源代码和系统内核之间的版本是匹配的。 那么问题是什么?