Skip to content

Instantly share code, notes, and snippets.

@oleksis
Last active February 21, 2024 19:48
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save oleksis/eb6d2f1cd2a6946faefb139aa0e38c35 to your computer and use it in GitHub Desktop.
Save oleksis/eb6d2f1cd2a6946faefb139aa0e38c35 to your computer and use it in GitHub Desktop.
Steps for compile Mainline Kernel Linux using WSL2

Compile Mainline Kernel for WSL2

Steps for compile Kernel Linux 5.15.0

Actual Kernel

$ uname -a
Linux DESKTOP-ID 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 GNU/Linux

Work Dir linux

$ mkdir -p linux
$ cd linux

Install Requirements for Build

$ sudo apt install git bc build-essential flex bison libssl-dev libelf-dev dwarves

Download Linux source code

$ wget https://github.com/torvalds/linux/archive/refs/tags/v5.15.zip -O v5.15.zip

Unzip Linux sources

$ unzip v5.15.zip

Download Config WSL

$ wget https://github.com/microsoft/WSL2-Linux-Kernel/raw/linux-msft-wsl-5.10.y/Microsoft/config-wsl

Copy Config WSL as Default Config to the Linux configs

$ cp config-wsl linux-5.15/arch/x86/configs/wsl_defconfig

Generate the Configuration and Build

$ cd linux-5.15
$ make KCONFIG_CONFIG=arch/x86/configs/wsl_defconfig -j4

Copy Linux kernel to YOUR_USER

$ cp arch/x86/boot/bzImage /mnt/c/Users/YOUR_USER/

Edit the configuration for WSL

$ vim /mnt/c/Users/YOUR_USER/.wslconfig
[wsl2]
kernel=C:\\Users\\YOUR_USER\\bzImage

Shutdown WSL

$ wsl --shutdown

Check shutdown the distro WSL

$ wsl -l -v

Start distro

$ wsl -d Debian

New Kernel Linux

$ uname -a
Linux HP450-2 5.15.0-oleksis-microsoft-standard-WSL2 #1 SMP Mon Nov 29 01:14:26 EST 2021 x86_64 x86_64 x86_64 GNU/Linux

Compile Mainline Kernel for WSL2

Steps for compile Kernel Linux 5.11.0 💕 Valentine's Day Edition 💕

Actual Kernel

$ uname -a
Linux DESKTOP-ID 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 GNU/Linux

Work Dir linux

$ mkdir -p linux
$ cd linux

Install Requirements for Build

$ sudo apt install git bc build-essential flex bison libssl-dev libelf-dev

Download Linux source code

$ wget https://github.com/torvalds/linux/archive/v5.11.zip -O v5.11.zip

Prepare the directory as Case Sensitive.

> $linuxSrc =  "linux-5.11"
> $net_netfilter = "$linuxSrc\net\netfilter"
> $uapi_netfilter = "$linuxSrc\include\uapi\linux\netfilter"
> $uapi_netfilter_ipv4 = "$linuxSrc\include\uapi\linux\netfilter_ipv4"
> $uapi_netfilter_ipv6 = "$linuxSrc\include\uapi\linux\netfilter_ipv6"
> $subfolders = @("$linuxSrc", "$linuxSrc\net", "$net_netfilter", "$linuxSrc\include", "$linuxSrc\include\uapi", "$linuxSrc\include\uapi\linux", "$uapi_netfilter", "$uapi_netfilter_ipv4", "$uapi_netfilter_ipv6")
> # Create sub-folders
> $subfolders | % {New-Item -Path ".\$_" -ItemType Dir}

Set Case Sensitive. Run as Administrator!

> @($net_netfilter, $uapi_netfilter, $uapi_netfilter_ipv4, $uapi_netfilter_ipv6) | % {fsutil.exe file setCaseSensitiveInfo "$_" enable}

Check Case Sensitive. Run as Administrator! (Optional)

> fsutil.exe file queryCaseSensitiveInfo $net_netfilter
El atributo que distingue mayúsculas de minúsculas del directorio .\linux-5.11\net\netfilter está habilitado.

Unzip Linux sources

$ unzip v5.11.zip

Download Config WSL

$ wget https://raw.githubusercontent.com/microsoft/WSL2-Linux-Kernel/linux-msft-wsl-4.19.y/Microsoft/config-wsl

Copy Config WSL as Default Config to the Linux configs

$ cp config-wsl linux-5.11/arch/x86/configs/wsl_defconfig

Generate the Configuration and Build

$ cd linux-5.11
$ make KCONFIG_CONFIG=arch/x86/configs/wsl_defconfig -j2

Copy Linux kernel to YOUR_USER

$ cp vmlinux /mnt/c/Users/YOUR_USER/

Edit the configuration for WSL

$ vim /mnt/c/Users/YOUR_USER/.wslconfig
[wsl2]
kernel=C:\\Users\\YOUR_USER\\vmlinux

Shutdown WSL

$ wsl --shutdown

Check shutdown the distro WSL

$ wsl -l -v

Start distro

$ wsl -d Debian

New Kernel Linux

$ uname -a
Linux DESKTOP-ID 5.11.0-oleksis #1 SMP Fri Feb 26 19:48:22 CST 2021 x86_64 GNU/Linux

Extras

Mount externals disk drivers. Run as Administrator!

wmic diskdrive list brief
wsl --mount \\.\PHYSICALDRIVE1 --bare
lsblk

Links

@homeassistant-hacs-m
Copy link

What's the point of case sensitivity? Isn't everything case sensitive inside the WSL2 where we're building?

@oleksis
Copy link
Author

oleksis commented Dec 11, 2023

Hi @homeassistant-hacs-m, please check more updated work of How compile the Linux Kernel

@homeassistant-hacs-m
Copy link

Hi @oleksis, the top instructions (5.15.0) worked just fine for me to add ecryptfs support.
I was just curious about the case sensitivity instructions for 5.11.0, wanted to learn.
(In the new thing you linked, what pops out to me is the usage of powershell.exe to escape wsl and find the user home, I wouldn't think of that!
Perhaps now that I learned the trick, I would replace it with powershell.exe 'cd $env:USERPROFILE; wsl pwd' to learn the location in terms of WSL and would do the actual copying and writing back in bash.)

@oleksis
Copy link
Author

oleksis commented Jan 19, 2024

Hi @homeassistant-hacs-m for more advanced commands check: wslinternals - A collection of some nifty WSL-related utilities

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment