linux内核编译测试环境搭建详解

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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值