如何在WSL中的ubuntu编译Linux内核并且安装使用ebpf?

如何在WSL中的ubuntu编译Linux内核并且安装使用ebpf?

环境:

  • wsl2
  • windows 11

步骤1 编译安装内核

https://kernel.org/找你想要的版本,

我这里使用6.8

获取源码

shell 复制代码
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可以修改会自己想要显示的名字

修改配置

shell 复制代码
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
shell 复制代码
cd ..
vim Makefile

这里的EXTRAVERSION和NAME随便修改。

编译

shell 复制代码
yes "\n" | make -j8 KCONFIG_CONFIG=Microsoft/config-wsl

等待一段时间出现:

Kernel: arch/x86/boot/bzImage is ready

说明编译成功了。

编译成功后配置

在windows系统中的:C:\Users[你的用户名]

目录下创建.wslconfig文件。将下面内容写入文件中。

shell 复制代码
[wsl2]
kernel=D:\\kernel

确保你的系统中有D盘,不然就将上面的D和下面的d替换为你的系统中存在的盘。

shell 复制代码
cp arch/x86/boot/bzImage /mnt/d/kernel

下面的必须执行。

shell 复制代码
make modules -j8
make modules_install -j8
make install -j8

重启WSL

shell 复制代码
wsl.exe --shutdown

测试

重新打开并且查看内核版本

shell 复制代码
uname -r

出现了包含自己修改的信息。

Done.

步骤2 安装bcc

对于ubuntu参考如下:https://github.com/iovisor/bcc/blob/master/INSTALL.md#ubuntu---source

安装依赖

参考你的版本选择不同代码复制下载。

shell 复制代码
# 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
shell 复制代码
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,编译

shell 复制代码
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

测试

shell 复制代码
cd example
python3 hello_world.py

打开另外一个terminal。运行:

shell 复制代码
ping www.baidu.com

在前一个terminal中输出:

结束!!!

Done.

相关推荐
码农小白12 分钟前
linux驱动:(22)中断节点和中断函数
linux·运维·服务器
4647的码农历程13 分钟前
Linux网络编程 -- 网络基础
linux·运维·网络
醉颜凉38 分钟前
银河麒麟桌面操作系统V10 SP1:取消安装应用的安全授权认证
运维·安全·操作系统·国产化·麒麟·kylin os·安全授权认证
C++忠实粉丝1 小时前
Linux环境基础开发工具使用(2)
linux·运维·服务器
康熙38bdc2 小时前
Linux 环境变量
linux·运维·服务器
存储服务专家StorageExpert2 小时前
DELL SC compellent存储的四种访问方式
运维·服务器·存储维护·emc存储
hakesashou3 小时前
python如何比较字符串
linux·开发语言·python
Ljubim.te3 小时前
Linux基于CentOS学习【进程状态】【进程优先级】【调度与切换】【进程挂起】【进程饥饿】
linux·学习·centos
cooldream20093 小时前
Linux性能调优技巧
linux
大G哥3 小时前
记一次K8S 环境应用nginx stable-alpine 解析内部域名失败排查思路
运维·nginx·云原生·容器·kubernetes