Skip to content

Commit ec39d33

Browse files
add return None statements
1 parent caecb8c commit ec39d33

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

tools/setup.sh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/bin/bash
2+
3+
echo "===================================================================="
4+
echo " ⚠️ WARNING ⚠️"
5+
echo " This script will run kernel-level BPF programs."
6+
echo " BPF programs run with kernel privileges and could potentially"
7+
echo " affect system stability if not used properly."
8+
echo ""
9+
echo " PLEASE REVIEW THE SOURCE CODE BEFORE RUNNING:"
10+
echo " https://github.com/pythonbpf/python-bpf"
11+
echo "===================================================================="
12+
echo
13+
14+
echo "This script will:"
15+
echo "1. Check and install required dependencies (libelf, clang, python, bpftool)"
16+
echo "2. Download example programs from the Python-BPF GitHub repository"
17+
echo "3. Create a Python virtual environment with necessary packages"
18+
echo "4. Start a Jupyter notebook server"
19+
echo
20+
21+
read -p "Would you like to continue? (y/N) " -n 1 -r
22+
echo
23+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
24+
echo "Script execution cancelled."
25+
exit 1
26+
fi
27+
28+
if [ "$EUID" -ne 0 ]; then
29+
echo "Please run this script with sudo."
30+
exit 1
31+
fi
32+
33+
WORK_DIR="/tmp/python_bpf_setup"
34+
REAL_USER=$(logname || echo "$SUDO_USER")
35+
36+
echo "Creating temporary directory: $WORK_DIR"
37+
mkdir -p "$WORK_DIR"
38+
cd "$WORK_DIR" || exit 1
39+
40+
if [ -f /etc/os-release ]; then
41+
. /etc/os-release
42+
DISTRO=$ID
43+
else
44+
echo "Cannot determine Linux distribution. Exiting."
45+
exit 1
46+
fi
47+
48+
install_dependencies() {
49+
case $DISTRO in
50+
ubuntu|debian|pop|mint|elementary|zorin)
51+
echo "Detected Ubuntu/Debian-based system"
52+
apt update
53+
54+
# Check and install libelf
55+
if ! dpkg -l libelf-dev >/dev/null 2>&1; then
56+
echo "Installing libelf-dev..."
57+
apt install -y libelf-dev
58+
else
59+
echo "libelf-dev is already installed."
60+
fi
61+
62+
# Check and install clang
63+
if ! command -v clang >/dev/null 2>&1; then
64+
echo "Installing clang..."
65+
apt install -y clang
66+
else
67+
echo "clang is already installed."
68+
fi
69+
70+
# Check and install python
71+
if ! command -v python3 >/dev/null 2>&1; then
72+
echo "Installing python3..."
73+
apt install -y python3 python3-pip python3-venv
74+
else
75+
echo "python3 is already installed."
76+
fi
77+
78+
# Check and install bpftool
79+
if ! command -v bpftool >/dev/null 2>&1; then
80+
echo "Installing bpftool..."
81+
apt install -y linux-tools-common linux-tools-generic
82+
83+
# If bpftool still not found, try installing linux-tools-$(uname -r)
84+
if ! command -v bpftool >/dev/null 2>&1; then
85+
KERNEL_VERSION=$(uname -r)
86+
apt install -y linux-tools-$KERNEL_VERSION
87+
fi
88+
else
89+
echo "bpftool is already installed."
90+
fi
91+
;;
92+
93+
arch|manjaro|endeavouros)
94+
echo "Detected Arch-based Linux system"
95+
96+
# Check and install libelf
97+
if ! pacman -Q libelf >/dev/null 2>&1; then
98+
echo "Installing libelf..."
99+
pacman -S --noconfirm libelf
100+
else
101+
echo "libelf is already installed."
102+
fi
103+
104+
# Check and install clang
105+
if ! command -v clang >/dev/null 2>&1; then
106+
echo "Installing clang..."
107+
pacman -S --noconfirm clang
108+
else
109+
echo "clang is already installed."
110+
fi
111+
112+
# Check and install python
113+
if ! command -v python3 >/dev/null 2>&1; then
114+
echo "Installing python3..."
115+
pacman -S --noconfirm python python-pip
116+
else
117+
echo "python3 is already installed."
118+
fi
119+
120+
# Check and install bpftool
121+
if ! command -v bpftool >/dev/null 2>&1; then
122+
echo "Installing bpftool..."
123+
pacman -S --noconfirm bpf linux-headers
124+
else
125+
echo "bpftool is already installed."
126+
fi
127+
;;
128+
129+
*)
130+
echo "Unsupported distribution: $DISTRO"
131+
echo "This script only supports Ubuntu/Debian and Arch Linux derivatives."
132+
exit 1
133+
;;
134+
esac
135+
}
136+
137+
echo "Checking and installing dependencies..."
138+
install_dependencies
139+
140+
# Download example programs
141+
echo "Downloading example programs from Python-BPF GitHub repository..."
142+
mkdir -p examples
143+
cd examples || exit 1
144+
145+
echo "Fetching example files list..."
146+
FILES=$(curl -s "https://api.github.com/repos/pythonbpf/Python-BPF/contents/examples" | grep -o '"path":"examples/[^"]*"' | awk -F'"' '{print $4}')
147+
148+
if [ -z "$FILES" ]; then
149+
echo "Failed to fetch file list from repository. Using fallback method..."
150+
# Fallback to downloading common example files
151+
EXAMPLES=(
152+
"binops_demo.py"
153+
"blk_request.py"
154+
"clone-matplotlib.ipynb"
155+
"clone_plot.py"
156+
"hello_world.py"
157+
"kprobes.py"
158+
"struct_and_perf.py"
159+
"sys_sync.py"
160+
"xdp_pass.py"
161+
)
162+
163+
for example in "${EXAMPLES[@]}"; do
164+
echo "Downloading: $example"
165+
curl -s -O "https://raw.githubusercontent.com/pythonbpf/Python-BPF/master/examples/$example"
166+
done
167+
else
168+
for file in $FILES; do
169+
filename=$(basename "$file")
170+
echo "Downloading: $filename"
171+
curl -s -o "$filename" "https://raw.githubusercontent.com/pythonbpf/Python-BPF/master/$file"
172+
done
173+
fi
174+
175+
cd "$WORK_DIR" || exit 1
176+
chown -R "$REAL_USER:$(id -gn "$REAL_USER")" .
177+
178+
echo "Creating Python virtual environment..."
179+
su - "$REAL_USER" -c "cd \"$WORK_DIR\" && python3 -m venv venv"
180+
181+
echo "Installing Python packages..."
182+
su - "$REAL_USER" -c "cd \"$WORK_DIR\" && source venv/bin/activate && pip install --upgrade pip && pip install jupyter pythonbpf pylibbpf matplotlib"
183+
184+
cat > "$WORK_DIR/start_jupyter.sh" << EOF
185+
#!/bin/bash
186+
cd "$WORK_DIR"
187+
source venv/bin/activate
188+
cd examples
189+
sudo ../venv/bin/python -m notebook --ip=0.0.0.0 --allow-root
190+
EOF
191+
192+
chmod +x "$WORK_DIR/start_jupyter.sh"
193+
chown "$REAL_USER:$(id -gn "$REAL_USER")" "$WORK_DIR/start_jupyter.sh"
194+
195+
echo "========================================================"
196+
echo "Setup complete! To start Jupyter Notebook, run:"
197+
echo "$ sudo $WORK_DIR/start_jupyter.sh"
198+
echo ""

0 commit comments

Comments
 (0)