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_LIST_IMAGES
="false"
38 DOWNLOAD_MODE
="system"
39 DOWNLOAD_READY_GPG
="false"
41 DOWNLOAD_SERVER
="images.linuxcontainers.org"
42 DOWNLOAD_SHOW_GPG_WARNING
="true"
43 DOWNLOAD_SHOW_HTTP_WARNING
="true"
44 DOWNLOAD_TARGET
="system"
46 DOWNLOAD_USE_CACHE
="false"
47 DOWNLOAD_VALIDATE
="true"
48 DOWNLOAD_VARIANT
="default"
56 if [ -z "${DOWNLOAD_KEYSERVER:-}" ]; then
57 DOWNLOAD_KEYSERVER
="hkp://pool.sks-keyservers.net"
59 # Deal with GPG over http proxy
60 if [ -n "${http_proxy:-}" ]; then
61 DOWNLOAD_KEYSERVER
="hkp://p80.pool.sks-keyservers.net:80"
65 # Make sure the usual locations are in PATH
66 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
68 # Some useful functions
70 if [ -d "$DOWNLOAD_TEMP" ]; then
86 if ! wget_wrapper
-T 30 -q https
://${DOWNLOAD_SERVER}/$1 -O $2 >/dev
/null
2>&1; then
87 if ! wget_wrapper
-T 30 -q http
://${DOWNLOAD_SERVER}/$1 -O $2 >/dev
/null
2>&1; then
88 if [ "$3" = "noexit" ]; then
91 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
94 elif [ "$DOWNLOAD_SHOW_HTTP_WARNING" = "true" ]; then
95 DOWNLOAD_SHOW_HTTP_WARNING
="false"
96 echo "WARNING: Failed to download the file over HTTPs." 1>&2
97 echo -n " The file was instead download over HTTP. " 1>&2
98 echo "A server replay attack may be possible!" 1>&2
104 if ! download_file
$1 $2 noexit
; then
105 if [ "$DOWNLOAD_VALIDATE" = "true" ]; then
106 if [ "$3" = "normal" ]; then
107 echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
119 if [ "$DOWNLOAD_VALIDATE" = "false" ]; then
123 if [ "$DOWNLOAD_READY_GPG" = "true" ]; then
127 echo "Setting up the GPG keyring"
129 mkdir
-p "$DOWNLOAD_TEMP/gpg"
130 chmod 700 "$DOWNLOAD_TEMP/gpg"
131 export GNUPGHOME
="$DOWNLOAD_TEMP/gpg"
134 for i
in $
(seq 3); do
135 if gpg
--keyserver $DOWNLOAD_KEYSERVER \
136 --recv-keys ${DOWNLOAD_KEYID} >/dev
/null
2>&1; then
142 if [ -z "$success" ]; then
143 echo "ERROR: Unable to fetch GPG key from keyserver."
147 DOWNLOAD_READY_GPG
="true"
151 if [ "$DOWNLOAD_VALIDATE" = "false" ]; then
152 if [ "$DOWNLOAD_SHOW_GPG_WARNING" = "true" ]; then
153 echo "WARNING: Running without gpg validation!" 1>&2
155 DOWNLOAD_SHOW_GPG_WARNING
="false"
159 if ! gpg
--verify $1 >/dev
/zero
2>&1; then
160 echo "ERROR: Invalid signature for $1" 1>&2
166 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
168 fields
=$
(echo $line |
awk '{ print $1 " " $2 " " $3 }')
169 [ "$fields" = "0 0 4294967295" ] && { echo no
; return; } || true
170 echo $fields |
grep -q " 0 1$" && { echo userns-root
; return; } || true
171 done < /proc
/self
/uid_map
173 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
174 { echo userns-root
; return; }
179 FILE_PATH
="${LXC_CACHE_PATH}/$1"
180 if [ -e "${FILE_PATH}-${DOWNLOAD_MODE}" ]; then
181 FILE_PATH
="${FILE_PATH}-${DOWNLOAD_MODE}"
183 if [ -e "$FILE_PATH.${DOWNLOAD_COMPAT_LEVEL}" ]; then
184 FILE_PATH
="${FILE_PATH}.${DOWNLOAD_COMPAT_LEVEL}"
192 LXC container image downloader
195 [ -h | --help ]: Print this help message and exit.
196 [ -l | --list ]: List all available images and exit.
199 [ -d | --dist <distribution> ]: The name of the distribution
200 [ -r | --release <release> ]: Release name/version
201 [ -a | --arch <architecture> ]: Architecture of the container
204 [ --variant <variant> ]: Variant of the image (default: "default")
205 [ --server <server> ]: Image server (default: "images.linuxcontainers.org")
206 [ --keyid <keyid> ]: GPG keyid (default: 0x...)
207 [ --keyserver <keyserver> ]: GPG keyserver to use. Environment variable: DOWNLOAD_KEYSERVER
208 [ --no-validate ]: Disable GPG validation (not recommended)
209 [ --flush-cache ]: Flush the local copy (if present)
210 [ --force-cache ]: Force the use of the local copy even if expired
212 LXC internal arguments (do not pass manually!):
213 [ --name <name> ]: The container name
214 [ --path <path> ]: The path to the container
215 [ --rootfs <rootfs> ]: The path to the container's rootfs
216 [ --mapped-uid <map> ]: A uid map (user namespaces)
217 [ --mapped-gid <map> ]: A gid map (user namespaces)
219 Environment Variables:
220 DOWNLOAD_KEYSERVER : The URL of the key server to use, instead of the default.
221 Can be further overridden by using optional argument --keyserver
227 options
=$
(getopt
-o d
:r
:a
:hl
-l dist
:,release
:,arch
:,help,list
,variant
:,\
228 server
:,keyid
:,keyserver
:,no-validate
,flush-cache
,force-cache
,name
:,path
:,\
229 rootfs
:,mapped-uid
:,mapped-gid
: -- "$@")
231 if [ $?
-ne 0 ]; then
235 eval set -- "$options"
239 -h|
--help) usage
&& exit 1;;
240 -l|
--list) DOWNLOAD_LIST_IMAGES
="true"; shift 1;;
241 -d|
--dist) DOWNLOAD_DIST
=$2; shift 2;;
242 -r|
--release) DOWNLOAD_RELEASE
=$2; shift 2;;
243 -a|
--arch) DOWNLOAD_ARCH
=$2; shift 2;;
244 --variant) DOWNLOAD_VARIANT
=$2; shift 2;;
245 --server) DOWNLOAD_SERVER
=$2; shift 2;;
246 --keyid) DOWNLOAD_KEYID
=$2; shift 2;;
247 --keyserver) DOWNLOAD_KEYSERVER
=$2; shift 2;;
248 --no-validate) DOWNLOAD_VALIDATE
="false"; shift 1;;
249 --flush-cache) DOWNLOAD_FLUSH_CACHE
="true"; shift 1;;
250 --force-cache) DOWNLOAD_FORCE_CACHE
="true"; shift 1;;
251 --name) LXC_NAME
=$2; shift 2;;
252 --path) LXC_PATH
=$2; shift 2;;
253 --rootfs) LXC_ROOTFS
=$2; shift 2;;
254 --mapped-uid) LXC_MAPPED_UID
=$2; shift 2;;
255 --mapped-gid) LXC_MAPPED_GID
=$2; shift 2;;
260 # Check for required binaries
261 for bin
in tar xz wget
; do
262 if ! type $bin >/dev
/null
2>&1; then
263 echo "ERROR: Missing required tool: $bin" 1>&2
269 if [ "$DOWNLOAD_VALIDATE" = "true" ]; then
270 if ! type gpg
>/dev
/null
2>&1; then
271 echo "ERROR: Missing recommended tool: gpg" 1>&2
272 echo "You can workaround this by using --no-validate." 1>&2
277 # Check that we have all variables we need
278 if [ -z "$LXC_NAME" ] ||
[ -z "$LXC_PATH" ] ||
[ -z "$LXC_ROOTFS" ]; then
279 if [ "$DOWNLOAD_LIST_IMAGES" != "true" ]; then
280 echo "ERROR: Not running through LXC." 1>&2
287 if [ "$USERNS" != "no" ]; then
288 if [ "$USERNS" = "yes" ]; then
289 if [ -z "$LXC_MAPPED_UID" ] ||
[ "$LXC_MAPPED_UID" = "-1" ]; then
290 echo "ERROR: In a user namespace without a map." 1>&2
294 DOWNLOAD_TARGET
="user"
297 DOWNLOAD_TARGET
="system"
301 if [ -z "$DOWNLOAD_DIST" ] ||
[ -z "$DOWNLOAD_RELEASE" ] || \
302 [ -z "$DOWNLOAD_ARCH" ]; then
303 DOWNLOAD_INTERACTIVE
="true"
306 # Trap all exit signals
307 trap cleanup EXIT HUP INT TERM
309 if ! type mktemp
>/dev
/null
2>&1; then
310 DOWNLOAD_TEMP
=/tmp
/lxc-download.$$
311 mkdir
-p $DOWNLOAD_TEMP
313 DOWNLOAD_TEMP
=$
(mktemp
-d)
317 if [ "$DOWNLOAD_LIST_IMAGES" = "true" ] || \
318 [ "$DOWNLOAD_INTERACTIVE" = "true" ]; then
323 DOWNLOAD_INDEX_PATH
=/meta
/1.0/index-
${DOWNLOAD_MODE}
325 echo "Downloading the image index"
326 if ! download_file
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL} \
327 ${DOWNLOAD_TEMP}/index noexit ||
328 ! download_sig
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL}.asc \
329 ${DOWNLOAD_TEMP}/index.asc noexit
; then
330 download_file
${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
331 download_sig
${DOWNLOAD_INDEX_PATH}.asc \
332 ${DOWNLOAD_TEMP}/index.asc normal
335 gpg_validate
${DOWNLOAD_TEMP}/index.asc
340 printf "DIST\tRELEASE\tARCH\tVARIANT\tBUILD\n"
349 [ -n "$DOWNLOAD_DIST" ] && [ "$1" != "$DOWNLOAD_DIST" ] && continue
350 [ -n "$DOWNLOAD_RELEASE" ] && [ "$2" != "$DOWNLOAD_RELEASE" ] && continue
351 [ -n "$DOWNLOAD_ARCH" ] && [ "$3" != "$DOWNLOAD_ARCH" ] && continue
352 [ -n "$DOWNLOAD_VARIANT" ] && [ "$4" != "$DOWNLOAD_VARIANT" ] && continue
353 [ -z "$5" ] ||
[ -z "$6" ] && continue
355 printf "$1\t$2\t$3\t$4\t$5\n"
356 done < ${DOWNLOAD_TEMP}/index
359 if [ "$DOWNLOAD_LIST_IMAGES" = "true" ]; then
366 if [ -z "$DOWNLOAD_DIST" ]; then
367 echo -n "Distribution: "
371 if [ -z "$DOWNLOAD_RELEASE" ]; then
373 read DOWNLOAD_RELEASE
376 if [ -z "$DOWNLOAD_ARCH" ]; then
377 echo -n "Architecture: "
385 if [ "$DOWNLOAD_TARGET" = "system" ]; then
386 LXC_CACHE_BASE
="$LOCALSTATEDIR/cache/lxc/"
388 LXC_CACHE_BASE
="$HOME/.cache/lxc/"
391 # Allow the setting of the LXC_CACHE_PATH with the usage of environment variables.
392 LXC_CACHE_PATH
=${LXC_CACHE_PATH:-"$LXC_CACHE_BASE"}
393 LXC_CACHE_PATH
=$LXC_CACHE_PATH/download
/$DOWNLOAD_DIST
394 LXC_CACHE_PATH
="$LXC_CACHE_PATH/$DOWNLOAD_RELEASE/$DOWNLOAD_ARCH/"
395 LXC_CACHE_PATH
="$LXC_CACHE_PATH/$DOWNLOAD_VARIANT"
397 if [ -d "$LXC_CACHE_PATH" ]; then
398 if [ "$DOWNLOAD_FLUSH_CACHE" = "true" ]; then
399 echo "Flushing the cache..."
400 rm -Rf $LXC_CACHE_PATH
401 elif [ "$DOWNLOAD_FORCE_CACHE" = "true" ]; then
402 DOWNLOAD_USE_CACHE
="true"
404 DOWNLOAD_USE_CACHE
="true"
405 if [ -e "$(relevant_file expiry)" ]; then
406 if [ "$(cat $(relevant_file expiry))" -lt $
(date +%s
) ]; then
407 echo "The cached copy has expired, re-downloading..."
408 DOWNLOAD_USE_CACHE
="false"
414 # Download what's needed
415 if [ "$DOWNLOAD_USE_CACHE" = "false" ]; then
420 DOWNLOAD_INDEX_PATH
=/meta
/1.0/index-
${DOWNLOAD_MODE}
422 echo "Downloading the image index"
423 if ! download_file
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL} \
424 ${DOWNLOAD_TEMP}/index noexit ||
425 ! download_sig
${DOWNLOAD_INDEX_PATH}.
${DOWNLOAD_COMPAT_LEVEL}.asc \
426 ${DOWNLOAD_TEMP}/index.asc noexit
; then
427 download_file
${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
428 download_sig
${DOWNLOAD_INDEX_PATH}.asc \
429 ${DOWNLOAD_TEMP}/index.asc normal
432 gpg_validate
${DOWNLOAD_TEMP}/index.asc
442 if [ "$1" != "$DOWNLOAD_DIST" ] || \
443 [ "$2" != "$DOWNLOAD_RELEASE" ] || \
444 [ "$3" != "$DOWNLOAD_ARCH" ] || \
445 [ "$4" != "$DOWNLOAD_VARIANT" ] || \
453 done < ${DOWNLOAD_TEMP}/index
455 if [ -z "$DOWNLOAD_URL" ]; then
456 echo "ERROR: Couldn't find a matching image." 1>&1
460 if [ -d "$LXC_CACHE_PATH" ] && [ -f "$LXC_CACHE_PATH/build_id" ] && \
461 [ "$(cat $LXC_CACHE_PATH/build_id)" = "$DOWNLOAD_BUILD" ]; then
462 echo "The cache is already up to date."
463 echo "Using image from local cache"
465 # Download the actual files
466 echo "Downloading the rootfs"
467 download_file
$DOWNLOAD_URL/rootfs.
tar.xz \
468 ${DOWNLOAD_TEMP}/rootfs.
tar.xz normal
469 download_sig
$DOWNLOAD_URL/rootfs.
tar.xz.asc \
470 ${DOWNLOAD_TEMP}/rootfs.
tar.xz.asc normal
471 gpg_validate
${DOWNLOAD_TEMP}/rootfs.
tar.xz.asc
473 echo "Downloading the metadata"
474 download_file
$DOWNLOAD_URL/meta.
tar.xz \
475 ${DOWNLOAD_TEMP}/meta.
tar.xz normal
476 download_sig
$DOWNLOAD_URL/meta.
tar.xz.asc \
477 ${DOWNLOAD_TEMP}/meta.
tar.xz.asc normal
478 gpg_validate
${DOWNLOAD_TEMP}/meta.
tar.xz.asc
480 if [ -d $LXC_CACHE_PATH ]; then
481 rm -Rf $LXC_CACHE_PATH
483 mkdir
-p $LXC_CACHE_PATH
484 mv ${DOWNLOAD_TEMP}/rootfs.
tar.xz
$LXC_CACHE_PATH
485 if ! tar Jxf
${DOWNLOAD_TEMP}/meta.
tar.xz
-C $LXC_CACHE_PATH; then
486 echo "ERROR: Invalid rootfs tarball." 2>&1
490 echo $DOWNLOAD_BUILD > $LXC_CACHE_PATH/build_id
492 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
493 chown
-R $LXC_MAPPED_UID $LXC_CACHE_BASE >/dev
/null
2>&1 || true
495 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
496 chgrp
-R $LXC_MAPPED_GID $LXC_CACHE_BASE >/dev
/null
2>&1 || true
498 echo "The image cache is now ready"
501 echo "Using image from local cache"
505 echo "Unpacking the rootfs"
508 excludelist
=$
(relevant_file excludes
)
509 if [ -f "${excludelist}" ]; then
511 EXCLUDES
="$EXCLUDES --exclude=$line"
515 tar --anchored ${EXCLUDES} --numeric-owner -xpJf \
516 ${LXC_CACHE_PATH}/rootfs.
tar.xz
-C ${LXC_ROOTFS}
518 mkdir
-p ${LXC_ROOTFS}/dev
/pts
/
520 # Setup the configuration
521 configfile
=$
(relevant_file config
)
522 fstab
=$
(relevant_file fstab
)
523 if [ ! -e $configfile ]; then
524 echo "ERROR: meta tarball is missing the configuration file" 1>&2
528 ## Extract all the network config entries
529 sed -i -e "/lxc.net.0/{w ${LXC_PATH}/config-network" -e "d}" \
532 ## Extract any other config entry
533 sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" ${LXC_PATH}/config
535 ## Append the defaults
536 echo "" >> ${LXC_PATH}/config
537 echo "# Distribution configuration" >> ${LXC_PATH}/config
538 cat $configfile >> ${LXC_PATH}/config
540 ## Add the container-specific config
541 echo "" >> ${LXC_PATH}/config
542 echo "# Container specific configuration" >> ${LXC_PATH}/config
543 if [ -e "${LXC_PATH}/config-auto" ]; then
544 cat ${LXC_PATH}/config-auto
>> ${LXC_PATH}/config
545 rm ${LXC_PATH}/config-auto
547 if [ -e "$fstab" ]; then
548 echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> ${LXC_PATH}/config
550 echo "lxc.uts.name = ${LXC_NAME}" >> ${LXC_PATH}/config
552 ## Re-add the previously removed network config
553 if [ -e "${LXC_PATH}/config-network" ]; then
554 echo "" >> ${LXC_PATH}/config
555 echo "# Network configuration" >> ${LXC_PATH}/config
556 cat ${LXC_PATH}/config-network
>> ${LXC_PATH}/config
557 rm ${LXC_PATH}/config-network
560 TEMPLATE_FILES
="${LXC_PATH}/config"
563 if [ -e $fstab ]; then
564 cp ${fstab} ${LXC_PATH}/fstab
565 TEMPLATE_FILES
="$TEMPLATE_FILES ${LXC_PATH}/fstab"
568 # Look for extra templates
569 if [ -e "$(relevant_file templates)" ]; then
571 fullpath
=${LXC_ROOTFS}/$line
572 [ ! -e "$fullpath" ] && continue
573 TEMPLATE_FILES
="$TEMPLATE_FILES $fullpath"
574 done < $
(relevant_file templates
)
577 # Replace variables in all templates
578 for file in $TEMPLATE_FILES; do
579 [ ! -f "$file" ] && continue
581 sed -i "s#LXC_NAME#$LXC_NAME#g" $file
582 sed -i "s#LXC_PATH#$LXC_PATH#g" $file
583 sed -i "s#LXC_ROOTFS#$LXC_ROOTFS#g" $file
584 sed -i "s#LXC_TEMPLATE_CONFIG#$LXC_TEMPLATE_CONFIG#g" $file
585 sed -i "s#LXC_HOOK_DIR#$LXC_HOOK_DIR#g" $file
588 # prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle
589 if [ -f ${LXC_ROOTFS}/etc
/init
/tty.conf
]; then
590 sed -i 's|mingetty|mingetty --nohangup|' ${LXC_ROOTFS}/etc
/init
/tty.conf
593 if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
594 chown
$LXC_MAPPED_UID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
596 if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
597 chgrp
$LXC_MAPPED_GID $LXC_PATH/config
$LXC_PATH/fstab
>/dev
/null
2>&1 || true
600 if [ -e "$(relevant_file create-message)" ]; then
603 cat "$(relevant_file create-message)"