目录

CentOS7安装bcc软件包

概述

从腾讯云购买的 CVM 实例的默认内核版本是 3.10.0-1160.71.1.el7.x86_64,因此要安装 bcc 软件的话,需要先升级内核。

  1. 内核版本4.1或以上
  2. 腾讯云镜像CentOS7.9使用

步骤

完全按照官方的安装步骤也不一定就可以的,因为 gcc llvm 的编译条件不是这么简单可以搞定的,尤其是对于不怎么搞 C++ 的同学来说,很少使用 cmake 这样的命令,因此不同的厂商提供的虚拟机,不同搞定镜像和不同的 OS 以及内核,都不可能一招搞定,只能按照报错的提示来调整,本文也主要是针对腾讯云的 CVM,操作系统对应的是 CentOS 7.9。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
yum install -y centos-release-scl
yum-config-manager --enable rhel-server-rhscl-7-rpms
yum install -y devtoolset-7 llvm-toolset-7 llvm-toolset-7-llvm-devel llvm-toolset-7-llvm-static llvm-toolset-7-clang-devel
source scl_source enable devtoolset-7 llvm-toolset-7

wget https://github.com/iovisor/bcc/releases/download/v0.23.0/bcc-src-with-submodule.tar.gz
tar -xzvf bcc-src-with-submodule.tar.gz
mkdir bcc/build; cd bcc/build
cmake3 ..
make
make install
 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
31
32
33
34
35
# 安装构建的依赖
sudo yum install -y epel-release
sudo yum update -y
sudo yum groupinstall -y "Development tools"
sudo yum install -y elfutils-libelf-devel cmake3 git bison flex ncurses-devel
sudo yum install -y luajit luajit-devel  # for Lua support

# 编译和安装LLVM
curl -LO http://releases.llvm.org/10.0.0/llvm-10.0.0.src.tar.xz
curl -LO http://releases.llvm.org/10.0.0/cfe-10.0.0.src.tar.xz
tar -xf cfe-10.0.0.src.tar.xz
tar -xf llvm-10.0.0.src.tar.xz

mkdir clang-build
mkdir llvm-build

cd llvm-build
cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86" -DCMAKE_CXX_COMPILER=$(which g++) -DCMAKE_C_COMPILER=$(which gcc) \
  -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
make
sudo make install

cd ../clang-build
cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
  -DCMAKE_BUILD_TYPE=Release ../
make
sudo make install
cd ..

# 下载和编译bcc
git clone https://github.com/iovisor/bcc.git
mkdir bcc/build; cd bcc/build
cmake3 ..
make
sudo make install

