]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-oci.in
setproctitle(): Handle potential NULL return from strrchr()
[mirror_lxc.git] / templates / lxc-oci.in
1 #!/bin/bash
2
3 # Create application containers from OCI images
4
5 # Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
6 # Copyright © 2017 Serge Hallyn <serge@hallyn.com>
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 # USA
22
23 set -eu
24
25 # Make sure the usual locations are in PATH
26 export PATH="$PATH:/usr/sbin:/usr/bin:/sbin:/bin"
27
28 # Check for required binaries
29 for bin in skopeo umoci jq; do
30 if ! command -v $bin >/dev/null 2>&1; then
31 echo "ERROR: Missing required tool: $bin" 1>&2
32 exit 1
33 fi
34 done
35
36 LOCALSTATEDIR=@LOCALSTATEDIR@
37 LXC_TEMPLATE_CONFIG=@LXCTEMPLATECONFIG@
38 LXC_HOOK_DIR=@LXCHOOKDIR@
39 MOUNT_HELPER="atomfs"
40 MOUNTED_WORKDIR=""
41
42 # Some useful functions
43 cleanup() {
44 if [ -d "${LXC_ROOTFS}.tmp" ]; then
45 rm -Rf "${LXC_ROOTFS}.tmp"
46 fi
47 if [ -n "${MOUNTED_WORKDIR}" ]; then
48 echo "${MOUNT_HELPER} unmount ${MOUNTED_WORKDIR}" >&2
49 "${MOUNT_HELPER}" umount "${MOUNTED_WORKDIR}"
50 MOUNTED_WORKDIR=""
51 fi
52 }
53
54 in_userns() {
55 [ -e /proc/self/uid_map ] || { echo no; return; }
56 while read -r line; do
57 fields="$(echo "$line" | awk '{ print $1 " " $2 " " $3 }')"
58 if [ "${fields}" = "0 0 4294967295" ]; then
59 echo no;
60 return;
61 fi
62 if echo "${fields}" | grep -q " 0 1$"; then
63 echo userns-root;
64 return;
65 fi
66 done < /proc/self/uid_map
67
68 if [ -e /proc/1/uid_map ]; then
69 if [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ]; then
70 echo userns-root
71 return
72 fi
73 fi
74 echo yes
75 }
76
77 getconfigpath() {
78 local basedir="$1" mfpath="$2" cdigest=""
79 # Ok we have the image config digest, now get the config ref from the manifest.
80 # shellcheck disable=SC2039
81 cdigest=$(jq -c -r '.config.digest' < "$mfpath")
82 if [ -z "${cdigest}" ]; then
83 echo "container config not found" >&2
84 return
85 fi
86
87 # cdigest is '<hashtype>:<hash>', so 'ht' gets type, hv gets value.
88 local ht="${cdigest%%:*}" hv="${cdigest#*:}" p=""
89 p="$basedir/blobs/$ht/$hv"
90 if [ ! -f "$p" ]; then
91 echo "config file did not exist for digest $cdigest" >&2
92 return 1
93 fi
94 echo "$p"
95 }
96
97 getmanifestpath() {
98 local basedir="$1" ref="$2" p=""
99 # if given 'sha256:<hash>' then return the blobs/sha256/hash
100 case "$ref" in
101 sha256:*)
102 p="$basedir/blobs/sha256/${ref#sha256:}"
103 [ -f "$p" ] && echo "$p" && return 0
104 echo "could not find manifest path to blob $ref. file did not exist: $p" >&2
105 return 1
106 ;;
107 esac
108 # find the reference by annotation
109 local blobref="" hashtype="" hashval=""
110 blobref=$(jq -c -r --arg q "$ref" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else empty end' < "${basedir}/index.json")
111 # blobref is 'hashtype:hash'
112 hashtype="${blobref%%:*}"
113 hashval="${blobref#*:}"
114 p="$basedir/blobs/$hashtype/$hashval"
115 [ -f "$p" ] && echo "$p" && return 0
116 echo "did not find manifest for $ref. file did not exist: $p" >&2
117 return 1
118 }
119
120 getlayermediatype() {
121 jq -c -r '.layers[0].mediaType' <"$1"
122 }
123
124 # Get entrypoint from oci image. Use sh if unspecified
125 getep() {
126 if [ "$#" -eq 0 ]; then
127 echo "/bin/sh"
128 return
129 fi
130
131 configpath="$1"
132
133 ep=$(jq -c '.config.Entrypoint[]?'< "${configpath}" | tr '\n' ' ')
134 cmd=$(jq -c '.config.Cmd[]?'< "${configpath}" | tr '\n' ' ')
135 if [ -z "${ep}" ]; then
136 ep="${cmd}"
137 if [ -z "${ep}" ]; then
138 ep="/bin/sh"
139 fi
140 elif [ -n "${cmd}" ]; then
141 ep="${ep} ${cmd}"
142 fi
143
144 echo "${ep}"
145 return
146 }
147
148 # get environment from oci image.
149 getenv() {
150 if [ "$#" -eq 0 ]; then
151 return
152 fi
153
154 configpath="$1"
155
156 env=$(jq -c -r '.config.Env[]'< "${configpath}")
157
158 echo "${env}"
159 return
160 }
161
162 # check var is decimal.
163 isdecimal() {
164 var="$1"
165 if [ "${var}" -eq "${var}" ] 2> /dev/null; then
166 return 0
167 else
168 return 1
169 fi
170 }
171
172 # get uid, gid from oci image.
173 getuidgid() {
174 configpath="$1"
175 rootpath="$2"
176 passwdpath="${rootpath}/etc/passwd"
177 grouppath="${rootpath}/etc/group"
178
179 usergroup=$(jq -c -r '.config.User' < "${configpath}")
180 # shellcheck disable=SC2039
181 usergroup=(${usergroup//:/ })
182
183 user=${usergroup[0]:-0}
184 if ! isdecimal "${user}"; then
185 if [ -f ${passwdpath} ]; then
186 user=$(grep "^${user}:" "${passwdpath}" | awk -F: '{print $3}')
187 else
188 user=0
189 fi
190 fi
191
192 group=${usergroup[1]:-}
193 if [ -z "${group}" ]; then
194 if [ -f "${passwdpath}" ]; then
195 group=$(grep "^[^:]*:[^:]*:${user}:" "${passwdpath}" | awk -F: '{print $4}')
196 else
197 group=0
198 fi
199 elif ! isdecimal "${group}"; then
200 if [ -f "${grouppath}" ]; then
201 group=$(grep "^${group}:" "${grouppath}" | awk -F: '{print $3}')
202 else
203 group=0
204 fi
205 fi
206
207 echo "${user:-0} ${group:-0}"
208 return
209 }
210
211 # get cwd from oci image.
212 getcwd() {
213 if [ "$#" -eq 0 ]; then
214 echo "/"
215 return
216 fi
217
218 configpath="$1"
219
220 cwd=$(jq -c -r '.config.WorkingDir // "/"' < "${configpath}")
221
222 echo "${cwd}"
223 return
224 }
225
226 usage() {
227 cat <<EOF
228 LXC container template for OCI images
229
230 Special arguments:
231 [ -h | --help ]: Print this help message and exit.
232
233 Required arguments:
234 [ -u | --url <url> ]: The OCI image URL
235
236 Optional arguments:
237 [ --username <username> ]: The username for the registry
238 [ --password <password> ]: The password for the registry
239 [ --mount-helper <command> ]: program that will be used to mount. default is 'atomfs'
240
241 mount-helper is expected to support being called with 'mount'
242 and 'umount' subcommands as below:
243
244 mount-helper mount oci:<oci_dir>:<oci_name> <mountpoint>
245 mount-helper umount <mountpoint>
246
247 LXC internal arguments (do not pass manually!):
248 [ --name <name> ]: The container name
249 [ --path <path> ]: The path to the container
250 [ --rootfs <rootfs> ]: The path to the container's rootfs
251 [ --mapped-uid <map> ]: A uid map (user namespaces)
252 [ --mapped-gid <map> ]: A gid map (user namespaces)
253 EOF
254 return 0
255 }
256
257 if ! options=$(getopt -o u:h -l help,url:,username:,password:,no-cache,dhcp,name:,path:,rootfs:,mapped-uid:,mapped-gid:,mount-helper: -- "$@"); then
258 usage
259 exit 1
260 fi
261 eval set -- "$options"
262
263 OCI_URL=""
264 OCI_USERNAME=
265 OCI_PASSWORD=
266 OCI_USE_CACHE="true"
267 OCI_USE_DHCP="false"
268
269 LXC_MAPPED_GID=
270 LXC_MAPPED_UID=
271 LXC_NAME=
272 LXC_PATH=
273 LXC_ROOTFS=
274
275 while :; do
276 case "$1" in
277 -h|--help) usage && exit 1;;
278 -u|--url) OCI_URL=$2; shift 2;;
279 --username) OCI_USERNAME=$2; shift 2;;
280 --password) OCI_PASSWORD=$2; shift 2;;
281 --no-cache) OCI_USE_CACHE="false"; shift 1;;
282 --dhcp) OCI_USE_DHCP="true"; shift 1;;
283 --name) LXC_NAME=$2; shift 2;;
284 --path) LXC_PATH=$2; shift 2;;
285 --rootfs) LXC_ROOTFS=$2; shift 2;;
286 --mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
287 --mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
288 --mount-helper) MOUNT_HELPER=$2; shift 2;;
289 *) break;;
290 esac
291 done
292
293 # Check that we have all variables we need
294 if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
295 echo "ERROR: Not running through LXC" 1>&2
296 exit 1
297 fi
298
299 if [ -z "$OCI_URL" ]; then
300 echo "ERROR: no OCI URL given"
301 exit 1
302 fi
303
304 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
305 echo "ERROR: password given but no username specified"
306 exit 1
307 fi
308
309 if [ "${OCI_USE_CACHE}" = "true" ]; then
310 if ! skopeo copy --help | grep -q 'dest-shared-blob-dir'; then
311 echo "INFO: skopeo doesn't support blob caching"
312 OCI_USE_CACHE="false"
313 fi
314 fi
315
316 USERNS=$(in_userns)
317
318 if [ "$USERNS" = "yes" ]; then
319 if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
320 echo "ERROR: In a user namespace without a map" 1>&2
321 exit 1
322 fi
323 fi
324
325 OCI_DIR="$LXC_PATH/oci"
326 if [ "${OCI_USE_CACHE}" = "true" ]; then
327 if [ "$USERNS" = "yes" ]; then
328 DOWNLOAD_BASE="${HOME}/.cache/lxc"
329 else
330 DOWNLOAD_BASE="${LOCALSTATEDIR}/cache/lxc"
331 fi
332 else
333 DOWNLOAD_BASE="$OCI_DIR"
334 fi
335 mkdir -p "${DOWNLOAD_BASE}"
336
337 # Trap all exit signals
338 trap cleanup EXIT HUP INT TERM
339
340 # Download the image
341 # shellcheck disable=SC2039
342 skopeo_args=("--remove-signatures" "--insecure-policy")
343 if [ -n "$OCI_USERNAME" ]; then
344 CREDENTIALS="${OCI_USERNAME}"
345
346 if [ -n "$OCI_PASSWORD" ]; then
347 CREDENTIALS="${CREDENTIALS}:${OCI_PASSWORD}"
348 fi
349
350 # shellcheck disable=SC2039
351 skopeo_args+=(--src-creds "${CREDENTIALS}")
352 fi
353
354 OCI_NAME="$LXC_NAME"
355 if [ "${OCI_USE_CACHE}" = "true" ]; then
356 skopeo_args+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
357 mkdir -p "${OCI_DIR}/blobs/"
358 ln -s "${DOWNLOAD_BASE}/sha256" "${OCI_DIR}/blobs/sha256"
359 fi
360
361 skopeo copy "${skopeo_args[@]}" "${OCI_URL}" "oci:${OCI_DIR}:${OCI_NAME}"
362
363 mfpath=$(getmanifestpath "${OCI_DIR}" "${OCI_NAME}")
364 OCI_CONF_FILE=$(getconfigpath "${OCI_DIR}" "$mfpath")
365 mediatype=$(getlayermediatype "$mfpath")
366 echo "mfpath=$mfpath conf=$OCI_CONF_FILE" 1>&2
367 echo "mediatype=$mediatype" >&2
368
369 case "$mediatype" in
370 #application/vnd.oci.image.layer.v1.tar+gzip
371 application/vnd.oci.image.layer.v1.tar*)
372 echo "Unpacking tar rootfs" 2>&1
373 # shellcheck disable=SC2039
374 umoci_args=("")
375 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
376 # shellcheck disable=SC2039
377 umoci_args+=(--rootless)
378 fi
379 # shellcheck disable=SC2039
380 # shellcheck disable=SC2068
381 umoci --log=error unpack ${umoci_args[@]} --image "${OCI_DIR}:${OCI_NAME}" "${LXC_ROOTFS}.tmp"
382 find "${LXC_ROOTFS}.tmp/rootfs" -mindepth 1 -maxdepth 1 -exec mv '{}' "${LXC_ROOTFS}/" \;
383 ;;
384 #application/vnd.stacker.image.layer.squashfs+zstd+verity
385 application/vnd.*.image.layer.squashfs*)
386 if ! command -v "${MOUNT_HELPER}" >/dev/null 2>&1; then
387 echo "media type $mediatype requires $MOUNT_HELPER" >&2
388 exit 1
389 fi
390 echo "$MOUNT_HELPER mount ${OCI_DIR}:${OCI_NAME} $LXC_ROOTFS" >&2
391 "$MOUNT_HELPER" mount "${OCI_DIR}:${OCI_NAME}" "$LXC_ROOTFS"
392 MOUNTED_WORKDIR="$LXC_ROOTFS"
393 ;;
394 *)
395 echo "Unknown media type $mediatype" >&2
396 exit 1
397 ;;
398 esac
399
400 LXC_CONF_FILE="${LXC_PATH}/config"
401 entrypoint=$(getep "${OCI_CONF_FILE}")
402 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
403 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
404
405 case "$mediatype" in
406 application/vnd.*.image.layer.squashfs*)
407 echo "lxc.hook.version = 1" >> "${LXC_CONF_FILE}"
408 # shellcheck disable=SC2016
409 echo "lxc.hook.pre-mount = $MOUNT_HELPER mount" \
410 '${LXC_ROOTFS_PATH}/../oci:${LXC_NAME} ${LXC_ROOTFS_PATH}' \
411 >> "${LXC_CONF_FILE}";;
412 esac
413
414 environment=$(getenv "${OCI_CONF_FILE}")
415 # shellcheck disable=SC2039
416 while read -r line; do
417 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
418 done <<< "${environment}"
419
420 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
421 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
422 fi
423
424 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
425 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
426 fi
427
428 if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf" ]; then
429 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/oci.common.conf" >> "${LXC_CONF_FILE}"
430 fi
431
432 if [ "${OCI_USE_DHCP}" = "true" ]; then
433 echo "lxc.hook.start-host = ${LXC_HOOK_DIR}/dhclient" >> "${LXC_CONF_FILE}"
434 echo "lxc.hook.stop = ${LXC_HOOK_DIR}/dhclient" >> "${LXC_CONF_FILE}"
435 fi
436
437 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
438 # set the hostname
439 cat <<EOF > "${LXC_ROOTFS}/etc/hostname"
440 ${LXC_NAME}
441 EOF
442
443 # set minimal hosts
444 cat <<EOF > "${LXC_ROOTFS}/etc/hosts"
445 127.0.0.1 localhost
446 127.0.1.1 ${LXC_NAME}
447 ::1 localhost ip6-localhost ip6-loopback
448 fe00::0 ip6-localnet
449 ff00::0 ip6-mcastprefix
450 ff02::1 ip6-allnodes
451 ff02::2 ip6-allrouters
452 EOF
453
454 # shellcheck disable=SC2039
455 uidgid=($(getuidgid "${OCI_CONF_FILE}" "${LXC_ROOTFS}" ))
456 # shellcheck disable=SC2039
457 echo "lxc.init.uid = ${uidgid[0]}" >> "${LXC_CONF_FILE}"
458 # shellcheck disable=SC2039
459 echo "lxc.init.gid = ${uidgid[1]}" >> "${LXC_CONF_FILE}"
460
461 cwd=$(getcwd "${OCI_CONF_FILE}")
462 echo "lxc.init.cwd = ${cwd}" >> "${LXC_CONF_FILE}"
463
464 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
465 chown "$LXC_MAPPED_UID" "$LXC_PATH/config" "$LXC_PATH/fstab" > /dev/null 2>&1 || true
466 fi
467 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
468 chgrp "$LXC_MAPPED_GID" "$LXC_PATH/config" "$LXC_PATH/fstab" > /dev/null 2>&1 || true
469 fi
470
471 exit 0