WSL 2 没有 /lib/modules/

Car*_*cia 12 kernel-modules c++ linux-kernel windows-subsystem-for-linux

我有一个 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创建了文件。

在那之后:

> sudo insmod lkm_example.ko
insmod: ERROR: could not insert module lkm_example.ko: Invalid module format

> dmesg
[200617.480635] lkm_example: no symbol version for module_layout
[200617.480656] lkm_example: loading out-of-tree module taints kernel.
[200617.481542] module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 0000000074f1d70f, val ffffffffc0000158


> sudo modinfo lkm_example.ko
filename:       /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.ko
version:        0.01
description:    A simple example Linux module.
author:         Carlos Garcia
license:        GPL
srcversion:     F8B272146BAA2381B6332DE
depends:
retpoline:      Y
name:           lkm_example
vermagic:       4.19.84-microsoft-standard+ SMP mod_unload modversions
Run Code Online (Sandbox Code Playgroud)

这是我的 Makefile

obj-m += lkm_example.o
all:
    make -C /home/<usr>/WSL2-Linux-Kernel M=$(PWD) modules
clean:
    make -C /home/<usr>/WSL2-Linux-Kernel M=$(PWD) clean
test:
    # We put a — in front of the rmmod command to tell make to ignore
    # an error in case the module isn’t loaded.
    -sudo rmmod lkm_example
    # Clear the kernel log without echo
    sudo dmesg -C
    # Insert the module
    sudo insmod lkm_example.ko
    # Display the kernel log
    dmesg
unload:
    sudo rm /dev/lkm_example
    sudo rmmod lkm_example
Run Code Online (Sandbox Code Playgroud)

[Edit2] 这是我的内核模块:

make -C /sys/modules/$(shell uname -r)/build M=$(PWD) modules
Run Code Online (Sandbox Code Playgroud)

Han*_*h J 11

我必须为一项任务执行此操作,因此我想我将在这里分享我的解决方案。

基础 WSL2 内核不允许加载模块。您必须编译并使用您自己的内核构建。

如何在 WSL2 中编译和使用内核

  1. sudo git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
  2. sudo apt install build-essential flex bison libssl-dev libelf-dev
  3. cd WSL2-Linux-Kernel
  4. cp Microsoft/config-wsl .config
  5. sudo make -j $(expr $(nproc) - 1))
  6. 从 Windows,复制\\wsl$\DISTRO\home\USER\WSL2-Linux-Kernel\arch\x86\boot\bzimageC:\Users\WIN10_USER
  7. 创建C:\Users\WIN10_USER\.wslconfig包含以下内容的文件:
[wsl2]
kernel=C:\\Users\\WIN10_USER\\bzimage
Run Code Online (Sandbox Code Playgroud)

注意:双斜线 (\\) 是故意的。此外,为了避免潜在的旧错误,请确保不要在任何一行上留下任何尾随空格。

  1. 在 PowerShell 中,运行 wsl --shutdown
  2. 重新开启你的 WSL2 风味


如何编译模块
注意:您需要从 /home/USER/ 执行这些操作或调整 Makefile 以匹配您的位置。

  1. 创建一个Makefile包含:
obj-m:=lkm_example.o

all:
    make -C $(shell pwd)/WSL2-Linux-Kernel M=$(shell pwd) modules

clean:
    make -C $(shell pwd)/WSL2-Linux-Kernel M=$(shell pwd) clean
Run Code Online (Sandbox Code Playgroud)
  1. make


.wslconfig 文件步骤的来源:https ://www.bleepingcomputer.com/news/microsoft/windows-10-wsl2-now-allows-you-to-configure-global-options/


bk2*_*204 9

Linux 版本 2 的 Windows 子系统使用来自 Microsoft 的自定义 Linux 内核,其中包含编译的所有驱动程序。虽然它支持模块,但它不包含任何模块,正如您从 配置文件中。因此,没有理由发送/lib/modules目录。

此外,大多数可用于 WSL 的 Linux 发行版根本不提供带有内核的 WSL 版本。由于 Microsoft 提供自己的产品,因此没有理由这样做。那些 Linux 发行版不提供 Microsoft 内核的构建包,因为他们不负责;微软是,你需要和他们谈谈包。

如果您使用标准工具,您可能可以将模块加载到内核中,但您可能需要针对适当的源代码树进行构建。您可以尝试在我上面链接的 GitHub 存储库中找到合适的版本,或者您可能需要联系 Microsoft 并要求 GPLv2 下的源,他们需要根据要求向您提供。

我会注意到 WSL 的设计不允许加载自定义内核模块;它不是设计成一个完整的 Linux 环境,而是允许人们在 Windows 上开发和运行标准的 Linux 应用程序。如果您想进行 Linux 内核开发,您可能需要完整的 Linux 安装。