执行命令历史记录,有时间整理。

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 升级内核
2023-02-05 10:17:56 yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-ml kernel-devel kernel-ml-headers kernel-ml-tools kernel-ml-tools-libs kernel-ml-tools-libs-devel
2023-02-05 10:18:02 grub2-set-default 0
2023-02-05 10:19:26 yum remove -y kernel-headers-3.10.0-1160.71.1.el7.x86_64
2023-02-05 10:19:33 yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-ml kernel-devel kernel-ml-headers kernel-ml-tools kernel-ml-tools-libs kernel-ml-tools-libs-devel
2023-02-05 10:22:27 grub2-set-default 0
# 重启验证
sudo reboot
uname -r
# 安装相关工具
yum install -y epel-release
yum update -y
yum groupinstall -y "Development tools"
yum install -y elfutils-libelf-devel cmake3 git bison flex ncurses-devel
yum install -y luajit luajit-devel  # for Lua support
# 下载llvm
34  2023-02-05 10:51:19 curl -LO https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/llvm-10.0.0.src.tar.xz
37  2023-02-05 10:58:20 tar -xf llvm-10.0.0.src.tar.xz
38  2023-02-05 10:58:34 cd llvm-10.0.0.src/
39  2023-02-05 10:58:34 mkdir build
40  2023-02-05 10:58:34 cd build
41  2023-02-05 10:58:45 cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" -G "Unix Makefiles" ../llvm
42  2023-02-05 10:58:50 yum install -y cmake
43  2023-02-05 10:58:55 cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" -G "Unix Makefiles" ../llvm
49  2023-02-05 10:59:29 cd build/
51  2023-02-05 10:59:38 cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" -G "Unix Makefiles" ../llvm
53  2023-02-05 11:00:16 cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" -G "Unix Makefiles" /root/llvm-10.0.0.src/
54  2023-02-05 11:00:41 gcc --version
55  2023-02-05 11:00:49 yum install centos-release-scl
56  2023-02-05 11:00:58 echo "source /opt/rh/devtoolset-7/enable" >> ~/.bash_profile
57  2023-02-05 11:00:58 source /opt/rh/devtoolset-7/enable
58  2023-02-05 11:01:07 gcc --version
62  2023-02-05 11:18:51 curl -OL https://packages.vmware.com/photon/photon_sources/1.0/cfe-10.0.0.src.tar.xz
63  2023-02-05 11:19:01 tar -xf cfe-10.0.0.src.tar.xz
64  2023-02-05 11:19:03 tar -xf llvm-10.0.0.src.tar.xz
65  2023-02-05 11:19:11 ls
66  2023-02-05 11:19:11 mkdir clang-build
67  2023-02-05 11:19:11 mkdir llvm-build
68  2023-02-05 11:19:17 cd llvm-build
69  2023-02-05 11:19:17 cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
70  2023-02-05 11:19:53 yum install centos-release-scl
71  2023-02-05 11:20:03 yum install devtoolset-7-gcc*
72  2023-02-05 11:20:41 cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
73  2023-02-05 11:20:48 gcc -version
74  2023-02-05 11:20:51 gcc --version
75  2023-02-05 11:21:07 gcc -V
76  2023-02-05 11:21:10 gcc --version
77  2023-02-05 11:21:16 cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
78  2023-02-05 11:21:34 source /opt/rh/devtoolset-7/enable
79  2023-02-05 11:21:35 cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
80  2023-02-05 11:21:48 cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
81  2023-02-05 11:21:51 gcc --version
82  2023-02-05 11:22:14 mv /usr/bin/gcc /usr/bin/gcc-4.8.5
83  2023-02-05 11:22:14 ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
84  2023-02-05 11:22:15 mv /usr/bin/g++ /usr/bin/g++-4.8.5
85  2023-02-05 11:22:25 ln -s /opt/rh/devtoolset-7/root/bin/g++ /usr/bin/g++
86  2023-02-05 11:22:29 gcc --version
87  2023-02-05 11:22:33 cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
88  2023-02-05 11:22:59 sudo yum install -y epel-release
89  2023-02-05 11:22:59 sudo yum update -y
90  2023-02-05 11:23:00 sudo yum groupinstall -y "Development tools"
91  2023-02-05 11:23:01 sudo yum install -y elfutils-libelf-devel cmake3 git bison flex ncurses-devel
92  2023-02-05 11:23:03 sudo yum install -y luajit luajit-devel  # for Lua support
93  2023-02-05 11:23:11 cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
94  2023-02-05 11:24:13 which gcc
95  2023-02-05 11:24:19 which g++
96  2023-02-05 11:24:23 g++ -V
97  2023-02-05 11:24:25 g++ --version
98  2023-02-05 11:24:37 cmake3 -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=$(which g++) -DCMAKE_C_COMPILER=$(which gcc) -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
99  2023-02-05 11:25:08 /usr/bin/cc --version
# 更新gcc
yum remove gcc -y
yum erase gcc
yum install centos-release-scl -y
yum install devtoolset-7-gcc* -y
which gcc
gcc --version
# 编译
cmake3 -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=$(which g++) -DCMAKE_C_COMPILER=$(which gcc) -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../llvm-10.0.0.src
make
cd ../clang-build
cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../cfe-10.0.0.src
cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../
cmake3 -G "Unix Makefiles" -DLLVM_TARGETS_TO_BUILD="BPF;X86"   -DCMAKE_BUILD_TYPE=Release ../clang-10.0.0.src
make
sudo make install
make

总结

如果只是为了测试和使用 bcc 工具,要在 CentOS 上费很多时间重新编译 LLVM 等是比较浪费时间的,还不如直接用内核版本更新的 Ubuntu。

参考资料

  1. CentOS7.9升级内核并安装bcc-tools
  2. centos7安装bcc-tools软件包
  3. Install bcc
警告
本文最后更新于 2017年2月1日,文中内容可能已过时,请谨慎参考。