Post

wsl2下kernel编译

kernel的编译,在wsl2下。 另外因为我要用qemu调试,所以步骤可能有一丢丢特殊

下载kernel

国内直接从阿里镜像下的,试过官方那个cdn的,基本跑不满

我这边直接用最新的了

解压 + 安装必须的依赖

下的gz的包,直接tar xzf解压

1
sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison

这些玩意有什么作用

PackageDescription
gitTracks and makes a record of all changes during development in the source code. It also allows reverting the changes.
fakerootCreates a fake root environment.
build-essentialInstalls development tools such as C, C++, gcc, and g++.
ncurses-devProvides API for text-based terminals.
xz-utilsProvides fast file compression and decompression.
libssl-devSupports SSL and TLS to encrypt data and secure internet connections.
bcSupports the interactive execution of statements (Basic Calculator).
flexGenerates lexical analyzers that convert characters into tokens.
libelf-devProvides a shared library for managing ELF files (executables, core dumps, object code).
bisonConverts grammar descriptions to C programs.

另外,因为要使用qemu,所以需要额外的安装qemu相关的东西

1
2
sudo apt-get install qemu
sudo apt-get install qemu-system

这个版本是真的不高,后面可以自行编译一个更高版本的qemu

1
2
qemu-io version 5.2.0 (Debian 1:5.2+dfsg-11+deb11u4)
Copyright (c) 2003-2020 Fabrice Bellard and the QEMU Project developers

指定编译架构配置kernel

使用qemu的方式来编译kernel,必须指定架构

1
2
3
export ARCH=x86
make x86_64_defconfig
make menuconfig

如果不指定架构的话

1
2
cp -v /boot/config-$(uname -r) .config
make menuconfig

这里主要要做的是让kernel带调试信息的编译

编译内核

1
make -j$(nproc)

下载 + 配置busybox + 编译

busybox是一个非常小巧的工具集,提供了很多常用的命令行工具。

下载链接

制作rootfs

rootfs是Linux系统中最基本的文件系统,是其他所有文件系统的挂载点

启动qemu调试

qemu-system-x86_64的启动命令就可以了,这个看版本找最佳实践就可以了

compile commands生成

这一步要做编译之后做

1
python3 scripts/clang-tools/gen_compile_commands.py

另外这个有gcc专有的compile flags,会影响clangd的解析

  • -mindirect-branch
  • -mfunction-return

直接处理一下就可以了

REF

  1. WSL2之QEMU安装与使用
  2. Debug Linux kernel on WSL2 with VScode
  3. Linux Kernel – How to setup your editor
This post is licensed under CC BY 4.0 by the author.

Trending Tags