`
haoningabc
  • 浏览: 1451126 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

编译内核模块的helloworld

阅读更多
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
上代码
[root@li408-34 hello]# 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);

Makefile
[root@li408-34 hello]# 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

★★★★★★★★★★★★★★★我是罪恶的分割线★★★★★★★★★★★
步骤:
环境linode
centos6
[root@li408-34 hello]# uname -a
Linux li408-34 3.8.4-linode50 #1 SMP Mon Mar 25 15:50:29 EDT 2013 i686 i686 i386 GNU/Linux
[root@li408-34 hello]# uname -r
3.8.4-linode50
[root@li408-34 hello]# 


由于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

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


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的差别还不算大,内核模块可用

回到罪恶的分割线上面的代码
[root@li408-34 hello]# ls
Makefile  hello.c
[root@li408-34 hello]# pwd
/root/kk/samples/hello
[root@li408-34 hello]#

[root@li408-34 hello]# 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'
[root@li408-34 hello]# 


[root@li408-34 hello]# lsmod
Module                  Size  Used by
[root@li408-34 hello]# 
[root@li408-34 hello]# ls     
Makefile  Module.symvers  hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  modules.order
[root@li408-34 hello]# insmod hello.ko
[root@li408-34 hello]# lsmod
Module                  Size  Used by
hello                    606  0 
[root@li408-34 hello]# 
[root@li408-34 hello]# cat /proc/modules
hello 606 0 - Live 0xede36000 (O)
[root@li408-34 hello]#
[root@li408-34 hello]# cd /sys/module/hello
[root@li408-34 hello]# ls
coresize  holders  initsize  initstate  notes  refcnt  sections  taint  uevent
[root@li408-34 hello]# 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
[root@li408-34 hello]# dmesg
.....
Hello, world
Goodbye, cruel world
[root@li408-34 hello]# 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


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

把上面ibm的的代码放到附件里了
分享到:
评论

相关推荐

    linux-2.4内核模块 helloworld

    以helloworld模块在linux-2.4内核下的编写,编译,连接和运行例,简单介绍下linux-2.4下内核模块的编写。

    Windows x64内核模块-HelloWorld

    Windows内核安全与驱动开发书的第一个示例程序,在新版本开发环境中调试通过,使用Visual Studio 2017加上WDK 1803环境编译。在调试过程中,由于x64驱动程序禁止使用嵌入汇编,采用Shellcode方式进行软件中断调用,...

    Linux内核驱动模块编程指南 (内核版本2.2, 2.4)The Linux Kernel Module Programming Guide CHS

    2.2. 编译内核模块 2.3. Hello World (part 2) 2.4. Hello World (part 3): 关于 __init and __exit宏 2.5. Hello World (part 4): 内核模块证书和内核模块文档说明 2.6. 从命令行传递参数给内核模块 2.7. 由多个...

    Linux实验课设报告

    6、Linux内核模块编程:内核模块编程、卸载模块编程、参数模块编程 8、Linux内存管理:编写模块程序、编译、插入模块、查看打印信息 8、Linux设备驱动:编写一个简单的字符设备驱动、查看设备号、编写测试程序 使用...

    linux内核编程环境搭建并编写helloworld模块

    一、linux内核编程环境搭建 升级kernel yum update kernel 原创文章 9获赞 10访问量 362 关注 私信 展开阅读全文 作者:权左

    leetcoderust-CSCI340-KernelHacking:CSCI340操作系统的内核模块编写作业

    内核模块的文件,您应该首先构建这些文件以熟悉编译和安装已编译内核模块的基础知识。 内核模块 向 Linux 内核添加代码有两种主要方式。 一种方法是在编译过程中选择或添加代码编译到内核中。 另一种方法是在运行时...

    (1)学习 Linux 内核的系统调用方法 理解并掌握 Limux 系统调用的实现框架、用户界面、参数传递、进入返回过程

    使用编译内核法和内核模块法这两种添加系统调用的方法添加一个不用传递参数的系统调用,其功能是简单输出类似“hello wold”这样的字符串。 三. 实验步骤 3.1内核编译法添加系统调用 (1)通过su root 输入密码后...

    centos7下编译hello.ko驱动程序

    1. 查看内核版本 [root@localhost test]# uname -r 3.10.0-862.el7.x86_64 2. 切换到相应的内核源码中 [root@localhost test]# cd /usr/src/kernels/3.10.0-862.el7.x86_64/drivers/ [root@localhost test]# ...

    linux设备驱动程序中英文加源码

    内核模块相比于应用程序 2.3.1. 用户空间和内核空间 2.3.2. 内核的并发 2.3.3. 当前进程 2.3.4. 几个别的细节 2.4. 编译和加载 2.4.1. 编译模块 2.4.2. 加载和卸载模块 2.4.3. 版本依赖 2.4.4. 平台依赖性 2.5. ...

    Linux内核编程 中文版

    1.1内核模块的编译文件 1.2 多文件内核模块 2.字符设备文件 2.1多内核版本源文件 3./proc文件系统 4.使用/proc进行输入 5.和设备文件对话(写和IOCTLS) 6.启动参数 7.系统调用 8.阻塞进程 9....

    linux设备驱动程序(第三版)CHM和配套源码

    2.3. 内核模块相比于应用程序 2.3.1. 用户空间和内核空间 2.3.2. 内核的并发 2.3.3. 当前进程 2.3.4. 几个别的细节 2.4. 编译和加载 2.4.1. 编译模块 2.4.2. 加载和卸载模块 2.4.3. 版本依赖 2.4.4. 平台...

    uClinux内核编程

    1.1内核模块的编译文件 4 1.2 多文件内核模块 5 2.字符设备文件 8 2.1多内核版本源文件 16 3./proc文件系统 17 4.使用/proc进行输入 22 5.和设备文件对话(写和IOCTLS) 30 6.启动参数 44 7....

    Ubuntu10.04下驱动开发环境搭建

    Ubuntu10.04下驱动开发环境的建立(Linux驱动开发源码树的建立),包括 Linux内核:代码的下载,编译,新内核替换老内核...helloworld驱动:源码,编译,安装模块和卸载模块 我在建立开发源码树时遇到的问题及解决方法

    kernel-module-fun:您可以使用内核模块做一些有趣的事情(所有“坏主意”)

    内容hello.c :一个简单的“ hello world”模块hello-packet.c :每次计算机收到数据包时都记录日志。 可以很容易地修改这一包,使其在50%的时间内丢包。 rootkit.c :一个简单的rootkit。编译它们我正在运行Linux ...

    [14本经典Android开发教程]-8-Linux内核阅读心得体会

    读核感悟 kbuild系统 内核模块的编译 22 读核感悟 kbuild系统 编译到内核和编译成模块的区别 24 读核感悟 kbuild系统 make bzImage的过程 26 读核感悟 kbuild系统 make menuconfig 31 读核感悟 文件系统 用C来实现...

    simple-rootkit:通过内核模块对 gcc 和 Python 的简单攻击,带有非常详细的注释

    在这里,我们将编译一个内核模块,它拦截每个“读取”系统调用,搜索一个字符串,如果它看起来像 gcc 编译器或 python 解释器,则替换它。 这旨在演示受感染的系统如何从完全安全的源代码构建恶意二进制文件。 更...

    linux内核阅读心得

    读核感悟-kbuild系统-内核模块的编译.........................................22 读核感悟-kbuild系统-编译到内核和编译成模块的区别...........................24 读核感悟-kbuild系统-make bzImage的过程.........

    linux内核编程.pdf

    1.1内核模块的编译文件........................................................................................................四 1.2 多文件内核模块........................................................

    驱动开发教程

    |-编译和加载内核HelloWorld ------------------------------ 2.内核编程基础 |-WIN64内核编程的基本规则 |-驱动程序与应用程序通信 |-内核里使用内存 |-内核里操作字符串 |-内核里操作文件 |-内核里操作注册表...

    Linux内核阅读

    读核感悟-kbuild系统-内核模块的编译.........................................22 读核感悟-kbuild系统-编译到内核和编译成模块的区别...........................24 读核感悟-kbuild系统-make bzImage的过程.........

Global site tag (gtag.js) - Google Analytics