Skip to content

Commit a1500d4

Browse files
committed
chore: add devtool command for building CI artifacts
Running resources/rebuild.sh script assumes an Ubuntu host (it relies on apt). Add a new `build_ci_artifacts` command in devtool that runs the script inside devctr. This allows us to build CI artifacts without necessarily being in an Ubuntu system. Also, extend resources/rebuild.sh to perform some cleanup after running commands so, at the end, it only produces the directory with the artifacts and no intermediate by-products. Signed-off-by: Babis Chalios <[email protected]>
1 parent 7be3b30 commit a1500d4

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

resources/rebuild.sh

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ source "$GIT_ROOT_DIR/tools/functions"
1616

1717
# Make sure we have all the needed tools
1818
function install_dependencies {
19-
sudo apt update
20-
sudo apt install -y bc flex bison gcc make libelf-dev libssl-dev squashfs-tools busybox-static tree cpio curl
19+
apt update
20+
apt install -y bc flex bison gcc make libelf-dev libssl-dev squashfs-tools busybox-static tree cpio curl patch docker.io
2121
}
2222

2323
function dir2ext4img {
@@ -30,13 +30,21 @@ function dir2ext4img {
3030
local TMP_MNT=$(mktemp -d)
3131
truncate -s "$SIZE" "$IMG"
3232
mkfs.ext4 -F "$IMG"
33-
sudo mount "$IMG" "$TMP_MNT"
34-
sudo tar c -C $DIR . |sudo tar x -C "$TMP_MNT"
33+
mount "$IMG" "$TMP_MNT"
34+
tar c -C $DIR . |tar x -C "$TMP_MNT"
3535
# cleanup
36-
sudo umount "$TMP_MNT"
36+
# Use the -l flag for lazy unmounting since sometimes umount fails
37+
# with "device busy" and simply calling `sync` doesn't help
38+
umount -l "$TMP_MNT"
3739
rmdir $TMP_MNT
3840
}
3941

42+
function prepare_docker {
43+
nohup /usr/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 &
44+
45+
# Wait for Docker socket to be created
46+
timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
47+
}
4048

4149
function compile_and_install {
4250
local C_FILE=$1
@@ -54,6 +62,9 @@ function build_rootfs {
5462
local rootfs="tmp_rootfs"
5563
mkdir -pv "$rootfs"
5664

65+
# Launch Docker
66+
prepare_docker
67+
5768
cp -rvf overlay/* $rootfs
5869

5970
# curl -O https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64-root.tar.xz
@@ -76,25 +87,29 @@ mkdir -pv $rootfs/var/lib/dpkg/
7687
EOF
7788

7889
# TBD what abt /etc/hosts?
79-
echo |sudo tee $rootfs/etc/resolv.conf
90+
echo | tee $rootfs/etc/resolv.conf
8091

8192
# Generate key for ssh access from host
8293
if [ ! -s id_rsa ]; then
8394
ssh-keygen -f id_rsa -N ""
8495
fi
85-
sudo install -d -m 0600 "$rootfs/root/.ssh/"
86-
sudo cp id_rsa.pub "$rootfs/root/.ssh/authorized_keys"
96+
install -d -m 0600 "$rootfs/root/.ssh/"
97+
cp id_rsa.pub "$rootfs/root/.ssh/authorized_keys"
8798
id_rsa=$OUTPUT_DIR/$ROOTFS_NAME.id_rsa
88-
sudo cp id_rsa $id_rsa
99+
cp id_rsa $id_rsa
89100

90101
# -comp zstd but guest kernel does not support
91102
rootfs_img="$OUTPUT_DIR/$ROOTFS_NAME.squashfs"
92-
sudo mv $rootfs/root/manifest $OUTPUT_DIR/$ROOTFS_NAME.manifest
93-
sudo mksquashfs $rootfs $rootfs_img -all-root -noappend
103+
mv $rootfs/root/manifest $OUTPUT_DIR/$ROOTFS_NAME.manifest
104+
mksquashfs $rootfs $rootfs_img -all-root -noappend
94105
rootfs_ext4=$OUTPUT_DIR/$ROOTFS_NAME.ext4
95106
dir2ext4img $rootfs $rootfs_ext4
96-
sudo rm -rf $rootfs
97-
sudo chown -Rc $USER. $OUTPUT_DIR
107+
rm -rf $rootfs
108+
for bin in fast_page_fault_helper fillmem init readmem; do
109+
rm $PWD/overlay/usr/local/bin/$bin
110+
done
111+
rm -f id_rsa{,.pub}
112+
rm -f nohup.out
98113
}
99114

100115

@@ -241,6 +256,10 @@ function build_al_kernels {
241256
if [[ "$KERNEL_VERSION" == @(all|6.1) ]]; then
242257
build_al_kernel $PWD/guest_configs/microvm-kernel-ci-$ARCH-6.1.config 5.10
243258
fi
259+
260+
# Undo kernel patches on top of AL configuration
261+
git restore $PWD/guest_configs
262+
rm -rf $PWD/guest_configs/*.orig
244263
}
245264

246265
function print_help {

tools/devtool

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ OPT_UNATTENDED=false
134134
# Get the target prefix to avoid repeated calls to uname -m
135135
TARGET_PREFIX="$(uname -m)-unknown-linux-"
136136

137+
# Container path to directory where we store built CI artifacts.
138+
CTR_CI_ARTIFACTS_PATH="${CTR_FC_ROOT_DIR}/resources/$(uname -m)"
137139

138140
# Check if Docker is available and exit if it's not.
139141
# Upon returning from this call, the caller can be certain Docker is available.
@@ -231,7 +233,7 @@ cmd_fix_perms() {
231233
run_devctr \
232234
--workdir "$CTR_FC_ROOT_DIR" \
233235
-- \
234-
chown -R "$(id -u):$(id -g)" "$CTR_FC_BUILD_DIR" "$CTR_TEST_RESULTS_DIR"
236+
chown -R "$(id -u):$(id -g)" "$CTR_FC_BUILD_DIR" "$CTR_TEST_RESULTS_DIR" "$CTR_CI_ARTIFACTS_PATH"
235237
}
236238

237239
# Builds the development container from its Dockerfile.
@@ -412,6 +414,10 @@ cmd_help() {
412414
echo " --performance Tweak various setting of the host running the tests (such as C- and P-states)"
413415
echo " to achieve consistent performance. Used for running performance tests in CI."
414416
echo ""
417+
echo " build_ci_artifacts [all|rootfs|kernels]"
418+
echo " Builds the rootfs and guest kernel artifacts we use for our CI."
419+
echo " Run './tools/devtool build_ci_artifacts help' for more details about the available commands."
420+
echo ""
415421

416422
cat <<EOF
417423
test_debug [-- [<pytest args>]]
@@ -1161,6 +1167,20 @@ cmd_install() {
11611167
done
11621168
}
11631169

1170+
cmd_build_ci_artifacts() {
1171+
# Check prerequisites
1172+
ensure_devctr
1173+
1174+
# We need to run nested Docker here, so run this container as privileged.
1175+
run_devctr \
1176+
--privileged \
1177+
--workdir "$CTR_FC_ROOT_DIR" \
1178+
-- \
1179+
./resources/rebuild.sh "$@"
1180+
1181+
cmd_fix_perms
1182+
}
1183+
11641184

11651185
main() {
11661186

0 commit comments

Comments
 (0)