1,首先要安装内核源码等准备工作。
具体见1-4部。点开看看
如果你想要动态加载的组建的话,直接跳过1就可以了。
2,新建一个test.c
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
MODULE_LICENSE("GPL");
static struct completion comp;
static struct task_struct *t;
static int hello_thread(void *arg)
{
printk("wait_for_completion...");
wait_for_completion(&comp);
printk("done!\n");
return 0;
}
int __init hello_completion_init(void)
{
init_completion(&comp);
t = kthread_run(hello_thread, NULL, "hello_thread");
return 0;
}
void __exit hello_completion_eixt(void)
{
complete(&comp);
kthread_stop(t);
}
module_init(hello_completion_init);
module_exit(hello_completion_eixt);
3,创建Makefile文件
obj-m = test.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.c *.mod.o modules.* Mod*
注意文件的书写规则。
规则
tab 命令
/usr/src/linux-source-3.2.0/linux-source-3.2.0,这个是内核解压后的文件路径。
4,用tmux 窗口执行make
make
5,安装生成的test.ko组件
insmod hello.ko
6,查看组件是否启动成功
lsmod
7,查看打印信息
查看模块信息:dmesg
打印输出最近一次的信息:
dmesg | tail -f
dmesg|grep action
注意需要开个tmux,先启动查看,因为安装后程序会立即执行。
8,打印信息
root@ubuntu-ts:~/linux-module# cat /proc/kmsg
<4>[15467.560074] done!