container: Rework uid/gid alignment with caller

Already create the builder user/group during container image build and
only align the IDs in the entrypoint if started with a non-zero USER_ID.
The primary gain is code simplification because this removes some
dynamics from the entrypoint.

As this refactoring avoids that gitlab-ci runners start the container as
root, it was also supposed to resolve the mismatch between the owner of
the checked-out repo and builder user. Unfortunately, this does not work
yet, and the reason is still unclear.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Jan Kiszka
2023-02-05 21:02:07 +01:00
parent a596e2ac33
commit 492b2c56ab
3 changed files with 16 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
#!/bin/bash
# kas-isar: update-binfmts --enable && [ -f /proc/sys/fs/binfmt_misc/status ]
# kas-isar: sudo update-binfmts --enable && [ -f /proc/sys/fs/binfmt_misc/status ]
if mount | grep -q "on / type aufs"; then
cat <<EOF >&2
@@ -13,29 +13,17 @@ may also need to update the host distribution (e.g. Debian Jessie -> Stretch).
EOF
fi
USER_ID=${USER_ID:-30000}
GROUP_ID=${GROUP_ID:-30000}
if [ "$USER_ID" == 0 ]; then
# We shall run everything as root
mkdir -p /builder
GOSU=""
elif [ "$USER_ID" == "$UID" ]; then
if [ -z "$USER_ID" ] || [ "$USER_ID" == 0 ]; then
# Not a kas-container call, or we shall run everything as root
GOSU=""
else
if ! grep -q "^builder:" /etc/group; then
groupadd -o --gid "$GROUP_ID" builder
fi
if ! id builder >/dev/null 2>&1; then
# Create a non-root user that will perform the actual build
useradd -o --uid "$USER_ID" --gid "$GROUP_ID" --create-home \
--home-dir /builder builder
fi
GROUP_ID=${GROUP_ID:-$(id -g)}
groupmod -o --gid "$GROUP_ID" builder
usermod -o --uid "$USER_ID" --gid "$GROUP_ID" builder >/dev/null
chown -R "$USER_ID":"$GROUP_ID" /builder
GOSU="gosu builder"
# kas-isar: sbuild-adduser builder >/dev/null 2>&1
fi
if [ "$PWD" = / ]; then