lxc-ubuntu-cloud.in revision d46417540f61ffb93344f3d338340588bf118ab4
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher#!/bin/bash
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# template script for generating ubuntu container for LXC based on released
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# cloud images.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher#
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher#
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# This library is free software; you can redistribute it and/or
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# modify it under the terms of the GNU Lesser General Public
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# License as published by the Free Software Foundation; either
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# version 2.1 of the License, or (at your option) any later version.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# This library is distributed in the hope that it will be useful,
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# but WITHOUT ANY WARRANTY; without even the implied warranty of
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Lesser General Public License for more details.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# You should have received a copy of the GNU Lesser General Public
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# License along with this library; if not, write to the Free Software
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherset -e
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherSTATE_DIR="@LOCALSTATEDIR@"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherHOOK_DIR="@LXCHOOKDIR@"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherCLONE_HOOK_FN="$HOOK_DIR/ubuntu-cloud-prep"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherLXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherKNOWN_RELEASES="lucid precise quantal saucy trusty"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherskip_arch_check=${UCTEMPLATE_SKIP_ARCH_CHECK:-0}
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ -r /etc/default/lxc ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher . /etc/default/lxc
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagheram_in_userns() {
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher [ -e /proc/self/uid_map ] || { echo no; return; }
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || { echo yes; return; }
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher [ "$line" = "0 0 4294967295" ] && { echo no; return; }
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo yes
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher}
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherin_userns=0
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher[ $(am_in_userns) = "yes" ] && in_userns=1
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghercopy_configuration()
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher{
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher path=$1
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rootfs=$2
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher name=$3
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher arch=$4
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher release=$5
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $arch = "i386" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher arch="i686"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # if there is exactly one veth network entry, make sure it has an
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # associated hwaddr.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $nics -eq 1 ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher grep -q "^lxc.network.hwaddr" $path/config || sed -i -e "/^lxc\.network\.type[ \t]*=[ \t]*veth/a lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # Generate the configuration file
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ## Create the fstab (empty by default)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher touch $path/fstab
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ## Relocate all the network config entries
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ## Relocate any other config entries
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ## Add all the includes
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "# Common configuration" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.common.conf" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.common.conf" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $in_userns -eq 1 ] && [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.userns.conf" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.userns.conf" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ## Add the container-specific config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "# Container specific configuration" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher [ -e "$path/config-auto" ] && cat $path/config-auto >> $path/config && rm $path/config-auto
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher cat <<EOF >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherlxc.mount = $path/fstab
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherlxc.utsname = $name
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherlxc.arch = $arch
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherEOF
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ## Re-add the previously removed network config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "# Network configuration" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher cat $path/config-network >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rm $path/config-network
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # Set initial timezone as on host
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ -f /etc/timezone ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher cat /etc/timezone > $rootfs/etc/timezone
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher elif [ -f /etc/sysconfig/clock ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher . /etc/sysconfig/clock
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo $ZONE > $rootfs/etc/timezone
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher else
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Timezone in container is not configured. Adjust it manually."
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher # rmdir /dev/shm for containers that have /run/shm
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher # get bind mounted to the host's /run/shm. So try to rmdir
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher # it, and in case that fails move it out of the way.
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher # NOTE: This can only be removed once 12.04 goes out of support
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher rmdir $rootfs/dev/shm 2>/dev/null || mv $rootfs/dev/shm $rootfs/dev/shm.bak
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher ln -s /run/shm $rootfs/dev/shm
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher fi
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher return 0
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher}
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagherusage()
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher{
346f41f1ede975cb2db0af570f5b454b9b306704Stephen Gallagher cat <<EOF
346f41f1ede975cb2db0af570f5b454b9b306704Stephen GallagherLXC Container configuration for Ubuntu Cloud images.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherGeneric Options
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher[ -r | --release <release> ]: Release name of container, defaults to host
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher[ --rootfs <path> ]: Path in which rootfs will be placed
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher[ -a | --arch ]: Architecture of container, defaults to host architecture
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher[ -T | --tarball ]: Location of tarball
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher[ -d | --debug ]: Run with 'set -x' to debug errors
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher[ -s | --stream]: Use specified stream rather than 'tryreleased'
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherAdditionally, clone hooks can be passed through (ie, --userdata). For those,
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghersee:
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher $CLONE_HOOK_FN --help
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherEOF
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny return 0
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny}
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyoptions=$(getopt -o a:hp:r:n:Fi:CLS:T:ds:u: -l arch:,help,rootfs:,path:,release:,name:,flush-cache,hostid:,auth-key:,cloud,no_locales,tarball:,debug,stream:,userdata:,mapped-uid: -- "$@")
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ $? -ne 0 ]; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny usage $(basename $0)
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny exit 1
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyfi
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidekeval set -- "$options"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenymapped_uid=-1
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny# default release is precise, or the systems release if recognized
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyrelease=precise
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ -f /etc/lsb-release ]; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny . /etc/lsb-release
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny rels="$KNOWN_RELEASES"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny for r in $rels; do
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny [ "$DISTRIB_CODENAME" = "$r" ] && release="$r"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny done
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyfi
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny# Code taken from debootstrap
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny arch=`/usr/bin/dpkg --print-architecture`
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyelif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny arch=`/usr/bin/udpkg --print-architecture`
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyelse
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek arch=$(uname -m)
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek if [ "$arch" = "i686" ]; then
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek arch="i386"
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek elif [ "$arch" = "x86_64" ]; then
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek arch="amd64"
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek elif [ "$arch" = "armv7l" ]; then
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek # note: arm images don't exist before oneiric; are called armhf in
4a1e58d85409fbb7a12ac244c3dbef8c0c1b15dfMichal Zidek # precise and later; and are not supported by the query, so we don't actually
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny # support them yet (see check later on). When Query2 is available,
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny # we'll use that to enable arm images.
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny arch="armhf"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny elif [ "$arch" = "aarch64" ]; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny arch="arm64"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny elif [ "$arch" = "ppc64le" ]; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny arch="ppc64el"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny fi
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyfi
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenydebug=0
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyhostarch=$arch
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenycloud=0
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenylocales=1
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyflushcache=0
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenystream="tryreleased"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenycloneargs=()
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenywhile true
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenydo
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny case "$1" in
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -h|--help) usage $0 && exit 0;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -p|--path) path=$2; shift 2;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -n|--name) name=$2; shift 2;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -F|--flush-cache) flushcache=1; shift 1;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -r|--release) release=$2; shift 2;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -a|--arch) arch=$2; shift 2;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -T|--tarball) tarball=$2; shift 2;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher -d|--debug) debug=1; shift 1;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher -s|--stream) stream=$2; shift 2;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny --rootfs) rootfs=$2; shift 2;;
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny -L|--no?locales) cloneargs[${#cloneargs[@]}]="--no-locales"; shift 1;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher -i|--hostid) cloneargs[${#cloneargs[@]}]="--hostid=$2"; shift 2;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher -u|--userdata) cloneargs[${#cloneargs[@]}]="--userdata=$2"; shift 2;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher -C|--cloud) cloneargs[${#cloneargs[@]}]="--cloud"; shift 1;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher -S|--auth-key) cloneargs[${#cloneargs[@]}]="--auth-key=$2"; shift 2;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher --mapped-uid) mapped_uid=$2; shift 2;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher --) shift 1; break ;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher *) break ;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher esac
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherdone
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherecho "mapped_uid is .$mapped_uid."
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghercloneargs=( "--name=$name" "${cloneargs[@]}" )
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ $debug -eq 1 ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher set -x
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ "$arch" = "i686" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher arch=i386
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ "$skip_arch_check" = "0" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher case "$hostarch:$arch" in
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher $arch:$arch) : ;; # the host == container
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher amd64:i386) :;; # supported "cross"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher arm64:arm*) :;; # supported "cross"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher armel:armhf) :;; # supported "cross"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher armhf:armel) :;; # supported "cross"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher *) echo "cannot create '$arch' container on hostarch '$hostarch'";
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher exit 1;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher esac
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ "$stream" != "daily" -a "$stream" != "released" -a "$stream" != "tryreleased" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Only 'daily' and 'released' and 'tryreleased' streams are supported"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher exit 1
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ -z "$path" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "'path' parameter is required"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher exit 1
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ "$(id -u)" != "0" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "This script should be run as 'root'"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher exit 1
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# detect rootfs
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherconfig="$path/config"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ -z "$rootfs" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher else
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rootfs=$path/rootfs
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghertype ubuntu-cloudimg-query
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghertype wget
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny# determine the url, tarball, and directory names
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny# download if needed
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenycache="$STATE_DIR/cache/lxc/cloud-$release"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ $in_userns -eq 1 ]; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny STATE_DIR="$HOME/.cache/lxc/"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny cache="$HOME/.cache/lxc/cloud-$release"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyfi
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenymkdir -p $cache
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ "$stream" = "tryreleased" ]; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny stream=released
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny ubuntu-cloudimg-query $release $stream $arch 1>/dev/null 2>/dev/null || stream=daily
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ -n "$tarball" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher url2="$tarball"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherelse
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if ! url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "There is no download available for release=$release, stream=$stream, arch=$arch"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny [ "$stream" = "daily" ] || echo "You may try with '--stream=daily'"
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny exit
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfilename=`basename $url2`
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherwgetcleanup()
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher{
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rm -f $filename
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher}
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherbuildcleanup()
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher{
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher cd $rootfs
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher umount -l $cache/$xdir || true
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rm -rf $cache
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher}
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# if the release doesn't have a *-rootfs.tar.gz, then create one from the
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# a tarball from the mounted image.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherbuild_root_tgz()
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher{
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher url=$1
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher filename=$2
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher xdir=`mktemp -d -p .`
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher tarname=`basename $url`
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher imgname="$release-*-cloudimg-$arch.img"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap buildcleanup EXIT SIGHUP SIGINT SIGTERM
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rm -f $tarname
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Downloading cloud image from $url"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher wget $url || { echo "Couldn't find cloud image $url."; exit 1; }
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Creating new cached cloud image rootfs"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher tar --wildcards -zxf "$tarname" "$imgname"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher mount -o loop $imgname $xdir
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher (cd $xdir; tar --numeric-owner -cpzf "../$filename" .)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher umount $xdir
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rm -f $tarname $imgname
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rmdir $xdir
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "New cloud image cache created"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap EXIT
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap SIGHUP
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap SIGINT
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap SIGTERM
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher}
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherdo_extract_rootfs() {
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher cd $cache
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $flushcache -eq 1 ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Clearing the cached images"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher rm -f $filename
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ ! -f $filename ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher wget $url2 || build_root_tgz $url1 $filename
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap EXIT
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap SIGHUP
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap SIGINT
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher trap SIGTERM
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Extracting container rootfs"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher mkdir -p $rootfs
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher cd $rootfs
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $in_userns -eq 1 ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher tar --anchored --exclude="dev/*" --numeric-owner -xpzf "$cache/$filename"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher mkdir -p $rootfs/dev/pts/
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher else
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher tar --numeric-owner -xpzf "$cache/$filename"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher fi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher}
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ -n "$tarball" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher do_extract_rootfs
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherelse
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher mkdir -p "$STATE_DIR/lock/subsys/"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher (
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher flock -x 9
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher do_extract_rootfs
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ) 9>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghercopy_configuration $path $rootfs $name $arch $release
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher"$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ $mapped_uid -ne -1 ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher chown $mapped_uid $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher chown -R $mapped_uid $STATE_DIR
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher chown -R $mapped_uid $cache
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherfi
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherecho "Container $name created."
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherexit 0
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# vi: ts=4 expandtab
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher