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@"
39 LXC_HOOK_DIR
="@LXCHOOKDIR@"
41 # Some useful functions
43 if [ -d "${DOWNLOAD_TEMP}" ]; then
44 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; }
54 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
55 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
56 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
57 done < /proc
/self
/uid_map
59 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
60 { echo userns-root
; return; }
68 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'`
69 if [ -z "${digest}" ]; then
70 echo "$q not found in index.json" >&2
74 # Ok we have the image config digest, now get the config from that,
76 cdigest
=`cat "${basedir}/blobs/sha256/${d}" | jq -c -r '.config.digest'`
77 if [ -z "${cdigest}" ]; then
78 echo "container config not found" >&2
83 echo "${basedir}/blobs/sha256/${d2}"
87 # get entrypoint from oci image. Use sh if unspecified
89 if [ "$#" -eq 0 ]; then
96 ep
=`cat "${configpath}" | jq -c -r '.config.Entrypoint[]?'`
97 cmd
=`cat "${configpath}" | jq -c -r '.config.Cmd[]?'`
98 if [ -z "${ep}" ]; then
100 if [ -z "${ep}" ]; then
103 elif [ -n "${cmd}" ]; then
111 # get environment from oci image.
113 if [ "$#" -eq 0 ]; then
119 env
=`cat "${configpath}" | jq -c -r '.config.Env[]'`
125 # FIXME 1: only support numerical values in the configuration file.
126 # FIXME 2: from the OCI image spec: "If group/gid is not specified,
127 # the default group and supplementary groups of the given user/uid in
128 # /etc/passwd from the container are applied."
130 if [ "$#" -eq 0 ]; then
137 uidgid
=`cat "${configpath}" | jq -c -r '.config.User // "0:0"'`
138 uidgid
=(${uidgid//:/ })
140 printf '%d %d' ${uidgid[0]:-0} ${uidgid[1]:-0} 2>/dev
/null || true
144 # get cwd from oci image.
146 if [ "$#" -eq 0 ]; then
153 cwd
=`cat "${configpath}" | jq -c -r '.config.WorkingDir // "/"'`
161 LXC container template for OCI images
164 [ -h | --help ]: Print this help message and exit.
167 [ -u | --url <url> ]: The OCI image URL
170 [ --username <username> ]: The username for the registry
171 [ --password <password> ]: The password for the registry
173 LXC internal arguments (do not pass manually!):
174 [ --name <name> ]: The container name
175 [ --path <path> ]: The path to the container
176 [ --rootfs <rootfs> ]: The path to the container's rootfs
177 [ --mapped-uid <map> ]: A uid map (user namespaces)
178 [ --mapped-gid <map> ]: A gid map (user namespaces)
184 options
=$
(getopt
-o u
:h
-l help,url
:,username
:,password
:,no-cache
,dhcp
,\
185 name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
187 if [ $?
-ne 0 ]; then
191 eval set -- "$options"
207 -h|
--help) usage
&& exit 1;;
208 -u|
--url) OCI_URL
=$2; shift 2;;
209 --username) OCI_USERNAME
=$2; shift 2;;
210 --password) OCI_PASSWORD
=$2; shift 2;;
211 --no-cache) OCI_USE_CACHE
="false"; shift 1;;
212 --dhcp) OCI_USE_DHCP
="true"; shift 1;;
213 --name) LXC_NAME
=$2; shift 2;;
214 --path) LXC_PATH
=$2; shift 2;;
215 --rootfs) LXC_ROOTFS
=$2; shift 2;;
216 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
217 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
222 # Check that we have all variables we need
223 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
224 echo "ERROR: Not running through LXC." 1>&2
228 if [ -z "$OCI_URL" ]; then
229 echo "ERROR: no OCI URL given"
233 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
234 echo "ERROR: password given but no username specified"
238 if [ "${OCI_USE_CACHE}" = "true" ]; then
239 if ! skopeo copy
--help |
grep -q 'dest-shared-blob-dir'; then
240 echo "INFO: skopeo doesn't support blob caching"
241 OCI_USE_CACHE
="false"
247 if [ "$USERNS" = "yes" ]; then
248 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
249 echo "ERROR: In a user namespace without a map." 1>&2
254 if [ "${OCI_USE_CACHE}" = "true" ]; then
255 if [ "$USERNS" = "yes" ]; then
256 DOWNLOAD_BASE
="${HOME}/.cache/lxc"
258 DOWNLOAD_BASE
="${LOCALSTATEDIR}/cache/lxc"
264 # Trap all exit signals
265 trap cleanup EXIT HUP INT TERM
267 if ! type mktemp
>/dev
/null
2>&1; then
268 DOWNLOAD_TEMP
="${DOWNLOAD_BASE}/lxc-oci.$$"
269 mkdir
-p $DOWNLOAD_TEMP
271 DOWNLOAD_TEMP
=$
(mktemp
-d -p "${DOWNLOAD_BASE}")
276 if [ -n "$OCI_USERNAME" ]; then
277 CREDENTIALS
="${OCI_USERNAME}"
278 if [ -n "$OCI_PASSWORD" ]; then
279 CREDENTIALS
="${CREDENTIALS}:${OCI_PASSWORD}"
281 skopeo_args
+=(--src-creds "${CREDENTIALS}")
283 if [ "${OCI_USE_CACHE}" = "true" ]; then
284 skopeo_args
+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
285 skopeo copy
${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest
"
286 ln -s "${DOWNLOAD_BASE}/sha256
" "${DOWNLOAD_TEMP}/blobs
/sha256
"
288 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
291 echo "Unpacking the rootfs"
293 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
294 umoci_args
+=(--rootless)
296 umoci unpack
${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp
"
297 rmdir "${LXC_ROOTFS}"
298 mv "${LXC_ROOTFS}.tmp
/rootfs
" "${LXC_ROOTFS}"
300 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
301 LXC_CONF_FILE="${LXC_PATH}/config
"
302 entrypoint=$(getep ${OCI_CONF_FILE})
303 echo "lxc.execute.cmd
= '${entrypoint}'" >> "${LXC_CONF_FILE}"
304 echo "lxc.mount.auto
= proc
:mixed sys
:mixed cgroup
:mixed
" >> "${LXC_CONF_FILE}"
306 environment=$(getenv ${OCI_CONF_FILE})
307 while read -r line; do
308 echo "lxc.environment
= ${line}" >> "${LXC_CONF_FILE}"
309 done <<< "${environment}"
311 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf
" ]; then
312 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/common.conf
" >> "${LXC_CONF_FILE}"
315 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf
" ]; then
316 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/userns.conf
" >> "${LXC_CONF_FILE}"
319 if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf
" ]; then
320 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/oci.common.conf
" >> "${LXC_CONF_FILE}"
323 if [ "${OCI_USE_DHCP}" = "true
" ]; then
324 echo "lxc.hook.start-host
= ${LXC_HOOK_DIR}/dhclient
" >> "${LXC_CONF_FILE}"
325 echo "lxc.hook.stop
= ${LXC_HOOK_DIR}/dhclient
" >> "${LXC_CONF_FILE}"
328 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
330 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
335 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
337 127.0.1.1 ${LXC_NAME}
338 ::1 localhost ip6-localhost ip6-loopback
340 ff00::0 ip6-mcastprefix
342 ff02::2 ip6-allrouters
345 uidgid=($(getuidgid ${OCI_CONF_FILE}))
346 echo "lxc.init.uid
= ${uidgid[0]}" >> "${LXC_CONF_FILE}"
347 echo "lxc.init.gid
= ${uidgid[1]}" >> "${LXC_CONF_FILE}"
349 cwd=$(getcwd ${OCI_CONF_FILE})
350 echo "lxc.init.cwd
= ${cwd}" >> "${LXC_CONF_FILE}"
352 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
353 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
355 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
356 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true