zoukankan      html  css  js  c++  java
  • linux 内核模块的helloWorld

    我的linux版本是Fedora14。

    要进行下面步骤之前请保证你的系统安装了内核的devel包和header包,

    命令是:

    yum install kernel-headers-$(uname -r)

    yum install kernel-devel-$(uname -r)

     

    helloWorldLKM.c代码:

    #include <linux/init.h>
    #include <linux/module.h>

    MODULE_LICENSE("GPL");

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

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

    module_init(hello_init);
    module_exit(hello_exit);

     

    完了之后在此目录下建立Makefile文件,内容如下:

    obj-m := helloWorldLKM.o
    KDIR  := /lib/modules/$(shell uname -r)/build
    PWD   := $(shell pwd)

    default:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

     

    如果你不写Makefile的话,以下命令有参考价值:

    #make –C /lib/modules/’uname –r’/build M=’pwd’ modules

    #make –C /lib/modules/’uname –r’/build M=’pwd’ clean

    #make –C /lib/modules/’uname –r’/build M=’pwd’ modules_install

    分别是编译,清除一些中间生成文件,加载模块。

    执行make,输出如下:

    make -C /lib/modules/2.6.35.13-91.fc14.i686/build M=/home/xiemingming modules
    make[1]: Entering directory `/usr/src/kernels/2.6.35.13-91.fc14.i686'
      CC [M]  /home/xiemingming/helloWorldLKM.o
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /home/xiemingming/helloWorldLKM.mod.o
      LD [M]  /home/xiemingming/helloWorldLKM.ko
    make[1]: Leaving directory `/usr/src/kernels/2.6.35.13-91.fc14.i686'

    生成了下列文件:

    [root@MyFedora excalibur]# ls hello*
    helloWorldLKM.c  helloWorldLKM.ko  helloWorldLKM.mod.c  helloWorldLKM.mod.o  helloWorldLKM.o

     

    查看可加载模块信息

    [root@MyFedora excalibur]# modinfo helloWorldLKM.ko
    filename:       helloWorldLKM.ko
    license:        GPL
    srcversion:     471D708B0804FE26B554DB1
    depends:       
    vermagic:       2.6.35.13-91.fc14.i686 SMP mod_unload 686

     

    加载内核模块

    [root@MyFedora excalibur]# sudo insmod helloWorldLKM.ko

     

    查看当前内核加载模块

    [root@MyFedora excalibur]# lsmod|grep hello
    helloWorldLKM            664  0

     

    移除加载的内核模块

    [root@MyFedora excalibur]# rmmod helloWorldLKM

    查看日志信息
    [root@MyFedora excalibur]# tail -f /var/log/messages
    May 12 22:39:20 MyFedora abrtd: Directory 'ccpp-1305211160-2402' creation detected
    May 12 22:39:20 MyFedora abrtd: Crash is in database already (dup of /var/spool/abrt/ccpp-1304198487-2047)
    May 12 22:39:20 MyFedora abrtd: Deleting crash ccpp-1305211160-2402 (dup of ccpp-1304198487-2047), sending dbus signal
    May 12 22:40:11 MyFedora pulseaudio[1724]: ratelimit.c: 197 events suppressed
    May 12 22:42:13 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
    May 12 22:45:41 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
    May 12 22:45:47 MyFedora kernel: [  942.291550] Hello, world
    May 12 22:46:12 MyFedora pulseaudio[1724]: ratelimit.c: 172 events suppressed
    May 12 22:46:17 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
    May 12 22:46:18 MyFedora kernel: [  973.214438] Goodbye, cruel world
    May 12 22:46:43 MyFedora pulseaudio[1724]: ratelimit.c: 196 events suppressed
    ^Z
    [3]+  Stopped                 tail -f /var/log/messages

    参考:http://www.cyberciti.biz/tips/build-linux-kernel-module-against-installed-kernel-source-tree.html

    http://oss.org.cn/kernel-book/ldd3/ch02s02.html

    http://fedoraproject.org/wiki/Docs/CustomKernel

    以及最重要的linux源码目录下的linux/Documentation/kbuild/modules.txt

  • 相关阅读:
    设置MYSQL允许用IP访问
    EasyUI中那些不容易被发现的坑——EasyUI重复请求2次的问题
    Oracle初级性能优化总结
    Asp.Net MVC3.0网站统计登录认证的在线人数
    App.config和Web.config配置文件的配置节点的解析
    App.config和Web.config配置文件的自定义配置节点
    Asp.Net Web API 2第十八课——Working with Entity Relations in OData
    win7凭据管理、win7多用户远程登录、主机头设置、nuget.org无法访问
    Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)
    C#基础知识系列八(const和readonly关键字)
  • 原文地址:https://www.cnblogs.com/sola/p/2044991.html
Copyright © 2011-2022 走看看