3 # Client script for LXC container images.
5 # Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
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.
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.
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
24 LOCALSTATEDIR
="@LOCALSTATEDIR@"
25 LXC_HOOK_DIR
="@LXCHOOKDIR@"
26 LXC_TEMPLATE_CONFIG
="@LXCTEMPLATECONFIG@"
31 DOWNLOAD_COMPAT_LEVEL
=1
33 DOWNLOAD_FLUSH_CACHE
="false"
34 DOWNLOAD_FORCE_CACHE
="false"
35 DOWNLOAD_INTERACTIVE
="false"
36 DOWNLOAD_KEYID
="0xBAEFF88C22F6E216"
37 DOWNLOAD_KEYSERVER
="hkp://pool.sks-keyservers.net"
38 DOWNLOAD_LIST_IMAGES
="false"
39 DOWNLOAD_MODE
="system"
40 DOWNLOAD_READY_GPG
="false"
42 DOWNLOAD_SERVER
="images.linuxcontainers.org"
43 DOWNLOAD_SHOW_GPG_WARNING
="true"
44 DOWNLOAD_SHOW_HTTP_WARNING
="true"
45 DOWNLOAD_TARGET
="system"
47 DOWNLOAD_USE_CACHE
="false"
48 DOWNLOAD_VALIDATE
="true"
49 DOWNLOAD_VARIANT
="default"
57 # Deal with GPG over http proxy
58 if [ -n "${http_proxy:-}" ]; then
59 DOWNLOAD_KEYSERVER
="hkp://p80.pool.sks-keyservers.net:80"
62 # Make sure the usual locations are in PATH
63 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
65 # Some useful functions
67 if [ -d "$DOWNLOAD_TEMP" ]; then
73 if ! wget
-T 30 -q https
://${DOWNLOAD_SERVER}/$1 -O $2 >/dev
/null
2>&1; then
74 if ! wget
-T 30 -q http
://${DOWNLOAD_SERVER}/$1 -O $2 >/dev
/null
2>&1; then
75 if [ "$3" = "noexit" ]; then
78 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
81 elif [ "$DOWNLOAD_SHOW_HTTP_WARNING" = "true" ]; then
82 DOWNLOAD_SHOW_HTTP_WARNING
="false"
83 echo "WARNING: Failed to download the file over HTTPs." 1>&2
84 echo -n " The file was instead download over HTTP. " 1>&2
85 echo "A server replay attack may be possible!" 1>&2
91 if ! download_file
$1 $2 noexit
; then
92 if [ "$DOWNLOAD_VALIDATE" = "true" ]; then
93 if [ "$3" = "normal" ]; then
94 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
106 if [ "$DOWNLOAD_VALIDATE" = "false" ]; then
110 if [ "$DOWNLOAD_READY_GPG" = "true" ]; then
114 echo "Setting up the GPG keyring"
116 mkdir
-p "$DOWNLOAD_TEMP/gpg"
117 chmod 700 "$DOWNLOAD_TEMP/gpg"
118 export GNUPGHOME
="$DOWNLOAD_TEMP/gpg"
119 if ! gpg
--keyserver $DOWNLOAD_KEYSERVER \
120 --recv-keys ${DOWNLOAD_KEYID} >/dev
/null
2>&1; then
121 echo "ERROR: Unable to fetch GPG key from keyserver."
125 DOWNLOAD_READY_GPG
="true"
129 if [ "$DOWNLOAD_VALIDATE" = "false" ]; then
130 if [ "$DOWNLOAD_SHOW_GPG_WARNING" = "true" ]; then
131 echo "WARNING: Running without gpg validation!" 1>&2
133 DOWNLOAD_SHOW_GPG_WARNING
="false"
137 if ! gpg
--verify $1 >/dev
/zero
2>&1; then
138 echo "ERROR: Invalid signature for $1" 1>&2
144 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
146 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
147 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
148 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
149 done < /proc
/self
/uid_map
151 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
152 { echo userns-root
; return; }
157 FILE_PATH
="${LXC_CACHE_PATH}/$1"
158 if [ -e "${FILE_PATH}-${DOWNLOAD_MODE}" ]; then
159 FILE_PATH
="${FILE_PATH}-${DOWNLOAD_MODE}"
161 if [ -e "$FILE_PATH.${DOWNLOAD_COMPAT_LEVEL}" ]; then
162 FILE_PATH
="${FILE_PATH}.${DOWNLOAD_COMPAT_LEVEL}"
170 LXC container image downloader
173 [ -d | --dist <distribution> ]: The name of the distribution
174 [ -r | --release <release> ]: Release name/version
175 [ -a | --arch <architecture> ]: Architecture of the container
178 [ -h | --help ]: This help message
179 [ -l | --list ]: List all available images
180 [ --variant <variant> ]: Variant of the image (default: "default")
181 [ --server <server> ]: Image server (default: "images.linuxcontainers.org")
182 [ --keyid <keyid> ]: GPG keyid (default: 0x...)
183 [ --keyserver <keyserver> ]: GPG keyserver to use
184 [ --no-validate ]: Disable GPG validation (not recommended)
185 [ --flush-cache ]: Flush the local copy (if present)
186 [ --force-cache ]; Force the use of the local copy even if expired
188 LXC internal arguments (do not pass manually!):
189 [ --name <name> ]: The container name
190 [ --path <path> ]: The path to the container
191 [ --rootfs <rootfs> ]: The path to the container's rootfs
192 [ --mapped-uid <map> ]: A uid map (user namespaces)
193 [ --mapped-gid <map> ]: A gid map (user namespaces)
198 options
=$
(getopt
-o d
:r
:a
:hl
-l dist
:,release
:,arch
:,help,list
,variant
:,\
199 server
:,keyid
:,no-validate
,flush-cache
,force-cache
,name
:,path
:,\
200 rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
202 if [ $?
-ne 0 ]; then
206 eval set -- "$options"
210 -h|
--help) usage
&& exit 1;;
211 -l|
--list) DOWNLOAD_LIST_IMAGES
="true"; shift 1;;
212 -d|
--dist) DOWNLOAD_DIST
=$2; shift 2;;
213 -r|
--release) DOWNLOAD_RELEASE
=$2; shift 2;;
214 -a|
--arch) DOWNLOAD_ARCH
=$2; shift 2;;
215 --variant) DOWNLOAD_VARIANT
=$2; shift 2;;
216 --server) DOWNLOAD_SERVER
=$2; shift 2;;
217 --keyid) DOWNLOAD_KEYID
=$2; shift 2;;
218 --no-validate) DOWNLOAD_VALIDATE
="false"; shift 1;;
219 --flush-cache) DOWNLOAD_FLUSH_CACHE
="true"; shift 1;;
220 --force-cache) DOWNLOAD_FORCE_CACHE
="true"; shift 1;;
221 --name) LXC_NAME
=$2; shift 2;;
222 --path) LXC_PATH
=$2; shift 2;;
223 --rootfs) LXC_ROOTFS
=$2; shift 2;;
224 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
225 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
230 # Check for required binaries
231 for bin
in tar xz wget
; do
232 if ! type $bin >/dev
/null
2>&1; then
233 echo "ERROR: Missing required tool: $bin" 1>&2
239 if [ "$DOWNLOAD_VALIDATE" = "true" ]; then
240 if ! type gpg
>/dev
/null
2>&1; then
241 echo "ERROR: Missing recommended tool: gpg" 1>&2
242 echo "You can workaround this by using --no-validate." 1>&2
247 # Check that we have all variables we need
248 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
249 echo "ERROR: Not running through LXC." 1>&2
255 if [ "$USERNS" != "no" ]; then
256 if [ "$USERNS" = "yes" ]; then
257 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
258 echo "ERROR: In a user namespace without a map." 1>&2
262 DOWNLOAD_TARGET
="user"
265 DOWNLOAD_TARGET
="system"
269 if [ -z "$DOWNLOAD_DIST" ] ||
[ -z "$DOWNLOAD_RELEASE" ] || \
270 [ -z "$DOWNLOAD_ARCH" ]; then
271 DOWNLOAD_INTERACTIVE
="true"
274 # Trap all exit signals
275 trap cleanup EXIT HUP INT TERM
277 if ! type mktemp
>/dev
/null
2>&1; then
278 DOWNLOAD_TEMP
=/tmp
/lxc-download.$$
279 mkdir
-p $DOWNLOAD_TEMP
281 DOWNLOAD_TEMP
=$
(mktemp
-d)
285 if [ "$DOWNLOAD_LIST_IMAGES" = "true" ] || \
286 [ "$DOWNLOAD_INTERACTIVE" = "true" ]; then
291 DOWNLOAD_INDEX_PATH
=/meta
/1.0/index-
${DOWNLOAD_MODE}
293 echo "Downloading the image index"
294 if ! download_file
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL} \
295 ${DOWNLOAD_TEMP}/index noexit ||
296 ! download_sig
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL}.asc \
297 ${DOWNLOAD_TEMP}/index.asc noexit
; then
298 download_file
${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
299 download_sig
${DOWNLOAD_INDEX_PATH}.asc \
300 ${DOWNLOAD_TEMP}/index.asc normal
303 gpg_validate
${DOWNLOAD_TEMP}/index.asc
308 printf "DIST\tRELEASE\tARCH\tVARIANT\tBUILD\n"
317 [ -n "$DOWNLOAD_DIST" ] && [ "$1" != "$DOWNLOAD_DIST" ] && continue
318 [ -n "$DOWNLOAD_RELEASE" ] && [ "$2" != "$DOWNLOAD_RELEASE" ] && continue
319 [ -n "$DOWNLOAD_ARCH" ] && [ "$3" != "$DOWNLOAD_ARCH" ] && continue
320 [ -n "$DOWNLOAD_VARIANT" ] && [ "$4" != "$DOWNLOAD_VARIANT" ] && continue
321 [ -z "$5" ] ||
[ -z "$6" ] && continue
323 printf "$1\t$2\t$3\t$4\t$5\n"
324 done < ${DOWNLOAD_TEMP}/index
327 if [ "$DOWNLOAD_LIST_IMAGES" = "true" ]; then
334 if [ -z "$DOWNLOAD_DIST" ]; then
335 echo -n "Distribution: "
339 if [ -z "$DOWNLOAD_RELEASE" ]; then
341 read DOWNLOAD_RELEASE
344 if [ -z "$DOWNLOAD_ARCH" ]; then
345 echo -n "Architecture: "
353 if [ "$DOWNLOAD_TARGET" = "system" ]; then
354 LXC_CACHE_BASE
="$LOCALSTATEDIR/cache/lxc/"
356 LXC_CACHE_BASE
="$HOME/.cache/lxc/"
359 LXC_CACHE_PATH
="$LXC_CACHE_BASE/download/$DOWNLOAD_DIST"
360 LXC_CACHE_PATH
="$LXC_CACHE_PATH/$DOWNLOAD_RELEASE/$DOWNLOAD_ARCH/"
361 LXC_CACHE_PATH
="$LXC_CACHE_PATH/$DOWNLOAD_VARIANT"
363 if [ -d "$LXC_CACHE_PATH" ]; then
364 if [ "$DOWNLOAD_FLUSH_CACHE" = "true" ]; then
365 echo "Flushing the cache..."
366 rm -Rf $LXC_CACHE_PATH
367 elif [ "$DOWNLOAD_FORCE_CACHE" = "true" ]; then
368 DOWNLOAD_USE_CACHE
="true"
370 DOWNLOAD_USE_CACHE
="true"
371 if [ -e "$(relevant_file expiry)" ]; then
372 if [ "$(cat $(relevant_file expiry))" -lt $
(date +%s
) ]; then
373 echo "The cached copy has expired, re-downloading..."
374 DOWNLOAD_USE_CACHE
="false"
380 # Download what's needed
381 if [ "$DOWNLOAD_USE_CACHE" = "false" ]; then
386 DOWNLOAD_INDEX_PATH
=/meta
/1.0/index-
${DOWNLOAD_MODE}
388 echo "Downloading the image index"
389 if ! download_file
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL} \
390 ${DOWNLOAD_TEMP}/index noexit ||
391 ! download_sig
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL}.asc \
392 ${DOWNLOAD_TEMP}/index.asc noexit
; then
393 download_file
${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
394 download_sig
${DOWNLOAD_INDEX_PATH}.asc \
395 ${DOWNLOAD_TEMP}/index.asc normal
398 gpg_validate
${DOWNLOAD_TEMP}/index.asc
408 if [ "$1" != "$DOWNLOAD_DIST" ] || \
409 [ "$2" != "$DOWNLOAD_RELEASE" ] || \
410 [ "$3" != "$DOWNLOAD_ARCH" ] || \
411 [ "$4" != "$DOWNLOAD_VARIANT" ] || \
419 done < ${DOWNLOAD_TEMP}/index
421 if [ -z "$DOWNLOAD_URL" ]; then
422 echo "ERROR: Couldn't find a matching image." 1>&1
426 if [ -d "$LXC_CACHE_PATH" ] && [ -f "$LXC_CACHE_PATH/build_id" ] && \
427 [ "$(cat $LXC_CACHE_PATH/build_id)" = "$DOWNLOAD_BUILD" ]; then
428 echo "The cache is already up to date."
429 echo "Using image from local cache"
431 # Download the actual files
432 echo "Downloading the rootfs"
433 download_file
$DOWNLOAD_URL/rootfs.
tar.xz \
434 ${DOWNLOAD_TEMP}/rootfs.
tar.xz normal
435 download_sig
$DOWNLOAD_URL/rootfs.
tar.xz.asc \
436 ${DOWNLOAD_TEMP}/rootfs.
tar.xz.asc normal
437 gpg_validate
${DOWNLOAD_TEMP}/rootfs.
tar.xz.asc
439 echo "Downloading the metadata"
440 download_file
$DOWNLOAD_URL/meta.
tar.xz \
441 ${DOWNLOAD_TEMP}/meta.
tar.xz normal
442 download_sig
$DOWNLOAD_URL/meta.
tar.xz.asc \
443 ${DOWNLOAD_TEMP}/meta.
tar.xz.asc normal
444 gpg_validate
${DOWNLOAD_TEMP}/meta.
tar.xz.asc
446 if [ -d $LXC_CACHE_PATH ]; then
447 rm -Rf $LXC_CACHE_PATH
449 mkdir
-p $LXC_CACHE_PATH
450 mv ${DOWNLOAD_TEMP}/rootfs.
tar.xz
$LXC_CACHE_PATH
451 if ! tar Jxf
${DOWNLOAD_TEMP}/meta.
tar.xz
-C $LXC_CACHE_PATH; then
452 echo "ERROR: Invalid rootfs tarball." 2>&1
456 echo $DOWNLOAD_BUILD > $LXC_CACHE_PATH/build_id
458 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
459 chown
-R $LXC_MAPPED_UID $LXC_CACHE_BASE >/dev
/null
2>&1 || true
461 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
462 chgrp
-R $LXC_MAPPED_GID $LXC_CACHE_BASE >/dev
/null
2>&1 || true
464 echo "The image cache is now ready"
467 echo "Using image from local cache"
471 echo "Unpacking the rootfs"
474 excludelist
=$
(relevant_file excludes
)
475 if [ -f "${excludelist}" ]; then
477 EXCLUDES
="$EXCLUDES --exclude=$line"
481 tar --anchored ${EXCLUDES} --numeric-owner -xpJf \
482 ${LXC_CACHE_PATH}/rootfs.
tar.xz
-C ${LXC_ROOTFS}
484 mkdir
-p ${LXC_ROOTFS}/dev
/pts
/
486 # Setup the configuration
487 configfile
=$
(relevant_file config
)
488 fstab
=$
(relevant_file fstab
)
489 if [ ! -e $configfile ]; then
490 echo "ERROR: meta tarball is missing the configuration file" 1>&2
494 ## Extract all the network config entries
495 sed -i -e "/lxc.network/{w ${LXC_PATH}/config-network" -e "d}" \
498 ## Extract any other config entry
499 sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" ${LXC_PATH}/config
501 ## Append the defaults
502 echo "" >> ${LXC_PATH}/config
503 echo "# Distribution configuration" >> ${LXC_PATH}/config
504 cat $configfile >> ${LXC_PATH}/config
506 ## Add the container-specific config
507 echo "" >> ${LXC_PATH}/config
508 echo "# Container specific configuration" >> ${LXC_PATH}/config
509 if [ -e "${LXC_PATH}/config-auto" ]; then
510 cat ${LXC_PATH}/config-auto
>> ${LXC_PATH}/config
511 rm ${LXC_PATH}/config-auto
513 if [ -e "$fstab" ]; then
514 echo "lxc.mount = ${LXC_PATH}/fstab" >> ${LXC_PATH}/config
516 echo "lxc.utsname = ${LXC_NAME}" >> ${LXC_PATH}/config
518 ## Re-add the previously removed network config
519 if [ -e "${LXC_PATH}/config-network" ]; then
520 echo "" >> ${LXC_PATH}/config
521 echo "# Network configuration" >> ${LXC_PATH}/config
522 cat ${LXC_PATH}/config-network
>> ${LXC_PATH}/config
523 rm ${LXC_PATH}/config-network
526 TEMPLATE_FILES
="${LXC_PATH}/config"
529 if [ -e $fstab ]; then
530 cp ${fstab} ${LXC_PATH}/fstab
531 TEMPLATE_FILES
="$TEMPLATE_FILES ${LXC_PATH}/fstab"
534 # Look for extra templates
535 if [ -e "$(relevant_file templates)" ]; then
537 fullpath
=${LXC_ROOTFS}/$line
538 [ ! -e "$fullpath" ] && continue
539 TEMPLATE_FILES
="$TEMPLATE_FILES $fullpath"
540 done < $
(relevant_file templates
)
543 # Replace variables in all templates
544 for file in $TEMPLATE_FILES; do
545 [ ! -f "$file" ] && continue
547 sed -i "s#LXC_NAME#$LXC_NAME#g" $file
548 sed -i "s#LXC_PATH#$LXC_PATH#g" $file
549 sed -i "s#LXC_ROOTFS#$LXC_ROOTFS#g" $file
550 sed -i "s#LXC_TEMPLATE_CONFIG#$LXC_TEMPLATE_CONFIG#g" $file
551 sed -i "s#LXC_HOOK_DIR#$LXC_HOOK_DIR#g" $file
554 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
555 chown
$LXC_MAPPED_UID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
557 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
558 chgrp
$LXC_MAPPED_GID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
561 if [ -e "$(relevant_file create-message)" ]; then
564 cat "$(relevant_file create-message)"