Skip to content

Commit a058f06

Browse files
committed
Don't include the uid/gid of the host user in the image
This way the image is usable by everybody, independantly of its ids. With docker, we modify the builder user in the entrypoint to match the uid:gid of the user launching the container, and then continue with the builder user thanks to gosu. With podman we use the `--userns` option to map the builder user to the user on the system. I haven't found a way with podman to use the same mechanism as in docker, and vis versa. Signed-off-by: Gaëtan Lehmann <[email protected]>
1 parent 5d1a377 commit a058f06

File tree

5 files changed

+54
-66
lines changed

5 files changed

+54
-66
lines changed

src/xcp_ng_dev/build.sh

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ fi
6767

6868
cd $(dirname "$0")
6969

70-
CUSTOM_ARGS=()
71-
7270
ALMA_VERSION=
7371
CENTOS_VERSION=
7472
case "$1" in
@@ -87,28 +85,8 @@ case "$1" in
8785
;;
8886
esac
8987

90-
CUSTOM_UID="$(id -u)"
91-
CUSTOM_GID="$(id -g)"
92-
93-
if [ "${CUSTOM_UID}" -eq 0 ] || [ "${CUSTOM_GID}" -eq 0 ]; then
94-
if [ -z "${SUDO_GID}" ] || [ -z "${SUDO_UID}" ] || [ -z "${SUDO_USER}" ] || \
95-
[ -z "${SUDO_COMMAND}" ] || [ "${SUDO_GID}" -eq 0 ] || [ "${SUDO_UID}" -eq 0 ]; then
96-
echo -e "[ERROR] This operation cannot be performed by the 'root' user directly:"
97-
echo -e "\tplease use an unprivileged user (eventually with 'sudo')"
98-
exit 1
99-
fi
100-
CUSTOM_UID="${SUDO_UID}"
101-
CUSTOM_GID="${SUDO_GID}"
102-
fi
103-
104-
# Support for seamless use of current host user
105-
# and Docker user "builder" inside the image
106-
CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_UID=${CUSTOM_UID}" )
107-
CUSTOM_ARGS+=( "--build-arg" "CUSTOM_BUILDER_GID=${CUSTOM_GID}" )
108-
10988
"$RUNNER" build \
11089
--platform "$PLATFORM" \
111-
"${CUSTOM_ARGS[@]}" \
11290
-t ghcr.io/xcp-ng/xcp-ng-build-env:${1} \
11391
--build-arg XCP_NG_BRANCH=${1} \
11492
--ulimit nofile=1024 \

src/xcp_ng_dev/cli.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,18 @@ def container(args):
148148
branch = args.branch
149149
docker_arch = args.platform or ("linux/amd64/v2" if branch == "9.0" else "linux/amd64")
150150

151-
docker_args = [RUNNER, "run", "-i", "-t",
152-
"-u", "builder",
153-
"--platform", docker_arch,
154-
]
151+
docker_args = [RUNNER, "run", "-i", "-t", "--platform", docker_arch]
152+
155153
if is_podman(RUNNER):
156-
docker_args += ["--userns=keep-id", "--security-opt", "label=disable"]
154+
# With podman we use the `--userns` option to map the builder user to
155+
# the user on the system.
156+
docker_args += [f"--userns=keep-id:uid=1000,gid=1000", "--security-opt", "label=disable"]
157+
else:
158+
# With docker, we modify the builder user in the entrypoint to match the
159+
# uid:gid of the user launching the container, and then continue with
160+
# the builder user thanks to gosu.
161+
docker_args += ["-e", f'BUILDER_UID={os.getuid()}', "-e", f'BUILDER_GID={os.getgid()}']
162+
157163
if args.rm:
158164
docker_args += ["--rm=true"]
159165

src/xcp_ng_dev/files/Dockerfile-8.x

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ ARG CENTOS_VERSION=7.5.1804
22

33
FROM centos:${CENTOS_VERSION}
44

