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
42 rm -Rf "${DOWNLOAD_TEMP}"
44 if [ -d "${LXC_ROOTFS}.tmp" ]; then
45 rm -Rf "${LXC_ROOTFS}.tmp"
50 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
52 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
53 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
54 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
55 done < /proc
/self
/uid_map
57 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
58 { echo userns-root
; return; }
66 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'`
67 if [ -z "${digest}" ]; then
68 echo "$q not found in index.json" >&2
72 # Ok we have the image config digest, now get the config from that,
74 cdigest
=`cat "${basedir}/blobs/sha256/${d}" | jq -c -r '.config.digest'`
75 if [ -z "${cdigest}" ]; then
76 echo "container config not found" >&2
81 echo "${basedir}/blobs/sha256/${d2}"
85 # get entrypoint from oci image. Use sh if unspecified
87 if [ "$#" -eq 0 ]; then
94 ep
=`cat "${configpath}" | jq -c -r '.config.Entrypoint[]?'`
95 cmd
=`cat "${configpath}" | jq -c -r '.config.Cmd[]?'`
96 if [ -z "${ep}" ]; then
98 if [ -z "${ep}" ]; then
101 elif [ -n "${cmd}" ]; then
109 # get environment from oci image.
111 if [ "$#" -eq 0 ]; then
117 env
=`cat "${configpath}" | jq -c -r '.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
134 [ --username <username> ]: The username for the registry
135 [ --password <password> ]: The password for the registry
137 LXC internal arguments (do not pass manually!):
138 [ --name <name> ]: The container name
139 [ --path <path> ]: The path to the container
140 [ --rootfs <rootfs> ]: The path to the container's rootfs
141 [ --mapped-uid <map> ]: A uid map (user namespaces)
142 [ --mapped-gid <map> ]: A gid map (user namespaces)
148 options
=$
(getopt
-o u
:h
-l help,url
:,username
:,password
:,\
149 name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
151 if [ $?
-ne 0 ]; then
155 eval set -- "$options"
169 -h|
--help) usage
&& exit 1;;
170 -u|
--url) OCI_URL
=$2; shift 2;;
171 --username) OCI_USERNAME
=$2; shift 2;;
172 --password) OCI_PASSWORD
=$2; shift 2;;
173 --name) LXC_NAME
=$2; shift 2;;
174 --path) LXC_PATH
=$2; shift 2;;
175 --rootfs) LXC_ROOTFS
=$2; shift 2;;
176 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
177 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
182 # Check that we have all variables we need
183 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
184 echo "ERROR: Not running through LXC." 1>&2
188 if [ -z "$OCI_URL" ]; then
189 echo "ERROR: no OCI URL given"
193 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
194 echo "ERROR: password given but no username specified"
200 if [ "$USERNS" != "no" ]; then
201 if [ "$USERNS" = "yes" ]; then
202 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
203 echo "ERROR: In a user namespace without a map." 1>&2
207 DOWNLOAD_TARGET
="user"
210 DOWNLOAD_TARGET
="system"
214 # Trap all exit signals
215 trap cleanup EXIT HUP INT TERM
217 if ! type mktemp
>/dev
/null
2>&1; then
218 DOWNLOAD_TEMP
=/tmp
/lxc-oci.$$
219 mkdir
-p $DOWNLOAD_TEMP
221 DOWNLOAD_TEMP
=$
(mktemp
-d)
224 # Download the image - TODO - cache
226 if [ -n "$OCI_USERNAME" ]; then
227 CREDENTIALS
="${OCI_USERNAME}"
228 if [ -n "$OCI_PASSWORD" ]; then
229 CREDENTIALS
="${CREDENTIALS}:${OCI_PASSWORD}"
231 skopeo_args
+=(--src-creds "${CREDENTIALS}")
233 skopeo copy
${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest
"
235 echo "Unpacking the rootfs
"
237 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
238 umoci_args+=(--rootless)
240 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
241 rmdir "${LXC_ROOTFS}"
242 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
244 OCI_CONF_FILE
=$
(getconfigpath
${DOWNLOAD_TEMP} latest
)
245 LXC_CONF_FILE
="${LXC_PATH}/config"
246 entrypoint
=$
(getep
${OCI_CONF_FILE})
247 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
248 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
250 environment
=$
(getenv
${OCI_CONF_FILE})
251 while read -r line
; do
252 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
253 done <<< "${environment}"
255 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
256 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
259 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
260 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
263 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
265 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
270 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
272 127.0.1.1 ${LXC_NAME}
275 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
276 chown
$LXC_MAPPED_UID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
278 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
279 chgrp
$LXC_MAPPED_GID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true