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 # Some useful functions
39 if [ -d "$DOWNLOAD_TEMP" ]; then
45 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
47 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
48 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
49 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
50 done < /proc
/self
/uid_map
52 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
53 { echo userns-root
; return; }
61 digest
=`cat "${basedir}/index.json" | jq --arg q "$q" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else null end' | sed -e 's/"//g'`
62 if [ -z "${digest}" ]; then
63 echo "$q not found in index.json" >&2
67 # Ok we have the image config digest, now get the config from that,
69 cdigest
=`cat "${basedir}/blobs/sha256/${d}" | jq '.config.digest' | sed -e 's/"//g'`
70 if [ -z "${cdigest}" ]; then
71 echo "container config not found" >&2
76 echo "${basedir}/blobs/sha256/${d2}"
80 # get entrypoint from oci image. Use sh if unspecified
82 if [ "$#" -eq 0 ]; then
89 ep
=`cat "${configpath}" | jq -c '.config.Entrypoint' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
90 cmd
=`cat "${configpath}" | jq -c '.config.Cmd' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
91 if [ "${ep}" = "null" ]; then
93 if [ "${ep}" = "null" ]; then
96 elif [ "${cmd}" != "null" ]; then
100 if [ -z "${ep}" ]; then
108 # get environment from oci image.
110 if [ "$#" -eq 0 ]; then
116 cat "${configpath}" > /tmp
/config
117 env
=`cat "${configpath}" | jq -c '.config.Env[]'`
125 LXC container template for OCI images
128 [ -h | --help ]: Print this help message and exit.
131 [ -u | --url <url> ]: The OCI image URL
133 LXC internal arguments (do not pass manually!):
134 [ --name <name> ]: The container name
135 [ --path <path> ]: The path to the container
136 [ --rootfs <rootfs> ]: The path to the container's rootfs
137 [ --mapped-uid <map> ]: A uid map (user namespaces)
138 [ --mapped-gid <map> ]: A gid map (user namespaces)
144 options
=$
(getopt
-o u
:h
-l help,url
:,name
:,path
:,\
145 rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
147 if [ $?
-ne 0 ]; then
151 eval set -- "$options"
162 -h|
--help) usage
&& exit 1;;
163 -u|
--url) OCI_URL
=$2; shift 2;;
164 --name) LXC_NAME
=$2; shift 2;;
165 --path) LXC_PATH
=$2; shift 2;;
166 --rootfs) LXC_ROOTFS
=$2; shift 2;;
167 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
168 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
173 # Check that we have all variables we need
174 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
175 echo "ERROR: Not running through LXC." 1>&2
179 if [ -z "$OCI_URL" ]; then
180 echo "ERROR: no OCI URL given"
186 if [ "$USERNS" != "no" ]; then
187 if [ "$USERNS" = "yes" ]; then
188 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
189 echo "ERROR: In a user namespace without a map." 1>&2
193 DOWNLOAD_TARGET
="user"
196 DOWNLOAD_TARGET
="system"
200 # Trap all exit signals
201 trap cleanup EXIT HUP INT TERM
203 if ! type mktemp
>/dev
/null
2>&1; then
204 DOWNLOAD_TEMP
=/tmp
/lxc-oci.$$
205 mkdir
-p $DOWNLOAD_TEMP
207 DOWNLOAD_TEMP
=$
(mktemp
-d)
210 # Download the image - TODO - cache
211 skopeo copy
"${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
214 echo "Unpacking the rootfs"
217 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
218 umoci_args
+=(--rootless)
220 umoci unpack
${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp
"
221 rmdir "${LXC_ROOTFS}"
222 mv "${LXC_ROOTFS}.tmp
/rootfs
" "${LXC_ROOTFS}"
223 rm -rf "${LXC_ROOTFS}.tmp
"
225 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
226 LXC_CONF_FILE="${LXC_PATH}/config
"
227 entrypoint=$(getep ${OCI_CONF_FILE})
228 echo "lxc.execute.cmd
= '${entrypoint}'" >> "${LXC_CONF_FILE}"
229 echo "lxc.mount.auto
= proc
:mixed sys
:mixed cgroup
:mixed
" >> "${LXC_CONF_FILE}"
231 environment=$(getenv ${OCI_CONF_FILE})
232 while read -r line; do
233 echo "lxc.environment
= ${line}" >> "${LXC_CONF_FILE}"
234 done <<< "${environment}"
236 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
238 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
243 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
245 127.0.1.1 ${LXC_NAME}
248 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
249 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
251 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
252 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true