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 --arg q "$q" '.manifests[] | if .annotations."org.opencontainers.image.ref.name" == $q then .digest else null end' | sed -e 's/"//g'`
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 '.config.digest' | sed -e 's/"//g'`
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 '.config.Entrypoint' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
95 cmd
=`cat "${configpath}" | jq -c '.config.Cmd' | sed -e 's/^\[//; s/\]$//; s/","/" "/'`
96 if [ "${ep}" = "null" ]; then
98 if [ "${ep}" = "null" ]; then
101 elif [ "${cmd}" != "null" ]; then
105 if [ -z "${ep}" ]; then
113 # get environment from oci image.
115 if [ "$#" -eq 0 ]; then
121 cat "${configpath}" > /tmp
/config
122 env
=`cat "${configpath}" | jq -c '.config.Env[]'`
130 LXC container template for OCI images
133 [ -h | --help ]: Print this help message and exit.
136 [ -u | --url <url> ]: The OCI image URL
139 [ --username <username> ]: The username for the registry
140 [ --password <password> ]: The password for the registry
142 LXC internal arguments (do not pass manually!):
143 [ --name <name> ]: The container name
144 [ --path <path> ]: The path to the container
145 [ --rootfs <rootfs> ]: The path to the container's rootfs
146 [ --mapped-uid <map> ]: A uid map (user namespaces)
147 [ --mapped-gid <map> ]: A gid map (user namespaces)
153 options
=$
(getopt
-o u
:h
-l help,url
:,username
:,password
:,\
154 name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
156 if [ $?
-ne 0 ]; then
160 eval set -- "$options"
174 -h|
--help) usage
&& exit 1;;
175 -u|
--url) OCI_URL
=$2; shift 2;;
176 --username) OCI_USERNAME
=$2; shift 2;;
177 --password) OCI_PASSWORD
=$2; shift 2;;
178 --name) LXC_NAME
=$2; shift 2;;
179 --path) LXC_PATH
=$2; shift 2;;
180 --rootfs) LXC_ROOTFS
=$2; shift 2;;
181 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
182 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
187 # Check that we have all variables we need
188 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
189 echo "ERROR: Not running through LXC." 1>&2
193 if [ -z "$OCI_URL" ]; then
194 echo "ERROR: no OCI URL given"
198 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
199 echo "ERROR: password given but no username specified"
205 if [ "$USERNS" != "no" ]; then
206 if [ "$USERNS" = "yes" ]; then
207 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
208 echo "ERROR: In a user namespace without a map." 1>&2
212 DOWNLOAD_TARGET
="user"
215 DOWNLOAD_TARGET
="system"
219 # Trap all exit signals
220 trap cleanup EXIT HUP INT TERM
222 if ! type mktemp
>/dev
/null
2>&1; then
223 DOWNLOAD_TEMP
=/tmp
/lxc-oci.$$
224 mkdir
-p $DOWNLOAD_TEMP
226 DOWNLOAD_TEMP
=$
(mktemp
-d)
229 # Download the image - TODO - cache
231 if [ -n "$OCI_USERNAME" ]; then
232 CREDENTIALS
="${OCI_USERNAME}"
233 if [ -n "$OCI_PASSWORD" ]; then
234 CREDENTIALS
="${CREDENTIALS}:${OCI_PASSWORD}"
236 skopeo_args
+=(--src-creds "${CREDENTIALS}")
238 skopeo copy
${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest
"
241 echo "Unpacking the rootfs
"
244 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
245 umoci_args+=(--rootless)
247 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
248 rmdir "${LXC_ROOTFS}"
249 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
251 OCI_CONF_FILE
=$
(getconfigpath
${DOWNLOAD_TEMP} latest
)
252 LXC_CONF_FILE
="${LXC_PATH}/config"
253 entrypoint
=$
(getep
${OCI_CONF_FILE})
254 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
255 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
257 environment
=$
(getenv
${OCI_CONF_FILE})
258 while read -r line
; do
259 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
260 done <<< "${environment}"
262 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
263 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
266 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
267 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
270 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
272 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
277 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
279 127.0.1.1 ${LXC_NAME}
282 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
283 chown
$LXC_MAPPED_UID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
285 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
286 chgrp
$LXC_MAPPED_GID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true