-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ci) support more arch and os vendor btf file embed
- Loading branch information
Showing
7 changed files
with
677 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.