简体   繁体   English

如何在QEMU上构建和运行Linux内核模块?

[英]How to build and run a Linux kernel module on QEMU?

My teacher gives me a linux kernel vmlinuz-3.17.2 and a rootfs.ext2 which can be loaded to qemu. 我的老师给了我一个Linux内核vmlinuz-3.17.2和一个rootfs.ext2,可以将其加载到qemu中。 And he asks me to build a simplest kernel module which prints a hello world as homework. 他要求我构建一个最简单的内核模块,该模块将作业打招呼的世界打印出来。

  • Firstly, I download the kernel source and run make oldconfig 首先,我下载内核源代码并运行make oldconfig
  • Secondly, I make the config to be PREEMPT and without modversions (according to uname -a of vmlinuz running in qemu) , then make prepare 其次,我将配置设为PREEMPT且未进行任何modversions(根据在qemu中运行的vmlinuz的uname -a),然后进行准备
  • Thirdly, I compile the kernel mod and copy hello.ko in rootfs.ext2 第三,我编译内核mod并在rootfs.ext2中复制hello.ko
  • Finally, In qemu, I run insmod hello.ko which exit without any prompt and echo $? 最后,在qemu中,我运行insmod hello.ko,该退出没有任何提示,并回显$? returns 0. 返回0。

However, I can't see anything in dmesg or /var/log/messages Is there anything wrong? 但是,我在dmesg或/ var / log / messages中看不到任何东西吗? How can I do with this? 我该怎么办? There is also nothing to be printed when I run rmmod hello.ko successfully. 当我成功运行rmmod hello.ko时,也没有任何内容可以打印。

My log level is 7 4 1 7 我的日志级别是7 4 1 7

I have make my hello.c as follows: 我将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);

Buildroot Buildroot里面

Buildroot is the easiest way to do it: Buildroot是最简单的方法:

Tested on Ubuntu 16.04. 在Ubuntu 16.04上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM