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