我无法从内核5.10.11构建简单的hello world内核模块 [英] I'm unable to build the simple hello world kernel module from kernel 5.10.11

查看:76
本文介绍了我无法从内核5.10.11构建简单的hello world内核模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在KALI中使用内核5.10.11,并且试图学习内核模块编程,但是无法构建模块.我已经尝试了Internet上提供的所有解决方案,但是它们对我不起作用,或者我做错了方法.

I am using kernel 5.10.11 in KALI and I am trying to learn kernel module programming but I am unable to build the module. I have tried all the solutions given on the internet but they are not working for me, or I am doing it the wrong way.

这是我的c文件

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int helloWorld_init(void)
{
    printk(KERN_DEBUG "Hello World!\n");
    return 0;
}

static void helloWorld_exit(void)
{
    printk(KERN_DEBUG "Removing Module\n");
}

module_init(helloWorld_init);
module_exit(helloWorld_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mukul Mehar");
MODULE_DESCRIPTION("first kernel module");

和Makefile:

obj-m += helloWorld.o
KDIR = /usr/src/linux-headers-5.10.11/ 

all:
    make -C $(KDIR) M=$(shell pwd) modules 

clean:
    make -C $(KDIR) M=$(shell pwd) clean

我收到的输出是:

make -C /usr/src/linux-headers-5.10.11/  M=/home/mukul/Documents/Eudyptula/challenge-1 modules 
make[1]: Entering directory '/usr/src/linux-headers-5.10.11'
make[2]: *** No rule to make target '/home/mukul/Documents/Eudyptula/challenge-1/helloWorld.o', needed by '/home/mukul/Documents/Eudyptula/challenge-1/helloWorld.mod'.  Stop.
make[1]: *** [Makefile:1805: /home/mukul/Documents/Eudyptula/challenge-1] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.10.11'
make: *** [Makefile:7: all] Error 2

推荐答案

您是否尝试过在与 helloworld.c 相同的目录中制作一个名为 Kbuild 的文件,其内容如下:

Make a file named Kbuild in the same directory as helloworld.c with the following content:

obj-m += helloworld.o

从同一目录启动构建:

$ make -C /lib/modules/`uname -r`/build M=`pwd`
make: Entering directory '/usr/src/linux-headers-5.4.0-65-generic'
  CC [M]  .../helloworld.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC [M]  .../helloworld.mod.o
  LD [M]  .../helloworld.ko
make: Leaving directory '/usr/src/linux-headers-5.4.0-65-generic'
$ ls -l helloworld.ko
-rw-rw-r-- 1 xxxx xxxx 4144 janv.  31 14:50 helloworld.ko

然后,使用 insmod / rmmod 将模块加载到内核中/从内核中卸载模块:

Then, use insmod/rmmod to load/unload the module into/from the kernel:

$ sudo insmod helloworld.ko
$ dmesg
[16448.154266] Hello World!
$ sudo rmmod helloworld.ko
$ dmesg
[16448.154266] Hello World!
[16497.208337] Removing Module

这篇关于我无法从内核5.10.11构建简单的hello world内核模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