Skip to content

Instantly share code, notes, and snippets.

@eddyz87
Created March 4, 2024 17:02
Show Gist options
  • Save eddyz87/204a47145452f1fc4792c8a55be53d6d to your computer and use it in GitHub Desktop.
Save eddyz87/204a47145452f1fc4792c8a55be53d6d to your computer and use it in GitHub Desktop.
BPF selftests execution in chroot
# Below assumes that kernel source code is under directory
# /home/${USER}/work/bpf-next/
# And user's intent is to compile/run selftests from chroot
# with development environment.
# The /home/${USER}/work/ directory is setup to be accessible
# from both host an chroot using the same path.
# First, setup the bullseye chroot
sudo /usr/sbin/debootstrap --variant=buildd --arch=amd64 bullseye bullseye-chroot/ http://deb.debian.org/debian
# Establish script to enter chroot,
# I share 'work' sub-dir from my home dir with this chroot
cat enter-bullseye-chroot.sh
#!/bin/bash
root=/home/${USER}/work/bullseye-chroot
function cleanup {
umount -R $root/dev
umount -R $root/proc
umount -R $root/home/${USER}/work
}
trap cleanup exit
mount --rbind /dev $root/dev
mount -t proc proc $root/proc
mount --bind /home/${USER}/work/ $root/home/${USER}/work
chroot bullseye-chroot
# Setup a local user inside chroot,
# I use same user name uid/gid as on my host system
sudo mkdir -p /home/${USER}/work/bullseye-chroot/home/${USER}/work
sudo chown -R ${USER} /home/${USER}/work/bullseye-chroot/home/${USER}
sudo chgrp -R ${USER} /home/${USER}/work/bullseye-chroot/home/${USER}
sudo ./enter-bullseye-chroot.sh
# Rest of the commands are from chroot, set USER as appropriate
export USER=????
useradd -d /home/${USER} -s /bin/bash ${USER}
usermod -a -G sudo ${USER}
passwd ${USER}
# Install build tools
apt install build-essential bc flex bison git libelf-dev libssl-dev \
docutils-common rsync curl zstd qemu-system-x86 sudo cmake \
libdw-dev lsb-release wget software-properties-common gnupg e2fsprogs
# Install fresh clang-18 snapshot, the llvm.sh sets up some repos
curl https://apt.llvm.org/llvm.sh --output /tmp/llvm.sh
bash /tmp/llvm.sh 18
apt install clang-tools-18
ln -s /usr/bin/clang-18 /usr/bin/clang
ln -s /usr/bin/llvm-strip-18 /usr/bin/llvm-strip
# Enter as a user
su ${USER}
# Minimal setup
mkdir /home/${USER}/bin
echo 'export PS1="<chroot> \t \W$ "' >> /home/${USER}/.bashrc
echo 'export PATH=/home/${USER}/bin:${PATH}' >> /home/${USER}/.bashrc
. /home/${USER}/.bashrc
# Get and compile pahole, use instructions from:
# https://git.kernel.org/pub/scm/devel/pahole/pahole.git/about/
cd /home/${USER}/work
git clone https://git.kernel.org/pub/scm/devel/pahole/pahole.git
cd pahole
git submodule update --init --recursive
mkdir build
cd build
cmake -D__LIB=lib ..
make -j
ln -s $(realpath pahole) /home/${USER}/bin/
# Assuming kernel resides in the bpf-next directory directory below
cd /home/${USER}/work/bpf-next/tools/testing/selftests/bpf
# Now get kernel source
cd /home/${USER}/work
git clone https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git one-more-kernel
# Run vmtests, this should download rootfs, build kernel and tests, run test_verifier
# vmtest.sh would ask for root password to mount rootfs image
cd one-more-kernel/tools/testing/selftests/bpf
./vmtest.sh -- ./test_verifier
# And now run test_progs
./vmtest.sh -- ./test_progs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment