lxc-ubuntu-cloud.in revision 542939c31bb73bab55f2fd71243b98f5559597d1
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# template script for generating ubuntu container for LXC based on released cloud
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
aa797403d51ff047727b77d64532001d6b6cc21aTimo Sirainen# This program is free software; you can redistribute it and/or modify
dff32d11a411a24f3b76003c1ae22c5a960f180eTimo Sirainen# it under the terms of the GNU General Public License version 2, as
b6b9c99fefbbc662bd9a0006566133c4480bf0e8Timo Sirainen# published by the Free Software Foundation.
b6b9c99fefbbc662bd9a0006566133c4480bf0e8Timo Sirainen# This program is distributed in the hope that it will be useful,
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen# but WITHOUT ANY WARRANTY; without even the implied warranty of
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dff32d11a411a24f3b76003c1ae22c5a960f180eTimo Sirainen# GNU General Public License for more details.
aa797403d51ff047727b77d64532001d6b6cc21aTimo Sirainen# You should have received a copy of the GNU General Public License along
8c2b4a45f17a5cb13bb01058ca37798cf48d91baTimo Sirainen# with this program; if not, write to the Free Software Foundation, Inc.,
8c2b4a45f17a5cb13bb01058ca37798cf48d91baTimo Sirainen# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5f5713d6468dca1acf3d350dd8a33057331f78c5Timo Sirainen # if there is exactly one veth network entry, make sure it has an
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen if [ $nics -eq 1 ]; then
caae18c876f81e261350e4957471efa453c0ea9fTimo Sirainen grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainenlxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
cf9d67e4a9bfee31cf3be05244555d51a3d1b9feTimo Sirainenlxc.utsname = $name
edd318d5866ac3fbc6e8df28fb24a4dfef93c884Timo Sirainenlxc.pts = 1024
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.rootfs = $rootfs
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.mount = $path/fstab
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.arch = $arch
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cap.drop = sys_module mac_admin
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.deny = a
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen# Allow any mknod (but not using the node)
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c *:* m
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = b *:* m
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# /dev/null and zero
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 1:3 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 1:5 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 5:1 rwm
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainenlxc.cgroup.devices.allow = c 5:0 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen#lxc.cgroup.devices.allow = c 4:0 rwm
aa797403d51ff047727b77d64532001d6b6cc21aTimo Sirainen#lxc.cgroup.devices.allow = c 4:1 rwm
b6b9c99fefbbc662bd9a0006566133c4480bf0e8Timo Sirainen# /dev/{,u}random
152db3f90f298b7fb2dbbd4276f0fc30a9bc30f6Timo Sirainenlxc.cgroup.devices.allow = c 1:9 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 1:8 rwm
89d6cd658eabf46e07e40037b0e641ed9be1a2a3Timo Sirainenlxc.cgroup.devices.allow = c 136:* rwm
89d6cd658eabf46e07e40037b0e641ed9be1a2a3Timo Sirainenlxc.cgroup.devices.allow = c 5:2 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 254:0 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 10:229 rwm
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainenlxc.cgroup.devices.allow = c 10:200 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 1:7 rwm
1df39b899804fd1dbc560f75382364822935c857Timo Sirainenlxc.cgroup.devices.allow = c 10:228 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenlxc.cgroup.devices.allow = c 10:232 rwm
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenproc proc proc nodev,noexec,nosuid 0 0
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainensysfs sys sysfs defaults 0 0
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen # rmdir /dev/shm for containers that have /run/shm
c4900d31385344bfadaee53a897daeafdb3063d8Timo Sirainen # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen # get bind mounted to the host's /run/shm. So try to rmdir
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen # it, and in case that fails move it out of the way.
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo SirainenLXC Container configuration for Ubuntu Cloud images.
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo SirainenGeneric Options
e4194f4703eeec32b432371ae30fc8f25ab720d8Timo Sirainen[ -r | --release <release> ]: Release name of container, defaults to host
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen[ -a | --arch ]: Arhcitecture of container, defaults to host arcitecture
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen[ -C | --cloud ]: Configure container for use with meta-data service, defaults to no
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen[ -T | --tarball ]: Location of tarball
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen[ -d | --debug ]: Run with 'set -x' to debug errors
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen[ -s | --stream]: Use specified stream rather than 'released'
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo SirainenOptions, mutually exclusive of "-C" and "--cloud":
0a53eb0283d7ec28c6105f61e118b96fce8ecb95Timo Sirainen [ -i | --hostid ]: HostID for cloud-init, defaults to random string
5e9bb72de1209cd39fdf3e95bdb26e047cc5594eTimo Sirainen [ -u | --userdata ]: Cloud-init user-data file to configure container on start
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen [ -S | --auth-key ]: SSH Public key file to inject into container
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen [ -L | --nolocales ]: Do not copy host's locales into container
15ab2452b0220a115f4351ad9d7fd5ec70ae7966Timo Sirainenoptions=$(getopt -o a:hp:r:n:Fi:CLS:T:ds: -l arch:,help,path:,release:,name:,flush-cache,hostid:,auth-key:,cloud,no_locales,tarball:,debug,stream:,userdata: -- "$@")
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainen# Code taken from debootstrap
2670cd577aa57eb9f915a4f4220ae48c9b4fc5fbTimo Sirainenif [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
1df39b899804fd1dbc560f75382364822935c857Timo Sirainenelif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
if [ -z "$path" ]; then
type wget
if [ -n "$tarball" ]; then
cd $rootfs
# if the release doesn't have a *-rootfs.tar.gz, then create one from the
# cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
url=$1
filename=$2
trap buildcleanup EXIT SIGHUP SIGINT SIGTERM
if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
trap EXIT
trap SIGHUP
trap SIGINT
trap SIGTERM
flock -x 200
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
echo "Container $name created."