lxc-ubuntu-cloud.in revision d46417540f61ffb93344f3d338340588bf118ab4
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# template script for generating ubuntu container for LXC based on released
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# cloud images.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
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# 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# 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 GallagherCLONE_HOOK_FN="$HOOK_DIR/ubuntu-cloud-prep"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen GallagherKNOWN_RELEASES="lucid precise quantal saucy trusty"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherskip_arch_check=${UCTEMPLATE_SKIP_ARCH_CHECK:-0}
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 # if there is exactly one veth network entry, make sure it has an
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 ## Relocate all the network config entries
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $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 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 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 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 Gallagherlxc.mount = $path/fstab
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherlxc.utsname = $name
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherlxc.arch = $arch
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ## Re-add the previously removed network config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "# Network configuration" >> $path/config
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ -f /etc/timezone ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher elif [ -f /etc/sysconfig/clock ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Timezone in container is not configured. Adjust it manually."
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 GallagherLXC Container configuration for Ubuntu Cloud images.
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 GallagherAdditionally, clone hooks can be passed through (ie, --userdata). For those,
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher $CLONE_HOOK_FN --help
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 Zeleny# default release is precise, or the systems release if recognized
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny# Code taken from debootstrap
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyelif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; 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 -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 --) shift 1; break ;;
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherecho "mapped_uid is .$mapped_uid."
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghercloneargs=( "--name=$name" "${cloneargs[@]}" )
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher *) echo "cannot create '$arch' container on hostarch '$hostarch'";
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ "$stream" != "daily" -a "$stream" != "released" -a "$stream" != "tryreleased" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Only 'daily' and 'released' and 'tryreleased' streams are supported"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ -z "$path" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "'path' parameter is required"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "This script should be run as 'root'"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# detect rootfs
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)
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny# determine the url, tarball, and directory names
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny# download if needed
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zeleny ubuntu-cloudimg-query $release $stream $arch 1>/dev/null 2>/dev/null || stream=daily
016e0d7202ff965018e41869c5ab501f86b0d081Jan Zelenyif [ -n "$tarball" ]; then
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'"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
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 Gallagher trap buildcleanup EXIT SIGHUP SIGINT SIGTERM
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher wget $url || { echo "Couldn't find cloud image $url."; exit 1; }
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher echo "Creating new cached cloud image rootfs"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher tar --wildcards -zxf "$tarname" "$imgname"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher (cd $xdir; tar --numeric-owner -cpzf "../$filename" .)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if [ $flushcache -eq 1 ]; then
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 if [ $in_userns -eq 1 ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher tar --anchored --exclude="dev/*" --numeric-owner -xpzf "$cache/$filename"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher tar --numeric-owner -xpzf "$cache/$filename"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherif [ -n "$tarball" ]; then
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ) 9>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallaghercopy_configuration $path $rootfs $name $arch $release
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher"$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherecho "Container $name created."
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher# vi: ts=4 expandtab