Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110073
  • 博文数量: 50
  • 博客积分: 968
  • 博客等级: 少尉
  • 技术积分: 492
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-17 09:51
文章分类

全部博文(50)

文章存档

2012年(2)

2011年(48)

我的朋友

分类: LINUX

2011-06-15 13:49:17

动态编译本机内核模块

 /hello.c/

 

  1. #include <linux/module.h> /* Needed by all modules */
  2. #include <linux/kernel.h> /* Needed for KERN_ALERT */
  3. #include <linux/init.h>
  4. static int test_init(void)
  5. {
  6.         printk("Hello world 1.\n");

  7.    /* A non 0 return means init_module failed; module can't be loaded.*/
  8.         return 0;
  9. }

  10. static void test_exit()
  11. {
  12.         printk("Goodbye world 1.\n");
  13. }

  14. module_init(test_init);
  15. module_exit(test_exit);

 

/Makefile/

 

  1. obj-m += hello.o

  2. PWD := $(shell pwd)

  3. KERN_VER = $(shell uname -r)

  4. KERN_DIR = /lib/modules/$(KERN_VER)/build

  5. all:
  6.         $(MAKE) -C $(KERN_DIR) M=$(PWD) modules

  7. clean:

  8.         rm –rf *.o *~ core .depend . *.cmd *.ko *.mod.c .tmp_versions

 

编译生成的内核模块可以直接加载:

insmod   hello.ko

 

卸载模块:

rmmod  hello.ko

 

可以通过tail /var/log/messages参看系统内核日志信息来查

看模块的加载与卸载情况。

阅读(1368) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~