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 -r '.config.Entrypoint[]?'< "${configpath}")
104 cmd
=$
(jq
-c -r '.config.Cmd[]?' < "${configpath}")
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 # FIXME 1: only support numerical values in the configuration file.
133 # FIXME 2: from the OCI image spec: "If group/gid is not specified,
134 # the default group and supplementary groups of the given user/uid in
135 # /etc/passwd from the container are applied."
137 if [ "$#" -eq 0 ]; then
144 uidgid
=$
(jq
-c -r '.config.User // "0:0"' < "${configpath}")
145 # shellcheck disable=SC2039
146 uidgid
=(${uidgid//:/ })
148 printf '%d %d' "${uidgid[0]:-0}" "${uidgid[1]:-0}" 2>/dev
/null || true
152 # get cwd from oci image.
154 if [ "$#" -eq 0 ]; then
161 cwd
=$
(jq
-c -r '.config.WorkingDir // "/"' < "${configpath}")
169 LXC container template for OCI images
172 [ -h | --help ]: Print this help message and exit.
175 [ -u | --url <url> ]: The OCI image URL
178 [ --username <username> ]: The username for the registry
179 [ --password <password> ]: The password for the registry
181 LXC internal arguments (do not pass manually!):
182 [ --name <name> ]: The container name
183 [ --path <path> ]: The path to the container
184 [ --rootfs <rootfs> ]: The path to the container's rootfs
185 [ --mapped-uid <map> ]: A uid map (user namespaces)
186 [ --mapped-gid <map> ]: A gid map (user namespaces)
191 if ! options
=$
(getopt
-o u
:h
-l help,url
:,username
:,password
:,no-cache
,dhcp
,name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@"); then
195 eval set -- "$options"
211 -h|
--help) usage
&& exit 1;;
212 -u|
--url) OCI_URL
=$2; shift 2;;
213 --username) OCI_USERNAME
=$2; shift 2;;
214 --password) OCI_PASSWORD
=$2; shift 2;;
215 --no-cache) OCI_USE_CACHE
="false"; shift 1;;
216 --dhcp) OCI_USE_DHCP
="true"; shift 1;;
217 --name) LXC_NAME
=$2; shift 2;;
218 --path) LXC_PATH
=$2; shift 2;;
219 --rootfs) LXC_ROOTFS
=$2; shift 2;;
220 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
221 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
226 # Check that we have all variables we need
227 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
228 echo "ERROR: Not running through LXC" 1>&2
232 if [ -z "$OCI_URL" ]; then
233 echo "ERROR: no OCI URL given"
237 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
238 echo "ERROR: password given but no username specified"
242 if [ "${OCI_USE_CACHE}" = "true" ]; then
243 if ! skopeo copy
--help |
grep -q 'dest-shared-blob-dir'; then
244 echo "INFO: skopeo doesn't support blob caching"
245 OCI_USE_CACHE
="false"
251 if [ "$USERNS" = "yes" ]; then
252 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
253 echo "ERROR: In a user namespace without a map" 1>&2
258 if [ "${OCI_USE_CACHE}" = "true" ]; then
259 if [ "$USERNS" = "yes" ]; then
260 DOWNLOAD_BASE
="${HOME}/.cache/lxc"
262 DOWNLOAD_BASE
="${LOCALSTATEDIR}/cache/lxc"
268 # Trap all exit signals
269 trap cleanup EXIT HUP INT TERM
271 if ! which mktemp
>/dev
/null
2>&1; then
272 DOWNLOAD_TEMP
="${DOWNLOAD_BASE}/lxc-oci.$$"
273 mkdir
-p "${DOWNLOAD_TEMP}"
275 DOWNLOAD_TEMP
=$
(mktemp
-d -p "${DOWNLOAD_BASE}")
279 # shellcheck disable=SC2039
281 if [ -n "$OCI_USERNAME" ]; then
282 CREDENTIALS
="${OCI_USERNAME}"
284 if [ -n "$OCI_PASSWORD" ]; then
285 CREDENTIALS
="${CREDENTIALS}:${OCI_PASSWORD}"
288 # shellcheck disable=SC2039
289 skopeo_args
+=(--src-creds "${CREDENTIALS}")
292 if [ "${OCI_USE_CACHE}" = "true" ]; then
293 # shellcheck disable=SC2039
294 # shellcheck disable=SC2068
295 skopeo_args
+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
296 # shellcheck disable=SC2039
297 # shellcheck disable=SC2068
298 skopeo copy
${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest
"
299 ln -s "${DOWNLOAD_BASE}/sha256
" "${DOWNLOAD_TEMP}/blobs
/sha256
"
301 # shellcheck disable=SC2039
302 # shellcheck disable=SC2068
303 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
306 echo "Unpacking the rootfs"
307 # shellcheck disable=SC2039
309 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
310 # shellcheck disable=SC2039
311 umoci_args
+=(--rootless)
313 # shellcheck disable=SC2039
314 # shellcheck disable=SC2068
315 umoci unpack
${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp
"
316 rmdir "${LXC_ROOTFS}"
317 mv "${LXC_ROOTFS}.tmp
/rootfs
" "${LXC_ROOTFS}"
319 OCI_CONF_FILE=$(getconfigpath "${DOWNLOAD_TEMP}" latest)
320 LXC_CONF_FILE="${LXC_PATH}/config
"
321 entrypoint=$(getep "${OCI_CONF_FILE}")
322 echo "lxc.execute.cmd
= '${entrypoint}'" >> "${LXC_CONF_FILE}"
323 echo "lxc.mount.auto
= proc
:mixed sys
:mixed cgroup
:mixed
" >> "${LXC_CONF_FILE}"
325 environment=$(getenv "${OCI_CONF_FILE}")
326 # shellcheck disable=SC2039
327 while read -r line; do
328 echo "lxc.environment
= ${line}" >> "${LXC_CONF_FILE}"
329 done <<< "${environment}"
331 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf
" ]; then
332 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/common.conf
" >> "${LXC_CONF_FILE}"
335 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf
" ]; then
336 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/userns.conf
" >> "${LXC_CONF_FILE}"
339 if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf
" ]; then
340 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/oci.common.conf
" >> "${LXC_CONF_FILE}"
343 if [ "${OCI_USE_DHCP}" = "true
" ]; then
344 echo "lxc.hook.start-host
= ${LXC_HOOK_DIR}/dhclient
" >> "${LXC_CONF_FILE}"
345 echo "lxc.hook.stop
= ${LXC_HOOK_DIR}/dhclient
" >> "${LXC_CONF_FILE}"
348 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
350 cat <<EOF > "${LXC_ROOTFS}/etc
/hostname
"
355 cat <<EOF > "${LXC_ROOTFS}/etc
/hosts
"
357 127.0.1.1 ${LXC_NAME}
358 ::1 localhost ip6-localhost ip6-loopback
360 ff00::0 ip6-mcastprefix
362 ff02::2 ip6-allrouters
365 # shellcheck disable=SC2039
366 uidgid=($(getuidgid "${OCI_CONF_FILE}"))
367 # shellcheck disable=SC2039
368 echo "lxc.init.uid
= ${uidgid[0]}" >> "${LXC_CONF_FILE}"
369 # shellcheck disable=SC2039
370 echo "lxc.init.gid
= ${uidgid[1]}" >> "${LXC_CONF_FILE}"
372 cwd=$(getcwd "${OCI_CONF_FILE}")
373 echo "lxc.init.cwd
= ${cwd}" >> "${LXC_CONF_FILE}"
375 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
376 chown "$LXC_MAPPED_UID" "$LXC_PATH/config
" "$LXC_PATH/fstab
" > /dev/null 2>&1 || true
378 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
379 chgrp "$LXC_MAPPED_GID" "$LXC_PATH/config
" "$LXC_PATH/fstab
" > /dev/null 2>&1 || true