我的老师给了我一个linux内核vmlinuz-3.17.2和一个rootfs.ext2,可以加载到qemu . 他要我构建一个最简单的内核模块,打印一个hello world作为家庭作业 .
-
首先,我下载内核源代码并运行make oldconfig
-
其次,我将配置设置为PREEMPT并且没有modversions(根据在qemu中运行的vmlinuz的uname -a),然后进行准备
-
第三,我编译内核mod并在rootfs.ext2中复制hello.ko
-
最后,在qemu中,我运行insmod hello.ko,退出时没有任何提示和echo $?返回0 .
但是,我在dmesg或/ var / log / messages中看不到任何内容有什么问题吗?我怎么能这样做?当我成功运行rmmod hello.ko时,也无需打印任何内容 .
我的日志级别是7 4 1 7
我把我的hello.c做成如下:
#include <linux/init.h>
#include <linux/module.h>
static int __init hello_init(void)
{
pr_info("Hello World");
return -1;
// I changed this to -1 deliberately, Because It seems that the code is not executed.
}
static void __exit hello_exit(void)
{
printk(KERN_ERR "Goodbye, cruel world\n");
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
1 回答
Buildroot
Buildroot是最简单的方法:
最小的树外例子:How to add a Linux kernel driver module as a Buildroot package?
最小树内示例:https://github.com/cirosantilli/buildroot/tree/kernel-module-2016.05
如何使用GDB调试模块:How to debug Linux kernel modules with QEMU?
在Ubuntu 16.04上测试过 .