lxc-download.in revision 3cd988ccdbbb5b5794b19ad1165e48168c2dd1d9
# Client script for LXC container images.
#
# Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
#
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
set -eu
LOCALSTATEDIR="@LOCALSTATEDIR@"
LXC_HOOK_DIR="@LXCHOOKDIR@"
LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
# Defaults
DOWNLOAD_FLUSH_CACHE="false"
DOWNLOAD_FORCE_CACHE="false"
DOWNLOAD_INTERACTIVE="false"
DOWNLOAD_KEYID="0xBAEFF88C22F6E216"
DOWNLOAD_LIST_IMAGES="false"
DOWNLOAD_MODE="system"
DOWNLOAD_READY_GPG="false"
DOWNLOAD_SHOW_GPG_WARNING="true"
DOWNLOAD_SHOW_HTTP_WARNING="true"
DOWNLOAD_TARGET="system"
DOWNLOAD_USE_CACHE="false"
DOWNLOAD_VALIDATE="true"
DOWNLOAD_VARIANT="default"
# Deal with GPG over http proxy
if [ -n "${http_proxy:-}" ]; then
DOWNLOAD_KEYSERVER="hkp://p80.pool.sks-keyservers.net:80"
fi
# Make sure the usual locations are in PATH
# Some useful functions
fi
}
if wget $@; then
return 0
fi
done
return 1
}
if ! wget_wrapper -T 30 -q https://${DOWNLOAD_SERVER}/$1 -O $2 >/dev/null 2>&1; then
if ! wget_wrapper -T 30 -q http://${DOWNLOAD_SERVER}/$1 -O $2 >/dev/null 2>&1; then
if [ "$3" = "noexit" ]; then
return 1
else
echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
exit 1
fi
DOWNLOAD_SHOW_HTTP_WARNING="false"
echo "WARNING: Failed to download the file over HTTPs." 1>&2
echo -n " The file was instead download over HTTP. " 1>&2
echo "A server replay attack may be possible!" 1>&2
fi
fi
}
if ! download_file $1 $2 noexit; then
if [ "$3" = "normal" ]; then
echo "ERROR: Failed to download http://${DOWNLOAD_SERVER}/$1" 1>&2
exit 1
else
return 1
fi
else
return 0
fi
fi
}
return
fi
return
fi
echo "Setting up the GPG keyring"
mkdir -p "$DOWNLOAD_TEMP/gpg"
chmod 700 "$DOWNLOAD_TEMP/gpg"
export GNUPGHOME="$DOWNLOAD_TEMP/gpg"
if gpg --keyserver $DOWNLOAD_KEYSERVER \
--recv-keys ${DOWNLOAD_KEYID} >/dev/null 2>&1; then
success=1
break
fi
done
echo "ERROR: Unable to fetch GPG key from keyserver."
exit 1
fi
DOWNLOAD_READY_GPG="true"
}
echo "WARNING: Running without gpg validation!" 1>&2
fi
DOWNLOAD_SHOW_GPG_WARNING="false"
return 0
fi
echo "ERROR: Invalid signature for $1" 1>&2
exit 1
fi
}
while read line; do
echo yes
}
FILE_PATH="${LXC_CACHE_PATH}/$1"
if [ -e "${FILE_PATH}-${DOWNLOAD_MODE}" ]; then
FILE_PATH="${FILE_PATH}-${DOWNLOAD_MODE}"
fi
FILE_PATH="${FILE_PATH}.${DOWNLOAD_COMPAT_LEVEL}"
fi
echo $FILE_PATH
}
cat <<EOF
LXC container image downloader
Required arguments:
[ -d | --dist <distribution> ]: The name of the distribution
[ -r | --release <release> ]: Release name/version
[ -a | --arch <architecture> ]: Architecture of the container
Optional arguments:
[ -h | --help ]: This help message
[ -l | --list ]: List all available images
[ --variant <variant> ]: Variant of the image (default: "default")
[ --server <server> ]: Image server (default: "images.linuxcontainers.org")
[ --keyid <keyid> ]: GPG keyid (default: 0x...)
[ --keyserver <keyserver> ]: GPG keyserver to use
[ --no-validate ]: Disable GPG validation (not recommended)
[ --flush-cache ]: Flush the local copy (if present)
[ --force-cache ]; Force the use of the local copy even if expired
LXC internal arguments (do not pass manually!):
[ --name <name> ]: The container name
[ --path <path> ]: The path to the container
[ --rootfs <rootfs> ]: The path to the container's rootfs
[ --mapped-uid <map> ]: A uid map (user namespaces)
[ --mapped-gid <map> ]: A gid map (user namespaces)
EOF
return 0
}
if [ $? -ne 0 ]; then
exit 1
fi
eval set -- "$options"
while :; do
case "$1" in
*) break;;
esac
done
# Check for required binaries
exit 1
fi
done
# Check for GPG
if [ "$DOWNLOAD_VALIDATE" = "true" ]; then
echo "ERROR: Missing recommended tool: gpg" 1>&2
echo "You can workaround this by using --no-validate." 1>&2
exit 1
fi
fi
# Check that we have all variables we need
echo "ERROR: Not running through LXC." 1>&2
exit 1
fi
if [ "$USERNS" != "no" ]; then
if [ "$USERNS" = "yes" ]; then
echo "ERROR: In a user namespace without a map." 1>&2
exit 1
fi
DOWNLOAD_MODE="user"
DOWNLOAD_TARGET="user"
else
DOWNLOAD_MODE="user"
DOWNLOAD_TARGET="system"
fi
fi
if [ -z "$DOWNLOAD_DIST" ] || [ -z "$DOWNLOAD_RELEASE" ] || \
[ -z "$DOWNLOAD_ARCH" ]; then
DOWNLOAD_INTERACTIVE="true"
fi
# Trap all exit signals
else
DOWNLOAD_TEMP=$(mktemp -d)
fi
# Simply list images
if [ "$DOWNLOAD_LIST_IMAGES" = "true" ] || \
[ "$DOWNLOAD_INTERACTIVE" = "true" ]; then
# Initialize GPG
# Grab the index
echo "Downloading the image index"
fi
# Parse it
echo ""
echo "---"
printf "DIST\tRELEASE\tARCH\tVARIANT\tBUILD\n"
echo "---"
while read line; do
# Basic CSV parser
IFS=";"
set -- $line
[ -z "$5" ] || [ -z "$6" ] && continue
printf "$1\t$2\t$3\t$4\t$5\n"
done < ${DOWNLOAD_TEMP}/index
echo "---"
if [ "$DOWNLOAD_LIST_IMAGES" = "true" ]; then
exit 1
fi
# Interactive mode
echo ""
if [ -z "$DOWNLOAD_DIST" ]; then
echo -n "Distribution: "
read DOWNLOAD_DIST
fi
if [ -z "$DOWNLOAD_RELEASE" ]; then
echo -n "Release: "
read DOWNLOAD_RELEASE
fi
if [ -z "$DOWNLOAD_ARCH" ]; then
echo -n "Architecture: "
read DOWNLOAD_ARCH
fi
echo ""
fi
# Setup the cache
if [ "$DOWNLOAD_TARGET" = "system" ]; then
else
LXC_CACHE_BASE="$HOME/.cache/lxc/"
fi
LXC_CACHE_PATH="$LXC_CACHE_BASE/download/$DOWNLOAD_DIST"
if [ -d "$LXC_CACHE_PATH" ]; then
if [ "$DOWNLOAD_FLUSH_CACHE" = "true" ]; then
echo "Flushing the cache..."
elif [ "$DOWNLOAD_FORCE_CACHE" = "true" ]; then
DOWNLOAD_USE_CACHE="true"
else
DOWNLOAD_USE_CACHE="true"
echo "The cached copy has expired, re-downloading..."
DOWNLOAD_USE_CACHE="false"
fi
fi
fi
fi
# Download what's needed
if [ "$DOWNLOAD_USE_CACHE" = "false" ]; then
# Initialize GPG
# Grab the index
echo "Downloading the image index"
fi
# Parse it
while read line; do
# Basic CSV parser
IFS=";"
set -- $line
if [ "$1" != "$DOWNLOAD_DIST" ] || \
[ "$2" != "$DOWNLOAD_RELEASE" ] || \
[ "$3" != "$DOWNLOAD_ARCH" ] || \
[ "$4" != "$DOWNLOAD_VARIANT" ] || \
[ -z "$6" ]; then
continue
fi
DOWNLOAD_URL=$6
break
done < ${DOWNLOAD_TEMP}/index
if [ -z "$DOWNLOAD_URL" ]; then
echo "ERROR: Couldn't find a matching image." 1>&1
exit 1
fi
if [ -d "$LXC_CACHE_PATH" ] && [ -f "$LXC_CACHE_PATH/build_id" ] && \
echo "The cache is already up to date."
echo "Using image from local cache"
else
# Download the actual files
echo "Downloading the rootfs"
echo "Downloading the metadata"
if [ -d $LXC_CACHE_PATH ]; then
fi
echo "ERROR: Invalid rootfs tarball." 2>&1
exit 1
fi
fi
fi
echo "The image cache is now ready"
fi
else
echo "Using image from local cache"
fi
# Unpack the rootfs
echo "Unpacking the rootfs"
EXCLUDES=""
if [ -f "${excludelist}" ]; then
while read line; do
done < $excludelist
fi
# Setup the configuration
if [ ! -e $configfile ]; then
echo "ERROR: meta tarball is missing the configuration file" 1>&2
exit 1
fi
## Extract all the network config entries
## Extract any other config entry
## Append the defaults
## Add the container-specific config
if [ -e "${LXC_PATH}/config-auto" ]; then
fi
if [ -e "$fstab" ]; then
fi
## Re-add the previously removed network config
if [ -e "${LXC_PATH}/config-network" ]; then
fi
TEMPLATE_FILES="${LXC_PATH}/config"
# Setup the fstab
if [ -e $fstab ]; then
TEMPLATE_FILES="$TEMPLATE_FILES ${LXC_PATH}/fstab"
fi
# Look for extra templates
while read line; do
[ ! -e "$fullpath" ] && continue
done < $(relevant_file templates)
fi
# Replace variables in all templates
[ ! -f "$file" ] && continue
done
fi
fi
echo ""
echo "---"
fi
exit 0