diff options
author | Pasha <pasha@member.fsf.org> | 2023-07-17 20:11:06 +0000 |
---|---|---|
committer | Pasha <pasha@member.fsf.org> | 2023-07-17 20:11:06 +0000 |
commit | 44821ae920438158ab32e439b111525b3607a3cc (patch) | |
tree | 07ddba560eef88b242c2614f607707a85c2f1498 | |
download | ebpf_on_container-44821ae920438158ab32e439b111525b3607a3cc.tar.gz ebpf_on_container-44821ae920438158ab32e439b111525b3607a3cc.tar.bz2 |
initial release
-rwxr-xr-x | ebpf_on_container.sh | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/ebpf_on_container.sh b/ebpf_on_container.sh new file mode 100755 index 0000000..e65c000 --- /dev/null +++ b/ebpf_on_container.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# Copyright (C) 2023 Pasha <pasha@bell01.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +show_version() { + echo "0.1a" + exit 1 +} + +show_usage() { + echo "Usage: $0 bpf_program_path container_id" + echo "example: $0 /usr/sbin/execsnoop-bpfcc \$(docker inspect --format=\"{{.Id}}\" 119cb41a9e09)" + + echo " $0 /usr/sbin/execsnoop-bpfcc \$(kubectl get pods -o jsonpath='{range .items[*].status.containerStatuses[*]}{.containerID}{\"\n\"}{end}' --field-selector metadata.name=test-deployment-7f456874f4-mxjg4 | cut -b 10-)" + exit 1 +} + +if [[ "$1" == "-v" ]]; then + show_version +fi + +if [ -z "$1" ] || [ -z "$2" ]; then + show_usage +fi + +bpf_prog_path=$1 +containerid=$2 + +echo "creating bpf map" + +BPF_FILE=/sys/fs/bpf/$containerid +if [ -f "$BPF_FILE" ]; then + echo "error bpf map exists." + echo "please unmap or remove \"$BPF_FILE\" and try again." + exit 1 +fi + +echo $BPF_FILE + +/usr/sbin/bpftool map create $BPF_FILE type hash key 8 value 8 entries 128 name cgroupset flags 0 + +cgroupid=$(containercgroup id $containerid) +if [ -z "$cgroupid" ] +then + echo "invalid container id" + echo "removing bpf map" + rm $BPF_FILE + exit 1 +fi + +echo "cgroupid: $cgroupid" +/usr/sbin/bpftool map update pinned $BPF_FILE key hex $cgroupid value hex 00 00 00 00 00 00 00 00 any + +$bpf_prog_path --cgroup $BPF_FILE + +echo "removing bpf map" +rm $BPF_FILE |