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
=4
33 DOWNLOAD_FLUSH_CACHE
="false"
34 DOWNLOAD_FORCE_CACHE
="false"
35 DOWNLOAD_INTERACTIVE
="false"
36 DOWNLOAD_KEYID
="0xE7FB0CAEC8173D669066514CBAEFF88C22F6E216"
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
83 if ! wget_wrapper
-T 30 -q https
://${DOWNLOAD_SERVER}/$1 -O $2 >/dev
/null
2>&1; then
84 if ! wget_wrapper
-T 30 -q http
://${DOWNLOAD_SERVER}/$1 -O $2 >/dev
/null
2>&1; then
85 if [ "$3" = "noexit" ]; then
88 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
91 elif [ "$DOWNLOAD_SHOW_HTTP_WARNING" = "true" ]; then
92 DOWNLOAD_SHOW_HTTP_WARNING
="false"
93 echo "WARNING: Failed to download the file over HTTPs." 1>&2
94 echo -n " The file was instead download over HTTP. " 1>&2
95 echo "A server replay attack may be possible!" 1>&2
101 if ! download_file
$1 $2 noexit
; then
102 if [ "$DOWNLOAD_VALIDATE" = "true" ]; then
103 if [ "$3" = "normal" ]; then
104 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
116 if [ "$DOWNLOAD_VALIDATE" = "false" ]; then
120 if [ "$DOWNLOAD_READY_GPG" = "true" ]; then
124 echo "Setting up the GPG keyring"
126 mkdir
-p "$DOWNLOAD_TEMP/gpg"
127 chmod 700 "$DOWNLOAD_TEMP/gpg"
128 export GNUPGHOME
="$DOWNLOAD_TEMP/gpg"
131 for i
in $
(seq 3); do
132 if gpg
--keyserver $DOWNLOAD_KEYSERVER \
133 --recv-keys ${DOWNLOAD_KEYID} >/dev
/null
2>&1; then
139 if [ -z "$success" ]; then
140 echo "ERROR: Unable to fetch GPG key from keyserver."
144 DOWNLOAD_READY_GPG
="true"
148 if [ "$DOWNLOAD_VALIDATE" = "false" ]; then
149 if [ "$DOWNLOAD_SHOW_GPG_WARNING" = "true" ]; then
150 echo "WARNING: Running without gpg validation!" 1>&2
152 DOWNLOAD_SHOW_GPG_WARNING
="false"
156 if ! gpg
--verify $1 >/dev
/zero
2>&1; then
157 echo "ERROR: Invalid signature for $1" 1>&2
163 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
165 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
166 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
167 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
168 done < /proc
/self
/uid_map
170 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
171 { echo userns-root
; return; }
176 FILE_PATH
="${LXC_CACHE_PATH}/$1"
177 if [ -e "${FILE_PATH}-${DOWNLOAD_MODE}" ]; then
178 FILE_PATH
="${FILE_PATH}-${DOWNLOAD_MODE}"
180 if [ -e "$FILE_PATH.${DOWNLOAD_COMPAT_LEVEL}" ]; then
181 FILE_PATH
="${FILE_PATH}.${DOWNLOAD_COMPAT_LEVEL}"
189 LXC container image downloader
192 [ -h | --help ]: Print this help message and exit.
193 [ -l | --list ]: List all available images and exit.
196 [ -d | --dist <distribution> ]: The name of the distribution
197 [ -r | --release <release> ]: Release name/version
198 [ -a | --arch <architecture> ]: Architecture of the container
201 [ --variant <variant> ]: Variant of the image (default: "default")
202 [ --server <server> ]: Image server (default: "images.linuxcontainers.org")
203 [ --keyid <keyid> ]: GPG keyid (default: 0x...)
204 [ --keyserver <keyserver> ]: GPG keyserver to use
205 [ --no-validate ]: Disable GPG validation (not recommended)
206 [ --flush-cache ]: Flush the local copy (if present)
207 [ --force-cache ]: Force the use of the local copy even if expired
209 LXC internal arguments (do not pass manually!):
210 [ --name <name> ]: The container name
211 [ --path <path> ]: The path to the container
212 [ --rootfs <rootfs> ]: The path to the container's rootfs
213 [ --mapped-uid <map> ]: A uid map (user namespaces)
214 [ --mapped-gid <map> ]: A gid map (user namespaces)
219 options
=$
(getopt
-o d
:r
:a
:hl
-l dist
:,release
:,arch
:,help,list
,variant
:,\
220 server
:,keyid
:,keyserver
:,no-validate
,flush-cache
,force-cache
,name
:,path
:,\
221 rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
223 if [ $?
-ne 0 ]; then
227 eval set -- "$options"
231 -h|
--help) usage
&& exit 1;;
232 -l|
--list) DOWNLOAD_LIST_IMAGES
="true"; shift 1;;
233 -d|
--dist) DOWNLOAD_DIST
=$2; shift 2;;
234 -r|
--release) DOWNLOAD_RELEASE
=$2; shift 2;;
235 -a|
--arch) DOWNLOAD_ARCH
=$2; shift 2;;
236 --variant) DOWNLOAD_VARIANT
=$2; shift 2;;
237 --server) DOWNLOAD_SERVER
=$2; shift 2;;
238 --keyid) DOWNLOAD_KEYID
=$2; shift 2;;
239 --keyserver) DOWNLOAD_KEYSERVER
=$2; shift 2;;
240 --no-validate) DOWNLOAD_VALIDATE
="false"; shift 1;;
241 --flush-cache) DOWNLOAD_FLUSH_CACHE
="true"; shift 1;;
242 --force-cache) DOWNLOAD_FORCE_CACHE
="true"; shift 1;;
243 --name) LXC_NAME
=$2; shift 2;;
244 --path) LXC_PATH
=$2; shift 2;;
245 --rootfs) LXC_ROOTFS
=$2; shift 2;;
246 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
247 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
252 # Check for required binaries
253 for bin
in tar xz wget
; do
254 if ! type $bin >/dev
/null
2>&1; then
255 echo "ERROR: Missing required tool: $bin" 1>&2
261 if [ "$DOWNLOAD_VALIDATE" = "true" ]; then
262 if ! type gpg
>/dev
/null
2>&1; then
263 echo "ERROR: Missing recommended tool: gpg" 1>&2
264 echo "You can workaround this by using --no-validate." 1>&2
269 # Check that we have all variables we need
270 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
271 if [ "$DOWNLOAD_LIST_IMAGES" != "true" ]; then
272 echo "ERROR: Not running through LXC." 1>&2
279 if [ "$USERNS" != "no" ]; then
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
286 DOWNLOAD_TARGET
="user"
289 DOWNLOAD_TARGET
="system"
293 if [ -z "$DOWNLOAD_DIST" ] ||
[ -z "$DOWNLOAD_RELEASE" ] || \
294 [ -z "$DOWNLOAD_ARCH" ]; then
295 DOWNLOAD_INTERACTIVE
="true"
298 # Trap all exit signals
299 trap cleanup EXIT HUP INT TERM
301 if ! type mktemp
>/dev
/null
2>&1; then
302 DOWNLOAD_TEMP
=/tmp
/lxc-download.$$
303 mkdir
-p $DOWNLOAD_TEMP
305 DOWNLOAD_TEMP
=$
(mktemp
-d)
309 if [ "$DOWNLOAD_LIST_IMAGES" = "true" ] || \
310 [ "$DOWNLOAD_INTERACTIVE" = "true" ]; then
315 DOWNLOAD_INDEX_PATH
=/meta
/1.0/index-
${DOWNLOAD_MODE}
317 echo "Downloading the image index"
318 if ! download_file
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL} \
319 ${DOWNLOAD_TEMP}/index noexit ||
320 ! download_sig
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL}.asc \
321 ${DOWNLOAD_TEMP}/index.asc noexit
; then
322 download_file
${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
323 download_sig
${DOWNLOAD_INDEX_PATH}.asc \
324 ${DOWNLOAD_TEMP}/index.asc normal
327 gpg_validate
${DOWNLOAD_TEMP}/index.asc
332 printf "DIST\tRELEASE\tARCH\tVARIANT\tBUILD\n"
341 [ -n "$DOWNLOAD_DIST" ] && [ "$1" != "$DOWNLOAD_DIST" ] && continue
342 [ -n "$DOWNLOAD_RELEASE" ] && [ "$2" != "$DOWNLOAD_RELEASE" ] && continue
343 [ -n "$DOWNLOAD_ARCH" ] && [ "$3" != "$DOWNLOAD_ARCH" ] && continue
344 [ -n "$DOWNLOAD_VARIANT" ] && [ "$4" != "$DOWNLOAD_VARIANT" ] && continue
345 [ -z "$5" ] ||
[ -z "$6" ] && continue
347 printf "$1\t$2\t$3\t$4\t$5\n"
348 done < ${DOWNLOAD_TEMP}/index
351 if [ "$DOWNLOAD_LIST_IMAGES" = "true" ]; then
358 if [ -z "$DOWNLOAD_DIST" ]; then
359 echo -n "Distribution: "
363 if [ -z "$DOWNLOAD_RELEASE" ]; then
365 read DOWNLOAD_RELEASE
368 if [ -z "$DOWNLOAD_ARCH" ]; then
369 echo -n "Architecture: "
377 if [ "$DOWNLOAD_TARGET" = "system" ]; then
378 LXC_CACHE_BASE
="$LOCALSTATEDIR/cache/lxc/"
380 LXC_CACHE_BASE
="$HOME/.cache/lxc/"
383 # Allow the setting of the LXC_CACHE_PATH with the usage of environment variables.
384 LXC_CACHE_PATH
=${LXC_CACHE_PATH:-"$LXC_CACHE_BASE"}
385 LXC_CACHE_PATH
=$LXC_CACHE_PATH/download
/$DOWNLOAD_DIST
386 LXC_CACHE_PATH
="$LXC_CACHE_PATH/$DOWNLOAD_RELEASE/$DOWNLOAD_ARCH/"
387 LXC_CACHE_PATH
="$LXC_CACHE_PATH/$DOWNLOAD_VARIANT"
389 if [ -d "$LXC_CACHE_PATH" ]; then
390 if [ "$DOWNLOAD_FLUSH_CACHE" = "true" ]; then
391 echo "Flushing the cache..."
392 rm -Rf $LXC_CACHE_PATH
393 elif [ "$DOWNLOAD_FORCE_CACHE" = "true" ]; then
394 DOWNLOAD_USE_CACHE
="true"
396 DOWNLOAD_USE_CACHE
="true"
397 if [ -e "$(relevant_file expiry)" ]; then
398 if [ "$(cat $(relevant_file expiry))" -lt $
(date +%s
) ]; then
399 echo "The cached copy has expired, re-downloading..."
400 DOWNLOAD_USE_CACHE
="false"
406 # Download what's needed
407 if [ "$DOWNLOAD_USE_CACHE" = "false" ]; then
412 DOWNLOAD_INDEX_PATH
=/meta
/1.0/index-
${DOWNLOAD_MODE}
414 echo "Downloading the image index"
415 if ! download_file
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL} \
416 ${DOWNLOAD_TEMP}/index noexit ||
417 ! download_sig
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL}.asc \
418 ${DOWNLOAD_TEMP}/index.asc noexit
; then
419 download_file
${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
420 download_sig
${DOWNLOAD_INDEX_PATH}.asc \
421 ${DOWNLOAD_TEMP}/index.asc normal
424 gpg_validate
${DOWNLOAD_TEMP}/index.asc
434 if [ "$1" != "$DOWNLOAD_DIST" ] || \
435 [ "$2" != "$DOWNLOAD_RELEASE" ] || \
436 [ "$3" != "$DOWNLOAD_ARCH" ] || \
437 [ "$4" != "$DOWNLOAD_VARIANT" ] || \
445 done < ${DOWNLOAD_TEMP}/index
447 if [ -z "$DOWNLOAD_URL" ]; then
448 echo "ERROR: Couldn't find a matching image." 1>&1
452 if [ -d "$LXC_CACHE_PATH" ] && [ -f "$LXC_CACHE_PATH/build_id" ] && \
453 [ "$(cat $LXC_CACHE_PATH/build_id)" = "$DOWNLOAD_BUILD" ]; then
454 echo "The cache is already up to date."
455 echo "Using image from local cache"
457 # Download the actual files
458 echo "Downloading the rootfs"
459 download_file
$DOWNLOAD_URL/rootfs.
tar.xz \
460 ${DOWNLOAD_TEMP}/rootfs.
tar.xz normal
461 download_sig
$DOWNLOAD_URL/rootfs.
tar.xz.asc \
462 ${DOWNLOAD_TEMP}/rootfs.
tar.xz.asc normal
463 gpg_validate
${DOWNLOAD_TEMP}/rootfs.
tar.xz.asc
465 echo "Downloading the metadata"
466 download_file
$DOWNLOAD_URL/meta.
tar.xz \
467 ${DOWNLOAD_TEMP}/meta.
tar.xz normal
468 download_sig
$DOWNLOAD_URL/meta.
tar.xz.asc \
469 ${DOWNLOAD_TEMP}/meta.
tar.xz.asc normal
470 gpg_validate
${DOWNLOAD_TEMP}/meta.
tar.xz.asc
472 if [ -d $LXC_CACHE_PATH ]; then
473 rm -Rf $LXC_CACHE_PATH
475 mkdir
-p $LXC_CACHE_PATH
476 mv ${DOWNLOAD_TEMP}/rootfs.
tar.xz
$LXC_CACHE_PATH
477 if ! tar Jxf
${DOWNLOAD_TEMP}/meta.
tar.xz
-C $LXC_CACHE_PATH; then
478 echo "ERROR: Invalid rootfs tarball." 2>&1
482 echo $DOWNLOAD_BUILD > $LXC_CACHE_PATH/build_id
484 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
485 chown
-R $LXC_MAPPED_UID $LXC_CACHE_BASE >/dev
/null
2>&1 || true
487 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
488 chgrp
-R $LXC_MAPPED_GID $LXC_CACHE_BASE >/dev
/null
2>&1 || true
490 echo "The image cache is now ready"
493 echo "Using image from local cache"
497 echo "Unpacking the rootfs"
500 excludelist
=$
(relevant_file excludes
)
501 if [ -f "${excludelist}" ]; then
503 EXCLUDES
="$EXCLUDES --exclude=$line"
507 tar --anchored ${EXCLUDES} --numeric-owner -xpJf \
508 ${LXC_CACHE_PATH}/rootfs.
tar.xz
-C ${LXC_ROOTFS}
510 mkdir
-p ${LXC_ROOTFS}/dev
/pts
/
512 # Setup the configuration
513 configfile
=$
(relevant_file config
)
514 fstab
=$
(relevant_file fstab
)
515 if [ ! -e $configfile ]; then
516 echo "ERROR: meta tarball is missing the configuration file" 1>&2
520 ## Extract all the network config entries
521 sed -i -e "/lxc.network/{w ${LXC_PATH}/config-network" -e "d}" \
524 ## Extract any other config entry
525 sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" ${LXC_PATH}/config
527 ## Append the defaults
528 echo "" >> ${LXC_PATH}/config
529 echo "# Distribution configuration" >> ${LXC_PATH}/config
530 cat $configfile >> ${LXC_PATH}/config
532 ## Add the container-specific config
533 echo "" >> ${LXC_PATH}/config
534 echo "# Container specific configuration" >> ${LXC_PATH}/config
535 if [ -e "${LXC_PATH}/config-auto" ]; then
536 cat ${LXC_PATH}/config-auto
>> ${LXC_PATH}/config
537 rm ${LXC_PATH}/config-auto
539 if [ -e "$fstab" ]; then
540 echo "lxc.mount = ${LXC_PATH}/fstab" >> ${LXC_PATH}/config
542 echo "lxc.utsname = ${LXC_NAME}" >> ${LXC_PATH}/config
544 ## Re-add the previously removed network config
545 if [ -e "${LXC_PATH}/config-network" ]; then
546 echo "" >> ${LXC_PATH}/config
547 echo "# Network configuration" >> ${LXC_PATH}/config
548 cat ${LXC_PATH}/config-network
>> ${LXC_PATH}/config
549 rm ${LXC_PATH}/config-network
552 TEMPLATE_FILES
="${LXC_PATH}/config"
555 if [ -e $fstab ]; then
556 cp ${fstab} ${LXC_PATH}/fstab
557 TEMPLATE_FILES
="$TEMPLATE_FILES ${LXC_PATH}/fstab"
560 # Look for extra templates
561 if [ -e "$(relevant_file templates)" ]; then
563 fullpath
=${LXC_ROOTFS}/$line
564 [ ! -e "$fullpath" ] && continue
565 TEMPLATE_FILES
="$TEMPLATE_FILES $fullpath"
566 done < $
(relevant_file templates
)
569 # Replace variables in all templates
570 for file in $TEMPLATE_FILES; do
571 [ ! -f "$file" ] && continue
573 sed -i "s#LXC_NAME#$LXC_NAME#g" $file
574 sed -i "s#LXC_PATH#$LXC_PATH#g" $file
575 sed -i "s#LXC_ROOTFS#$LXC_ROOTFS#g" $file
576 sed -i "s#LXC_TEMPLATE_CONFIG#$LXC_TEMPLATE_CONFIG#g" $file
577 sed -i "s#LXC_HOOK_DIR#$LXC_HOOK_DIR#g" $file
580 # prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle
581 if [ -f ${LXC_ROOTFS}/etc
/init
/tty.conf
]; then
582 sed -i 's|mingetty|mingetty --nohangup|' ${LXC_ROOTFS}/etc
/init
/tty.conf
585 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
586 chown
$LXC_MAPPED_UID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
588 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
589 chgrp
$LXC_MAPPED_GID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
592 if [ -e "$(relevant_file create-message)" ]; then
595 cat "$(relevant_file create-message)"