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 LXC_TEMPLATE_CONFIG
="@LXCTEMPLATECONFIG@"
39 # Some useful functions
41 if [ -d "$DOWNLOAD_TEMP" ]; then
47 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
49 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
50 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
51 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
52 done < /proc
/self
/uid_map
54 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
55 { echo userns-root
; return; }
63 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'`
64 if [ -z "${digest}" ]; then
65 echo "$q not found in index.json" >&2
69 # Ok we have the image config digest, now get the config from that,
71 cdigest
=`cat "${basedir}/blobs/sha256/${d}" | jq '.config.digest' | sed -e 's/"//g'`
72 if [ -z "${cdigest}" ]; then
73 echo "container config not found" >&2
78 echo "${basedir}/blobs/sha256/${d2}"
82 # get entrypoint from oci image. Use sh if unspecified
84 if [ "$#" -eq 0 ]; then
91 ep
=`cat "${configpath}" | jq -c '.config.Entrypoint' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
92 cmd
=`cat "${configpath}" | jq -c '.config.Cmd' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
93 if [ "${ep}" = "null" ]; then
95 if [ "${ep}" = "null" ]; then
98 elif [ "${cmd}" != "null" ]; then
102 if [ -z "${ep}" ]; then
110 # get environment from oci image.
112 if [ "$#" -eq 0 ]; then
118 cat "${configpath}" > /tmp
/config
119 env
=`cat "${configpath}" | jq -c '.config.Env[]'`
127 LXC container template for OCI images
130 [ -h | --help ]: Print this help message and exit.
133 [ -u | --url <url> ]: The OCI image URL
135 LXC internal arguments (do not pass manually!):
136 [ --name <name> ]: The container name
137 [ --path <path> ]: The path to the container
138 [ --rootfs <rootfs> ]: The path to the container's rootfs
139 [ --mapped-uid <map> ]: A uid map (user namespaces)
140 [ --mapped-gid <map> ]: A gid map (user namespaces)
146 options
=$
(getopt
-o u
:h
-l help,url
:,name
:,path
:,\
147 rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
149 if [ $?
-ne 0 ]; then
153 eval set -- "$options"
164 -h|
--help) usage
&& exit 1;;
165 -u|
--url) OCI_URL
=$2; shift 2;;
166 --name) LXC_NAME
=$2; shift 2;;
167 --path) LXC_PATH
=$2; shift 2;;
168 --rootfs) LXC_ROOTFS
=$2; shift 2;;
169 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
170 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
175 # Check that we have all variables we need
176 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
177 echo "ERROR: Not running through LXC." 1>&2
181 if [ -z "$OCI_URL" ]; then
182 echo "ERROR: no OCI URL given"
188 if [ "$USERNS" != "no" ]; then
189 if [ "$USERNS" = "yes" ]; then
190 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
191 echo "ERROR: In a user namespace without a map." 1>&2
195 DOWNLOAD_TARGET
="user"
198 DOWNLOAD_TARGET
="system"
202 # Trap all exit signals
203 trap cleanup EXIT HUP INT TERM
205 if ! type mktemp
>/dev
/null
2>&1; then
206 DOWNLOAD_TEMP
=/tmp
/lxc-oci.$$
207 mkdir
-p $DOWNLOAD_TEMP
209 DOWNLOAD_TEMP
=$
(mktemp
-d)
212 # Download the image - TODO - cache
213 skopeo copy
"${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
216 echo "Unpacking the rootfs"
219 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
220 umoci_args
+=(--rootless)
222 umoci unpack
${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp
"
223 rmdir "${LXC_ROOTFS}"
224 mv "${LXC_ROOTFS}.tmp
/rootfs
" "${LXC_ROOTFS}"
225 rm -rf "${LXC_ROOTFS}.tmp
"
227 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
228 LXC_CONF_FILE="${LXC_PATH}/config
"
229 entrypoint=$(getep ${OCI_CONF_FILE})
230 echo "lxc.execute.cmd
= '${entrypoint}'" >> "${LXC_CONF_FILE}"
231 echo "lxc.mount.auto
= proc
:mixed sys
:mixed cgroup
:mixed
" >> "${LXC_CONF_FILE}"
233 environment=$(getenv ${OCI_CONF_FILE})
234 while read -r line; do
235 echo "lxc.environment
= ${line}" >> "${LXC_CONF_FILE}"
236 done <<< "${environment}"
238 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf
" ]; then
239 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/common.conf
" >> "${LXC_CONF_FILE}"
242 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf
" ]; then
243 echo "lxc.include
= ${LXC_TEMPLATE_CONFIG}/userns.conf
" >> "${LXC_CONF_FILE}"
246 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
248 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
253 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
255 127.0.1.1 ${LXC_NAME}
258 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
259 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
261 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
262 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true