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 ! which $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@"
40 # Some useful functions
42 if [ -d "${DOWNLOAD_TEMP}" ]; then
43 rm -Rf "${DOWNLOAD_TEMP}"
46 if [ -d "${LXC_ROOTFS}.tmp" ]; then
47 rm -Rf "${LXC_ROOTFS}.tmp"
52 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
53 while read -r line
; do
54 fields
="$(echo "$line" | awk '{ print $1 " " $2 " " $3 }')"
55 if [ "${fields}" = "0 0 4294967295" ]; then
59 if echo "${fields}" |
grep -q " 0 1$"; then
63 done < /proc
/self
/uid_map
65 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && { echo userns-root
; return; }
73 digest
=$
(jq
-c -r --arg q
"$q" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else empty end' < "${basedir}/index.json")
74 if [ -z "${digest}" ]; then
75 echo "$q not found in index.json" >&2
79 # Ok we have the image config digest, now get the config from that
80 # shellcheck disable=SC2039
82 cdigest
=$
(jq
-c -r '.config.digest' < "${basedir}/blobs/sha256/${d}")
83 if [ -z "${cdigest}" ]; then
84 echo "container config not found" >&2
88 # shellcheck disable=SC2039
90 echo "${basedir}/blobs/sha256/${d2}"
94 # Get entrypoint from oci image. Use sh if unspecified
96 if [ "$#" -eq 0 ]; then
103 ep
=$
(jq
-c '.config.Entrypoint[]?'< "${configpath}" |
tr '\n' ' ')
104 cmd
=$
(jq
-c '.config.Cmd[]?'< "${configpath}" |
tr '\n' ' ')
105 if [ -z "${ep}" ]; then
107 if [ -z "${ep}" ]; then
110 elif [ -n "${cmd}" ]; then
118 # get environment from oci image.
120 if [ "$#" -eq 0 ]; then
126 env
=$
(jq
-c -r '.config.Env[]'< "${configpath}")
132 # check var is decimal.
135 if [ "${var}" -eq "${var}" ] 2> /dev
/null
; then
142 # get uid, gid from oci image.
146 passwdpath
="${rootpath}/etc/passwd"
147 grouppath
="${rootpath}/etc/group"
149 usergroup
=$
(jq
-c -r '.config.User' < "${configpath}")
150 # shellcheck disable=SC2039
151 usergroup
=(${usergroup//:/ })
153 user
=${usergroup[0]:-0}
154 if ! isdecimal
"${user}"; then
155 if [ -f ${passwdpath} ]; then
156 user
=$
(grep "^${user}:" "${passwdpath}" | awk -F: '{print $3}')
162 group=${usergroup[1]:-}
163 if [ -z "${group}" ]; then
164 if [ -f "${passwdpath}" ]; then
165 group=$(grep "^
[^
:]*:[^
:]*:${user}:" "${passwdpath}" | awk -F: '{print $4}')
169 elif ! isdecimal "${group}"; then
170 if [ -f "${grouppath}" ]; then
171 group=$(grep "^${group}:" "${grouppath}" | awk -F: '{print $3}')
177 echo "${user:-0} ${group:-0}"
181 # get cwd from oci image.
183 if [ "$#" -eq 0 ]; then
190 cwd
=$
(jq
-c -r '.config.WorkingDir // "/"' < "${configpath}")
198 LXC container template for OCI images
201 [ -h | --help ]: Print this help message and exit.
204 [ -u | --url <url> ]: The OCI image URL
207 [ --username <username> ]: The username for the registry
208 [ --password <password> ]: The password for the registry
210 LXC internal arguments (do not pass manually!):
211 [ --name <name> ]: The container name
212 [ --path <path> ]: The path to the container
213 [ --rootfs <rootfs> ]: The path to the container's rootfs
214 [ --mapped-uid <map> ]: A uid map (user namespaces)
215 [ --mapped-gid <map> ]: A gid map (user namespaces)
220 if ! options
=$
(getopt
-o u
:h
-l help,url
:,username
:,password
:,no-cache
,dhcp
,name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@"); then
224 eval set -- "$options"
240 -h|
--help) usage
&& exit 1;;
241 -u|
--url) OCI_URL
=$2; shift 2;;
242 --username) OCI_USERNAME
=$2; shift 2;;
243 --password) OCI_PASSWORD
=$2; shift 2;;
244 --no-cache) OCI_USE_CACHE
="false"; shift 1;;
245 --dhcp) OCI_USE_DHCP
="true"; shift 1;;
246 --name) LXC_NAME
=$2; shift 2;;
247 --path) LXC_PATH
=$2; shift 2;;
248 --rootfs) LXC_ROOTFS
=$2; shift 2;;
249 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
250 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
255 # Check that we have all variables we need
256 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
257 echo "ERROR: Not running through LXC" 1>&2
261 if [ -z "$OCI_URL" ]; then
262 echo "ERROR: no OCI URL given"
266 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
267 echo "ERROR: password given but no username specified"
271 if [ "${OCI_USE_CACHE}" = "true" ]; then
272 if ! skopeo copy
--help |
grep -q 'dest-shared-blob-dir'; then
273 echo "INFO: skopeo doesn't support blob caching"
274 OCI_USE_CACHE
="false"
280 if [ "$USERNS" = "yes" ]; then
281 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
282 echo "ERROR: In a user namespace without a map" 1>&2
287 if [ "${OCI_USE_CACHE}" = "true" ]; then
288 if [ "$USERNS" = "yes" ]; then
289 DOWNLOAD_BASE
="${HOME}/.cache/lxc"
291 DOWNLOAD_BASE
="${LOCALSTATEDIR}/cache/lxc"
296 mkdir
-p "${DOWNLOAD_BASE}"
298 # Trap all exit signals
299 trap cleanup EXIT HUP INT TERM
301 if ! which mktemp
>/dev
/null
2>&1; then
302 DOWNLOAD_TEMP
="${DOWNLOAD_BASE}/lxc-oci.$$"
303 mkdir
-p "${DOWNLOAD_TEMP}"
305 DOWNLOAD_TEMP
=$
(mktemp
-d -p "${DOWNLOAD_BASE}")
309 # shellcheck disable=SC2039
311 if [ -n "$OCI_USERNAME" ]; then
312 CREDENTIALS
="${OCI_USERNAME}"
314 if [ -n "$OCI_PASSWORD" ]; then
315 CREDENTIALS
="${CREDENTIALS}:${OCI_PASSWORD}"
318 # shellcheck disable=SC2039
319 skopeo_args
+=(--src-creds "${CREDENTIALS}")
322 if [ "${OCI_USE_CACHE}" = "true" ]; then
323 # shellcheck disable=SC2039
324 # shellcheck disable=SC2068
325 skopeo_args
+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
326 # shellcheck disable=SC2039
327 # shellcheck disable=SC2068
328 skopeo copy
${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest
"
329 ln -s "${DOWNLOAD_BASE}/sha256
" "${DOWNLOAD_TEMP}/blobs
/sha256
"
331 # shellcheck disable=SC2039
332 # shellcheck disable=SC2068
333 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
336 echo "Unpacking the rootfs"
337 # shellcheck disable=SC2039
339 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
340 # shellcheck disable=SC2039
341 umoci_args
+=(--rootless)
343 # shellcheck disable=SC2039
344 # shellcheck disable=SC2068
345 umoci
--log=error unpack
${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp
"
346 rmdir "${LXC_ROOTFS}"
347 mv "${LXC_ROOTFS}.tmp
/rootfs
" "${LXC_ROOTFS}"
349 OCI_CONF_FILE=$(getconfigpath "${DOWNLOAD_TEMP}" latest)
350 LXC_CONF_FILE="${LXC_PATH}/config
"
351 entrypoint=$(getep "${OCI_CONF_FILE}")
352 echo "lxc.execute.cmd
= '${entrypoint}'" >> "${LXC_CONF_FILE}"
353 echo "lxc.mount.auto
= proc
:mixed sys
:mixed cgroup
:mixed
" >> "${LXC_CONF_FILE}"
355 environment=$(getenv "${OCI_CONF_FILE}")
356 # shellcheck disable=SC2039
357 while read -r line; do
358 echo "lxc.environment
= ${line}" >> "${LXC_CONF_FILE}"
359 done <<< "${environment}"
361 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf
" ]; then
362 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/common.conf
" >> "${LXC_CONF_FILE}"
365 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf
" ]; then
366 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/userns.conf
" >> "${LXC_CONF_FILE}"
369 if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf
" ]; then
370 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/oci.common.conf
" >> "${LXC_CONF_FILE}"
373 if [ "${OCI_USE_DHCP}" = "true
" ]; then
374 echo "lxc.hook.start-host
= ${LXC_HOOK_DIR}/dhclient
" >> "${LXC_CONF_FILE}"
375 echo "lxc.hook.stop
= ${LXC_HOOK_DIR}/dhclient
" >> "${LXC_CONF_FILE}"
378 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
380 cat <<EOF > "${LXC_ROOTFS}/etc
/hostname
"
385 cat <<EOF > "${LXC_ROOTFS}/etc
/hosts
"
387 127.0.1.1 ${LXC_NAME}
388 ::1 localhost ip6-localhost ip6-loopback
390 ff00::0 ip6-mcastprefix
392 ff02::2 ip6-allrouters
395 # shellcheck disable=SC2039
396 uidgid=($(getuidgid "${OCI_CONF_FILE}" "${LXC_ROOTFS}" ))
397 # shellcheck disable=SC2039
398 echo "lxc.init.uid
= ${uidgid[0]}" >> "${LXC_CONF_FILE}"
399 # shellcheck disable=SC2039
400 echo "lxc.init.gid
= ${uidgid[1]}" >> "${LXC_CONF_FILE}"
402 cwd=$(getcwd "${OCI_CONF_FILE}")
403 echo "lxc.init.cwd
= ${cwd}" >> "${LXC_CONF_FILE}"
405 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
406 chown "$LXC_MAPPED_UID" "$LXC_PATH/config
" "$LXC_PATH/fstab
" > /dev/null 2>&1 || true
408 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
409 chgrp "$LXC_MAPPED_GID" "$LXC_PATH/config
" "$LXC_PATH/fstab
" > /dev/null 2>&1 || true