下载 WSL 核心源码:https://github.com/microsoft/WSL2-Linux-Kernel/tags
root@FENG:~/workspace_redroid# date Sun Dec 10 15:42:26 CST 2023 root@FENG:~/workspace_redroid# root@FENG:~/workspace_redroid# wget https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/tags/linux-msft-wsl-5.15.133.1.tar.gz --2023-12-10 15:42:30-- https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/tags/linux-msft-wsl-5.15.133.1.tar.gz Resolving github.com (github.com)... 198.18.0.20 Connecting to github.com (github.com)|198.18.0.20|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://codeload.github.com/microsoft/WSL2-Linux-Kernel/tar.gz/refs/tags/linux-msft-wsl-5.15.133.1 [following] --2023-12-10 15:42:31-- https://codeload.github.com/microsoft/WSL2-Linux-Kernel/tar.gz/refs/tags/linux-msft-wsl-5.15.133.1 Resolving codeload.github.com (codeload.github.com)... 198.18.0.66 Connecting to codeload.github.com (codeload.github.com)|198.18.0.66|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/x-gzip] Saving to: ‘linux-msft-wsl-5.15.133.1.tar.gz’ linux-msft-wsl-5.15.133.1.tar [ <=> ] 189.80M 5.29MB/s in 38s 2023-12-10 15:43:10 (4.93 MB/s) - ‘linux-msft-wsl-5.15.133.1.tar.gz’ saved [199022201]
- 解压源码压缩包
tar -zxvf linux-msft-wsl-5.15.133.1.tar.gz
cd WSL2-Linux-Kernel-linux-msft-wsl-5.15.133.1/ cp Microsoft/config-wsl .config
启用核心特性
cat >> .config <<EOF # ipv6 should enabled CONFIG_IPV6_ROUTER_PREF=y CONFIG_IPV6_ROUTE_INFO=y CONFIG_IPV6_MULTIPLE_TABLES=y CONFIG_IPV6_SUBTREES=y # codec2 required, can use ION for legacy kernel CONFIG_DMABUF_HEAPS=y CONFIG_DMABUF_HEAPS_SYSTEM=y # optional, can use memfd CONFIG_STAGING=y CONFIG_ASHMEM=y # binderfs required CONFIG_ANDROID=y CONFIG_ANDROID_BINDER_IPC=y CONFIG_ANDROID_BINDERFS=y CONFIG_ANDROID_BINDER_DEVICES="binder,hwbinder,vndbinder" EOF
编译核心
修改.config
,停用CONFIG_DEBUG_INFO_BTF
sed -i 's/CONFIG_DEBUG_INFO_BTF=y/CONFIG_DEBUG_INFO_BTF=n/g' .config
备注:不停用的话,会报
BTF: .tmp_vmlinux.btf: pahole (pahole) is not available Failed to generate BTF for vmlinux Try to disable CONFIG_DEBUG_INFO_BTF make: *** [Makefile:1227: vmlinux] Error 1
安装相关依赖
sudo apt update -y sudo apt install flex -y sudo apt install bison -y sudo apt install -y openssl sudo apt install -y libssl-dev sudo apt install -y bc sudo apt install -y libelf-dev
开始编译
sudo make
- 编译成功,镜像保存在
arch/x86_64/boot/bzImage
使用新镜像文件
打开powershell
cd ~ wsl cat >> .wslconfig <<EOF [wsl2] kernel=<KERNEL-PATH> EOF
- <KERNEL-PATH> 需要替换成你的路径(自定义 Linux 内核的绝对 Windows 路径)。例如,我的是
kernel=kernel=C:\\Users\\FENG\\WSL2-Linux-Kernel-linux-msft-wsl-5.15.133.1\\arch\\x86\\boot\\bzImage
重启 wsl
打开 powershell
wsl --shutdown wsl uname -a
- 没有报错并且显示编译时间为今天即完事,遇到其他问题私聊。