在我们实现自定义内核之后我们就可以开始第一次关于内核的编程。我们是在树莓派上进行模块的安装与卸载
hello.c
#include<linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual DSB/GPL");
static int hello_init(void)
{
printk(KERN_ALERT"hello world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT"goodbay world\n");
}
module_init(hello_init);
module_exit(hello_exit);
然后在编写Makefile
Makefile
obj-m +=hello.o
KERNELDIR:=/work/linux
PWD=/work/95
all:
make -C $(KERNELDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
clean:
rm -rf *.ko *.mod.c *.omodules.* Module.symvers
在这里对makefile进行解释
make -C 内核源码绝对路径 M=模块源码文件所在的绝对路径 modules
为什么我们要加
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
因为我们是在树莓派上使用的,所以我们需要交叉编译
然后clean里面删除的东西就是make完产生的哪些文件,可以通过make clean来进行删除
如果我们编译通过了,并且要通过scp来发送的时候出现以下错误
[root@localhost /]# scp linux.txt 172.16.26.2:/
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
5b:4e:bf:fa:a0:98:c2:a6:96?1e:d2:3d:3e:59:b6.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:2
RSA host key for 172.16.26.2 has changed and you have requested strict checking.
Host key verification failed.
我们就要通过删除ssh文件
他的路径在Add correct host key in /root/.ssh/known_hosts to get rid of this message.中有显示。
删除这个文件后就可以正常使用scp了
当我们完成传输完成后
sudo insmod .ko文件
完成安装
然后rmmod删除
查看消息dmesg