Skip to content

Commit

Permalink
feat(ci) support more arch and os vendor btf file embed
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyoush committed Oct 19, 2024
1 parent 4bf09cb commit 406dd00
Show file tree
Hide file tree
Showing 7 changed files with 677 additions and 28 deletions.
6 changes: 5 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ before:
- sudo add-apt-repository "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main"
- sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 15CF4D18AF4F7421
- sudo apt update
- sudo apt-get install -y gcc flex bison make autoconf libelf-dev gcc-aarch64-linux-gnu libc6-dev-arm64-cross pkg-config llvm clang
- sudo apt-get install -y gcc flex bison make autoconf libelf-dev gcc-aarch64-linux-gnu libc6-dev-arm64-cross pkg-config llvm clang rsync
- git submodule update --init --recursive

builds:
Expand All @@ -23,6 +23,7 @@ builds:
env:
- CGO_ENABLED=1
- CC=aarch64-linux-gnu-gcc
- BUILD_ARCH=arm64
flags:
- -tags=static
ldflags:
Expand All @@ -36,13 +37,15 @@ builds:
pre:
- bash -c 'sudo make clean || true'
- bash -c 'sudo make build-bpf || true'
- bash -c 'sudo make btfgen || true'

- id: kyanos-amd64
binary: kyanos
env:
- CGO_ENABLED=1
- CC=clang
- CGO_LDFLAGS="-Xlinker -rpath=. -static"
- BUILD_ARCH=x86_64
flags:
- -tags=static
ldflags:
Expand All @@ -56,6 +59,7 @@ builds:
pre:
- bash -c 'sudo make clean || true'
- bash -c 'sudo make build-bpf || true'
- bash -c 'sudo make btfgen || true'

