A small example project to setup a Debian/Ubuntu-based WSL2 instance for Linux kernel module development.
While starting with kernel module development on most Linux distributions, you just need to obtain the linux-headers-*
package for your kernel version using your package manager and you're golden.
With WSL2, the issue is that the WSL kernel is shared between all WSL2 instances and managed separately from the userspace. Thus, the version of the kernel (uname -r
) will typically be different from what the distribution userspace was originally built for, and installing the linux-headers-*
package will install headers for the wrong kernel version.
To compile kernel modules for the WSL2 kernel, we will need to download and build the correct kernel version ourselves, which is what this project is trying to do for you.
- Clone this repository into your WSL home directory (note that you should NOT use a directory on your Windows partition):
wsl # switch to WSL
cd ~ # move to the home directory INSIDE the WSL filesystem
git clone github.com/MatejKafka/wsl2-kernel-module
- Install dependencies, clone and build the WSL2 kernel for your current kernel version. This step will take some time (~15 minutes on my laptop):
cd wsl2-kernel-module
make
- Compile the "hello world"
first_module
:
# compile and load the module
make -C first_module load
# unload it
make -C first_module unload
- Check that the module was correctly loaded and unloaded:
# view the last 2 messages in the kernel log
dmesg | tail -n 5
# ...
# [ 1290.261435] first_module: module loaded
# [ 1290.267202] first_module: unloading module
To create a new kernel module:
- Add a new subdirectory.
- Copy the
Makefile
fromfirst_module
. - Modify the
MODULE_NAME
variable in theMakefile
to match the subdirectory name - Create a new
.c
file with the filename matchingMODULE_NAME
. - Compile and load the module by running
make -C <dirname> load
.