2

I have the source code of a hello world kernel module that works in Ubuntu 20 in a laptop. Now I am trying to compile the same code in Ubuntu 20 but inside WSL2. For that I am using this:

make -C /sys/modules/$(shell uname -r)/build M=$(PWD) modules

The problem is that /lib/modules is empty. It seems that WSL2 does not bring anything in /lib/modules/4.19.104-microsoft-standard/build

I tried getting the headers using:

sudo apt search linux-headers-`uname -r`

Sorting... Done
Full Text Search... Done

But nothing get's populated in the modules folder

Is there anything I need to do in order that folder contains all required modules?

4

The Windows Subsystem for Linux version 2 uses a custom Linux kernel from Microsoft that contains all of its drivers compiled in. While it has support for modules, it doesn't contain any, as you can see from the configuration file. Therefore, there's no reason to ship a /lib/modules directory.

In addition, most of the Linux distros available for WSL don't ship their WSL versions with a kernel at all. Since Microsoft ships their own, there's no reason to. Those Linux distros don't ship build packages for Microsoft's kernel because they aren't responsible for it; Microsoft is, and you'd need to talk to them about the packages.

It may be the case that you can load the modules into the kernel if you use the standard tools, but you'll likely need to build against an appropriate source tree. You can either try to find the appropriate version in the GitHub repository I linked to above, or you may need to contact Microsoft and ask for the source under the GPLv2, which they are required to provide you upon request.

I will note that WSL is not designed to allow loading custom kernel modules; it's not designed to be a full Linux environment, but rather to allow folks to develop and run standard Linux applications on Windows. If you want to do Linux kernel development, you'll probably need a full Linux installation.

| improve this answer | |
-1

WLS2 does not have a Linux Kernel.

there are no kernel headers for WSL's "kernel" (actually a Windows driver)

Source

You might be able to get away with compiling still by getting another kernel

# If asked to install grub, you can simply skip using Esc
sudo update && sudo apt install linux-generic -y


ls /lib/modules
5.7.2-arch1-1

You will probably have different output, but you should have kernel headers installed. You can modify your original command using whatever you get in lib/modules

make -C /lib/modules/<kernel source from previous step>/build M=$(PWD) modules
| improve this answer | |
  • Thank you very much for your answer! I believe the biggest difference from WSL and WSL2 is that the latest has a real kernel: github.com/microsoft/WSL2-Linux-Kernel. I still don't know how to get the headers, I am too new to this :) – Carlos Garcia Jun 23 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.