本来是打算拿CentOS 7.6来作为ebpf开发的arm64环境的,奈何制作启动盘在飞腾机器上一直没法引导。又想到Anolis 7是完全兼容CentOS7的,而且内核环境更加友好,对国产CPU架构也支持的不错,所以本文选取了Anolis OS 7.7 作为eBPF开发环境搭建系统。
如果是x86_64架构,那无需阅读本文了,因为Centos7 eBPF开发环境搭建等文章也介绍得十分详细。另外,本文不过是参考其之后而有的arm64翻版。
简述一下,我们主要做以下事情:
安装 cmake
cmake直接下载sh包,运行安装:
1
2
3
4
5
6
7
|
# 下载
wget https://cmake.org/files/v3.21/cmake-3.21.0-linux-aarch64.sh
# 安装
./cmake-3.21.0-linux-aarch64.sh --prefix=/usr/local --exclude-subdir
# 运行
cmake --version
cmake version 3.21.0
|
升级 gcc
因为需要安装llvm,而默认的gcc4.8.5无法满足编译llvm的最低要求。因此,需要升级gcc。
因为没有找到安装包,因而采用源码编译安装的方式升级。以下提供两种方法,选择其中之一即可,推荐方法一,这样可以避免一些意想不到的麻烦。
方法一(推荐):centos-release-scl
因为Anolis 7和CentOS 7是兼容的,所以我们当然可以使用CentOS的源来安装软件,使用阿里云的CentOS7 arm64的源:
1
2
|
# https://mirrors.aliyun.com/centos-altarch/
wget http://mirrors.aliyun.com/repo/Centos-altarch-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
|
然后,修改CentOS-Base.repo
文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[centosbase]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aliyun.com/centos-altarch/$releasever/os/$basearch/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos-altarch/7/os/$basearch/RPM-GPG-KEY-CentOS-7
enabled=1
#released updates
[centosupdates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.aliyun.com/centos-altarch/$releasever/updates/$basearch/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos-altarch/7/os/$basearch/RPM-GPG-KEY-CentOS-7
enabled=1
#additional packages that may be useful
[centosextras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.aliyun.com/centos-altarch/$releasever/extras/$basearch/
gpgcheck=0
gpgkey=http://mirrors.aliyun.com/centos-altarch/7/os/$basearch/RPM-GPG-KEY-CentOS-7
enabled=1
#additional packages that extend functionality of existing packages
[centoscentosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.aliyun.com/centos-altarch/$releasever/centosplus/$basearch/
gpgcheck=0
enabled=1
gpgkey=http://mirrors.aliyun.com/centos-altarch/7/os/$basearch/RPM-GPG-KEY-CentOS-7
|
然后,
1
2
3
|
# 更新yum
sudo yum clean all
sudo yum makecache
|
安装gcc7,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
yum install centos-release-scl
yum install devtoolset-7
# 当前bash会话生效
scl enable devtoolset-7 bash
# 重启后生效
echo "source /opt/rh/devtoolset-7/enable" >> ~/.bash_profile
source /opt/rh/devtoolset-7/enable
# gcc
gcc --version
gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
方法二:源码编译
在此之前,需要先安装gcc4.8.5用于编译。
1
2
3
4
5
6
|
# 先安装 gcc 4.8.5
sudo yum install -y gcc gcc-c++
# 安装编译依赖
sudo yum -y install bzip2 wget gmp-devel mpfr-devel libmpc-devel make
sudo yum -y install zlib-devel
|
这里我选择升级到7.3.0
1
2
3
4
5
6
7
8
9
10
11
12
|
# 下载gcc 7.3.0 源码
wget https://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz
# 解压
tar -zxvf gcc-7.3.0.tar.gz
# 下载依赖包 gmp, mpfr, mpc
./contrib/download_prerequisites
# 创建编译目录
cd gcc-7.3.0
mkdir build && cd build
#
../configure --prefix=/usr/local/gcc-7.3.0 --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,go --enable-plugin --enable-initfini-array --disable-libgcj --enable-gnu-indirect-function --disable-multilib
|
然后编译安装:
1
2
|
make -j4
make install
|
因为机器上还存在gcc 4.8.5, 所以需要设置一下环境变量:
1
2
3
4
5
6
7
8
9
10
11
|
sudo ln -s /usr/local/gcc-7.3.0/bin/gcc /usr/local/bin/gcc
sudo ln -s /usr/local/gcc-7.3.0/bin/g++ /usr/local/bin/g++
sudo ln -s /usr/local/bin/gcc /usr/local/bin/cc
sudo ln -s /usr/local/bin/g++ /usr/local/bin/c++
# 编辑/etc/profile
sudo vi /etc/profile
# 添加内容, 保存后重启
export PATH=/usr/local/gcc-7.3.0/bin:$PATH
export CC=/usr/local/bin/cc
export C++=/usr/local/bin/c++
|
安装 llvm
Anolis 7默认的llvm不能满足bpf的编译,这里我选择下载12.0.1
版本。
首先,下载llvm-project.src.tar.xz包:
1
2
|
# github 下载太慢了 使用了代理
wget https://mirror.ghproxy.com/https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/llvm-project-12.0.1.src.tar.xz
|
编译llvm还需要安装pyhton3:
1
|
sudo yum install -y python3
|
解压然后编译安装(编译的过程很漫长):
1
2
3
4
5
6
7
8
|
tar -xvf llvm-project-12.0.1.tar.xz
cd llvm-project-12.0.1
mkdir build && cd build
# (如果是方法一安装的gcc,请忽略此步)设置LD PATH 不然会编译报错
export LD_LIBRARY_PATH=/usr/local/gcc-7.3.0/lib64:$LD_LIBRARY_PATH
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" -G "Unix Makefiles" ../llvm
make -j4
make install
|
libbpf-bootstrap
安装依赖:
1
|
sudo install -y elfutils-libelf elfutils-libelf-devel zlib-devel
|
下载:
1
2
3
4
5
6
7
|
git clone --recurse-submodules https://github.com/libbpf/libbpf-bootstrap
cd libbpf-bootstrap
git submodule update --init --recursive
# 运行example
cd examples/c
make
sudo ./bootstrap
|
The End
软件列表:
softare | version | note |
cmake | 3.21.0 | 编译 llvm 需要,libbpf-bootstrap需要 |
gcc | 7.3.0、7.3.1 | 编译 llvm 需要 |
python | python3 | 编译llvm需要 |
llvm | 12.0.1 | bpf 需要 |
libbpf | @432a434 | C,C++ 开发bpf程序依赖 |
libbpf-bootstrap | master | bpf 开发框架 |
BPF之路一: bpf系统调用 - 知乎
CentOS7编译和安装GCC7.5 - He_LiangLiang - 博客园