archives:
- builds:
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ build-bpf: $(LIBBPF_OBJ) $(wildcard bpf/*.[ch]) | $(OUTPUT)
kyanos: $(GO_FILES)
$(call msg,BINARY,$@)
export CGO_LDFLAGS="-Xlinker -rpath=. -static" && go build


.PHONY: btfgen
btfgen:
./btfgen.sh

# delete failed targets
.DELETE_ON_ERROR:

Expand Down
72 changes: 54 additions & 18 deletions bpf/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,54 @@ func LoadBPF(options ac.AgentOptions) (*BPF, error) {
return bf, nil
}

var osReleaseFiles = []string{
"/etc/os-release",
"/usr/lib/os-release",
}

type Release struct {
Id string
VersionId string
}

func getRelease() (*Release, error) {
var errors []error
for _, path := range osReleaseFiles {
data, err := os.ReadFile(path)
if err != nil {
errors = append(errors, err)
continue
}

var release Release
for _, line := range strings.Split(string(data), "\n") {
line := strings.TrimSpace(line)
parts := strings.Split(line, "=")
if len(parts) < 2 {
continue
}
key, value := parts[0], parts[1]
key = strings.TrimSpace(key)
switch key {
case "ID":
release.Id = strings.TrimSpace(value)
break
case "VERSION_ID":
release.VersionId = strings.TrimSpace(value)
break
}
}
if release.Id != "" {
return &release, nil
}
}

if len(errors) != 0 {
return nil, fmt.Errorf("%v", errors)
}

return nil, fmt.Errorf("can't get release info from %v", osReleaseFiles)
}
func getBestMatchedBTFFile() ([]uint8, error) {
if bpf.IsKernelSupportHasBTF() {
return nil, nil
Expand All @@ -167,25 +215,13 @@ func getBestMatchedBTFFile() ([]uint8, error) {
si.GetSysInfo()
common.AgentLog.Debugf("[sys info] vendor: %s, os_arch: %s, kernel_arch: %s", si.OS.Vendor, si.OS.Architecture, si.Kernel.Architecture)

if si.OS.Vendor != "ubuntu" && si.OS.Vendor != "centos" {
common.AgentLog.Fatalf("Current only support centos and ubuntu, current is %s\n completed OS info is: %v", si.OS.Vendor, si.OS)
}
if si.OS.Architecture != "amd64" {
common.AgentLog.Fatal("Current only support amd64")
}
if si.Kernel.Architecture != "x86_64" {
common.AgentLog.Fatal("Current only support x86_64")
}
osInfo, err := common.GetOSInfo()
osId := osInfo.GetOSReleaseFieldValue(common.OS_ID)
versionId := strings.Replace(osInfo.GetOSReleaseFieldValue(common.OS_VERSION_ID), "\"", "", -1)
kernelRelease := osInfo.GetOSReleaseFieldValue(common.OS_KERNEL_RELEASE)
arch := osInfo.GetOSReleaseFieldValue(common.OS_ARCH)

var btfFileDir string
btfFileDir += "custom-archive"
btfFileDir += "/" + si.OS.Vendor
if si.OS.Vendor == "centos" {
btfFileDir += "/" + si.OS.Release[:1]
} else {
btfFileDir += "/" + si.OS.Release[:5]
}
btfFileDir += "/" + si.Kernel.Architecture
btfFileDir := fmt.Sprintf("custom-archive/%s/%s/%s/%s.btf", osId, versionId, arch, kernelRelease)
dir, err := bpf.BtfFiles.ReadDir(btfFileDir)
if err != nil {
common.AgentLog.Warnf("btf file not exists, path: %s", btfFileDir)
Expand Down
88 changes: 88 additions & 0 deletions bpf/loader/loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package loader_test

import (
"fmt"
"os"
"strings"
"sync"
"testing"

"golang.org/x/sys/unix"
)

var kernelVersionInfo struct {
once sync.Once
version string
err error
}

func GetKernelVersion() (string, error) {
kernelVersionInfo.once.Do(func() {

var uname unix.Utsname
err := unix.Uname(&uname)
if err != nil {
kernelVersionInfo.err = fmt.Errorf(": %w", err)
} else {
kernelVersionInfo.version = strings.TrimSpace(unix.ByteSliceToString(uname.Release[:]))
}
})

return kernelVersionInfo.version, kernelVersionInfo.err
}

var osReleaseFiles = []string{
"/etc/os-release",
"/usr/lib/os-release",
}

type Release struct {
Id string
VersionId string
}

func GetRelease() (*Release, error) {
var errors []error
for _, path := range osReleaseFiles {
data, err := os.ReadFile(path)
if err != nil {
errors = append(errors, err)
continue
}

var release Release
for _, line := range strings.Split(string(data), "\n") {
line := strings.TrimSpace(line)
parts := strings.Split(line, "=")
if len(parts) < 2 {
continue
}
key, value := parts[0], parts[1]
key = strings.TrimSpace(key)
switch key {
case "ID":
release.Id = strings.TrimSpace(value)
break
case "VERSION_ID":
release.VersionId = strings.TrimSpace(value)
break
}
}
if release.Id != "" {
return &release, nil
}
}

if len(errors) != 0 {
return nil, fmt.Errorf("%v", errors)
}

return nil, fmt.Errorf("can't get release info from %v", osReleaseFiles)
}
func TestRelease(t *testing.T) {
v, _ := GetKernelVersion()
fmt.Sprintln(v)

r, _ := GetRelease()
fmt.Sprintln(r)
}
96 changes: 88 additions & 8 deletions btfgen.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,89 @@
#!/bin/bash
current_dir=$(pwd)
rm -rf ./bpf/custom-archive
cd ../btfhub || exit
make bring
./tools/btfgen.sh -a x86_64 -o "$current_dir"/bpf/agent_x86_bpfel.o
cd "$current_dir" || exit
cp -R ../btfhub/custom-archive ./bpf/
rm -f ./bpf/custom-archive/.gitignore
BASEDIR=$(dirname "${0}")
cd ${BASEDIR}/../
BASEDIR=$(pwd)
cd ${BASEDIR}

BTFHUB_REPO="https://github.com/aquasecurity/btfhub.git"
BTFHUB_ARCH_REPO="https://github.com/aquasecurity/btfhub-archive.git"


KYANOS_BPF_CORE="${BASEDIR}/bpf/agent_x86_bpfel.o"

BTFHUB_DIR="${BASEDIR}/deps/btfhub"
BTFHUB_ARCH_DIR="${BASEDIR}/deps/btfhub-archive"

ARCH=$(uname -m)

case ${ARCH} in
"x86_64")
ARCH="x86_64"
;;
"aarch64")
ARCH="arm64"
;;
*)
die "unsupported architecture"
;;
esac


die() {
echo ${@}
exit 1
}

branch_clean() {
cd ${1} || die "could not change dirs"

# small sanity check
[ ! -d ./.git ] && die "$(basename $(pwd)) not a repo dir"

git fetch -a || die "could not fetch ${1}" # make sure its updated
git clean -fdX # clean leftovers
git reset --hard # reset letfovers
git checkout origin/main -b main-$$
git branch -D main
git branch -m main-$$ main # origin/main == main

cd ${BASEDIR}
}

CMDS="rsync git cp rm mv"
for cmd in ${CMDS}; do
command -v $cmd 2>&1 >/dev/null || die "cmd ${cmd} not found"
done
[ ! -f ${KYANOS_BPF_CORE} ] && die "tracee CO-RE obj not found"

[ ! -d ${BTFHUB_DIR} ] && git clone "${BTFHUB_REPO}" ${BTFHUB_DIR}
[ ! -d ${BTFHUB_ARCH_DIR} ] && git clone "${BTFHUB_ARCH_REPO}" ${BTFHUB_ARCH_DIR}

if [ -z ${SKIP_FETCH} ]; then
branch_clean ${BTFHUB_DIR}
branch_clean ${BTFHUB_ARCH_DIR}
fi

cd ${BTFHUB_DIR}


# sync only supported kernels

ARCH_EXCLUDE=$(printf "x86_64\naarch64\n" | grep -v $(uname -m) | xargs)
rsync -avz \
${BTFHUB_ARCH_DIR}/ \
--exclude=.git* \
--exclude=README.md \
--exclude=${ARCH_EXCLUDE} \
./archive/

# generate tailored BTFs

[ ! -f ./tools/btfgen.sh ] && die "could not find btfgen.sh"
./tools/btfgen.sh -a ${BUILD_ARCH} -o ${KYANOS_BPF_CORE}


# move tailored BTFs to dist

[ ! -d ${BASEDIR}/bpf/custom-archive ] && mkdir -p ${BASEDIR}/bpf/custom-archive
rm -rf ${BASEDIR}/bpf/custom-archive/* || true
mv ./custom-archive/* ${BASEDIR}/bpf/custom-archive
Loading

0 comments on commit 406dd00

Please sign in to comment.