linux设备驱动学习(1)--内核源码树安装

最近开始看LDD3,按书说的,得安装内核源码树。


查看/usr/src/kernels下是否已经安装了内核源码树
如果没有,使用yum 安装
[root@CJS test]# yum install kernel-devel-`uname -r`

安装后在/usr/src/kernels可以看到3.11.10-301.fc20.i686


ps:开始到https://www.kernel.org/下载linux-3.11.10.tar.xz,编译安装折腾了
很久,结果写个hello world模块后,insmod时出现insmod: ERROR: could not insert module ./hello.ko: Invalid module format
也不知道为啥。用yum安装,连编译都不用了,直接可以用。
两个版本的modinfo hello.ko如下
[root@CJS test]# modinfo hello.ko
filename:       /opt/mini2440/test/hello.ko
license:        Dual BSD/GPL
depends:        
vermagic:       3.11.10 SMP mod_unload 686 


[root@CJS test]# modinfo hello.ko
filename:       /opt/mini2440/test/hello.ko
license:        Dual BSD/GPL
depends:        
vermagic:       3.11.10-301.fc20.i686 SMP mod_unload 686 


难道vermagic非得要一模一样?google一下还真如此,这个vermagic是可以通过源码修改的,
这里就不折腾了。





编写hello.c Makefile
[root@CJS test]# vi 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@CJS test]# vi Makefile
obj-m := hello.o
KERNELDIR := /usr/src/kernels/3.11.10-301.fc20.i686
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 $(TARGET)
编译
[root@CJS test]# make
[root@CJS test]# ls
hello.c   hello.mod.c  hello.o   modules.order
hello.ko  hello.mod.o  Makefile  Module.symvers


加载
[root@CJS test]# insmod hello.ko
查看
[root@CJS test]# lsmod
Module                  Size  Used by
hello                  12448  0 
卸载
[root@CJS test]# rmmod hello.ko


查看hello.c中的printk输出
[root@CJS test]# cat /var/log/syslog |grep world 
cat: /var/log/syslog: No such file or directory
没有该文件??
再试
[root@CJS test]# cat   /var/log/messages |grep world 
cat: /var/log/messages: No such file or directory
还是一样
查看var/log
[root@CJS test]# ls /var/log/
anaconda       cups         journal  speech-dispatcher  Xorg.1.log
audit          dnf.log      lastlog  sssd               Xorg.1.log.old
boot.log       dnf.rpm.log  pluto    tallylog           yum.log
btmp           gdm          ppp      wtmp
btmp-20140305  glusterfs    README   Xorg.0.log
chrony         grubby       samba    Xorg.0.log.old
google一下fedora syslog
找到原因了,这些在新版本早就没有了
在https://fedoraproject.org/wiki/Changes/NoDefaultSyslog中给出新的方法
[root@CJS test]# journalctl | grep world
.........
Mar 28 15:11:16 CJS kernel: Hello, world
Mar 28 15:33:32 CJS kernel: Goodbye, cruel world
现在可以看到加载和卸载时的打印信息了....
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值