驱动第一章:hello world

1、代码
[root@proEnv29 test2]# 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,cruel world \n");
}

module_init(hello_init);
module_exit(hello_exit);


[root@proEnv29 test2]#

2、Makefile
[root@proEnv29 test2]# cat Makefile
CONFIG_MODULE_SIG=n
#如果已经定义KERNELRELEASE,则说明是从内核构造系统调用的,
#因此可利用其内建语句。
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
#否则,是直接从命令行调用,
#这时要调用内核构造系统。
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

clean:
rm -f *.o *.ko *.mod.c .hello*


[root@proEnv29 test2]# ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers
3、编译
[root@proEnv29 test2]# make
make -C /lib/modules/3.10.0-327.36.2.el7.x86_64/build M=/home/maokx/deviceDrivers/test2 modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-327.36.2.el7.x86_64'
  CC [M]  /home/maokx/deviceDrivers/test2/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/maokx/deviceDrivers/test2/hello.mod.o
  LD [M]  /home/maokx/deviceDrivers/test2/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-327.36.2.el7.x86_64'

4、加载与卸载
[root@proEnv29 test2]# insmod hello.ko
[root@proEnv29 test2]# modinfo hello
modinfo: ERROR: Module hello not found.
[root@proEnv29 test2]# lsmod|grep hello
hello                  12496  0 
[root@proEnv29 test2]# modinfo hello.ko
filename:       /home/maokx/deviceDrivers/test2/hello.ko
license:        Dual BSD/GPL
rhelversion:    7.2
srcversion:     08163D92C9B5B3FB43ECECA
depends:        
vermagic:       3.10.0 SMP mod_unload modversions 
[root@proEnv29 test2]# rmmod hello
[root@proEnv29 test2]# 


[root@proEnv29 ~]# > /var/log/messages
[root@proEnv29 ~]# tail -f /var/log/messages
Jul  4 15:10:26 proEnv29 kernel: Hello,world 


Jul  4 15:11:58 proEnv29 kernel: Goodbye,cruel world 
^C
[root@proEnv29 ~]# 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员如山石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值