]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-oci.in
lxc-oci: support index files with multiple manifests
[mirror_lxc.git] / templates / lxc-oci.in
1 #!/bin/bash
2
3 # Create application containers from OCI images
4
5 # Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
6 # Copyright © 2017 Serge Hallyn <serge@hallyn.com>
7 #
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.
12
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.
17
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
21 # USA
22
23 set -eu
24 # set -x # debug
25
26 # Make sure the usual locations are in PATH
27 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
28
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
33 exit 1
34 fi
35 done
36
37 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
38
39 # Some useful functions
40 cleanup() {
41 if [ -d "${DOWNLOAD_TEMP}" ]; then
42 rm -Rf "${DOWNLOAD_TEMP}"
43 fi
44 if [ -d "${LXC_ROOTFS}.tmp" ]; then
45 rm -Rf "${LXC_ROOTFS}.tmp"
46 fi
47 }
48
49 in_userns() {
50 [ -e /proc/self/uid_map ] || { echo no; return; }
51 while read line; do
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
56
57 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
58 { echo userns-root; return; }
59 echo yes
60 }
61
62 getconfigpath() {
63 basedir="$1"
64 q="$2"
65
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
69 return
70 fi
71
72 # Ok we have the image config digest, now get the config from that,
73 d=${digest:7}
74 cdigest=`cat "${basedir}/blobs/sha256/${d}" | jq -c -r '.config.digest'`
75 if [ -z "${cdigest}" ]; then
76 echo "container config not found" >&2
77 return
78 fi
79
80 d2=${cdigest:7}
81 echo "${basedir}/blobs/sha256/${d2}"
82 return
83 }
84
85 # get entrypoint from oci image. Use sh if unspecified
86 getep() {
87 if [ "$#" -eq 0 ]; then
88 echo "/bin/sh"
89 return
90 fi
91
92 configpath="$1"
93
94 ep=`cat "${configpath}" | jq -c -r '.config.Entrypoint[]?'`
95 cmd=`cat "${configpath}" | jq -c -r '.config.Cmd[]?'`
96 if [ -z "${ep}" ]; then
97 ep="${cmd}"
98 if [ -z "${ep}" ]; then
99 ep="/bin/sh"
100 fi
101 elif [ -n "${cmd}" ]; then
102 ep="${ep} ${cmd}"
103 fi
104
105 echo ${ep}
106 return
107 }
108
109 # get environment from oci image.
110 getenv() {
111 if [ "$#" -eq 0 ]; then
112 return
113 fi
114
115 configpath="$1"
116
117 env=`cat "${configpath}" | jq -c -r '.config.Env[]'`
118
119 echo "${env}"
120 return
121 }
122
123 usage() {
124 cat <<EOF
125 LXC container template for OCI images
126
127 Special arguments:
128 [ -h | --help ]: Print this help message and exit.
129
130 Required arguments:
131 [ -u | --url <url> ]: The OCI image URL
132
133 Optional arguments:
134 [ --username <username> ]: The username for the registry
135 [ --password <password> ]: The password for the registry
136
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)
143
144 EOF
145 return 0
146 }
147
148 options=$(getopt -o u:h -l help,url:,username:,password:,\
149 name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")
150
151 if [ $? -ne 0 ]; then
152 usage
153 exit 1
154 fi
155 eval set -- "$options"
156
157 OCI_URL=""
158 OCI_USERNAME=
159 OCI_PASSWORD=
160
161 LXC_MAPPED_GID=
162 LXC_MAPPED_UID=
163 LXC_NAME=
164 LXC_PATH=
165 LXC_ROOTFS=
166
167 while :; do
168 case "$1" in
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;;
178 *) break;;
179 esac
180 done
181
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
185 exit 1
186 fi
187
188 if [ -z "$OCI_URL" ]; then
189 echo "ERROR: no OCI URL given"
190 exit 1
191 fi
192
193 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
194 echo "ERROR: password given but no username specified"
195 exit 1
196 fi
197
198 USERNS=$(in_userns)
199
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
204 exit 1
205 fi
206 DOWNLOAD_MODE="user"
207 DOWNLOAD_TARGET="user"
208 else
209 DOWNLOAD_MODE="user"
210 DOWNLOAD_TARGET="system"
211 fi
212 fi
213
214 # Trap all exit signals
215 trap cleanup EXIT HUP INT TERM
216
217 if ! type mktemp >/dev/null 2>&1; then
218 DOWNLOAD_TEMP=/tmp/lxc-oci.$$
219 mkdir -p $DOWNLOAD_TEMP
220 else
221 DOWNLOAD_TEMP=$(mktemp -d)
222 fi
223
224 # Download the image - TODO - cache
225 skopeo_args=("")
226 if [ -n "$OCI_USERNAME" ]; then
227 CREDENTIALS="${OCI_USERNAME}"
228 if [ -n "$OCI_PASSWORD" ]; then
229 CREDENTIALS="${CREDENTIALS}:${OCI_PASSWORD}"
230 fi
231 skopeo_args+=(--src-creds "${CREDENTIALS}")
232 fi
233 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
234
235 echo "Unpacking the rootfs"
236 umoci_args=("")
237 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
238 umoci_args+=(--rootless)
239 fi
240 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
241 rmdir "${LXC_ROOTFS}"
242 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
243
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}"
249
250 environment=$(getenv ${OCI_CONF_FILE})
251 while read -r line; do
252 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
253 done <<< "${environment}"
254
255 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
256 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
257 fi
258
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}"
261 fi
262
263 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
264 # set the hostname
265 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
266 ${LXC_NAME}
267 EOF
268
269 # set minimal hosts
270 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
271 127.0.0.1 localhost
272 127.0.1.1 ${LXC_NAME}
273 EOF
274
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
277 fi
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
280 fi
281
282 exit 0