3 # Create application containers from OCI images
5 # Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
6 # Copyright © 2017 Serge Hallyn <serge@hallyn.com>
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.
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.
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
25 # Make sure the usual locations are in PATH
26 export PATH
="$PATH:/usr/sbin:/usr/bin:/sbin:/bin"
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
36 LOCALSTATEDIR
=@LOCALSTATEDIR@
37 LXC_TEMPLATE_CONFIG
=@LXCTEMPLATECONFIG@
38 LXC_HOOK_DIR
=@LXCHOOKDIR@
42 # Some useful functions
44 if [ -d "${LXC_ROOTFS}.tmp" ]; then
45 rm -Rf "${LXC_ROOTFS}.tmp"
47 if [ -n "${MOUNTED_WORKDIR}" ]; then
48 echo "${MOUNT_HELPER} unmount ${MOUNTED_WORKDIR}" >&2
49 "${MOUNT_HELPER}" umount
"${MOUNTED_WORKDIR}"
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
62 if echo "${fields}" |
grep -q " 0 1$"; then
66 done < /proc
/self
/uid_map
68 if [ -e /proc
/1/uid_map
]; then
69 if [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ]; then
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
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
98 local basedir
="$1" ref
="$2" p
=""
99 # if given 'sha256:<hash>' then return the blobs/sha256/hash
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
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
120 getlayermediatype
() {
121 jq
-c -r '.layers[0].mediaType' <"$1"
124 # Get entrypoint from oci image. Use sh if unspecified
126 if [ "$#" -eq 0 ]; then
133 ep
=$
(jq
-c '.config.Entrypoint[]?'< "${configpath}" |
tr '\n' ' ')
134 cmd
=$
(jq
-c '.config.Cmd[]?'< "${configpath}" |
tr '\n' ' ')
135 if [ -z "${ep}" ]; then
137 if [ -z "${ep}" ]; then
140 elif [ -n "${cmd}" ]; then
148 # get environment from oci image.
150 if [ "$#" -eq 0 ]; then
156 env
=$
(jq
-c -r '.config.Env[]'< "${configpath}")
162 # check var is decimal.
165 if [ "${var}" -eq "${var}" ] 2> /dev
/null
; then
172 # get uid, gid from oci image.
176 passwdpath
="${rootpath}/etc/passwd"
177 grouppath
="${rootpath}/etc/group"
179 usergroup
=$
(jq
-c -r '.config.User' < "${configpath}")
180 # shellcheck disable=SC2039
181 usergroup
=(${usergroup//:/ })
183 user
=${usergroup[0]:-0}
184 if ! isdecimal
"${user}"; then
185 if [ -f ${passwdpath} ]; then
186 user
=$
(grep "^${user}:" "${passwdpath}" | awk -F: '{print $3}')
192 group=${usergroup[1]:-}
193 if [ -z "${group}" ]; then
194 if [ -f "${passwdpath}" ]; then
195 group=$(grep "^
[^
:]*:[^
:]*:${user}:" "${passwdpath}" | awk -F: '{print $4}')
199 elif ! isdecimal "${group}"; then
200 if [ -f "${grouppath}" ]; then
201 group=$(grep "^${group}:" "${grouppath}" | awk -F: '{print $3}')
207 echo "${user:-0} ${group:-0}"
211 # get cwd from oci image.
213 if [ "$#" -eq 0 ]; then
220 cwd
=$
(jq
-c -r '.config.WorkingDir // "/"' < "${configpath}")
228 LXC container template for OCI images
231 [ -h | --help ]: Print this help message and exit.
234 [ -u | --url <url> ]: The OCI image URL
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'
241 mount-helper is expected to support being called with 'mount'
242 and 'umount' subcommands as below:
244 mount-helper mount oci:<oci_dir>:<oci_name> <mountpoint>
245 mount-helper umount <mountpoint>
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)
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
261 eval set -- "$options"
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;;
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
299 if [ -z "$OCI_URL" ]; then
300 echo "ERROR: no OCI URL given"
304 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
305 echo "ERROR: password given but no username specified"
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"
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
325 OCI_DIR
="$LXC_PATH/oci"
326 if [ "${OCI_USE_CACHE}" = "true" ]; then
327 if [ "$USERNS" = "yes" ]; then
328 DOWNLOAD_BASE
="${HOME}/.cache/lxc"
330 DOWNLOAD_BASE
="${LOCALSTATEDIR}/cache/lxc"
333 DOWNLOAD_BASE
="$OCI_DIR"
335 mkdir
-p "${DOWNLOAD_BASE}"
337 # Trap all exit signals
338 trap cleanup EXIT HUP INT TERM
341 # shellcheck disable=SC2039
342 skopeo_args
=("--remove-signatures" "--insecure-policy")
343 if [ -n "$OCI_USERNAME" ]; then
344 CREDENTIALS
="${OCI_USERNAME}"
346 if [ -n "$OCI_PASSWORD" ]; then
347 CREDENTIALS
="${CREDENTIALS}:${OCI_PASSWORD}"
350 # shellcheck disable=SC2039
351 skopeo_args
+=(--src-creds "${CREDENTIALS}")
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"
361 skopeo copy
"${skopeo_args[@]}" "${OCI_URL}" "oci:${OCI_DIR}:${OCI_NAME}"
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
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
375 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
376 # shellcheck disable=SC2039
377 umoci_args
+=(--rootless)
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}/" \;
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
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"
395 echo "Unknown media
type $mediatype" >&2
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}"
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}";;
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}"
420 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf
" ]; then
421 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/common.conf
" >> "${LXC_CONF_FILE}"
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}"
428 if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf
" ]; then
429 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/oci.common.conf
" >> "${LXC_CONF_FILE}"
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}"
437 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
439 cat <<EOF > "${LXC_ROOTFS}/etc
/hostname
"
444 cat <<EOF > "${LXC_ROOTFS}/etc
/hosts
"
446 127.0.1.1 ${LXC_NAME}
447 ::1 localhost ip6-localhost ip6-loopback
449 ff00::0 ip6-mcastprefix
451 ff02::2 ip6-allrouters
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}"
461 cwd=$(getcwd "${OCI_CONF_FILE}")
462 echo "lxc.init.cwd
= ${cwd}" >> "${LXC_CONF_FILE}"
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
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