]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-oci.in
Merge pull request #2190 from brauner/2018-02-16/lxc_local_template
[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 LOCALSTATEDIR="@LOCALSTATEDIR@"
38 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
39 LXC_HOOK_DIR="@LXCHOOKDIR@"
40
41 # Some useful functions
42 cleanup() {
43 if [ -d "${DOWNLOAD_TEMP}" ]; then
44 rm -Rf "${DOWNLOAD_TEMP}"
45 fi
46 if [ -d "${LXC_ROOTFS}.tmp" ]; then
47 rm -Rf "${LXC_ROOTFS}.tmp"
48 fi
49 }
50
51 in_userns() {
52 [ -e /proc/self/uid_map ] || { echo no; return; }
53 while read line; do
54 fields=$(echo $line | awk '{ print $1 " " $2 " " $3 }')
55 [ "$fields" = "0 0 4294967295" ] && { echo no; return; } || true
56 echo $fields | grep -q " 0 1$" && { echo userns-root; return; } || true
57 done < /proc/self/uid_map
58
59 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
60 { echo userns-root; return; }
61 echo yes
62 }
63
64 getconfigpath() {
65 basedir="$1"
66 q="$2"
67
68 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'`
69 if [ -z "${digest}" ]; then
70 echo "$q not found in index.json" >&2
71 return
72 fi
73
74 # Ok we have the image config digest, now get the config from that,
75 d=${digest:7}
76 cdigest=`cat "${basedir}/blobs/sha256/${d}" | jq -c -r '.config.digest'`
77 if [ -z "${cdigest}" ]; then
78 echo "container config not found" >&2
79 return
80 fi
81
82 d2=${cdigest:7}
83 echo "${basedir}/blobs/sha256/${d2}"
84 return
85 }
86
87 # get entrypoint from oci image. Use sh if unspecified
88 getep() {
89 if [ "$#" -eq 0 ]; then
90 echo "/bin/sh"
91 return
92 fi
93
94 configpath="$1"
95
96 ep=`cat "${configpath}" | jq -c -r '.config.Entrypoint[]?'`
97 cmd=`cat "${configpath}" | jq -c -r '.config.Cmd[]?'`
98 if [ -z "${ep}" ]; then
99 ep="${cmd}"
100 if [ -z "${ep}" ]; then
101 ep="/bin/sh"
102 fi
103 elif [ -n "${cmd}" ]; then
104 ep="${ep} ${cmd}"
105 fi
106
107 echo ${ep}
108 return
109 }
110
111 # get environment from oci image.
112 getenv() {
113 if [ "$#" -eq 0 ]; then
114 return
115 fi
116
117 configpath="$1"
118
119 env=`cat "${configpath}" | jq -c -r '.config.Env[]'`
120
121 echo "${env}"
122 return
123 }
124
125 # FIXME 1: only support numerical values in the configuration file.
126 # FIXME 2: from the OCI image spec: "If group/gid is not specified,
127 # the default group and supplementary groups of the given user/uid in
128 # /etc/passwd from the container are applied."
129 getuidgid() {
130 if [ "$#" -eq 0 ]; then
131 echo "0 0"
132 return
133 fi
134
135 configpath="$1"
136
137 uidgid=`cat "${configpath}" | jq -c -r '.config.User // "0:0"'`
138 uidgid=(${uidgid//:/ })
139
140 printf '%d %d' ${uidgid[0]:-0} ${uidgid[1]:-0} 2>/dev/null || true
141 return
142 }
143
144 # get cwd from oci image.
145 getcwd() {
146 if [ "$#" -eq 0 ]; then
147 echo "/"
148 return
149 fi
150
151 configpath="$1"
152
153 cwd=`cat "${configpath}" | jq -c -r '.config.WorkingDir // "/"'`
154
155 echo "${cwd}"
156 return
157 }
158
159 usage() {
160 cat <<EOF
161 LXC container template for OCI images
162
163 Special arguments:
164 [ -h | --help ]: Print this help message and exit.
165
166 Required arguments:
167 [ -u | --url <url> ]: The OCI image URL
168
169 Optional arguments:
170 [ --username <username> ]: The username for the registry
171 [ --password <password> ]: The password for the registry
172
173 LXC internal arguments (do not pass manually!):
174 [ --name <name> ]: The container name
175 [ --path <path> ]: The path to the container
176 [ --rootfs <rootfs> ]: The path to the container's rootfs
177 [ --mapped-uid <map> ]: A uid map (user namespaces)
178 [ --mapped-gid <map> ]: A gid map (user namespaces)
179
180 EOF
181 return 0
182 }
183
184 options=$(getopt -o u:h -l help,url:,username:,password:,no-cache,dhcp,\
185 name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")
186
187 if [ $? -ne 0 ]; then
188 usage
189 exit 1
190 fi
191 eval set -- "$options"
192
193 OCI_URL=""
194 OCI_USERNAME=
195 OCI_PASSWORD=
196 OCI_USE_CACHE="true"
197 OCI_USE_DHCP="false"
198
199 LXC_MAPPED_GID=
200 LXC_MAPPED_UID=
201 LXC_NAME=
202 LXC_PATH=
203 LXC_ROOTFS=
204
205 while :; do
206 case "$1" in
207 -h|--help) usage && exit 1;;
208 -u|--url) OCI_URL=$2; shift 2;;
209 --username) OCI_USERNAME=$2; shift 2;;
210 --password) OCI_PASSWORD=$2; shift 2;;
211 --no-cache) OCI_USE_CACHE="false"; shift 1;;
212 --dhcp) OCI_USE_DHCP="true"; shift 1;;
213 --name) LXC_NAME=$2; shift 2;;
214 --path) LXC_PATH=$2; shift 2;;
215 --rootfs) LXC_ROOTFS=$2; shift 2;;
216 --mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
217 --mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
218 *) break;;
219 esac
220 done
221
222 # Check that we have all variables we need
223 if [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
224 echo "ERROR: Not running through LXC." 1>&2
225 exit 1
226 fi
227
228 if [ -z "$OCI_URL" ]; then
229 echo "ERROR: no OCI URL given"
230 exit 1
231 fi
232
233 if [ -n "$OCI_PASSWORD" ] && [ -z "$OCI_USERNAME" ]; then
234 echo "ERROR: password given but no username specified"
235 exit 1
236 fi
237
238 if [ "${OCI_USE_CACHE}" = "true" ]; then
239 if ! skopeo copy --help | grep -q 'dest-shared-blob-dir'; then
240 echo "INFO: skopeo doesn't support blob caching"
241 OCI_USE_CACHE="false"
242 fi
243 fi
244
245 USERNS=$(in_userns)
246
247 if [ "$USERNS" = "yes" ]; then
248 if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
249 echo "ERROR: In a user namespace without a map." 1>&2
250 exit 1
251 fi
252 fi
253
254 if [ "${OCI_USE_CACHE}" = "true" ]; then
255 if [ "$USERNS" = "yes" ]; then
256 DOWNLOAD_BASE="${HOME}/.cache/lxc"
257 else
258 DOWNLOAD_BASE="${LOCALSTATEDIR}/cache/lxc"
259 fi
260 else
261 DOWNLOAD_BASE=/tmp
262 fi
263
264 # Trap all exit signals
265 trap cleanup EXIT HUP INT TERM
266
267 if ! type mktemp >/dev/null 2>&1; then
268 DOWNLOAD_TEMP="${DOWNLOAD_BASE}/lxc-oci.$$"
269 mkdir -p $DOWNLOAD_TEMP
270 else
271 DOWNLOAD_TEMP=$(mktemp -d -p "${DOWNLOAD_BASE}")
272 fi
273
274 # Download the image
275 skopeo_args=("")
276 if [ -n "$OCI_USERNAME" ]; then
277 CREDENTIALS="${OCI_USERNAME}"
278 if [ -n "$OCI_PASSWORD" ]; then
279 CREDENTIALS="${CREDENTIALS}:${OCI_PASSWORD}"
280 fi
281 skopeo_args+=(--src-creds "${CREDENTIALS}")
282 fi
283 if [ "${OCI_USE_CACHE}" = "true" ]; then
284 skopeo_args+=(--dest-shared-blob-dir "${DOWNLOAD_BASE}")
285 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
286 ln -s "${DOWNLOAD_BASE}/sha256" "${DOWNLOAD_TEMP}/blobs/sha256"
287 else
288 skopeo copy ${skopeo_args[@]} "${OCI_URL}" "oci:${DOWNLOAD_TEMP}:latest"
289 fi
290
291 echo "Unpacking the rootfs"
292 umoci_args=("")
293 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
294 umoci_args+=(--rootless)
295 fi
296 umoci unpack ${umoci_args[@]} --image "${DOWNLOAD_TEMP}:latest" "${LXC_ROOTFS}.tmp"
297 rmdir "${LXC_ROOTFS}"
298 mv "${LXC_ROOTFS}.tmp/rootfs" "${LXC_ROOTFS}"
299
300 OCI_CONF_FILE=$(getconfigpath ${DOWNLOAD_TEMP} latest)
301 LXC_CONF_FILE="${LXC_PATH}/config"
302 entrypoint=$(getep ${OCI_CONF_FILE})
303 echo "lxc.execute.cmd = '${entrypoint}'" >> "${LXC_CONF_FILE}"
304 echo "lxc.mount.auto = proc:mixed sys:mixed cgroup:mixed" >> "${LXC_CONF_FILE}"
305
306 environment=$(getenv ${OCI_CONF_FILE})
307 while read -r line; do
308 echo "lxc.environment = ${line}" >> "${LXC_CONF_FILE}"
309 done <<< "${environment}"
310
311 if [ -e "${LXC_TEMPLATE_CONFIG}/common.conf" ]; then
312 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/common.conf" >> "${LXC_CONF_FILE}"
313 fi
314
315 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ] && [ -e "${LXC_TEMPLATE_CONFIG}/userns.conf" ]; then
316 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/userns.conf" >> "${LXC_CONF_FILE}"
317 fi
318
319 if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf" ]; then
320 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/oci.common.conf" >> "${LXC_CONF_FILE}"
321 fi
322
323 if [ "${OCI_USE_DHCP}" = "true" ]; then
324 echo "lxc.hook.start-host = ${LXC_HOOK_DIR}/dhclient" >> "${LXC_CONF_FILE}"
325 echo "lxc.hook.stop = ${LXC_HOOK_DIR}/dhclient" >> "${LXC_CONF_FILE}"
326 fi
327
328 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_CONF_FILE}"
329 # set the hostname
330 cat <<EOF > ${LXC_ROOTFS}/etc/hostname
331 ${LXC_NAME}
332 EOF
333
334 # set minimal hosts
335 cat <<EOF > ${LXC_ROOTFS}/etc/hosts
336 127.0.0.1 localhost
337 127.0.1.1 ${LXC_NAME}
338 ::1 localhost ip6-localhost ip6-loopback
339 fe00::0 ip6-localnet
340 ff00::0 ip6-mcastprefix
341 ff02::1 ip6-allnodes
342 ff02::2 ip6-allrouters
343 EOF
344
345 uidgid=($(getuidgid ${OCI_CONF_FILE}))
346 echo "lxc.init.uid = ${uidgid[0]}" >> "${LXC_CONF_FILE}"
347 echo "lxc.init.gid = ${uidgid[1]}" >> "${LXC_CONF_FILE}"
348
349 cwd=$(getcwd ${OCI_CONF_FILE})
350 echo "lxc.init.cwd = ${cwd}" >> "${LXC_CONF_FILE}"
351
352 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
353 chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
354 fi
355 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
356 chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
357 fi
358
359 exit 0