3
# template script for generating ubuntu container for LXC based on released cloud
6
# Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License version 2, as
10
# published by the Free Software Foundation.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License along
18
# with this program; if not, write to the Free Software Foundation, Inc.,
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
if [ -r /etc/default/lxc ]; then
36
if [ $arch = "i386" ]; then
40
# if there is exactly one veth network entry, make sure it has an
42
nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
43
if [ $nics -eq 1 ]; then
44
grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
45
lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
49
cat <<EOF >> $path/config
55
lxc.mount = $path/fstab
57
lxc.cap.drop = sys_module mac_admin
58
lxc.pivotdir = lxc_putold
60
# uncomment the next line to run the container unconfined:
61
#lxc.aa_profile = unconfined
63
lxc.cgroup.devices.deny = a
64
# Allow any mknod (but not using the node)
65
lxc.cgroup.devices.allow = c *:* m
66
lxc.cgroup.devices.allow = b *:* m
68
lxc.cgroup.devices.allow = c 1:3 rwm
69
lxc.cgroup.devices.allow = c 1:5 rwm
71
lxc.cgroup.devices.allow = c 5:1 rwm
72
lxc.cgroup.devices.allow = c 5:0 rwm
73
#lxc.cgroup.devices.allow = c 4:0 rwm
74
#lxc.cgroup.devices.allow = c 4:1 rwm
76
lxc.cgroup.devices.allow = c 1:9 rwm
77
lxc.cgroup.devices.allow = c 1:8 rwm
78
lxc.cgroup.devices.allow = c 136:* rwm
79
lxc.cgroup.devices.allow = c 5:2 rwm
81
lxc.cgroup.devices.allow = c 254:0 rwm
83
lxc.cgroup.devices.allow = c 10:229 rwm
85
lxc.cgroup.devices.allow = c 10:200 rwm
87
lxc.cgroup.devices.allow = c 1:7 rwm
89
lxc.cgroup.devices.allow = c 10:228 rwm
91
lxc.cgroup.devices.allow = c 10:232 rwm
94
cat <<EOF > $path/fstab
95
proc proc proc nodev,noexec,nosuid 0 0
96
sysfs sys sysfs defaults 0 0
99
# rmdir /dev/shm in precise and quantal containers.
100
# I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
101
# get bind mounted to the host's /run/shm. So try to rmdir
102
# it, and in case that fails move it out of the way.
103
if [ $release = "precise" ] || [ $release = "quantal" ]; then
104
[ -d "$rootfs/dev/shm" ] && rmdir $rootfs/dev/shm
105
[ -e "$rootfs/dev/shm" ] && mv $rootfs/dev/shm $rootfs/dev/shm.bak
106
ln -s /run/shm $rootfs/dev/shm
115
LXC Container configuration for Ubuntu Cloud images.
118
[ -r | --release <release> ]: Release name of container, defaults to host
119
[ -a | --arch ]: Arhcitecture of container, defaults to host arcitecture
120
[ -C | --cloud ]: Configure container for use with meta-data service, defaults to no
121
[ -T | --tarball ]: Location of tarball
122
[ -d | --debug ]: Run with 'set -x' to debug errors
123
[ -s | --stream]: Use specified stream rather than 'released'
125
Options, mutually exclusive of "-C" and "--cloud":
126
[ -i | --hostid ]: HostID for cloud-init, defaults to random string
127
[ -u | --userdata ]: Cloud-init user-data file to configure container on start
128
[ -S | --auth-key ]: SSH Public key file to inject into container
129
[ -L | --nolocales ]: Do not copy host's locales into container
135
options=$(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: -- "$@")
136
if [ $? -ne 0 ]; then
140
eval set -- "$options"
143
if [ -f /etc/lsb-release ]; then
145
case "$DISTRIB_CODENAME" in
146
lucid|natty|oneiric|precise|quantal)
147
release=$DISTRIB_CODENAME
154
# Code taken from debootstrap
155
if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
156
arch=`/usr/bin/dpkg --print-architecture`
157
elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
158
arch=`/usr/bin/udpkg --print-architecture`
161
if [ "$arch" = "i686" ]; then
163
elif [ "$arch" = "x86_64" ]; then
165
elif [ "$arch" = "armv7l" ]; then
166
# note: arm images don't exist before oneiric; are called armhf in
167
# precise and later; and are not supported by the query, so we don't actually
168
# support them yet (see check later on). When Query2 is available,
169
# we'll use that to enable arm images.
183
-h|--help) usage $0 && exit 0;;
184
-p|--path) path=$2; shift 2;;
185
-n|--name) name=$2; shift 2;;
186
-F|--flush-cache) flushcache=1; shift 1;;
187
-r|--release) release=$2; shift 2;;
188
-a|--arch) arch=$2; shift 2;;
189
-i|--hostid) host_id=$2; shift 2;;
190
-u|--userdata) userdata=$2; shift 2;;
191
-C|--cloud) cloud=1; shift 1;;
192
-S|--auth-key) auth_key=$2; shift 2;;
193
-L|--no_locales) locales=0; shift 2;;
194
-T|--tarball) tarball=$2; shift 2;;
195
-d|--debug) debug=1; shift 1;;
196
-s|--stream) stream=$2; shift 2;;
197
--) shift 1; break ;;
202
if [ $debug -eq 1 ]; then
206
if [ "$arch" == "i686" ]; then
210
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
211
echo "can't create amd64 container on i386"
215
if [ $arch != "i386" -a $arch != "amd64" ]; then
216
echo "Only i386 and amd64 are supported by the ubuntu cloud template."
220
if [ "$stream" != "daily" -a "$stream" != "released" ]; then
221
echo "Only 'daily' and 'released' streams are supported"
225
if [ -n "$userdata" -a ! -f "$userdata" ]; then
226
echo "Userdata does not exist"
230
if [ -z "$path" ]; then
231
echo "'path' parameter is required"
235
if [ "$(id -u)" != "0" ]; then
236
echo "This script should be run as 'root'"
242
type ubuntu-cloudimg-query
245
# determine the url, tarball, and directory names
247
cache="/var/cache/lxc/cloud-$release"
251
if [ -n "$tarball" ]; then
254
url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`
255
url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
258
filename=`basename $url2`
263
umount -l $cache/$xdir || true
267
# if the release doesn't have a *-rootfs.tar.gz, then create one from the
268
# cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
269
# a tarball from the mounted image.
275
xdir=`mktemp -d -p .`
276
tarname=`basename $url`
277
imgname="$release-*-cloudimg-$arch.img"
278
trap buildcleanup EXIT
279
if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
281
echo "Downloading cloud image from $url"
282
wget $url || { echo "Couldn't find cloud image $url."; exit 1; }
284
echo "Creating new cached cloud image rootfs"
285
tar --wildcards -zxf $tarname $imgname
286
mount -o loop $imgname $xdir
287
(cd $xdir; tar zcf ../$filename .)
289
rm -f $tarname $imgname
291
echo "New cloud image cache created"
295
mkdir -p /var/lock/subsys/
300
if [ $flushcache -eq 1 ]; then
301
echo "Clearing the cached images"
305
if [ ! -f $filename ]; then
306
wget $url2 || build_root_tgz $url1 $filename
309
echo "Extracting container rootfs"
312
tar -zxf $cache/$filename
315
if [ $cloud -eq 0 ]; then
316
echo "Configuring for running outside of a cloud environment"
317
echo "If you want to configure for a cloud evironment, please use '-- -C' to create the container"
319
seed_d=$rootfs/var/lib/cloud/seed/nocloud-net
320
rhostid=$(uuidgen | cut -c -8)
321
host_id=${hostid:-$rhostid}
324
cat > "$seed_d/meta-data" <<EOF
325
instance_id: lxc-$host_id
328
rm $rootfs/etc/hostname
330
if [ $locales -eq 1 ]; then
331
cp /usr/lib/locale/locale-archive $rootfs/usr/lib/locale/locale-archive
335
if [ -n "$auth_key" -a -f "$auth_key" ]; then
336
u_path="/home/ubuntu/.ssh"
337
root_u_path="$rootfs/$u_path"
338
mkdir -p $root_u_path
339
cp $auth_key "$root_u_path/authorized_keys"
340
chroot $rootfs chown -R ubuntu: "$u_path"
342
echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
345
if [ -f "$userdata" ]; then
346
echo "Using custom user-data"
347
cp $userdata $seed_d/user-data
350
if [ -z "$MIRROR" ]; then
351
MIRROR="http://archive.ubuntu.com/ubuntu"
354
cat > "$seed_d/user-data" <<EOF
356
output: {all: '| tee -a /var/log/cloud-init-output.log'}
358
manage_etc_hosts: localhost
359
locale: $(/usr/bin/locale | awk -F= '/LANG=/ {print$NF}')
363
chroot $rootfs /usr/sbin/usermod -U ubuntu
364
echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
365
echo "Please login as user ubuntu with password ubuntu."
369
echo "Configured for running in a cloud environment."
370
echo "If you do not have a meta-data service, this container will likely be useless."
374
) 200>/var/lock/subsys/lxc-ubucloud
376
copy_configuration $path $rootfs $name $arch $release
378
echo "Container $name created."