lxc-download.in revision 3cd988ccdbbb5b5794b19ad1165e48168c2dd1d9
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Client script for LXC container images.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# This library is free software; you can redistribute it and/or
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# modify it under the terms of the GNU Lesser General Public
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# License as published by the Free Software Foundation; either
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# version 2.1 of the License, or (at your option) any later version.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# This library is distributed in the hope that it will be useful,
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# but WITHOUT ANY WARRANTY; without even the implied warranty of
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Lesser General Public License for more details.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# You should have received a copy of the GNU Lesser General Public
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# License along with this library; if not, write to the Free Software
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Deal with GPG over http proxy
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -n "${http_proxy:-}" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk DOWNLOAD_KEYSERVER="hkp://p80.pool.sks-keyservers.net:80"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Make sure the usual locations are in PATH
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Some useful functions
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if wget $@; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if ! wget_wrapper -T 30 -q https://${DOWNLOAD_SERVER}/$1 -O $2 >/dev/null 2>&1; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if ! wget_wrapper -T 30 -q http://${DOWNLOAD_SERVER}/$1 -O $2 >/dev/null 2>&1; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "WARNING: Failed to download the file over HTTPs." 1>&2
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo -n " The file was instead download over HTTP. " 1>&2
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if ! download_file $1 $2 noexit; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk --recv-keys ${DOWNLOAD_KEYID} >/dev/null 2>&1; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk while read line; do
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ "$fields" = "0 0 4294967295" ] && { echo no; return; } || true
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo $fields | grep -q " 0 1$" && { echo userns-root; return; } || true
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkLXC container image downloader
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkRequired arguments:
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ -d | --dist <distribution> ]: The name of the distribution
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ -r | --release <release> ]: Release name/version
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ -a | --arch <architecture> ]: Architecture of the container
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkOptional arguments:
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ -h | --help ]: This help message
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ -l | --list ]: List all available images
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --variant <variant> ]: Variant of the image (default: "default")
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --server <server> ]: Image server (default: "images.linuxcontainers.org")
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --keyid <keyid> ]: GPG keyid (default: 0x...)
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --keyserver <keyserver> ]: GPG keyserver to use
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --no-validate ]: Disable GPG validation (not recommended)
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --flush-cache ]: Flush the local copy (if present)
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --force-cache ]; Force the use of the local copy even if expired
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkLXC internal arguments (do not pass manually!):
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --name <name> ]: The container name
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --path <path> ]: The path to the container
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --rootfs <rootfs> ]: The path to the container's rootfs
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --mapped-uid <map> ]: A uid map (user namespaces)
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk[ --mapped-gid <map> ]: A gid map (user namespaces)
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkoptions=$(getopt -o d:r:a:hl -l dist:,release:,arch:,help,list,variant:,\
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkserver:,keyid:,keyserver:,no-validate,flush-cache,force-cache,name:,path:,\
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkeval set -- "$options"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkwhile :; do
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk *) break;;
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Check for required binaries
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Check for GPG
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "You can workaround this by using --no-validate." 1>&2
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Check that we have all variables we need
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -z "$LXC_NAME" ] || [ -z "$LXC_PATH" ] || [ -z "$LXC_ROOTFS" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -z "$LXC_MAPPED_UID" ] || [ "$LXC_MAPPED_UID" = "-1" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -z "$DOWNLOAD_DIST" ] || [ -z "$DOWNLOAD_RELEASE" ] || \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ -z "$DOWNLOAD_ARCH" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Trap all exit signals
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Simply list images
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Initialize GPG
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Grab the index
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "Downloading the image index"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if ! download_file ${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL} \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk ! download_sig ${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL}.asc \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk download_file ${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Parse it
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "---"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "---"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk while read line; do
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Basic CSV parser
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ -n "$DOWNLOAD_DIST" ] && [ "$1" != "$DOWNLOAD_DIST" ] && continue
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ -n "$DOWNLOAD_RELEASE" ] && [ "$2" != "$DOWNLOAD_RELEASE" ] && continue
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ -n "$DOWNLOAD_ARCH" ] && [ "$3" != "$DOWNLOAD_ARCH" ] && continue
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ -n "$DOWNLOAD_VARIANT" ] && [ "$4" != "$DOWNLOAD_VARIANT" ] && continue
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "---"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Interactive mode
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -z "$DOWNLOAD_DIST" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo -n "Distribution: "
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -z "$DOWNLOAD_RELEASE" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo -n "Release: "
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -z "$DOWNLOAD_ARCH" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo -n "Architecture: "
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Setup the cache
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkLXC_CACHE_PATH="$LXC_CACHE_PATH/$DOWNLOAD_RELEASE/$DOWNLOAD_ARCH/"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -d "$LXC_CACHE_PATH" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "Flushing the cache..."
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ "$(cat $(relevant_file expiry))" -lt $(date +%s) ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "The cached copy has expired, re-downloading..."
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Download what's needed
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Initialize GPG
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Grab the index
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "Downloading the image index"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if ! download_file ${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL} \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk ! download_sig ${DOWNLOAD_INDEX_PATH}.${DOWNLOAD_COMPAT_LEVEL}.asc \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk download_file ${DOWNLOAD_INDEX_PATH} ${DOWNLOAD_TEMP}/index normal
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Parse it
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk while read line; do
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Basic CSV parser
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ -z "$6" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -z "$DOWNLOAD_URL" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -d "$LXC_CACHE_PATH" ] && [ -f "$LXC_CACHE_PATH/build_id" ] && \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ "$(cat $LXC_CACHE_PATH/build_id)" = "$DOWNLOAD_BUILD" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "The cache is already up to date."
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "Using image from local cache"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk # Download the actual files
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "Downloading the rootfs"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "Downloading the metadata"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -d $LXC_CACHE_PATH ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if ! tar Jxf ${DOWNLOAD_TEMP}/meta.tar.xz -C $LXC_CACHE_PATH; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk chown -R $LXC_MAPPED_UID $LXC_CACHE_BASE >/dev/null 2>&1 || true
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk chgrp -R $LXC_MAPPED_GID $LXC_CACHE_BASE >/dev/null 2>&1 || true
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "The image cache is now ready"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "Using image from local cache"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Unpack the rootfs
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkecho "Unpacking the rootfs"
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -f "${excludelist}" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk while read line; do
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Setup the configuration
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ ! -e $configfile ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "ERROR: meta tarball is missing the configuration file" 1>&2
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk## Extract all the network config entries
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpksed -i -e "/lxc.network/{w ${LXC_PATH}/config-network" -e "d}" \
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk## Extract any other config entry
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpksed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" ${LXC_PATH}/config
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk## Append the defaults
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkecho "# Distribution configuration" >> ${LXC_PATH}/config
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk## Add the container-specific config
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkecho "# Container specific configuration" >> ${LXC_PATH}/config
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -e "${LXC_PATH}/config-auto" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -e "$fstab" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "lxc.mount = ${LXC_PATH}/fstab" >> ${LXC_PATH}/config
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk## Re-add the previously removed network config
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -e "${LXC_PATH}/config-network" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Setup the fstab
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -e $fstab ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Look for extra templates
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk while read line; do
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ ! -e "$fullpath" ] && continue
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk# Replace variables in all templates
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk [ ! -f "$file" ] && continue
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk sed -i "s#LXC_TEMPLATE_CONFIG#$LXC_TEMPLATE_CONFIG#g" $file
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -n "$LXC_MAPPED_UID" ] && [ "$LXC_MAPPED_UID" != "-1" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk chown $LXC_MAPPED_UID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkif [ -n "$LXC_MAPPED_GID" ] && [ "$LXC_MAPPED_GID" != "-1" ]; then
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk chgrp $LXC_MAPPED_GID $LXC_PATH/config $LXC_PATH/fstab >/dev/null 2>&1 || true
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk echo "---"