WSL 2 (Ubuntu18.04) compiles the Linux kernel (5.7.9) and replaces the original WSL 2 kernel

Ready to work

Configuration library

  Since a lot of libraries are needed during the compilation process, it needs to be configured in advance. If the errors encountered during the compilation process are recorded in the error message below. The command for the library to be installed is:

sudo apt-get install libncurses5-dev libncursesw5-dev
sudo apt-get install bison flex
sudo apt-get install libelf-dev libssl-dev

Prepare the Linux kernel

Download the Linux kernel you want to compile   at https://www.kernel.org . In order to adapt to the following configuration files, the author downloaded version 5.7.9.

  After moving the compressed package to the ~directory, decompress the file, specifically:

xz -d linux-5.7.9.tar.xz
tar -xf linux-5.7.9.tar

Download WSL 2 configuration file

  Because it is the process of compiling and replacing the Linux kernel for WSL 2, directly compiling the kernel will not be able to adapt to the WSL 2 system, resulting in failure to start normally. The method of directly compiling the kernel is also recorded at the end of the article. So go to download the relevant configuration file, because the official file is older, so go to GitHub to download the newer version, the address link , the configuration file adapts to the 5.7.x kernel.

  Name the file config-wsl. In linux-5.7.9creating a new folder under the folder Microsoft, and config-wslmove folders Microsoftin.

  The above process is as follows:

Insert picture description here
  You can config-wslmake certain modifications to the file, such as changing the suffix of the kernel version name to -microsoft-wsl-lyg-port, and the personalized marking information has been completed, as shown below. But note that the suffix can not be too long , it may cause kernel does not boot properly; while the suffix can not contain (, , ), { , }or the compiler will report an error.

Insert picture description here

Compile

linux-5.7.9Use the command   in the folder to compile as follows, and complete the compilation in about 5~10mins.

sudo make KCONFIG_CONFIG=Microsoft/config-wsl -j4

  As shown below:

Insert picture description here

Insert picture description here

Replace the kernel

  First check the original kernel information, as:

Insert picture description here

  Next, replace the kernel. It can be seen from the "Compilation Result Map" that the compiled kernel file is stored in it ~/linux-5.7.9/arch/x86/boot/bzImage, so copy the file to the E://disk as follows:

Insert picture description here

  Close WSL (you need to use Power Shell commands wsl --shutdown, otherwise the replacement cannot be completed). Rename the file kerneland copy it to the path where WSL is located C:\Windows\System32\lxss\tools, replacing the original kernel, as follows:

Insert picture description here

  Finally, restart WSL and check the kernel version information and creation time, as follows:

Insert picture description here

  WSL runs successfully and outputs the new version information, successfully replaced.

Related error

NO.1

The libncurses5-dev and libncursesw5-dev libraries are required during the make menuconfig process

sudo apt-get install libncurses5-dev libncursesw5-dev

NO.2

/bin/sh: 1: flex: not found
scripts/Makefile.host:9: recipe for target ‘scripts/kconfig/lexer.lex.c’ failed

sudo apt-get install bison
sudo apt-get install flex

NO.3

“CANNOT GENERATE ORC METADATA FOR CONFIG_UNWINDER_ORC=Y, PLEASE INSTALL LIBELF-DEV, LIBELF-DEVEL OR ELFUTILS-LIBELF-DEVEL”. STOP.

sudo apt install libelf-dev
sudo apt install libssl-dev

NO.4

scripts/kconfig/Makefile:71: recipe for target ‘syncconfig’ failed
make[2]: *** [syncconfig] Error 1
Makefile:588: recipe for target ‘syncconfig’ failed
make[1]: *** [syncconfig] Error 2
Makefile:695: recipe for target ‘include/config/auto.conf.cmd’ failed
make: *** [include/config/auto.conf.cmd] Error 2

It sudocan be used by directly elevating the permissions , specifically:

sudo make KCONFIG_CONFIG=Microsoft/config-wsl

Digression: WSL directly compiles the Linux kernel

  Download the Linux kernel, unzip it, enter the directory and execute the command make menuconfigas follows:

Insert picture description here

  Then enter the graphical interface, the configuration information can directly follow the default settings, so just exit directly, as follows:

Insert picture description here

  Then compile the kernel, using the following commands in turn:

sudo make -j4
sudo make modules_install
sudo make install

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

  At this point, the kernel compilation is completed. The compilation time is about 5mins.


Reference

https://blog.csdn.net/qq_40856284/article/details/106535962
https://blog.csdn.net/ibless/article/details/82349507
https://github.com/xieyubo/WSL2-Linux-Kernel/blob/wsl-xyb-port-5.7.y/Microsoft/config-wsl
https://www.kernel.org/

Guess you like

Origin blog.csdn.net/m0_46161993/article/details/109062992