使用高性能、轻量级 lima 方案:
# 1. 安装 lima
$ brew update
$ brew install lima
# 2. 创建如下配置文件
$ cat ebpf-dev.yaml
images:
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
arch: "x86_64"
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img"
arch: "aarch64"
cpus: 4
memory: "10GiB"
mounts:
- location: "~"
writable: true
- location: "/tmp/lima"
writable: true
provision:
- mode: system
script: |
apt-get update
apt-get install -y apt-transport-https ca-certificates curl clang llvm jq
apt-get install -y libelf-dev libpcap-dev libbfd-dev binutils-dev build-essential make
apt-get install -y linux-tools-common linux-tools-5.15.0-41-generic bpfcc-tools
apt-get install -y python3-pip
# 3. 使用上面配置启动一个 vm (需要一段时间下载镜像和安装包)
$ limactl start --name ebpf-dev ./ebpf-dev.yaml # 命令提示选择时,直接回车即可
# 4. 进入 vm
$ limactl shell ebpf-dev
$ sudo -s # 切换到 root 账号
# 5. 编译安装 libbpf
root@lima-ebpf-dev:~# cd /root
root@lima-ebpf-dev:~# git clone --branch v1.2.2 https://github.com/libbpf/libbpf.git
root@lima-ebpf-dev:~# cd libbpf/src
root@lima-ebpf-dev:src# make install
# 6. 查看安装的头文件
root@lima-ebpf-dev:~/bpftool/src# ls -l /usr/include/bpf/
total 400
-rw-r--r-- 1 root root 20168 Jun 28 07:49 bpf.h
-rw-r--r-- 1 root root 19571 Jun 28 07:49 bpf_core_read.h
-rw-r--r-- 1 root root 3750 Jun 28 07:49 bpf_endian.h
-rw-r--r-- 1 root root 172987 Jun 28 07:49 bpf_helper_defs.h
-rw-r--r-- 1 root root 14790 Jun 28 07:49 bpf_helpers.h
-rw-r--r-- 1 root root 37885 Jun 28 07:49 bpf_tracing.h
-rw-r--r-- 1 root root 18928 Jun 28 07:49 btf.h
-rw-r--r-- 1 root root 65749 Jun 28 07:49 libbpf.h
-rw-r--r-- 1 root root 2773 Jun 28 07:49 libbpf_common.h
-rw-r--r-- 1 root root 5189 Jun 28 07:49 libbpf_legacy.h
-rw-r--r-- 1 root root 242 Jun 28 07:49 libbpf_version.h
-rw-r--r-- 1 root root 9339 Jun 28 07:49 skel_internal.h
-rw-r--r-- 1 root root 8618 Jun 28 07:49 usdt.bpf.h
# 7. 编译安装 bpftool
root@lima-ebpf-dev:~# cd /root
root@lima-ebpf-dev:~# git clone --branch v7.2.0 https://github.com/libbpf/bpftool.git
root@lima-ebpf-dev:~# cd bpftool
root@lima-ebpf-dev:~# git submodule update --init
root@lima-ebpf-dev:~# cd src
root@lima-ebpf-dev:src# make install
# 8. 下载和安装 go
root@lima-ebpf-dev:~# cd /root
root@lima-ebpf-dev:~# wget https://go.dev/dl/go1.20.5.linux-amd64.tar.gz
root@lima-ebpf-dev:~# tar -xzvf https://go.dev/dl/go1.20.5.linux-amd64.tar.gz -C /usr/local
root@lima-ebpf-dev:~# PATH=/usr/local/go/bin:$PATH
root@lima-ebpf-dev:~# echo 'PATH=/usr/local/go/bin:$PATH' >>~/.bashrc
root@lima-ebpf-dev:~# echo 'GOPROXY="https://goproxy.cn,https://goproxy.io,direct"' >> ~/.bashrc
root@lima-ebpf-dev:~# source ~/.bashrc
root@lima-ebpf-dev:~# go version
go version go1.20.5 linux/amd64