我有一个 hello world 内核模块的源代码,它可以在笔记本电脑的 Ubuntu 20 中运行。现在我正在尝试在 Ubuntu 20 中但在 WSL2 中编译相同的代码。为此,我正在使用这个:
make -C /sys/modules/$(shell uname -r)/build M=$(PWD) modules
Run Code Online (Sandbox Code Playgroud)
问题是那/lib/modules是空的。好像 WSL2 什么都带不进来/lib/modules/4.19.104-microsoft-standard/build
我尝试使用以下方法获取标题:
sudo apt search linux-headers-`uname -r`
Sorting... Done
Full Text Search... Done
Run Code Online (Sandbox Code Playgroud)
但是在模块文件夹中没有填充任何内容
为了该文件夹包含所有必需的模块,我需要做什么吗?
[编辑]
感谢@HannahJ,越来越近。
我在做:
> sudo make -C /home/<user>/WSL2-Linux-Kernel M=$(pwd) modules
SL2-Linux-Kernel M=$(pwd) modules
make: Entering directory '/home/<user>/WSL2-Linux-Kernel'
CC [M] /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.mod.o
LD [M] /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.ko
make: Leaving directory '/home/<user>/WSL2-Linux-Kernel'
Run Code Online (Sandbox Code Playgroud)
最后,我lkm_example.ko创建了文件。
在那之后:
> …Run Code Online (Sandbox Code Playgroud) 从 Linux 中的内核模块调用 C 文件中的内核函数的正确方法是什么?
我想打电话给exit_task_namespaces在linux/nsproxy.c从我第一次内核模块
我正在这样做:
#include <linux/nsproxy.h>
…
static ssize_t device_read(struct file *flip, char *buffer, size_t len, loff_t *offset)
{
struct task_struct *task = current;
…
exit_task_namespaces(task);
…
}
Run Code Online (Sandbox Code Playgroud)
当我尝试时,make出现以下错误:
ERROR: "exit_task_namespaces" [/home/.../lkm_example.ko] undefined!
make[2]: *** [scripts/Makefile.modpost:94: __modpost] Error 1
make[1]: *** [Makefile:1673: modules] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-73-generic'
make: *** [Makefile:3: all] Error 2
Run Code Online (Sandbox Code Playgroud)
我可以在文件中看到/usr/src/linux-headers-5.4.0-73-generic/include/linux/nsproxy.h该方法存在。
这是我的 Makefile:
obj-m += lkm_example.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
Run Code Online (Sandbox Code Playgroud)