Run your own kernel with WSL2
In case you need a specific kernel feature that the vanilla kernel shipped from Microsoft for WSL2 does not include then you can build a custom one and instruct WSL2 to load it instead of loading the default one. This is simpler then it sounds, so I have gathered the steps below as a future cookie for me and others. Take a note of the expiration date though. Cookies do tend to expire…
As an example, I will add Open-vswitch
support at the kernel since I wanted that for some networking tests. You can go and configure anything you need.
- Inside WSL2 console, fetch the kernel code. Here we fetch the same Microsoft published Linux kernel though you could grab one from kernel.org if you feel so.
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git — depth=1 -b linux-msft-wsl-6.1.y
2. Install the required packages so as to be able to build the kernel:
sudo apt update && sudo apt install build-essential flex bison libssl-dev libelf-dev libncurses5-dev
3. Configure the Microsoft WSL2 kernel:
make menuconfig KCONFIG_CONFIG=Microsoft/config-wsl
4. Enable the kernel feature you need. In my case it will be: Networking support -> Open vSwitch
5. Compile the kernel:
make -j$(nproc) KCONFIG_CONFIG=Microsoft/config-wsl
6. Install the kernel modules and headers:
make modules_install headers_install
7. Create a directory and copy your custom kernel there. Replace USER
with your username:
mkdir -p /mnt/c/Users/USER/.wsl-kernels/
cp arch/x86/boot/bzImage /mnt/c/Users/USER/.wsl-kernels/
8. Configure WSL2 to point to this new kernel. Create or edit the file /mnt/c/Users/USER/.wslconfig
with the following content:
[wsl2]
kernel=C:\\Users\\USER\\.wsl-kernels\\bzImage
9. Shutdown and start WSL:
wsl --shutdown
wsl
10. Verify kernel version:
uname -r