Skip to content

Commit f644523

Browse files
committed
commit
1 parent 64ec967 commit f644523

File tree

2 files changed

+135
-101
lines changed

2 files changed

+135
-101
lines changed

selfdrive/test/build.sh

Lines changed: 22 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,38 @@
11
#!/usr/bin/env bash
22
set -e
3-
trap restore_root ERR
43
ORG_PWD="$PWD"
4+
SELF_PATH="$(realpath $0)"
5+
SCRIPT_DIR="$(dirname "$SELF_PATH")"
56

67
REPO="$HOME/work/openpilot/openpilot"
78
CACHE_ROOTFS_TARBALL_PATH="/tmp/rootfs_cache.tar"
89

9-
unpack_rootfs_tarball() {
10-
cd /
11-
sudo tar -xf "$CACHE_ROOTFS_TARBALL_PATH" 2>/dev/null || true
12-
cd
13-
}
14-
15-
commit_root() {
16-
sudo mkdir -p /base /newroot /upper /work
17-
18-
sudo unshare -f --kill-child -m $ORG_PWD/selfdrive/test/build.sh build_inside_namespace
19-
ec=$?
20-
echo "end of ns"
21-
22-
sudo rm -rf /base /newroot /work
23-
24-
# finally, create the rootfs diff tarball (to be pushed into the CI native cache)
25-
mkdir -p /tmp/rootfs_cache
26-
sudo rm -f "$CACHE_ROOTFS_TARBALL_PATH" # remove the old tarball from previous run, if exists
27-
cd /upper
28-
sudo tar -cf "$CACHE_ROOTFS_TARBALL_PATH" .
29-
cd
30-
31-
sudo rm -rf /upper
32-
33-
unpack_rootfs_tarball
34-
35-
# before the next tasks are run, finalize the environment for them
36-
prepare_mounts
37-
38-
exit $ec
39-
}
40-
41-
prepare_mounts() {
42-
# create and mount the required volumes where they're expected
43-
mkdir -p /tmp/openpilot /tmp/scons_cache /tmp/comma_download_cache /tmp/openpilot_cache
44-
sudo mount --bind "$REPO" /tmp/openpilot
45-
46-
sudo mount --bind "$REPO/.ci_cache/scons_cache" /tmp/scons_cache || true
47-
sudo mount --bind "$REPO/.ci_cache/comma_download_cache" /tmp/comma_download_cache || true
48-
sudo mount --bind "$REPO/.ci_cache/openpilot_cache" /tmp/openpilot_cache || true
49-
50-
# needed for the unit tests not to fail
51-
sudo chmod 755 /sys/fs/pstore
52-
}
53-
54-
restore_root() {
55-
echo failed at ${BASH_LINENO[0]}
56-
}
57-
58-
build_inside_namespace() {
59-
mount --bind / /base
60-
mount -t overlay overlay -o lowerdir=/base,upperdir=/upper,workdir=/work /newroot
61-
rm -f /newroot/etc/resolv.conf
62-
touch /newroot/etc/resolv.conf
63-
cat /etc/resolv.conf > /newroot/etc/resolv.conf
64-
65-
mkdir -p /newroot/old
66-
cd /newroot
67-
pivot_root . old
68-
69-
mount -t proc proc /proc
70-
mount -t devtmpfs devtmpfs /dev
71-
mkdir -p /dev/pts
72-
mount -t devpts devpts /dev/pts
73-
mount -t proc proc /proc
74-
mount -t sysfs sysfs /sys
75-
76-
touch /root_committed
77-
sudo -u runner /home/runner/work/openpilot/openpilot/selfdrive/test/build.sh
78-
ec=$?
79-
exit $ec
80-
}
81-
82-
if [ "$1" = "build_inside_namespace" ]
83-
then
84-
build_inside_namespace
85-
exit
86-
fi
10+
source "$SCRIPT_DIR/build_common.sh"
8711

12+
# if the rootfs diff tarball (also created by this script) got restored from the CI native cache
8813
if [ -f "$CACHE_ROOTFS_TARBALL_PATH" ]
8914
then
90-
# if the rootfs diff tarball (also created by this script) got restored from the CI native cache, unpack it, upgrading the rootfs
15+
# apply it, upgrading the rootfs
9116
echo "restoring rootfs from the native build cache"
92-
unpack_rootfs_tarball
17+
apply_rootfs_diff
9318
rm "$CACHE_ROOTFS_TARBALL_PATH"
9419

