]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-download.in
templates/lxc-download.in: use GPG option --receive-keys instead of --recv-keys
[mirror_lxc.git] / templates / lxc-download.in
CommitLineData
71d3a659
SG
1#!/bin/sh
2
3# Client script for LXC container images.
4#
5# Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
6#
7# This library is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Lesser General Public
9# License as published by the Free Software Foundation; either
10# version 2.1 of the License, or (at your option) any later version.
11
12# This library is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# Lesser General Public License for more details.
16
17# You should have received a copy of the GNU Lesser General Public
18# License along with this library; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20# USA
21
22set -eu
23
71d3a659 24LOCALSTATEDIR="@LOCALSTATEDIR@"
f74e080c
SG
25LXC_HOOK_DIR="@LXCHOOKDIR@"
26LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
71d3a659
SG
27
28# Defaults
71d3a659 29DOWNLOAD_ARCH=
f74e080c 30DOWNLOAD_BUILD=
7a930fe7 31DOWNLOAD_COMPAT_LEVEL=7
f74e080c 32DOWNLOAD_DIST=
71d3a659 33DOWNLOAD_FLUSH_CACHE="false"
41670b35 34DOWNLOAD_FORCE_CACHE="false"
f74e080c 35DOWNLOAD_INTERACTIVE="false"
dd2dbcb9 36DOWNLOAD_KEYID="0xE7FB0CAEC8173D669066514CBAEFF88C22F6E216"
f74e080c 37DOWNLOAD_LIST_IMAGES="false"
71d3a659 38DOWNLOAD_MODE="system"
b0f0932a 39DOWNLOAD_READY_GPG="false"
f74e080c
SG
40DOWNLOAD_RELEASE=
41DOWNLOAD_SERVER="images.linuxcontainers.org"
42DOWNLOAD_SHOW_GPG_WARNING="true"
43DOWNLOAD_SHOW_HTTP_WARNING="true"
44DOWNLOAD_TARGET="system"
45DOWNLOAD_URL=
46DOWNLOAD_USE_CACHE="false"
47DOWNLOAD_VALIDATE="true"
48DOWNLOAD_VARIANT="default"
edb5452c 49DOWNLOAD_TEMP=
71d3a659 50
f74e080c
SG
51LXC_MAPPED_GID=
52LXC_MAPPED_UID=
71d3a659
SG
53LXC_NAME=
54LXC_PATH=
55LXC_ROOTFS=
71d3a659 56
a9a53b50 57if [ -z "${DOWNLOAD_KEYSERVER:-}" ]; then
832cb182 58 DOWNLOAD_KEYSERVER="hkp://pool.sks-keyservers.net"
a6a7c7d1 59
832cb182
CB
60 # Deal with GPG over http proxy
61 if [ -n "${http_proxy:-}" ]; then
62 DOWNLOAD_KEYSERVER="hkp://p80.pool.sks-keyservers.net:80"
41440801 63 DOWNLOAD_GPG_PROXY="--keyserver-options http-proxy=\"${http_proxy}\""
832cb182 64 fi
4eb706b3
SG
65fi
66
207bf0e4
SG
67# Make sure the usual locations are in PATH
68export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
69
71d3a659
SG
70# Some useful functions
71cleanup() {
832cb182
CB
72 if [ -d "${DOWNLOAD_TEMP}" ]; then
73 rm -Rf "${DOWNLOAD_TEMP}"
74 fi
71d3a659
SG
75}
76
acabe1fa 77wget_wrapper() {
832cb182
CB
78 for _ in $(seq 3); do
79 if wget "$@"; then
80 return 0
81 fi
82 done
acabe1fa 83
832cb182 84 return 1
acabe1fa
SG
85}
86
71d3a659 87download_file() {
832cb182
CB
88 if ! wget_wrapper -T 30 -q "https://${DOWNLOAD_SERVER}/$1" -O "$2" >/dev/null 2>&1; then
89 if ! wget_wrapper -T 30 -q "http://${DOWNLOAD_SERVER}/$1" -O "$2" >/dev/null 2>&1; then
90 if [ "$3" = "noexit" ]; then
91 return 1
92 else
93 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
94 exit 1
95 fi
96 elif [ "${DOWNLOAD_SHOW_HTTP_WARNING}" = "true" ]; then
97 DOWNLOAD_SHOW_HTTP_WARNING="false"
98 echo "WARNING: Failed to download the file over HTTPs" 1>&2
99 echo " The file was instead download over HTTP " 1>&2
100 echo "A server replay attack may be possible!" 1>&2
71d3a659 101 fi
832cb182 102 fi
71d3a659
SG
103}
104
fad96766 105download_sig() {
832cb182
CB
106 if ! download_file "$1" "$2" noexit; then
107 if [ "${DOWNLOAD_VALIDATE}" = "true" ]; then
108 if [ "$3" = "normal" ]; then
109 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
110 exit 1
111 else
112 return 1
113 fi
114 else
115 return 0
fad96766 116 fi
832cb182 117 fi
fad96766
DE
118}
119
71d3a659 120gpg_setup() {
832cb182
CB
121 if [ "${DOWNLOAD_VALIDATE}" = "false" ]; then
122 return
123 fi
124
125 if [ "${DOWNLOAD_READY_GPG}" = "true" ]; then
126 return
127 fi
128
129 echo "Setting up the GPG keyring"
130
131 mkdir -p "${DOWNLOAD_TEMP}/gpg"
132 chmod 700 "${DOWNLOAD_TEMP}/gpg"
133 export GNUPGHOME="${DOWNLOAD_TEMP}/gpg"
134
135 success=
136 for _ in $(seq 3); do
18e18d4c 137 if gpg --keyserver "${DOWNLOAD_KEYSERVER}" ${DOWNLOAD_GPG_PROXY:-} \
409040e7 138 --receive-keys "${DOWNLOAD_KEYID}" >/dev/null 2>&1; then
832cb182
CB
139 success=1
140 break
71d3a659 141 fi
832cb182
CB
142 break
143 done
71d3a659 144
832cb182
CB
145 if [ -z "${success}" ]; then
146 echo "ERROR: Unable to fetch GPG key from keyserver"
147 exit 1
148 fi
b0f0932a 149
832cb182 150 DOWNLOAD_READY_GPG="true"
71d3a659
SG
151}
152
153gpg_validate() {
832cb182
CB
154 if [ "${DOWNLOAD_VALIDATE}" = "false" ]; then
155 if [ "${DOWNLOAD_SHOW_GPG_WARNING}" = "true" ]; then
156 echo "WARNING: Running without gpg validation!" 1>&2
71d3a659 157 fi
832cb182
CB
158 DOWNLOAD_SHOW_GPG_WARNING="false"
159 return 0
160 fi
71d3a659 161
832cb182
CB
162 if ! gpg --verify "$1" >/dev/null 2>&1; then
163 echo "ERROR: Invalid signature for $1" 1>&2
164 exit 1
165 fi
71d3a659
SG
166}
167
168in_userns() {
832cb182
CB
169 [ -e /proc/self/uid_map ] || { echo no; return; }
170 while read -r line; do
171 fields="$(echo "$line" | awk '{ print $1 " " $2 " " $3 }')"
172 if [ "${fields}" = "0 0 4294967295" ]; then
173 echo no;
174 return;
175 fi
176 if echo "${fields}" | grep -q " 0 1$"; then
177 echo userns-root;
178 return;
179 fi
180 done < /proc/self/uid_map
181
16a312e1
LP
182 if [ -e /proc/1/uid_map ]; then
183 if [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ]; then
184 echo userns-root
185 return
186 fi
187 fi
832cb182 188 echo yes
71d3a659
SG
189}
190
191relevant_file() {
832cb182
CB
192 FILE_PATH="${LXC_CACHE_PATH}/$1"
193
194 if [ -e "${FILE_PATH}-${DOWNLOAD_MODE}" ]; then
195 FILE_PATH="${FILE_PATH}-${DOWNLOAD_MODE}"
196 fi
71d3a659 197
832cb182
CB
198 if [ -e "${FILE_PATH}.${DOWNLOAD_COMPAT_LEVEL}" ]; then
199 FILE_PATH="${FILE_PATH}.${DOWNLOAD_COMPAT_LEVEL}"
200 fi
201
202 echo "${FILE_PATH}"
71d3a659
SG
203}
204
205usage() {
832cb182 206 cat <<EOF
71d3a659
SG
207LXC container image downloader
208
7d540a26 209Special arguments:
832cb182
CB
210[ -h | --help ]: Print this help message and exit
211[ -l | --list ]: List all available images and exit
7d540a26 212
71d3a659
SG
213Required arguments:
214[ -d | --dist <distribution> ]: The name of the distribution
215[ -r | --release <release> ]: Release name/version
216[ -a | --arch <architecture> ]: Architecture of the container
71d3a659
SG
217
218Optional arguments:
219[ --variant <variant> ]: Variant of the image (default: "default")
220[ --server <server> ]: Image server (default: "images.linuxcontainers.org")
221[ --keyid <keyid> ]: GPG keyid (default: 0x...)
d2e5c5d1 222[ --keyserver <keyserver> ]: GPG keyserver to use. Environment variable: DOWNLOAD_KEYSERVER
71d3a659
SG
223[ --no-validate ]: Disable GPG validation (not recommended)
224[ --flush-cache ]: Flush the local copy (if present)
e145b7bb 225[ --force-cache ]: Force the use of the local copy even if expired
71d3a659
SG
226
227LXC internal arguments (do not pass manually!):
228[ --name <name> ]: The container name
229[ --path <path> ]: The path to the container
230[ --rootfs <rootfs> ]: The path to the container's rootfs
2133f58c
SH
231[ --mapped-uid <map> ]: A uid map (user namespaces)
232[ --mapped-gid <map> ]: A gid map (user namespaces)
d2e5c5d1
TK
233
234Environment Variables:
235DOWNLOAD_KEYSERVER : The URL of the key server to use, instead of the default.
236 Can be further overridden by using optional argument --keyserver
237
71d3a659 238EOF
832cb182 239 return 0
71d3a659
SG
240}
241
3f7be9d0 242if ! options=$(getopt -o d:r:a:hl -l dist:,release:,arch:,help,list,variant:,\
3cd988cc 243server:,keyid:,keyserver:,no-validate,flush-cache,force-cache,name:,path:,\
3f7be9d0 244rootfs:,mapped-uid:,mapped-gid: -- "$@"); then
832cb182
CB
245 usage
246 exit 1
71d3a659
SG
247fi
248eval set -- "$options"
249
250while :; do
832cb182
CB
251 case "$1" in
252 -h|--help) usage && exit 1;;
253 -l|--list) DOWNLOAD_LIST_IMAGES="true"; shift 1;;
254 -d|--dist) DOWNLOAD_DIST="$2"; shift 2;;
255 -r|--release) DOWNLOAD_RELEASE="$2"; shift 2;;
256 -a|--arch) DOWNLOAD_ARCH="$2"; shift 2;;
257 --variant) DOWNLOAD_VARIANT="$2"; shift 2;;
258 --server) DOWNLOAD_SERVER="$2"; shift 2;;
259 --keyid) DOWNLOAD_KEYID="$2"; shift 2;;
260 --keyserver) DOWNLOAD_KEYSERVER="$2"; shift 2;;
261 --no-validate) DOWNLOAD_VALIDATE="false"; shift 1;;
262 --flush-cache) DOWNLOAD_FLUSH_CACHE="true"; shift 1;;
263 --force-cache) DOWNLOAD_FORCE_CACHE="true"; shift 1;;
264 --name) LXC_NAME="$2"; shift 2;;
265 --path) LXC_PATH="$2"; shift 2;;
266 --rootfs) LXC_ROOTFS="$2"; shift 2;;
267 --mapped-uid) LXC_MAPPED_UID="$2"; shift 2;;
268 --mapped-gid) LXC_MAPPED_GID="$2"; shift 2;;
269 *) break;;
270 esac
71d3a659
SG
271done
272
273# Check for required binaries
274for bin in tar xz wget; do
832cb182
CB
275 if ! command -V "${bin}" >/dev/null 2>&1; then
276 echo "ERROR: Missing required tool: ${bin}" 1>&2
277 exit 1
278 fi
71d3a659
SG
279done
280
281# Check for GPG
3f7be9d0 282if [ "${DOWNLOAD_VALIDATE}" = "true" ]; then
832cb182
CB
283 if ! command -V gpg >/dev/null 2>&1; then
284 echo "ERROR: Missing recommended tool: gpg" 1>&2
285 echo "You can workaround this by using --no-validate" 1>&2
286 exit 1
287 fi
71d3a659
SG
288fi
289
290# Check that we have all variables we need
3f7be9d0 291if [ -z "${LXC_NAME}" ] || [ -z "${LXC_PATH}" ] || [ -z "${LXC_ROOTFS}" ]; then
832cb182
CB
292 if [ "${DOWNLOAD_LIST_IMAGES}" != "true" ]; then
293 echo "ERROR: Please pass the name, path, and rootfs for the container" 1>&2
294 exit 1
295 fi
71d3a659
SG
296fi
297
3f7be9d0 298USERNS="$(in_userns)"
f74e080c 299
3f7be9d0 300if [ "${USERNS}" != "no" ]; then
832cb182
CB
301 if [ "${USERNS}" = "yes" ]; then
302 if [ -z "${LXC_MAPPED_UID}" ] || [ "${LXC_MAPPED_UID}" = "-1" ]; then
303 echo "ERROR: In a user namespace without a map" 1>&2
304 exit 1
71d3a659 305 fi
832cb182
CB
306 DOWNLOAD_MODE="user"
307 DOWNLOAD_TARGET="user"
308 else
309 DOWNLOAD_MODE="user"
310 DOWNLOAD_TARGET="system"
311 fi
71d3a659
SG
312fi
313
832cb182
CB
314if [ -z "${DOWNLOAD_DIST}" ] || [ -z "${DOWNLOAD_RELEASE}" ] || [ -z "${DOWNLOAD_ARCH}" ]; then
315 DOWNLOAD_INTERACTIVE="true"
71d3a659
SG
316fi
317
318# Trap all exit signals
319trap cleanup EXIT HUP INT TERM
843a5874 320
edb5452c
SC
321# /tmp may be mounted in tmpfs or noexec
322if mountpoint -q /tmp; then
832cb182 323 DOWNLOAD_TEMP="${LXC_PATH}"
edb5452c
SC
324fi
325
3f7be9d0 326if ! command -V mktemp >/dev/null 2>&1; then
832cb182 327 DOWNLOAD_TEMP="${DOWNLOAD_TEMP}/tmp/lxc-download.$$"
30c8676e
CB
328elif [ -n "${DOWNLOAD_TEMP}" ]; then
329 mkdir -p "${DOWNLOAD_TEMP}"
eb44984a 330 DOWNLOAD_TEMP="$(mktemp -p "${DOWNLOAD_TEMP}" -d)"
30c8676e
CB
331else
332 DOWNLOAD_TEMP="${DOWNLOAD_TEMP}$(mktemp -d)"
843a5874 333fi
71d3a659 334
10a5fab6 335# Simply list images
832cb182
CB
336if [ "${DOWNLOAD_LIST_IMAGES}" = "true" ] || [ "${DOWNLOAD_INTERACTIVE}" = "true" ]; then
337 # Initialize GPG
338 gpg_setup
339
340 # Grab the index
341 DOWNLOAD_INDEX_PATH="/meta/1.0/index-${DOWNLOAD_MODE}"
342
343 echo "Downloading the image index"
344 if ! download_file "${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL}" "${DOWNLOAD_TEMP}/index" noexit ||
345 ! download_sig "${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL}.asc" "${DOWNLOAD_TEMP}/index.asc" noexit; then
346 download_file "${DOWNLOAD_INDEX_PATH}" "${DOWNLOAD_TEMP}/index" normal
347 download_sig "${DOWNLOAD_INDEX_PATH}.asc" "${DOWNLOAD_TEMP}/index.asc" normal
348 fi
349
350 gpg_validate "${DOWNLOAD_TEMP}/index.asc"
351
352 # Parse it
353 echo ""
354 echo "---"
355 printf "DIST\tRELEASE\tARCH\tVARIANT\tBUILD\n"
356 echo "---"
357 while IFS=';' read -r f1 f2 f3 f4 f5 f6; do
358 [ -n "${DOWNLOAD_DIST}" ] && [ "$f1" != "${DOWNLOAD_DIST}" ] && continue
359 [ -n "${DOWNLOAD_RELEASE}" ] && [ "$f2" != "${DOWNLOAD_RELEASE}" ] && continue
360 [ -n "${DOWNLOAD_ARCH}" ] && [ "$f3" != "${DOWNLOAD_ARCH}" ] && continue
361 [ -n "${DOWNLOAD_VARIANT}" ] && [ "$f4" != "${DOWNLOAD_VARIANT}" ] && continue
362 [ -z "${f5}" ] || [ -z "${f6}" ] && continue
363
364 printf "%s\t%s\t%s\t%s\t%s\n" "${f1}" "${f2}" "${f3}" "${f4}" "${f5}"
365 unset f1 f2 f3 f4 f5 f6
366 done < "${DOWNLOAD_TEMP}/index"
367 echo "---"
368
369 if [ "${DOWNLOAD_LIST_IMAGES}" = "true" ]; then
370 exit 1
371 fi
b0f0932a 372
832cb182
CB
373 # Interactive mode
374 echo ""
b0f0932a 375
832cb182
CB
376 if [ -z "${DOWNLOAD_DIST}" ]; then
377 echo "Distribution: "
378 read -r DOWNLOAD_DIST
379 fi
b0f0932a 380
832cb182
CB
381 if [ -z "${DOWNLOAD_RELEASE}" ]; then
382 echo "Release: "
383 read -r DOWNLOAD_RELEASE
384 fi
b0f0932a 385
832cb182
CB
386 if [ -z "${DOWNLOAD_ARCH}" ]; then
387 echo "Architecture: "
388 read -r DOWNLOAD_ARCH
389 fi
b0f0932a 390
832cb182 391 echo ""
10a5fab6
SG
392fi
393
71d3a659 394# Setup the cache
3f7be9d0 395if [ "${DOWNLOAD_TARGET}" = "system" ]; then
832cb182 396 LXC_CACHE_BASE="${LOCALSTATEDIR}/cache/lxc/"
71d3a659 397else
832cb182 398 LXC_CACHE_BASE="${HOME}/.cache/lxc/"
71d3a659
SG
399fi
400
6dc6f80b 401# Allow the setting of the LXC_CACHE_PATH with the usage of environment variables.
3f7be9d0
WG
402LXC_CACHE_PATH="${LXC_CACHE_PATH:-"${LXC_CACHE_BASE}"}"
403LXC_CACHE_PATH="${LXC_CACHE_PATH}/download/${DOWNLOAD_DIST}"
404LXC_CACHE_PATH="${LXC_CACHE_PATH}/${DOWNLOAD_RELEASE}/${DOWNLOAD_ARCH}/"
405LXC_CACHE_PATH="${LXC_CACHE_PATH}/${DOWNLOAD_VARIANT}"
b56661fe 406
3f7be9d0 407if [ -d "${LXC_CACHE_PATH}" ]; then
832cb182
CB
408 if [ "${DOWNLOAD_FLUSH_CACHE}" = "true" ]; then
409 echo "Flushing the cache..."
410 rm -Rf "${LXC_CACHE_PATH}"
411 elif [ "${DOWNLOAD_FORCE_CACHE}" = "true" ]; then
412 DOWNLOAD_USE_CACHE="true"
413 else
414 DOWNLOAD_USE_CACHE="true"
415 if [ -e "$(relevant_file expiry)" ]; then
416 if [ "$(cat "$(relevant_file expiry)")" -lt "$(date +%s)" ]; then
417 echo "The cached copy has expired, re-downloading..."
418 DOWNLOAD_USE_CACHE="false"
419 fi
71d3a659 420 fi
832cb182 421 fi
71d3a659
SG
422fi
423
424# Download what's needed
3f7be9d0 425if [ "${DOWNLOAD_USE_CACHE}" = "false" ]; then
832cb182
CB
426 # Initialize GPG
427 gpg_setup
428
429 # Grab the index
430 DOWNLOAD_INDEX_PATH="/meta/1.0/index-${DOWNLOAD_MODE}"
431
432 echo "Downloading the image index"
433 if ! download_file "${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL}" "${DOWNLOAD_TEMP}/index" noexit ||
434 ! download_sig "${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL}.asc" "${DOWNLOAD_TEMP}/index.asc" noexit; then
435 download_file "${DOWNLOAD_INDEX_PATH}" "${DOWNLOAD_TEMP}/index" normal
436 download_sig "${DOWNLOAD_INDEX_PATH}.asc" "${DOWNLOAD_TEMP}/index.asc" normal
437 fi
438
439 gpg_validate "${DOWNLOAD_TEMP}/index.asc"
440
441 # Parse it
442 while IFS=';' read -r f1 f2 f3 f4 f5 f6; do
443 if [ "${f1}" != "${DOWNLOAD_DIST}" ] || \
444 [ "${f2}" != "${DOWNLOAD_RELEASE}" ] || \
445 [ "${f3}" != "${DOWNLOAD_ARCH}" ] || \
446 [ "${f4}" != "${DOWNLOAD_VARIANT}" ] || \
447 [ -z "${f6}" ]; then
448 continue
71d3a659
SG
449 fi
450
832cb182
CB
451 DOWNLOAD_BUILD="${f5}"
452 DOWNLOAD_URL="${f6}"
71d3a659 453
832cb182
CB
454 unset f1 f2 f3 f4 f5 f6
455 break
456 done < "${DOWNLOAD_TEMP}/index"
3f7be9d0 457
832cb182
CB
458 if [ -z "${DOWNLOAD_URL}" ]; then
459 echo "ERROR: Couldn't find a matching image" 1>&1
460 exit 1
461 fi
71d3a659 462
832cb182
CB
463 if [ -d "${LXC_CACHE_PATH}" ] && [ -f "${LXC_CACHE_PATH}/build_id" ] && \
464 [ "$(cat "${LXC_CACHE_PATH}/build_id")" = "${DOWNLOAD_BUILD}" ]; then
465 echo "The cache is already up to date"
466 echo "Using image from local cache"
467 else
468 # Download the actual files
469 echo "Downloading the rootfs"
470 download_file "${DOWNLOAD_URL}/rootfs.tar.xz" "${DOWNLOAD_TEMP}/rootfs.tar.xz" normal
471 download_sig "${DOWNLOAD_URL}/rootfs.tar.xz.asc" "${DOWNLOAD_TEMP}/rootfs.tar.xz.asc" normal
472 gpg_validate "${DOWNLOAD_TEMP}/rootfs.tar.xz.asc"
473
474 echo "Downloading the metadata"
475 download_file "${DOWNLOAD_URL}/meta.tar.xz" "${DOWNLOAD_TEMP}/meta.tar.xz" normal
476 download_sig "$DOWNLOAD_URL/meta.tar.xz.asc" "${DOWNLOAD_TEMP}/meta.tar.xz.asc" normal
477 gpg_validate "${DOWNLOAD_TEMP}/meta.tar.xz.asc"
478
479 if [ -d "${LXC_CACHE_PATH}" ]; then
480 rm -Rf "${LXC_CACHE_PATH}"
481 fi
482 mkdir -p "${LXC_CACHE_PATH}"
483 mv "${DOWNLOAD_TEMP}/rootfs.tar.xz" "${LXC_CACHE_PATH}"
484 if ! tar Jxf "${DOWNLOAD_TEMP}/meta.tar.xz" -C "${LXC_CACHE_PATH}"; then
485 echo "ERROR: Invalid rootfs tarball." 2>&1
486 exit 1
487 fi
3f7be9d0 488
832cb182 489 echo "${DOWNLOAD_BUILD}" > "${LXC_CACHE_PATH}/build_id"
71d3a659 490
832cb182
CB
491 if [ -n "${LXC_MAPPED_UID}" ] && [ "${LXC_MAPPED_UID}" != "-1" ]; then
492 # As the script is run in strict mode (set -eu), all commands
493 # exiting with non 0 would make the script stop.
494 # || true or || : (more portable) prevents that.
495 chown -R "${LXC_MAPPED_UID}" "${LXC_CACHE_BASE}" >/dev/null 2>&1 || :
71d3a659 496 fi
832cb182
CB
497 if [ -n "${LXC_MAPPED_GID}" ] && [ "${LXC_MAPPED_GID}" != "-1" ]; then
498 chgrp -R "${LXC_MAPPED_GID}" "${LXC_CACHE_BASE}" >/dev/null 2>&1 || :
71d3a659 499 fi
832cb182
CB
500 echo "The image cache is now ready"
501 fi
71d3a659 502else
832cb182 503 echo "Using image from local cache"
71d3a659
SG
504fi
505
506# Unpack the rootfs
507echo "Unpacking the rootfs"
fecf101c
SG
508
509EXCLUDES=""
510excludelist=$(relevant_file excludes)
511if [ -f "${excludelist}" ]; then
832cb182
CB
512 while read -r line; do
513 EXCLUDES="${EXCLUDES} --exclude=${line}"
514 done < "${excludelist}"
71d3a659
SG
515fi
516
3f7be9d0
WG
517# Do not surround ${EXCLUDES} by quotes. This does not work. The solution could
518# use array but this is not POSIX compliant. The only POSIX compliant solution
519# is to use a function wrapper, but the latter can't be used here as the args
520# are dynamic. We thus need to ignore the warning brought by shellcheck.
521# shellcheck disable=SC2086
832cb182 522tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_CACHE_PATH}/rootfs.tar.xz" -C "${LXC_ROOTFS}"
fecf101c 523
3f7be9d0 524mkdir -p "${LXC_ROOTFS}/dev/pts/"
fecf101c 525
71d3a659 526# Setup the configuration
3f7be9d0
WG
527configfile="$(relevant_file config)"
528fstab="$(relevant_file fstab)"
529if [ ! -e "${configfile}" ]; then
832cb182
CB
530 echo "ERROR: meta tarball is missing the configuration file" 1>&2
531 exit 1
71d3a659
SG
532fi
533
534## Extract all the network config entries
832cb182 535sed -i -e "/lxc.net.0/{w ${LXC_PATH}/config-network" -e "d}" "${LXC_PATH}/config"
71d3a659
SG
536
537## Extract any other config entry
3f7be9d0 538sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" "${LXC_PATH}/config"
71d3a659
SG
539
540## Append the defaults
832cb182
CB
541{
542 echo ""
543 echo "# Distribution configuration"
544 cat "$configfile"
545} >> "${LXC_PATH}/config"
71d3a659
SG
546
547## Add the container-specific config
832cb182
CB
548{
549 echo ""
550 echo "# Container specific configuration"
551 if [ -e "${LXC_PATH}/config-auto" ]; then
552 cat "${LXC_PATH}/config-auto"
3f7be9d0 553 rm "${LXC_PATH}/config-auto"
832cb182
CB
554 fi
555 if [ -e "${fstab}" ]; then
556 echo "lxc.mount.fstab = ${LXC_PATH}/fstab"
557 fi
558 echo "lxc.uts.name = ${LXC_NAME}"
559} >> "${LXC_PATH}/config"
71d3a659
SG
560
561## Re-add the previously removed network config
562if [ -e "${LXC_PATH}/config-network" ]; then
832cb182
CB
563 {
564 echo ""
565 echo "# Network configuration"
566 cat "${LXC_PATH}/config-network"
3f7be9d0 567 rm "${LXC_PATH}/config-network"
832cb182 568 } >> "${LXC_PATH}/config"
71d3a659
SG
569fi
570
571TEMPLATE_FILES="${LXC_PATH}/config"
572
573# Setup the fstab
3f7be9d0 574if [ -e "${fstab}" ]; then
832cb182
CB
575 cp "${fstab}" "${LXC_PATH}/fstab"
576 TEMPLATE_FILES="${TEMPLATE_FILES};${LXC_PATH}/fstab"
71d3a659
SG
577fi
578
579# Look for extra templates
580if [ -e "$(relevant_file templates)" ]; then
832cb182
CB
581 while read -r line; do
582 fullpath="${LXC_ROOTFS}/${line}"
583 [ ! -e "${fullpath}" ] && continue
584 TEMPLATE_FILES="${TEMPLATE_FILES};${fullpath}"
585 done < "$(relevant_file templates)"
71d3a659
SG
586fi
587
588# Replace variables in all templates
3f7be9d0
WG
589OLD_IFS=${IFS}
590IFS=";"
591for file in ${TEMPLATE_FILES}; do
592 [ ! -f "${file}" ] && continue
832cb182
CB
593 sed -i "s#LXC_NAME#${LXC_NAME}#g" "${file}"
594 sed -i "s#LXC_PATH#${LXC_PATH}#g" "${file}"
595 sed -i "s#LXC_ROOTFS#${LXC_ROOTFS}#g" "${file}"
596 sed -i "s#LXC_TEMPLATE_CONFIG#${LXC_TEMPLATE_CONFIG}#g" "${file}"
597 sed -i "s#LXC_HOOK_DIR#${LXC_HOOK_DIR}#g" "${file}"
71d3a659 598done
3f7be9d0 599IFS=${OLD_IFS}
71d3a659 600
491a01cf 601# prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle
3f7be9d0 602if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then
832cb182 603 sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf"
6e53ca56
SC
604fi
605
3f7be9d0 606if [ -n "${LXC_MAPPED_UID}" ] && [ "${LXC_MAPPED_UID}" != "-1" ]; then
832cb182 607 chown "${LXC_MAPPED_UID}" "${LXC_PATH}/config" "${LXC_PATH}/fstab" >/dev/null 2>&1 || :
71d3a659 608fi
832cb182 609
3f7be9d0 610if [ -n "${LXC_MAPPED_GID}" ] && [ "${LXC_MAPPED_GID}" != "-1" ]; then
832cb182 611 chgrp "${LXC_MAPPED_GID}" "${LXC_PATH}/config" "${LXC_PATH}/fstab" >/dev/null 2>&1 || :
2133f58c 612fi
71d3a659
SG
613
614if [ -e "$(relevant_file create-message)" ]; then
832cb182
CB
615 echo ""
616 echo "---"
617 cat "$(relevant_file create-message)"
71d3a659
SG
618fi
619
620exit 0