"Hello world" Driver Build

Test Server CentOS 5.7
Hostname: linuxdba
Kernel version: 2.6.18-274.3.1.el5

1. Hello.c
linuxdba --> cat hello.c

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

MODULE_LICENSE("Dual BSD/GPL");

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

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


module_init(hello_init);
module_exit(hello_exit);

2. Build.(Download and install kernel source code)
 2.1 Embed this code to some drivers, such as "uio" driver.
linuxdba --> pwd
/usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio
linuxdba --> ll
total 32
-rw-r--r-- 1 root root   301 Oct 12 20:21 hello.c
-rw-r--r-- 1 root root   444 Oct 12 20:02 Kconfig
-rw-r--r-- 1 root root    44 Oct 12 20:25 Makefile
-rw-r--r-- 1 root root     0 Oct 12 20:23 Module.markers
-rw-r--r-- 1 root root   358 Oct 12 20:23 Module.symvers
-rw-r--r-- 1 root root 15344 Oct 12 20:02 uio.c
linuxdba --> cat Makefile
obj-$(CONFIG_UIO) += uio.o
obj-m += hello.o
linuxdba --> cd ../../
linuxdba --> make modules SUBDIRS=$PWD/drivers/uio

  WARNING: Symbol version dump /usr/src/redhat/BUILD/kernel-2.6.18/linux-

2.6.18.i386/Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/uio.o
  CC [M]  /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/hello.o
  Building modules, stage 2.
  MODPOST
  CC      /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/hello.mod.o
  LD [M]  /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/hello.ko
  CC      /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/uio.mod.o
  LD [M]  /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/uio/uio.ko

 2.2 Create one source tree as one module.
linuxdba --> mkdir drivers/hello
linuxdba --> ll drivers/hello/
total 8
-rw-r--r-- 1 root root 301 Oct 12 21:01 hello.c
-rw-r--r-- 1 root root  17 Oct 12 21:01 Makefile
linuxdba --> cat drivers/hello/Makefile
obj-m += hello.o
linuxdba --> grep hello drivers/Makefile
obj-$(CONFIG_HELLO)  += hello/             // Add this line to this file.
linuxdba --> make modules SUBDIRS=$PWD/drivers/hello

  WARNING: Symbol version dump /usr/src/redhat/BUILD/kernel-2.6.18/linux-

2.6.18.i386/Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/hello/hello.o
  Building modules, stage 2.
  MODPOST
  CC      /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/hello/hello.mod.o
  LD [M]  /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.i386/drivers/hello/hello.ko

3. Install the driver.
linuxdba --> insmod drivers/hello/hello.ko
or
linuxdba --> insmod drivers/uio/hello.ko

4. Uninstall the driver.
linuxdba --> rmmod drivers/hello/hello.ko
or
linuxdba --> rmmod drivers/uio/hello.ko

You will get the desired informations from "/var/log/messages".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值