2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ubuntu 20.04.3 LTS でeBPFをインストール

Last updated at Posted at 2021-12-04

環境

マシン conohaの最小構成
OS   Ubuntu 20.04.3 LTS

やり方

今回はパッケージからインストールするとバージョンが古すぎる為、ソースからコンパイルする方法を行います。

  1. 依存関係を持つアプリケーションをインストール
  2. BCCのインストールとコンパイル

依存関係を持つアプリケーションのインストール

sudo apt install -y bison build-essential cmake flex git libedit-dev   libllvm7 llvm-7-dev libclang-7-dev python zlib1g-dev libelf-dev libfl-dev python3-distutils

BCCのインストールとコンパイル

BCCのリポジトリをクローン

git clone https://github.com/iovisor/bcc.git

実行後はbccディレクトリが作成される

~$ ls
bcc  hello.txt

bcc配下にbuild用ディレクトリを作成

..mkdir bcc/build
cd bcc/build

bccのビルド,インストールを行う

cmake ..

実行後、makeファイルや各設定ファイルが作成される

~/bcc/build$ ls
CMakeCache.txt  cmake_install.cmake  examples       Makefile  src    tools
CMakeFiles      CTestTestfile.cmake  introspection  man       tests

cmakeで作成されたmakeファイルを基に、コンパイルを行う

make

makeでコンパイルしたアプリケーションをインストールする

make install

Python3のビルド?ライブラリのビルド?

Python3を使ってeBPFのコードを変換を行いバインドを行うので、必要

何を行っているのか要検証

cmake -DPYTHON_CMD=python3 ..

実行後、makeファイルや各設定ファイルが作成される

$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CTestTestfile.cmake  examples  install_manifest.txt  introspection  Makefile  man  src  tests  tools

python用のソースコードからコンパイルとインストールを行う

pushd src/python/
make
sudo make install
popd

テスト

インストールが実際にできたテストする
https://github.com/iovisor/bcc/blob/master/examples/tracing/hello_fields.py を使用する

# !/usr/bin/python
#
# This is a Hello World example that formats output as fields.

from bcc import BPF
from bcc.utils import printb

# define BPF program
prog = """
int hello(void *ctx) {
    bpf_trace_printk("Hello, World!\\n");
    return 0;
}
"""

# load BPF program
b = BPF(text=prog)
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello")

# header
print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "MESSAGE"))

# format output
while 1:
    try:
        (task, pid, cpu, flags, ts, msg) = b.trace_fields()
    except ValueError:
        continue
    except KeyboardInterrupt:
        exit()
    printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))

このコードは、cloneのシステムコールを検知して標準出力に表示するソースコードです。

出力結果が下記のようになれば成功です。

$ sudo -E python3 hello_field.py 
TIME(s)            COMM             PID    MESSAGE
2790412.579803000  <...>            232728 Hello, World!
2790412.584491000  <...>            293749 Hello, World!
2790412.586786000  <...>            232728 Hello, World!
2790412.590722000  <...>            293751 Hello, World!
2790412.603958000  <...>            232728 Hello, World!
2790412.607811000  <...>            293753 Hello, World!
2790412.610399000  <...>            293754 Hello, World!
2790412.615617000  <...>            293754 Hello, World!
2790412.618347000  <...>            293754 Hello, World!
2790412.620822000  <...>            293754 Hello, World!
2790412.623829000  <...>            293754 Hello, World!

参考文献

この記事は下記のサイトを参考に書いています
eBPF入門
https://github.com/iovisor/bcc/blob/master/INSTALL.md

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?