lxc-ubuntu-cloud.in revision 26312a76c8a1078976a5b391ece4f650a6f1b000
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# template script for generating ubuntu container for LXC based on released
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# cloud images.
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# This library is free software; you can redistribute it and/or
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# modify it under the terms of the GNU Lesser General Public
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# License as published by the Free Software Foundation; either
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# version 2.1 of the License, or (at your option) any later version.
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# This library is distributed in the hope that it will be useful,
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# but WITHOUT ANY WARRANTY; without even the implied warranty of
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# Lesser General Public License for more details.
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# You should have received a copy of the GNU Lesser General Public
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# License along with this library; if not, write to the Free Software
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
662bb64be818407c6719a69780411f7ca8d6c96fAki TuomiKNOWN_RELEASES="precise trusty xenial yakkety zesty"
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi# Make sure the usual locations are in PATH
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || { echo yes; return; }
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map)
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi [ "$line" = "0 0 4294967295" ] && { echo no; return; }
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi # if there is exactly one veth network entry, make sure it has an
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi if [ $nics -eq 1 ]; then
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi 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
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.common.conf" ]; then
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.common.conf" >> $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" ]; then
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" >> $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi if [ $in_userns -eq 1 ] && [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.userns.conf" ]; then
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.userns.conf" >> $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi echo "# Container specific configuration" >> $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi [ -e "$path/config-auto" ] && cat $path/config-auto >> $path/config && rm $path/config-auto
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomilxc.utsname = $name
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomilxc.arch = $arch
8109f3187f5ece5565de1813209af42dc7bb768bTimo Sirainen echo "# Network configuration" >> $path/config
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi if [ -f /etc/timezone ]; then
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi elif [ -f /etc/sysconfig/clock ]; then
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi echo "Timezone in container is not configured. Adjust it manually."
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi # rmdir /dev/shm for containers that have /run/shm
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi # get bind mounted to the host's /run/shm. So try to rmdir
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi # it, and in case that fails move it out of the way.
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi # NOTE: This can only be removed once 12.04 goes out of support
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi if [ ! -L $rootfs/dev/shm ] && [ -e $rootfs/dev/shm ]; then
662bb64be818407c6719a69780411f7ca8d6c96fAki Tuomi rmdir $rootfs/dev/shm 2>/dev/null || mv $rootfs/dev/shm $rootfs/dev/shm.bak
662bb64be818407c6719a69780411f7ca8d6c96fAki TuomiLXC Container configuration for Ubuntu Cloud images.
options=$(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:,vendordata:,mapped-uid:,mapped-gid: -- "$@")
eval set -- "$options"
if [ -z "$path" ]; then
if [ -z "$rootfs" ]; then
type wget
if [ -n "$tarball" ]; then
cd $cache
if [ $flushcache -eq 1 ]; then
trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM
if [ ! -f $filename ]; then
trap EXIT
trap SIGHUP
trap SIGINT
trap SIGTERM
cd $rootfs
if [ $in_userns -eq 1 ]; then
if [ -n "$tarball" ]; then
flock -x 9
"$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
echo "Container $name created."