安装基本工具

  • 使用如下命令安装
1
2
sudo apt update && sudo apt -y upgrade
sudo apt -y install build-essential libncurses-dev bison flex libssl-dev libelf-dev cpu-checker qemu-kvm libvirtd virt-manager git
  • 在这里出现一个报错,请看问题一

编译内核

  • 先查看正在使用的wsl2是的内核版本是哪个版本
  • 使用win + R,然后输入wsl,进入wsl后输入uname -r,就会出现相应的内核版本,比如我的版本就是5.15.146.1的内核版本,即5.15版本

image-20240906162303809

  • 然后使用git命令拉取微软官方修改的wsl2内核的仓库这里建议直接去官网下载压缩包,github拉取太大的文件老是会出现一些状况
1
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
  • 由于拉取的仓库太大,所以我选择拉取相应内核的分支,可以省下不少时间和存储空间,拉取时间较长,干点别的事情,拉取的时候还出现了一个问题,详细请看问题2
1
git clone --branch linux-msft-wsl-5.15.y --single-branch https://github.com/microsoft/WSL2-Linux-Kernel.git
  • 进入相应版本的内核,我这边是下载zip压缩包后解压,这边直接cd进入即可
1
cd WSL2-Linux-Kernel-linux-msft-wsl-5.15.y/
  • 然后使用如下命令将当前内核配置复制一份到所在目录文件下
1
zcat /proc/config.gz > .config
  • 使用make menuconfig,查看和修改 .config 文件中的配置选项。这边使用该命令出现一个问题,详细请看问题3
  • 进入如下界面,选择Virtualization回车键进去

image-20240906190035785

  • 进入后按照如下设置virtio-net 也被选中 ( 如果有 )
    • 可以编译成模块再加载,但我选择直接编译进内核,同时可以编译 Multipath 依赖的支持,以便后续 systemd 可以少一个报错

image-20240906190320257

  • 按两次 ESC,再导航至 Processor type and features -> Linux guest support,确定 KVM Guest support 已被选中

image-20240906210535695

image-20240906210608456

image-20240906210631128

  • 之后保存并退出

image-20240906190428102

  • 开始编译
1
make -j 8
  • 编译时出现脚本没有执行权限,直接一键给执行权限,然后再编译
1
2
chmod +x scripts/*.sh
chmod +x scripts/remove-stale-files
  • 编译要一步一步找错误了,编译时出错找错就不详细说明了,编译需要点时间,直接干别的事情

  • 出现这个就算是编译好了

image-20240906201314300

  • 在d盘新建文件夹bzImage
  • 然后使用wsl2下的命令行输入,复制的目标路径为Windows文件目录,注意:内核不要保存在有中文路径的目录
1
cp arch/x86/boot/bzImage /mnt/d/bzImage
  • 然后修改配置
1
vim /mnt/c/Users/<username>/.wslconfig
  • 然后写入
1
2
3
[wsl2]
nestedVirtualization=true
kernel=D:\\bzImage
  • 然后重启wsl2,打开Powershell,输入命令
1
wsl --shutdown Ubuntu
  • 然后再打开新的wsl窗口即可

  • 验证:打开wsl后输入,这个时间会出现你编译内核的时间

1
uname -ar

image-20240906204439531

  • 验证支不支持虚拟化kvm-ok

image-20240906204509637

问题

问题1

  • 安装基本工具的时候出现了如下问题
  • 一个是关于 ldconfig.real 的警告,另一个是关于 libvirt 包无法定位的问题。
1
2
3
4
5
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'qemu-system-x86' instead of 'qemu-kvm'
E: Unable to locate package libvirtd
  • 解决如下:关于 libvirt 包无法定位的问题
    • 安装以下包来替代 libvirtd
1
2
3
sudo apt install libvirt-daemon-system libvirt-clients
sudo apt install libvirt-clients
sudo apt install virt-manager
  • 这样就可以在wsl上使用qemu了,比在VMware上使用流畅很多,而且打pwn的动调也可以使用wsl了,不用两边切换了
  • 用qemu开虚拟机已经非常流畅不会卡了

问题2

  • 在拉取仓库的时候遇到如下问题:通常是由于网络问题、服务器响应超时或者存储空间不足等问题导致的。
1
RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function 和 fetch-pack: unexpected disconnect while reading sideband packet
  • 增加缓存容量
1
git config --global http.postBuffer 3000000000
  • 最后还是出错,直接上github官网上下载对应内核版本的zip压缩包

问题3

  • 使用make menuconfig命令出现报错。
1
2
3
4
5
6
7
8
9
10
*
* Unable to find the ncurses package.
* Install ncurses (ncurses-devel or libncurses-dev
* depending on your distribution).
*
* You may also need to install pkg-config to find the
* ncurses installed in a non-default location.
*
make[1]: *** [scripts/kconfig/Makefile:211: scripts/kconfig/mconf-cfg] Error 1
make: *** [Makefile:627: menuconfig] Error 2
  • 问题是缺少 ncurses 包,使用如下命令解决
1
2
sudo apt update
sudo apt install libncurses-dev pkg-config
  • 安装完包后再运行,仍出现错误
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  UPD     scripts/kconfig/mconf-cfg
HOSTCC scripts/kconfig/mconf.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
/bin/sh: 1: flex: not found
make[1]: *** [scripts/Makefile.host:9: scripts/kconfig/lexer.lex.c] Error 127
make: *** [Makefile:627: menuconfig] Error 2
  • 问题是:flex 工具没有安装,使用如下命令安装
1
2
sudo apt update
sudo apt install flex
  • 再次运行还是错误bison 工具没有安装
1
2
sudo apt update
sudo apt install bison
  • 仍然出现报错
1
2
3
4
5
6
7
8
9
10
11
12
  YACC    scripts/kconfig/parser.tab.[ch]
HOSTCC scripts/kconfig/lexer.lex.o
HOSTCC scripts/kconfig/menu.o
HOSTCC scripts/kconfig/parser.tab.o
HOSTCC scripts/kconfig/preprocess.o
HOSTCC scripts/kconfig/symbol.o
HOSTCC scripts/kconfig/util.o
HOSTLD scripts/kconfig/mconf
sh: 1: ./scripts/cc-version.sh: Permission denied
scripts/Kconfig.include:44: Sorry, this compiler is not supported.
make[1]: *** [scripts/kconfig/Makefile:48: menuconfig] Error 1
make: *** [Makefile:627: menuconfig] Error 2
  • ./scripts/cc-version.sh: Permission denied表示 ./scripts/cc-version.sh 脚本没有执行权限。修改权限即可
1
2
3
4
5
6
7
8
chmod +x ./scripts/cc-version.sh
chmod +x ./scripts/min-tool-version.sh
chmod +x ./scripts/as-version.sh
chmod +x ./scripts/ld-version.sh

# 不想一个一个加执行权限直接如下命令
find . -type f -name "*.sh" -exec chmod +x {} +

  • Sorry, this compiler is not supported:这个错误表示当前的编译器版本不被支持。对于内核编译,推荐使用较新的 GCC 版本,通常是 GCC 7 或更高版本。你可以通过以下命令安装
1
2
3
4
5
6
7
sudo apt update
sudo apt install gcc g++ # 安装 GCC 和 G++

# 设置编译器环境变量
export CC=gcc-7
export CXX=g++-7

  • 确认你的系统上安装了适合的汇编器版本。对于内核编译,推荐使用较新的汇编器版本。通常,binutils 包含了所需的汇编器工具。你可以通过以下命令安装或更新 binutils
1
2
sudo apt update
sudo apt install binutils
  • 问题解决:

image-20240906182120686