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
26 # Make sure the usual locations are in PATH
27 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
29 # Check for required binaries
30 for bin
in skopeo umoci jq
; do
31 if ! type $bin >/dev
/null
2>&1; then
32 echo "ERROR: Missing required tool: $bin" 1>&2
37 LOCALSTATEDIR
="@LOCALSTATEDIR@"
38 LXC_TEMPLATE_CONFIG
="@LXCTEMPLATECONFIG@"
40 # Some useful functions
42 if [ -d "${DOWNLOAD_TEMP}" ]; then
43 rm -Rf "${DOWNLOAD_TEMP}"
45 if [ -d "${LXC_ROOTFS}.tmp" ]; then
46 rm -Rf "${LXC_ROOTFS}.tmp"
51 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
53 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
54 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
55 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
56 done < /proc
/self
/uid_map
58 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
59 { echo userns-root
; return; }
67 digest
=`cat "${basedir}/index.json" | jq -c -r --arg q "$q" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else empty end'`
68 if [ -z "${digest}" ]; then
69 echo "$q not found in index.json" >&2
73 # Ok we have the image config digest, now get the config from that,
75 cdigest
=`cat "${basedir}/blobs/sha256/${d}" | jq -c -r '.config.digest'`
76 if [ -z "${cdigest}" ]; then
77 echo "container config not found" >&2
82 echo "${basedir}/blobs/sha256/${d2}"
86 # get entrypoint from oci image. Use sh if unspecified
88 if [ "$#" -eq 0 ]; then
95 ep
=`cat "${configpath}" | jq -c -r '.config.Entrypoint[]?'`
96 cmd
=`cat "${configpath}" | jq -c -r '.config.Cmd[]?'`
97 if [ -z "${ep}" ]; then
99 if [ -z "${ep}" ]; then
102 elif [ -n "${cmd}" ]; then
110 # get environment from oci image.
112 if [ "$#" -eq 0 ]; then
118 env
=`cat "${configpath}" | jq -c -r '.config.Env[]'`
124 # FIXME 1: only support numerical values in the configuration file.
125 # FIXME 2: from the OCI image spec: "If group/gid is not specified,
126 # the default group and supplementary groups of the given user/uid in
127 # /etc/passwd from the container are applied."
129 if [ "$#" -eq 0 ]; then
136 uidgid
=`cat "${configpath}" | jq -c -r '.config.User // "0:0"'`
137 uidgid
=(${uidgid//:/ })
139 printf '%d %d' ${uidgid[0]:-0} ${uidgid[1]:-0} 2>/dev
/null || true
145 LXC container template for OCI images
148 [ -h | --help ]: Print this help message and exit.
151 [ -u | --url <url> ]: The OCI image URL
154 [ --username <username> ]: The username for the registry
155 [ --password <password> ]: The password for the registry
157 LXC internal arguments (do not pass manually!):
158 [ --name <name> ]: The container name
159 [ --path <path> ]: The path to the container
160 [ --rootfs <rootfs> ]: The path to the container's rootfs
161 [ --mapped-uid <map> ]: A uid map (user namespaces)
162 [ --mapped-gid <map> ]: A gid map (user namespaces)
168 options
=$
(getopt
-o u
:h
-l help,url
:,username
:,password
:,no-cache
,\
169 name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
171 if [ $?
-ne 0 ]; then
175 eval set -- "$options"
190 -h|
--help) usage
&& exit 1;;
191 -u|
--url) OCI_URL
=$2; shift 2;;
192 --username) OCI_USERNAME
=$2; shift 2;;
193 --password) OCI_PASSWORD
=$2; shift 2;;
194 --no-cache) OCI_USE_CACHE
="false"; shift 1;;
195 --name) LXC_NAME
=$2; shift 2;;
196 --path) LXC_PATH
=$2; shift 2;;
197 --rootfs) LXC_ROOTFS
=$2; shift 2;;
198 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
199 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
204 # Check that we have all variables we need
205 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
206 echo "ERROR: Not running through LXC." 1>&2
210 if [ -z "$OCI_URL" ]; then
211 echo "ERROR: no OCI URL given"
215 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
216 echo "ERROR: password given but no username specified"
220 if [ "${OCI_USE_CACHE}" = "true" ]; then
221 if ! skopeo copy
--help |
grep -q 'dest-shared-blob-dir'; then
222 echo "INFO: skopeo doesn't support blob caching"
223 OCI_USE_CACHE
="false"
229 if [ "$USERNS" = "yes" ]; then
230 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
231 echo "ERROR: In a user namespace without a map." 1>&2
236 if [ "${OCI_USE_CACHE}" = "true" ]; then
237 if [ "$USERNS" = "yes" ]; then
238 DOWNLOAD_BASE
="${HOME}/.cache/lxc"
240 DOWNLOAD_BASE
="${LOCALSTATEDIR}/cache/lxc"
246 # Trap all exit signals
247 trap cleanup EXIT HUP INT TERM
249 if ! type mktemp
>/dev
/null
2>&1; then
250 DOWNLOAD_TEMP
="${DOWNLOAD_BASE}/lxc-oci.$$"
251 mkdir
-p $DOWNLOAD_TEMP
253 DOWNLOAD_TEMP
=$
(mktemp
-d -p "${DOWNLOAD_BASE}")
258 if [ -n "$OCI_USERNAME" ]; then
259 CREDENTIALS
="${OCI_USERNAME}"
260 if [ -n "$OCI_PASSWORD" ]; then
261 CREDENTIALS
="${CREDENTIALS}:${OCI_PASSWORD}"
263 skopeo_args
+=(--src-creds "${CREDENTIALS}")
265 if [ "${OCI_USE_CACHE}" = "true" ]; then
266 skopeo_args
+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
267 skopeo copy
${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest
"
268 ln -s "${DOWNLOAD_BASE}/sha256
" "${DOWNLOAD_TEMP}/blobs
/sha256
"
270 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
273 echo "Unpacking the rootfs"
275 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
276 umoci_args
+=(--rootless)
278 umoci unpack
${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp
"
279 rmdir "${LXC_ROOTFS}"
280 mv "${LXC_ROOTFS}.tmp
/rootfs
" "${LXC_ROOTFS}"
282 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
283 LXC_CONF_FILE="${LXC_PATH}/config
"
284 entrypoint=$(getep ${OCI_CONF_FILE})
285 echo "lxc.execute.cmd
= '${entrypoint}'" >> "${LXC_CONF_FILE}"
286 echo "lxc.mount.auto
= proc
:mixed sys
:mixed cgroup
:mixed
" >> "${LXC_CONF_FILE}"
288 environment=$(getenv ${OCI_CONF_FILE})
289 while read -r line; do
290 echo "lxc.environment
= ${line}" >> "${LXC_CONF_FILE}"
291 done <<< "${environment}"
293 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf
" ]; then
294 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/common.conf
" >> "${LXC_CONF_FILE}"
297 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf
" ]; then
298 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/userns.conf
" >> "${LXC_CONF_FILE}"
301 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
303 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
308 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
310 127.0.1.1 ${LXC_NAME}
313 uidgid=($(getuidgid ${OCI_CONF_FILE}))
314 echo "lxc.init.uid
= ${uidgid[0]}" >> "${LXC_CONF_FILE}"
315 echo "lxc.init.gid
= ${uidgid[1]}" >> "${LXC_CONF_FILE}"
317 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
318 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
320 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
321 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true