一、内核树的建立
(转载)
root@emboard-desktop:/# uname -r
2.6.24-19-generic
2.6.24-19-generic
(
个人建议将make的内核版本和系统的内核版本弄成一致,否则在insmoud ./hello.ko的时候会出现以下错误:
insmod: error inserting './hello.ko': -1 invalid module format
)
2,然后把下载的内核解压编译:(注意目录)
root@emboard-desktop:/workdir/LDD3/kernel# tar -jxvf linux-source-2.6.28.tar.bz2
root@emboard-desktop:/workdir/LDD3/kernel# ls
linux-2.6.28 linux-2.6.28.tar.bz2
root@emboard-desktop:/workdir/LDD3/kernel# cd linux-2.6.28
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# ls
COPYING Kbuild Module.symvers System.map crypto fs ipc mm samples sound vmlinux
CREDITS MAINTAINERS README arch drivers include kernel modules.order scripts usr vmlinux.o
Documentation Makefile REPORTING-BUGS block firmware init lib net security virt
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# ls
COPYING Kbuild Module.symvers System.map crypto fs ipc mm samples sound vmlinux
CREDITS MAINTAINERS README arch drivers include kernel modules.order scripts usr vmlinux.o
Documentation Makefile REPORTING-BUGS block firmware init lib net security virt
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# make menuconfig
直接退出.
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# make
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# make bzImage
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# make modules
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# make modules_install
编译成功后打开下面的目录,就会看到刚刚编译的内核.
root@emboard-desktop:/workdir/LDD3/kernel/linux-2.6.28# cd /lib/modules/
root@emboard-desktop:/lib/modules# ls
2.6.24-19-generic 2.6.28
root@emboard-desktop:/lib/modules# ls
2.6.24-19-generic 2.6.28
root@emboard-desktop:/lib/modules# cd 2.6.28/
root@emboard-desktop:/lib/modules/2.6.28# ls
build modules.alias modules.dep modules.inputmap modules.ofmap modules.pcimap modules.symbols source
kernel modules.ccwmap modules.ieee1394map modules.isapnpmap modules.order modules.seriomap modules.usbmap
在上面就可以看到"build"目录,这个等下要用到.到此为止内核树就建立完成了!
root@emboard-desktop:/lib/modules/2.6.28# ls
build modules.alias modules.dep modules.inputmap modules.ofmap modules.pcimap modules.symbols source
kernel modules.ccwmap modules.ieee1394map modules.isapnpmap modules.order modules.seriomap modules.usbmap
在上面就可以看到"build"目录,这个等下要用到.到此为止内核树就建立完成了!
二、makefile
obj-m :hello.o
KERNELDIR ?=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
三、hello.c程序(见书本)
四、结果
在federo 9的环境下,uname -r得到如下:
2.6.25.14.fc9.i686
我所下载的内核版本是2.6.25.14,make 都能正确进行,只是在最后insmod ./hello.ko的时候,出现如下错误:
insmod: error inserting './hello.ko': -1 invalid module format
估计是自己下载的内核版本与federo的内核版本不一致所致,目前还没有解决这个问题,由于下载不到2.6.25.14.fc9.i686内核代码,准备在ubuntu下面在试试,希望哪位做成功的高手赐教。给出能成功实现实例的解决方案。
2011.5.30解决方法:
1.执行命令insmod ./hello.ko插入模块,失败,错误信息是insmod: error inserting './hello.ko': -1 Invalid module format。
2.执行命令cat /var/log/message | tail,打印信息version magic '2.6.25.14 mod_unload 686' should be '2.6.25-14.fc9.i686 3MP mod_unload 686 4KSTACKS'。
4.执行命令uname -r,可以看到Fedora 9的版本信息是2.6.25-14.fc9.i686。进入/usr/src/linux-2.6.25.14(我的实例是放在home/liyimin/下载/kernel2.6.25/linux-2.6.25.14 中,所以,路径为:),编辑Makefile,将EXTRAVERSION = .14修改为EXTRAVERSION = -14.fc9.i686。
5. 在linux-2.6.25.14 目录下执行命令cp /boot/config-2.6.25-14.fc9.i686 ./.config,覆盖原有配置文件。
6. 进入/usr/src/linux-2.6.25.14,执行make menuconfig配置内核,执行make编译内核。
7. 最后,再次编译驱动程序hello.c得到hello.ko。执行insmod ./hello.ko,即可正确insert模块。
8.查看内核模块的方法:lsmod