Building a custom kernel for WSL 2 on openSUSE
If you want to customize a WSL 2 kernel, I recommend starting from the default WSL 2 kernel maintained by Microsoft.
The Microsoft WSL 2 kernel tracks upstream LTS branch kernel releases. The WSL 2 kernel also contains a handful of out-of-tree patches and sane defaults for the WSL environment.
Download a tar of the latest release from the microsoft/WSL2-Linux-Kernel GitHub repository.
curl -s https://api.github.com/repos/microsoft/WSL2-Linux-Kernel/releases/latest | jq -r '.name' | sed 's/$/.tar.gz/' | sed 's#^#https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/tags/#' | wget -i -
Untar the .tar.gz file.
tar -xf *.tar.gz
Change to the kernel directory.
cd "$(find -type d -name "WSL2-Linux-Kernel-linux-msft-wsl-*")"
Install kernel building dependencies.
sudo bash -c "zypper in -y -t pattern devel_basis && zypper in -y bc openssl openssl-devel dwarves rpm-build libelf-devel"
Copy the Microsoft default kernel config files.
These are found in the "Microsoft" folder of the kernel source directory.
This can be copied to the default, which is .config in the kernel source directory:
cp Microsoft/config-wsl .config
Or for arm64 devices:
cp Microsoft/config-wsl-arm64 .config
You can then edit .config:
- Using a text editor, such as nano or vim.
- Interactively on the terminal using make config:
make config
- With a TUI using make menuconfig:
make menuconfig
When you have edited .config, like enabling KVM guest support, and are prepared to build your kernel run:
make -j 8
Take a brief walk.
Copy the compressed kernel to your Windows user home folder:
powershell.exe /C 'Copy-Item .\arch\x86\boot\bzImage $env:USERPROFILE'
Point to your custom kernel in .wslconfig:
powershell.exe /C 'Write-Output [wsl2]`nkernel=$env:USERPROFILE\bzImage | % {$_.replace("\","\\")} | Out-File $env:USERPROFILE\.wslconfig -encoding ASCII'
Restart WSL:
wsl.exe --shutdown
Confirm you are running your new kernel by checking the date, which should be very recent:
wsl.exe uname -a