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
110 LXC container template for OCI images
113 [ -h | --help ]: Print this help message and exit.
116 [ -u | --url <url> ]: The OCI image URL
118 LXC internal arguments (do not pass manually!):
119 [ --name <name> ]: The container name
120 [ --path <path> ]: The path to the container
121 [ --rootfs <rootfs> ]: The path to the container's rootfs
122 [ --mapped-uid <map> ]: A uid map (user namespaces)
123 [ --mapped-gid <map> ]: A gid map (user namespaces)
129 options
=$
(getopt
-o u
:h
-l help,url
:,name
:,path
:,\
130 rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
132 if [ $?
-ne 0 ]; then
136 eval set -- "$options"
147 -h|
--help) usage
&& exit 1;;
148 -u|
--url) OCI_URL
=$2; shift 2;;
149 --name) LXC_NAME
=$2; shift 2;;
150 --path) LXC_PATH
=$2; shift 2;;
151 --rootfs) LXC_ROOTFS
=$2; shift 2;;
152 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
153 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
158 # Check that we have all variables we need
159 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
160 echo "ERROR: Not running through LXC." 1>&2
164 if [ -z "$OCI_URL" ]; then
165 echo "ERROR: no OCI URL given"
171 if [ "$USERNS" != "no" ]; then
172 if [ "$USERNS" = "yes" ]; then
173 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
174 echo "ERROR: In a user namespace without a map." 1>&2
178 DOWNLOAD_TARGET
="user"
181 DOWNLOAD_TARGET
="system"
185 # Trap all exit signals
186 trap cleanup EXIT HUP INT TERM
188 if ! type mktemp
>/dev
/null
2>&1; then
189 DOWNLOAD_TEMP
=/tmp
/lxc-oci.$$
190 mkdir
-p $DOWNLOAD_TEMP
192 DOWNLOAD_TEMP
=$
(mktemp
-d)
195 # Download the image - TODO - cache
196 skopeo copy
"${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
199 echo "Unpacking the rootfs"
202 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
203 umoci_args
+=(--rootless)
205 umoci unpack
${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp
"
206 rmdir "${LXC_ROOTFS}"
207 mv "${LXC_ROOTFS}.tmp
/rootfs
" "${LXC_ROOTFS}"
208 rm -rf "${LXC_ROOTFS}.tmp
"
210 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
211 LXC_CONF_FILE="${LXC_PATH}/config
"
212 entrypoint=$(getep ${OCI_CONF_FILE})
213 echo "lxc.execute.cmd
= '${entrypoint}'" >> "${LXC_CONF_FILE}"
214 echo "lxc.mount.auto
= proc
:mixed sys
:mixed cgroup
:mixed
" >> "${LXC_CONF_FILE}"
216 echo "lxc.uts.name
= ${LXC_NAME}" >> "${LXC_CONF_FILE}"
218 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
223 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
225 127.0.1.1 ${LXC_NAME}
228 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
229 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
231 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
232 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true