最近开始看LDD3,按书说的,得安装内核源码树。
如果没有,使用yum 安装
[root@CJS test]# yum install kernel-devel-`uname -r`
安装后在/usr/src/kernels可以看到3.11.10-301.fc20.i686
很久,结果写个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
现在可以看到加载和卸载时的打印信息了....