如何在WSL中的ubuntu编译Linux内核并且安装使用ebpf?
- 步骤1 编译安装内核
- 获取源码
- 修改配置
- 编译
- 编译成功后配置
- 重启WSL
- 测试
- 步骤2 安装bcc
- 安装依赖
- 下载bcc,编译
- 测试
环境:
- wsl2
- windows 11
步骤1 编译安装内核
去https://kernel.org/找你想要的版本,
我这里使用6.8
获取源码
wget -O- https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.8.tar.xz
tar -xvf linux-6.8.tar.xz
cd linux-6.8
mkdir Microsoft
wget -O Microsoft/config-wsl https://github.com/microsoft/WSL2-Linux-Kernel/raw/linux-msft-wsl-6.1.y/arch/x86/configs/config-wsl
cd Microsoft
vim config-wsl
将下面的配置添加到config-wsl中。
在config-wsl中的CONFIG_LOCALVERSION=“xxxxxx”
里面的xxxxxxx可以修改会自己想要显示的名字
修改配置
CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y
# [optional, for tc filters]
CONFIG_NET_CLS_BPF=m
# [optional, for tc actions]
CONFIG_NET_ACT_BPF=m
CONFIG_BPF_JIT=y
# [for Linux kernel versions 4.1 through 4.6]
CONFIG_HAVE_BPF_JIT=y
# [for Linux kernel versions 4.7 and later]
CONFIG_HAVE_EBPF_JIT=y
# [optional, for kprobes]
CONFIG_BPF_EVENTS=y
# Need kernel headers through /sys/kernel/kheaders.tar.xz
CONFIG_IKHEADERS=y
cd ..
vim Makefile
这里的EXTRAVERSION和NAME随便修改。
编译
yes "\n" | make -j8 KCONFIG_CONFIG=Microsoft/config-wsl
等待一段时间出现:
Kernel: arch/x86/boot/bzImage is ready
说明编译成功了。
编译成功后配置
在windows系统中的:C:\Users[你的用户名]
目录下创建.wslconfig文件。将下面内容写入文件中。
[wsl2]
kernel=D:\\kernel
确保你的系统中有D盘,不然就将上面的D和下面的d替换为你的系统中存在的盘。
cp arch/x86/boot/bzImage /mnt/d/kernel
下面的必须执行。
make modules -j8
make modules_install -j8
make install -j8
重启WSL
wsl.exe --shutdown
测试
重新打开并且查看内核版本
uname -r
出现了包含自己修改的信息。
Done.
步骤2 安装bcc
对于ubuntu参考如下:https://github.com/iovisor/bcc/blob/master/INSTALL.md#ubuntu—source
安装依赖
参考你的版本选择不同代码复制下载。
# For Focal (20.04.1 LTS)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm12 llvm-12-dev libclang-12-dev python zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev arping netperf iperf
# For Hirsute (21.04) or Impish (21.10)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm12 llvm-12-dev libclang-12-dev python3 zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev arping netperf iperf
# For Jammy (22.04)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm14 llvm-14-dev libclang-14-dev python3 zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev libdebuginfod-dev arping netperf iperf
# For Lunar Lobster (23.04)
sudo apt install -y zip bison build-essential cmake flex git libedit-dev \
libllvm15 llvm-15-dev libclang-15-dev python3 zlib1g-dev libelf-dev libfl-dev python3-setuptools \
liblzma-dev libdebuginfod-dev arping netperf iperf libpolly-15-dev
# For other versions
sudo apt-get -y install zip bison build-essential cmake flex git libedit-dev \
libllvm3.7 llvm-3.7-dev libclang-3.7-dev python zlib1g-dev libelf-dev python3-setuptools \
liblzma-dev arping netperf iperf
# For Lua support
sudo apt-get -y install luajit luajit-5.1-dev
sudo apt install -y python-is-python3
sudo apt install -y bison build-essential cmake flex git libedit-dev libllvm11 llvm-11-dev libclang-11-dev zlib1g-dev libelf-dev libfl-dev python3-distutils
下载bcc,编译
git clone https://github.com/iovisor/bcc.git
mkdir bcc/build; cd bcc/build
cmake ..
make
sudo make install
cmake -DPYTHON_CMD=python3 .. # build python3 binding
pushd src/python/
make
sudo make install
popd
测试
cd example
python3 hello_world.py
打开另外一个terminal。运行:
ping www.baidu.com
在前一个terminal中输出:
结束!!!
Done.