9520
# before the next tasks are run, finalize the environment for them
96-
prepare_mounts
21+
prepare_build
9722

9823
# EXITS HERE - if the rootfs could been prepared entirely from the cache, there's no need for any further action like re-building
9924
exit 0
10025
else
10126
# otherwise, we'll have to install everything from scratch and build the tarball to be available for the next run
102-
echo "no native build cache entry restored, rebuilding"
27+
if ! [ -f /root_committed ]
28+
then
29+
echo "no native build cache entry restored, rebuilding"
30+
fi
10331
fi
10432

105-
# in order to be able to build a diff rootfs tarball, we need to commit its initial state by moving it on-the-fly to overlayfs;
106-
# below, we prepare the system and the new rootfs itself
107-
108-
if ! [ -e /root_committed ]
109-
then
33+
# in order to be able to build a diff rootfs tarball, we need to commit its initial state
34+
# by moving it on-the-fly to overlayfs; below, we prepare the system and the new rootfs itself
11035
commit_root
111-
fi
11236

11337
# -------- at this point, the original rootfs was committed and all the changes to it done below will be saved to the newly created rootfs diff tarball --------
11438

@@ -118,11 +42,11 @@ DEBIAN_FRONTEND=noninteractive
11842

11943
mkdir -p /tmp/tools
12044
cp "$REPO/tools/install_ubuntu_dependencies.sh" /tmp/tools/
121-
sudo /tmp/tools/install_ubuntu_dependencies.sh &>/dev/null
45+
sudo /tmp/tools/install_ubuntu_dependencies.sh
12246

12347
sudo apt-get install -y --no-install-recommends \
12448
sudo tzdata locales ssh pulseaudio xvfb x11-xserver-utils gnome-screenshot python3-tk python3-dev \
125-
apt-utils alien unzip tar curl xz-utils dbus gcc-arm-none-eabi tmux vim libx11-6 wget &>/dev/null
49+
apt-utils alien unzip tar curl xz-utils dbus gcc-arm-none-eabi tmux vim libx11-6 wget
12650

