Docker environment for aya rust ebpf compilation - Stack Overflow

时间: 2025-01-05 admin 业界

I am trying to create a eBPF program that runs on Ubuntu. It is written in rust using the aya library.

My computer is running MacOs so as far as I understand, I cant install all the kernel related things on my computer, also the resulting project is meant to run in Docker containers, so I am trying to do this in Docker.

My approach is a Multistage build.

  • Install all the tools needed for the builds
  • Compile dependencies so this is not repeated every time
  • Build the project
  • Run the project.
FROM ubuntu:latest AS build_environment
SHELL ["/bin/bash", "-c"]

# Install tools
RUN apt-get update && apt-get install -y build-essential curl
RUN curl  -sSf | bash -s -- -y && \
    source $HOME/.cargo/env && \
    rustup install stable && \
    rustup toolchain install nightly --component rust-src && \
    source $HOME/.cargo/env && rustup target add bpfel-unknown-none --toolchain nightly \
    cargo install bpf-linker

# Install only dependencies.
FROM build_environment AS prebuild
# Creates temporary files to compile with
WORKDIR /usr/src/middleware
COPY Cargo.toml Cargo.lock ./

RUN mkdir -p src/bin && \
    echo "fn main() {println!(\"hi\"); loop{};}" > src/main.rs && \
    echo "fn main() {}" > src/bin/hook.rs

RUN source $HOME/.cargo/env && cargo build   # Install only dependencies
RUN source $HOME/.cargo/env && cargo clean -p middleware # Remove the dummy code

FROM prebuild AS build

# Add actual source code
COPY src src
# Compile source code
RUN source $HOME/.cargo/env && cargo build --target bpfel-unknown-none --bin hook && cargo build

FROM ubuntu:latest AS runtime
WORKDIR /app

RUN apt-get update && apt-get install -y curl
RUN apt-get install -y net-tools
#RUN apt-get -y install llvm
RUN apt install -y llvm

COPY --from=build /usr/src/middleware/target .
COPY --from=build /usr/src/middleware/target/debug/hook ./
COPY --from=build /usr/src/middleware/target/debug/middleware ./

CMD ["sh", "-c", "RUST_LOG=debug ./middleware"]

The hook.rs and main.rs files are the same as here: /book/start/hello-xdp/#verifying-the-program

However, hook.rs is in the bin folder rather than in a separate project (no extra Cargo.toml).

I previously got this to run, however I would always get an error about the .init section in the hook binary (invalid program section '.init', when the program started. I figured this might be because of the missing target, so I added

10: source $HOME/.cargo/env && rustup target add bpfel-unknown-none --toolchain nightly \

and added the —target in

31: RUN source $HOME/.cargo/env && cargo build --target bpfel-unknown-none --bin hook && cargo build

which now fails because of

156.8 
156.8   nightly-x86_64-unknown-linux-gnu installed - rustc 1.85.0-nightly (dd84b7d5e 2024-12-27)
156.8 
156.8 info: checking for self-update
157.2 error: component 'rust-std' for target 'bpfel-unknown-none' is unavailable for download for channel 'nightly'

Since this is my first time working with ebpf and my second project with rust i am out of depth. I would appreciate any help or pointers towards what i have to change to get the program to work and why that needs to be changed.

EDIT:

Leaving only line 31 but removing line 10 leads to:

4.856 error[E0463]: can't find crate for `core`
4.857   |
4.858   = note: the `bpfel-unknown-none` target may not be installed
4.858   = help: consider downloading the target with `rustup target add bpfel-unknown-none`