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; }
57 # get entrypoint from oci image. Use sh if unspecified
58 # TODO - we can get other things like resource limits here
64 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'`
65 if [ -z "${digest}" ]; then
66 echo "$q not found in index.json" >&2
71 # Ok we have the image config digest, now get the config from that,
73 cdigest
=`cat "${basedir}/blobs/sha256/${d}" | jq '.config.digest' | sed -e 's/"//g'`
74 if [ -z "${cdigest}" ]; then
75 echo "container config not found" >&2
81 ep
=`cat "${basedir}/blobs/sha256/${d2}" | jq -c '.config.Entrypoint' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
82 cmd
=`cat "${basedir}/blobs/sha256/${d2}" | jq -c '.config.Cmd' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
83 if [ "${ep}" = "null" ]; then
85 if [ "${ep}" = "null" ]; then
88 elif [ "${cmd}" != "null" ]; then
92 if [ -z "${ep}" ]; then
102 LXC container template for OCI images
105 [ -h | --help ]: Print this help message and exit.
108 [ -u | --url <url> ]: The OCI image URL
110 LXC internal arguments (do not pass manually!):
111 [ --name <name> ]: The container name
112 [ --path <path> ]: The path to the container
113 [ --rootfs <rootfs> ]: The path to the container's rootfs
114 [ --mapped-uid <map> ]: A uid map (user namespaces)
115 [ --mapped-gid <map> ]: A gid map (user namespaces)
121 options
=$
(getopt
-o u
:h
-l help,name
:,path
:,\
122 rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
124 if [ $?
-ne 0 ]; then
128 eval set -- "$options"
139 -h|
--help) usage
&& exit 1;;
140 -u|
--url) OCI_URL
=$2; shift 2;;
141 --name) LXC_NAME
=$2; shift 2;;
142 --path) LXC_PATH
=$2; shift 2;;
143 --rootfs) LXC_ROOTFS
=$2; shift 2;;
144 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
145 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
150 # Check that we have all variables we need
151 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
152 echo "ERROR: Not running through LXC." 1>&2
156 if [ -z "$OCI_URL" ]; then
157 echo "ERROR: no OCI URL given"
163 if [ "$USERNS" != "no" ]; then
164 if [ "$USERNS" = "yes" ]; then
165 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
166 echo "ERROR: In a user namespace without a map." 1>&2
170 DOWNLOAD_TARGET
="user"
173 DOWNLOAD_TARGET
="system"
177 # Trap all exit signals
178 trap cleanup EXIT HUP INT TERM
180 if ! type mktemp
>/dev
/null
2>&1; then
181 DOWNLOAD_TEMP
=/tmp
/lxc-oci.$$
182 mkdir
-p $DOWNLOAD_TEMP
184 DOWNLOAD_TEMP
=$
(mktemp
-d)
187 # Download the image - TODO - cache
188 skopeo copy
"${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
191 echo "Unpacking the rootfs"
193 umoci unpack
--image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
194 rmdir "${LXC_ROOTFS}"
195 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
196 entrypoint
=$
(getep
${DOWNLOAD_TEMP} latest
)
197 rm -rf "${LXC_ROOTFS}.tmp"
199 LXC_CONF_FILE
="${LXC_PATH}/config"
200 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
201 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
203 echo "lxc.uts.name = ${LXC_NAME}" >> ${LXC_PATH}/config
205 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
206 chown
$LXC_MAPPED_UID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
208 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
209 chgrp
$LXC_MAPPED_GID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true