欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

编译内核模块的helloworld

程序员文章站 2022-03-07 11:32:54
...
http://www.tuicool.com/articles/jYreMf
树莓派编译网卡


编译内核模块的helloworld的例子如下
环境:
1.linode虚拟机
2.centos系统,其他系统都行
3.下面ibm的代码,sample.tar.gz
4.编译内核模块需要源码


参考链接
http://blog.csdn.net/tigerjb/article/details/6010997
http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmodules/index.html
上代码
[code="java"]
# cat hello.c
/*
* $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $
* The code samples are covered by a dual BSD/GPL license from
* Linux Device Drivers, Third Edition, by Jonathan Corbet,
* Alessanfdro Rubini, and Greg Kroah-Hartman.Copyright
* 2005 O鈥橰eilly Media, Inc., 0-596-00590-3.
*/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
printk(KERN_ALERT "Hello, haoning-------------\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
[/code]
Makefile
[code="java"]# cat Makefile

# 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/$(shell uname -r)/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 \
*.order *.symvers

.PHONY: modules modules_install clean

else
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
[/code]
★★★★★★★★★★★★★★★我是罪恶的分割线★★★★★★★★★★★
步骤:
环境linode
centos6
[code="java"]
# uname -a
Linux li408-34 3.8.4-linode50 #1 SMP Mon Mar 25 15:50:29 EDT 2013 i686 i686 i386 GNU/Linux
# uname -r
3.8.4-linode50
#
[/code]

由于linode的内核都是自己编译的
上网查了一些自己编译内核的方法,但是还是启动报错
参考http://www.xiaozhou.net/ittech/vps-ittech/upgrade_linode_vps_kernel_manually-2011-04-28.htm
★这个写得很好,学习了

后来自己下了
linux-3.8.tar.xz
xz -d linux-3.8.tar.xz
tar xvf linux-3.8.tar
cd linux-3.8
cp /proc/config.gz .
gunzip config.gz
mv config .config

[color=red]这一步是用系统当前的config文件
作为内核的make menuconfig或者make confi*生的文件
配置项太jb多了
然后可能需要改[/color]

CONFIG_PARAVIRT_GUEST=y
CONFIG_XEN=y
CONFIG_PARAVIRT=y
CONFIG_PARAVIRT_CLOCK=y
CONFIG_XEN_BLKDEV_FRONTEND=y
CONFIG_XEN_NETDEV_FRONTEND=y
CONFIG_HVC_XEN=y
CONFIG_XEN_SCRUB_PAGES=y

CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=y

CONFIG_IP_NF_SECURITY=n

CONFIG_IP6_NF_SECURITY=n



#rm -rf /boot/* 看好了,这里可能原来就有启动的东西了,还是别删了
#开始编译
make modules
make
#安装内核
make install
make modules_install

然后就会在/boot下面生成vmlinuz System.map
据说改了 /boot/grub/menu.lst重新启动 ,linode选pv-grub-x86-32
就能重新启动使用新内核了,但是我没试验成功,反正我是为了编译内核模块,又不是重新然linode好使,可能虚拟机还是和真实机器有莫名其买的差距吧,没深究

继续编译内核模块:
ln -s /root/kernel/linux-3.8 /lib/modules/3.8.4-linode50/build 

把新编译的源码链接到 系统已经存在的内核源码
其实不太对,但是两个都是3.8的差别还不算大,内核模块可用

回到罪恶的分割线上面的代码
[code="java"]
# ls
Makefile hello.c
# pwd
/root/kk/samples/hello
#
[/code]
[code="java"]
# make
make -C /lib/modules/3.8.4-linode50/build M=/root/kk/samples/hello modules
make[1]: Entering directory `/root/kernel/linux-3.8'
CC [M] /root/kk/samples/hello/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /root/kk/samples/hello/hello.mod.o
LD [M] /root/kk/samples/hello/hello.ko
make[1]: Leaving directory `/root/kernel/linux-3.8'
#
[/code]

[code="java"]
# lsmod
Module Size Used by
#
# ls
Makefile Module.symvers hello.c hello.ko hello.mod.c hello.mod.o hello.o modules.order
# insmod hello.ko
# lsmod
Module Size Used by
hello 606 0
#
# cat /proc/modules
hello 606 0 - Live 0xede36000 (O)
#
# cd /sys/module/hello
# ls
coresize holders initsize initstate notes refcnt sections taint uevent
# tree -a
.
|-- coresize
|-- holders
|-- initsize
|-- initstate
|-- notes
| `-- .note.gnu.build-id
|-- refcnt
|-- sections
| |-- .gnu.linkonce.this_module
| |-- .note.gnu.build-id
| |-- .rodata.str1.1
| |-- .strtab
| |-- .symtab
| `-- .text
|-- taint
`-- uevent

3 directories, 13 files
# dmesg
.....
Hello, world
Goodbye, cruel world
# tail -f /var/log/messages
Apr 11 11:37:59 li408-34 abrtd: Init complete, entering main loop
Apr 11 11:37:59 li408-34 init: Failed to spawn hvc0 main process: unable to execute: No such file or directory
Apr 11 11:42:23 li408-34 kernel: Hello, haoning-------------
Apr 11 11:43:56 li408-34 kernel: Goodbye, cruel world
Apr 11 11:44:12 li408-34 kernel: Hello, haoning-------------
Apr 11 13:51:49 li408-34 kernel: Goodbye, cruel world
Apr 11 13:51:59 li408-34 kernel: Hello, haoning-------------
Apr 11 14:44:21 li408-34 yum[4371]: Installed: tree-1.5.3-2.el6.i686
Apr 11 15:05:21 li408-34 kernel: Goodbye, cruel world
Apr 11 15:05:51 li408-34 kernel: Hello, world

[/code]
表示内核已经正确装载了
lsmod查看模块
rmmod 删除模块

把上面ibm的的代码放到附件里了
相关标签: 内核