Daily Shaarli

All links of one day in a single page.

January 20, 2022

Compile kernel for WSL2

Compile the kernel

# Install the needed packages
sudo apt install -y build-essential bison dwarves flex libssl-dev libelf-dev

# Clone kernel source
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
cd WSL2-Linux-Kernel

# Config kernel
cp Microsoft/config-wsl .config
sed -i 's/microsoft-standard-WSL2/generic/' .config # Change the LOCALVERSION value
sed -i 's/# CONFIG_SOUND is not set/CONFIG_SOUND=y\nCONFIG_SND=y/' .config # Enable sound modules
make olddefconfig

# Now that everything is ready, let's compile the kernel
make -j $(nproc)

# Copy the kernel into a directory in the Windows filesystem 
mkdir /mnt/c/wslkernel
cp arch/x86/boot/bzImage /mnt/c/wslkernel/custom_kernel

# Last step, the kernel needs to be referenced in the file .wslconfig 
nano /mnt/c/Users/<your username>/.wslconfig

The content of the .wslconfig should look like this:

[wsl2]
kernel = c:\\wslkernel\\custom_kernel

Now, reboot WSL by running the following command in PowerShell:

wsl --shutdown

Launch again a terminal with your WSL2 distro and confirm the new kernel is now being the one used:

uname -a

You should see generic in the output.

Install headers (for developing kernel modules)

sed -i 's/SPDX-License-Identifier: GPL-2.0/SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note/g' include/uapi/misc/d3dkmthk.h
sudo make headers_install INSTALL_HDR_PATH=/usr

sudo mkdir -p /lib/modules/$(uname -r)
sudo ln -s <path to WSL2-Linux-Kernel> /lib/modules/$(uname -r)/build