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