12751
sudo rm -rf /var/lib/apt/lists/*
12852
sudo apt-get clean
@@ -155,7 +79,6 @@ sudo ln -s /opt/intel/oneapi-tbb-2021.12.0/lib/intel64/gcc4.8/libtbbmalloc.so.2
15579
sudo mkdir -p /etc/ld.so.conf.d
15680
sudo bash -c "echo /opt/intel/oclcpuexp_2024.17.3.0.09_rel/x64 > /etc/ld.so.conf.d/libintelopenclexp.conf"
15781
sudo ldconfig -f /etc/ld.so.conf.d/libintelopenclexp.conf
158-
cd /
15982
rm -rf /tmp/opencl-driver-intel
16083
cd
16184

@@ -166,18 +89,16 @@ NVIDIA_DRIVER_CAPABILITIES=graphics,utility,compute
16689
QTWEBENGINE_DISABLE_SANDBOX=1
16790

16891
# install and set up the Python dependencies needed
169-
sudo cp "/home/runner/work/openpilot/openpilot/pyproject.toml" "/home/runner/work/openpilot/openpilot/uv.lock" "/home/runner/work/openpilot/openpilot/tools/install_python_dependencies.sh" \
170-
/home/runner/
92+
cp "$REPO/pyproject.toml" "$REPO/uv.lock" "$HOME/"
93+
mkdir "$HOME/tools"
94+
cp "$REPO/tools/install_python_dependencies.sh" "$HOME/tools/"
17195

172-
cd
173-
rm -rf .venv
96+
VIRTUAL_ENV=/home/$USER/.venv
97+
PATH="$VIRTUAL_ENV/bin:$PATH"
17498

175-
mkdir aaa
176-
cd aaa
177-
../install_python_dependencies.sh
17899
cd
179-
rm pyproject.toml uv.lock install_python_dependencies.sh
180-
100+
tools/install_python_dependencies.sh
101+
rm -rf tools/ pyproject.toml uv.lock .cache
181102

182103
# add a git safe directory for compiling openpilot
183104
sudo git config --global --add safe.directory /tmp/openpilot

selfdrive/test/build_common.sh

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
error_handler() {
2+
echo failed at ${BASH_LINENO[0]}
3+
}
4+
trap error_handler ERR
5+
6+
create_rootfs_diff() {
7+
sudo rm -f "$CACHE_ROOTFS_TARBALL_PATH" # remove the old diff tarball from previous run, if exists
8+
cd /upper
9+
sudo tar -cf "$CACHE_ROOTFS_TARBALL_PATH" .
10+
cd
11+
}
12+
13+
apply_rootfs_diff() {
14+
cd /
15+
sudo tar -xf "$CACHE_ROOTFS_TARBALL_PATH" 2>/dev/null || true
16+
cd
17+
}
18+
19+
prepare_build() {
20+
# create and mount the required volumes where they're expected
21+
mkdir -p /tmp/openpilot /tmp/scons_cache /tmp/comma_download_cache /tmp/openpilot_cache
22+
sudo mount --bind "$REPO" /tmp/openpilot
23+
24+
sudo mount --bind "$REPO/.ci_cache/scons_cache" /tmp/scons_cache || true
25+
sudo mount --bind "$REPO/.ci_cache/comma_download_cache" /tmp/comma_download_cache || true
26+
sudo mount --bind "$REPO/.ci_cache/openpilot_cache" /tmp/openpilot_cache || true
27+
28+
# needed for the unit tests not to fail
29+
sudo chmod 755 /sys/fs/pstore
30+
}
31+
32+
post_commit_root() {
33+
# we have the diff tarball, now let's remove the folder too
34+
sudo rm -rf /upper
35+
36+
# now we apply it straight away
37+
apply_rootfs_diff
38+
39+
# before the next tasks are run, finalize the environment for them
40+
prepare_build
41+
}
42+
43+
# warning: this function initiates a somewhat complicated program flow, follow carefully
44+
# (even despite this part was made sure to not be too relevant for the rest of the job)
45+
commit_root() {
46+
# if that's a first execution
47+
if ! [ -e /root_committed ]
48+
then
49+
# prepare directories
50+
sudo mkdir -p /base /newroot /upper /work
51+
52+
# re-execute the main script (causing it to go straight to `build_inside_namespace`), but
53+
# inside the newly created namespace, in a way which would cause all mounts
54+
# created to automatically umount before it exits
55+
sudo unshare -f --kill-child -m "$SELF_PATH" build_inside_namespace
56+
ec=$?
57+
58+
# after it exited, remove the created directories (except the one containing created diff)
59+
sudo rm -rf /base /newroot /work
60+
61+
# finally, create the rootfs diff tarball (to be pushed into the CI native cache)
62+
create_rootfs_diff
63+
64+
# after creating the rootfs diff, bring the system into a state as if it was restored from cache
65+
post_commit_root
66+
67+
exit $ec
68+
fi
69+
}
70+
71+
reexecute() {
72+
touch /root_committed
73+
sudo -u runner "$SELF_PATH"
74+
ec=$?
75+
exit $ec
76+
}
77+
78+
# that's where the script goes after being re-executed for the first time
79+
build_inside_namespace() {
80+
# initialize the mounts namespace on overlayfs to be able to prepare the rootfs diff
81+
mount --bind / /base
82+
mount -t overlay overlay -o lowerdir=/base,upperdir=/upper,workdir=/work /newroot
83+
84+
# apply the current DNS config (beware: systemd often symlinks /etc/resolv.conf, that's why it's needed)
85+
rm -f /newroot/etc/resolv.conf
86+
touch /newroot/etc/resolv.conf
87+
cat /etc/resolv.conf > /newroot/etc/resolv.conf
88+
89+
# switch the namespace's root mount to the newly created one
90+
mkdir -p /newroot/old
91+
cd /newroot
92+
pivot_root . old
93+
94+
# initialize basic required POSIX-standard additional mounts
95+
mount -t proc proc /proc
96+
mount -t devtmpfs devtmpfs /dev
97+
mkdir -p /dev/pts
98+
mount -t devpts devpts /dev/pts
99+
mount -t proc proc /proc
100+
mount -t sysfs sysfs /sys
101+
102+
# re-execute the main script for the 2nd time, causing it to go back to the main flow
103+
# (but this time already inside the newly created namespace)
104+
reexecute
105+
106+
# after the main flow terminates and the namespace exist, post_commit_root is executed - be sure to look at it
107+
}
108+
109+
if [ "$1" = "build_inside_namespace" ]
110+
then
111+
build_inside_namespace
112+
exit
113+
fi

0 commit comments

Comments
 (0)