├── Dockerfile ├── README.md ├── docker-run.sh ├── example ├── hello.c └── hello.py ├── linuxkit-complier.sh └── linuxkit-dl.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM docker/for-desktop-kernel:5.10.76-505289bcc85427a04d8d797e06cbca92eee291f4 AS ksrc 2 | 3 | LABEL maintainer="zeonll@outlook.com" 4 | 5 | FROM ubuntu:21.04 6 | 7 | WORKDIR / 8 | COPY --from=ksrc /kernel-dev.tar / 9 | RUN tar xf kernel-dev.tar && rm kernel-dev.tar 10 | 11 | RUN apt-get update 12 | RUN apt install -y kmod python3-bpfcc wget 13 | RUN apt install -y make gcc flex bison libelf-dev bc 14 | RUN apt install -y libssl-dev vim 15 | 16 | 17 | COPY linuxkit-dl.sh /root 18 | COPY linuxkit-complier.sh /root 19 | 20 | RUN sh /root/linuxkit-dl.sh 21 | RUN echo "download success" 22 | 23 | RUN sh /root/linuxkit-complier.sh 24 | RUN echo "complier successs" 25 | 26 | CMD mount -t debugfs debugfs /sys/kernel/debug && /bin/bash -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What 2 | A docker of learning eBPF at MacOS/Win. 3 | 4 | Ubuntu: 21.04 5 | 6 | Kernel: 5.10.76 7 | 8 | ## Run it! 9 | ``` 10 | cd ebpf-for-desktop/ 11 | docker build -t ebpf:v1 . 12 | sh ./docker-run.sh 13 | ``` 14 | 15 | ## How 16 | MacOS/Win is short of some linuxkit/header file, so I built this image based on 17 | [for-desktop-kernel](https://hub.docker.com/r/docker/for-desktop-kernel) image 18 | and [linux-kernel source code](https://mirrors.aliyun.com/linux-kernel/v5.x/linux-5.10.76.tar.gz). 19 | 20 | 21 | ## FAQ 22 | If could run ebpf programs, try to run `/root/linuxkit-complier.sh` again. 23 | -------------------------------------------------------------------------------- /docker-run.sh: -------------------------------------------------------------------------------- 1 | docker run -it --rm \ 2 | --name ghmac \ 3 | --privileged \ 4 | -v "$(pwd)/example:/root/example" \ 5 | --pid=host \ 6 | ebpf-destop:v1 -------------------------------------------------------------------------------- /example/hello.c: -------------------------------------------------------------------------------- 1 | int hello_world(void *ctx) 2 | { 3 | bpf_trace_printk("Hello, World!"); 4 | return 0; 5 | } -------------------------------------------------------------------------------- /example/hello.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # 1) import bcc library 3 | from bcc import BPF 4 | 5 | # 2) load BPF program 6 | b = BPF(src_file="hello.c") 7 | # 3) attach kprobe 8 | b.attach_kprobe(event="do_sys_openat2", fn_name="hello_world") 9 | # 4) read and print /sys/kernel/debug/tracing/trace_pipe 10 | b.trace_print() -------------------------------------------------------------------------------- /linuxkit-complier.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd /lib/modules/$(uname -r) 3 | 4 | 5 | echo "coping file to source/" 6 | mkdir -p /usr/src/$(uname -r)/ 7 | cp -r /usr/src/$(uname -r)/ . 8 | mv $(uname -r) source 9 | 10 | echo "coping file to build/" 11 | cp -r /usr/src/$(uname -r)/ . 12 | mv $(uname -r) build 13 | 14 | # cp -r /lib/modules/$(uname -r)/source /usr/src/$(uname -r) 15 | 16 | echo "creating config file" 17 | cd /usr/src/$(uname -r) 18 | make -r menuconfig 19 | make -r defconfig 20 | 21 | echo 'CONFIG_BPF=y' >> .config 22 | echo 'CONFIG_BPF_SYSCALL=y' >> .config 23 | echo 'CONFIG_BPF_JIT=y' >> .config 24 | echo 'CONFIG_HAVE_EBPF_JIT=y' >> .config 25 | echo 'CONFIG_BPF_EVENTS=y' >> .config 26 | echo 'CONFIG_FTRACE_SYSCALLS=y' >> .config 27 | echo 'CONFIG_KALLSYMS_ALL=y' >> .config 28 | 29 | 30 | echo "preparing" 31 | make -r prepare 32 | 33 | bash 34 | 35 | -------------------------------------------------------------------------------- /linuxkit-dl.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd /usr/src 3 | 4 | echo "downloading" 5 | wget -O $(uname -r).tar.gz https://mirrors.aliyun.com/linux-kernel/v5.x/linux-5.10.76.tar.gz 6 | tar -zxvf $(uname -r).tar.gz -C . 7 | mv linux-5.10.76/ $(uname -r) 8 | 9 | echo "creating dir" 10 | mkdir -p /lib/modules/$(uname -r) 11 | cd /lib/modules/$(uname -r) 12 | 13 | 14 | bash --------------------------------------------------------------------------------