Debian Codegraph @

container-entrypoint

raw
1 #!/bin/sh
2 #
3 # kas - setup tool for bitbake based projects
4 #
5 # Copyright (c) Siemens AG, 2017-2026
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 do_enable_qemu_binfmts()
26 {
27 if ! [ -f /proc/sys/fs/binfmt_misc/status ]; then
28 sudo mount -t binfmt_misc null /proc/sys/fs/binfmt_misc || return
29 fi
30 for CONF in /usr/lib/binfmt.d/qemu-*.conf; do
31 if ! [ -f /proc/sys/fs/binfmt_misc/"$(basename "${CONF%%.conf}")" ]; then
32 sudo sh -c "cat '$CONF' > /proc/sys/fs/binfmt_misc/register" || return
33 fi
34 done
35 }
36
37 enable_qemu_binfmts()
38 {
39 if ! do_enable_qemu_binfmts; then
40 echo "kas-container: failed to set up binfmt_misc" >&2
41 exit 1
42 fi
43 }
44
45 # For rootless nesting of the uid_ns, we need to provide a list of sub
46 # uids/gids which can be used in the sub namespace. As each id in the
47 # sub namespace must be mappable into the parent namespace, we need to
48 # compute the mapping based on the layout of the parent: Podman maps
49 # to an intermediate namespace, hence giving us a user-id map we
50 # actually can use for further splitting. Docker maps directly, hence
51 # we need to compute the number of sub ids we can use (which have a
52 # mapping in the parent ns)
53 #
54 # Note, that the default range (65k) does NOT allow to map nobody/nogroup,
55 # in the sub-namespace, as this usually is id 65k-1 and we loose at least
56 # one id per nesting. If we get a larger range, we just map as much as
57 # we can and by that make nobody/nogroup usable.
58 # See man user_namespaces for details.
59 setup_userns_mappings()
60 {
61 UID_ROW=$(sort -r /proc/self/uid_map | head -1)
62 UID_OUTER=$(printf '%s' "$UID_ROW" | awk '{print $1}')
63 UID_INNER=$(printf '%s' "$UID_ROW" | awk '{print $2}')
64 UID_COUNT=$(printf '%s' "$UID_ROW" | awk '{print $3}')
65 GID_ROW=$(sort -r /proc/self/gid_map | head -1)
66 GID_OUTER=$(printf '%s' "$GID_ROW" | awk '{print $1}')
67 GID_INNER=$(printf '%s' "$GID_ROW" | awk '{print $2}')
68 GID_COUNT=$(printf '%s' "$GID_ROW" | awk '{print $3}')
69
70 # docker (direct mapping)
71 if [ "$UID_OUTER" = "1" ]; then
72 UID_INNER="$(($(id -u builder) + 1))"
73 [ "$UID_INNER" -lt "$UID_COUNT" ] && \
74 UID_COUNT="$((UID_COUNT - UID_INNER))"
75 fi
76 if [ "$GID_OUTER" = "1" ]; then
77 GID_INNER="$(($(id -g builder) + 1))"
78 [ "$GID_INNER" -lt "$GID_COUNT" ] && \
79 GID_COUNT="$((GID_COUNT - GID_INNER))"
80 fi
81
82 echo "builder:${UID_INNER}:${UID_COUNT}" | sudo tee /etc/subuid > /dev/null
83 echo "builder:${GID_INNER}:${GID_COUNT}" | sudo tee /etc/subgid > /dev/null
84 }
85
86 # kas-isar: enable_qemu_binfmts
87
88 chown_managed_dirs()
89 {
90 for DIR in /build /work /sstate /downloads /repo-ref; do
91 if [ -d "$DIR" ]; then
92 chown "$1":"$2" "$DIR"
93 fi
94 done
95
96 # SSH Directory Permissions
97 if [ -d "/builder/.ssh" ]; then
98 chown -R "$1":"$2" "/builder/.ssh"
99 fi
100 }
101
102 restore_managed_dirs_owner()
103 {
104 chown_managed_dirs 0 0
105 }
106
107 if mount | grep -q "on / type aufs"; then
108 cat <<EOF >&2
109 WARNING: Generation of wic images will fail!
110
111 Your docker host setup uses broken aufs as storage driver. Adjust the docker
112 configuration to use a different driver (overlay, overlay2, devicemapper). You
113 may also need to update the host distribution (e.g. Debian Jessie -> Stretch).
114
115 EOF
116 fi
117
118 # if the container is started from a not supported terminal, fallback to xterm
119 if [ -n "$TERM" ]; then
120 infocmp "$TERM" > /dev/null 2>&1 || TERM=xterm
121 fi
122
123 if [ -z "$USER_ID" ] && [ -n "$CI_PROJECT_DIR" ]; then
124 # Work around for gitlab-runner not aligning checked out repo ownership
125 # with our builder user. We handle that internally in kas, but we
126 # need the exception here as well for git calls outside of kas.
127 sudo git config --system safe.directory "$CI_PROJECT_DIR"
128
129 # Account for externally specified git clone path
130 if [ -n "$GIT_CLONE_PATH" ]; then
131 sudo git config --system --add safe.directory "$GIT_CLONE_PATH"
132 fi
133 fi
134 if [ -z "$USER_ID" ] || [ "$USER_ID" = 0 ]; then
135 # Not a kas-container call, or we shall run everything as root
136 GOSU=""
137 else
138 GROUP_ID=${GROUP_ID:-$(id -g)}
139
140 groupmod -o --gid "$GROUP_ID" builder
141 usermod -o --uid "$USER_ID" --gid "$GROUP_ID" builder >/dev/null
142 chown -R "$USER_ID":"$GROUP_ID" /builder
143 # copy host SSH config into home of builder
144 if [ -d /var/kas/userdata/.ssh ]; then
145 cp -a /var/kas/userdata/.ssh /builder/
146 fi
147 # adjust timezone to host
148 if [ -n "${KAS_HOST_TZ}" ] && [ -f "/usr/share/zoneinfo/${KAS_HOST_TZ}" ]; then
149 echo "${KAS_HOST_TZ}" > /etc/timezone
150 ln -sf "/usr/share/zoneinfo/${KAS_HOST_TZ}" /etc/localtime
151 fi
152
153 GOSU="gosu builder"
154 fi
155 # after all uid / gid changes are done, setup the namespace mappings
156 # kas-isar: setup_userns_mappings
157
158 # kas-container on rootless docker workaround
159 if [ -n "$USER_ID" ] && [ "$USER_ID" -ne 0 ] && \
160 [ "$KAS_DOCKER_ROOTLESS" = "1" ] && [ "$(stat -c %u /repo)" -eq 0 ]; then
161 # Docker rootless does not support keeping the user namespace
162 # (podman option --userns=keep-id). By that, the bind mounts
163 # are owned by root.
164 git config --system safe.directory /repo
165 chown_managed_dirs "$USER_ID" "$GROUP_ID"
166
167 # Copy userdata to a writable location so we can chown it.
168 if [ -d "/var/kas/userdata" ]; then
169 mv /var/kas/userdata /var/kas/userdata.orig
170 cp -r /var/kas/userdata.orig /var/kas/userdata
171 chown -R "$USER_ID":"$GROUP_ID" /var/kas/userdata
172 fi
173 fi
174
175 if [ "$PWD" = / ]; then
176 cd /builder || exit 1
177 fi
178
179 if [ -n "$1" ]; then
180 case "$1" in
181 build|checkout|clean*|diff|dump|for-all-repos|lock|menu|purge|shell|-*)
182 # We must restore the dir owner after every kas invocation.
183 # This is cheap as only the top-level dirs are changed (non recursive).
184 if [ "$KAS_DOCKER_ROOTLESS" = "1" ]; then
185 trap restore_managed_dirs_owner EXIT INT TERM
186 $GOSU kas "$@"
187 else
188 # SC2086: Double quote to prevent globbing and word splitting.
189 # shellcheck disable=2086
190 exec $GOSU kas "$@"
191 fi
192 ;;
193 *)
194 if [ -n "$USER_ID" ]; then
195 echo "kas-container: this container does not support \"kas $1\"" >&2
196 exit 1
197 fi
198 # SC2086: Double quote to prevent globbing and word splitting.
199 # shellcheck disable=2086
200 exec $GOSU "$@"
201 ;;
202 esac
203 else
204 # SC2086: Double quote to prevent globbing and word splitting.
205 # shellcheck disable=2086
206 exec $GOSU bash
207 fi