5-
ARG CUSTOM_BUILDER_UID=""
6-
ARG CUSTOM_BUILDER_GID=""
7-
85
# Remove all repositories
96
RUN rm /etc/yum.repos.d/*
107

118
# Add only the specific CentOS 7.5 repositories, because that's what XS used for the majority of packages
129
ARG CENTOS_VERSION
1310
COPY files/CentOS-Vault.repo.in /etc/yum.repos.d/CentOS-Vault-7.5.repo
14-
RUN sed -e "s/@CENTOS_VERSION@/${CENTOS_VERSION}/g" -i /etc/yum.repos.d/CentOS-Vault-7.5.repo
11+
RUN sed -i -e "s/@CENTOS_VERSION@/${CENTOS_VERSION}/g" /etc/yum.repos.d/CentOS-Vault-7.5.repo
1512

1613
# Add our repositories
1714
# Repository file depends on the target version of XCP-ng, and is pre-processed by build.sh
1815
ARG XCP_NG_BRANCH=8.3
1916
COPY files/xcp-ng.repo.8.x.in /etc/yum.repos.d/xcp-ng.repo
20-
RUN sed -e "s/@XCP_NG_BRANCH@/${XCP_NG_BRANCH}/g" -i /etc/yum.repos.d/xcp-ng.repo
17+
RUN sed -i -e "s/@XCP_NG_BRANCH@/${XCP_NG_BRANCH}/g" /etc/yum.repos.d/xcp-ng.repo
2118

2219
# Install GPG key
2320
RUN curl -sSf https://xcp-ng.org/RPM-GPG-KEY-xcpng -o /etc/pki/rpm-gpg/RPM-GPG-KEY-xcpng
@@ -58,23 +55,18 @@ RUN yum clean all
5855
# OCaml in XS may be older than in CentOS
5956
RUN sed -i "/gpgkey/a exclude=ocaml*" /etc/yum.repos.d/Cent* /etc/yum.repos.d/epel*
6057

61-
# Set up the builder user
62-
RUN bash -c ' \
63-
OPTS=(); \
64-
if [ -n "${CUSTOM_BUILDER_UID}" ]; then \
65-
OPTS+=("-u" "${CUSTOM_BUILDER_UID}"); \
66-
fi; \
67-
if [ -n "${CUSTOM_BUILDER_GID}" ]; then \
68-
OPTS+=("-g" "${CUSTOM_BUILDER_GID}"); \
69-
if ! getent group "${CUSTOM_BUILDER_GID}" >/dev/null; then \
70-
groupadd -g "${CUSTOM_BUILDER_GID}" builder; \
71-
fi; \
72-
fi; \
73-
useradd "${OPTS[@]}" builder; \
74-
' \
58+
# create the builder user
59+
RUN groupadd -g 1000 builder \
60+
&& useradd -u 1000 -g 1000 builder \
7561
&& echo "builder:builder" | chpasswd \
7662
&& echo "builder ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
7763

7864
RUN mkdir -p /usr/local/bin
65+
RUN curl -fsSL "https://github.com/tianon/gosu/releases/download/1.17/gosu-amd64" -o /usr/local/bin/gosu \
66+
&& chmod +x /usr/local/bin/gosu
7967
COPY files/init-container.sh /usr/local/bin/init-container.sh
80-
COPY files/rpmmacros /home/builder/.rpmmacros
68+
COPY files/entrypoint.sh /usr/local/bin/entrypoint.sh
69+
COPY --chown=builder:builder files/rpmmacros /home/builder/.rpmmacros
70+
71+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
72+
CMD ["bash"]

src/xcp_ng_dev/files/Dockerfile-9.x

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
FROM ghcr.io/almalinux/10-base:10.0
22

3-
ARG CUSTOM_BUILDER_UID=""
4-
ARG CUSTOM_BUILDER_GID=""
5-
63
# Add our repositories
74
# temporary bootstrap repository
85
COPY files/xcp-ng-8.99.repo /etc/yum.repos.d/xcp-ng.repo
@@ -55,25 +52,19 @@ RUN dnf config-manager --enable crb
5552
# workaround sudo not working (e.g. in podman 4.9.3 in Ubuntu 24.04)
5653
RUN chmod 0400 /etc/shadow
5754
58-
# Set up the builder user
59-
RUN bash -c ' \
60-
OPTS=(); \
61-
if [ -n "${CUSTOM_BUILDER_UID}" ]; then \
62-
OPTS+=("-u" "${CUSTOM_BUILDER_UID}"); \
63-
fi; \
64-
if [ -n "${CUSTOM_BUILDER_GID}" ]; then \
65-
OPTS+=("-g" "${CUSTOM_BUILDER_GID}"); \
66-
if ! getent group "${CUSTOM_BUILDER_GID}" >/dev/null; then \
67-
groupadd -g "${CUSTOM_BUILDER_GID}" builder; \
68-
fi; \
69-
fi; \
70-
useradd "${OPTS[@]}" builder; \
71-
' \
55+
# create the builder user
56+
RUN groupadd -g 1000 builder \
57+
&& useradd -u 1000 -g 1000 builder \
7258
&& echo "builder:builder" | chpasswd \
7359
&& echo "builder ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
7460
7561
RUN mkdir -p /usr/local/bin
62+
RUN curl -fsSL "https://github.com/tianon/gosu/releases/download/1.17/gosu-amd64" -o /usr/local/bin/gosu \
63+
&& chmod +x /usr/local/bin/gosu
7664
COPY files/init-container.sh /usr/local/bin/init-container.sh
77-
65+
COPY files/entrypoint.sh /usr/local/bin/entrypoint.sh
7866
# FIXME: check it we really need any of this
79-
# COPY files/rpmmacros /home/builder/.rpmmacros
67+
# COPY --chown=builder:builder files/rpmmacros /home/builder/.rpmmacros
68+
69+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
70+
CMD ["bash"]

src/xcp_ng_dev/files/entrypoint.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
if [ -n "${SCRIPT_DEBUG}" ]; then
5+
set -x
6+
fi
7+
8+
if [ "${BUILDER_UID}" ]; then
9+
# BUILDER_UID is defined, update the builder ids, and continue with the builder user
10+
if [ "${BUILDER_GID}" != "1000" ]; then
11+
groupmod -g "${BUILDER_GID}" builder
12+
fi
13+
if [ "${BUILDER_UID}" != "1000" ]; then
14+
usermod -u "${BUILDER_UID}" -g "${BUILDER_GID}" builder
15+
fi
16+
find ~builder -maxdepth 1 -type f | xargs chown builder:builder
17+
exec /usr/local/bin/gosu builder "$@"
18+
else
19+
# no BUILDER_ID, just continue as the current user
20+
exec "$@"
21+
fi

0 commit comments

Comments
 (0)