1
Description: update ubuntu templates to provide macaddr and more
2
Allow specifying ssh authkeys. In cloud template, copy locales
3
by default and allow a tarball to be specified.
4
Author: Ben Howard <ben.howard@canonical.com>
7
Index: lxc/templates/lxc-ubuntu-cloud.in
8
===================================================================
9
--- lxc.orig/templates/lxc-ubuntu-cloud.in 2012-02-15 23:54:13.293919345 -0600
10
+++ lxc/templates/lxc-ubuntu-cloud.in 2012-02-16 09:51:23.387617847 -0600
15
+ # if there is exactly one veth network entry, make sure it has an
16
+ # associated hwaddr.
17
+ nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
18
+ if [ $nics -eq 1 ]; then
19
+ grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
20
+lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
24
cat <<EOF >> $path/config
31
-$1 -h|--help [-a|--arch] [-F | --flush-cache] [-r|--release <release>]
32
-release: lucid | maverick | natty | oneiric | precise
33
-arch: amd64 or i386: defaults to host arch
34
+LXC Container configuration for Ubuntu Cloud images.
37
+[ -r | --release <release> ]: Release name of container, defaults to host
38
+[ -a | --arch ]: Arhcitecture of container, defaults to host arcitecture
39
+[ -C | --cloud ]: Configure container for use with meta-data service, defaults to no
40
+[ -T | --tarball ]: Location of tarball
42
+Options, mutually exclusive of "-C" and "--cloud":
43
+ [ -i | --hostid ]: HostID for cloud-init, defaults to random string
44
+ [ -u | --userdata ]: Cloud-init user-data file to configure container on start
45
+ [ -S | --auth_key ]: SSH Public key file to inject into container
46
+ [ -L | --nolocales ]: Do not copy host's locales into container
52
-options=$(getopt -o a:hp:r:n:Fi:C -l arch:,help,path:,release:,name:,flush-cache,hostid:,cloud -- "$@")
53
+options=$(getopt -o a:hp:r:n:Fi:CLS:T: -l arch:,help,path:,release:,name:,flush-cache,hostid:,auth-key:,cloud,no_locales,tarball: -- "$@")
66
- -h|--help) usage $0 && exit 0;;
67
- -p|--path) path=$2; shift 2;;
68
- -n|--name) name=$2; shift 2;;
69
- -F|--flush-cache) flushcache=1; shift 1;;
70
- -r|--release) release=$2; shift 2;;
71
- -a|--arch) arch=$2; shift 2;;
72
- -i|--hostid) host_id=$2; shift 2;;
73
- -u|--userdata) userdata=$2; shift 2;;
74
- -C|--cloud) cloud=1; shift 1;;
75
- --) shift 1; break ;;
76
+ -h|--help) usage $0 && exit 0;;
77
+ -p|--path) path=$2; shift 2;;
78
+ -n|--name) name=$2; shift 2;;
79
+ -F|--flush-cache) flushcache=1; shift 1;;
80
+ -r|--release) release=$2; shift 2;;
81
+ -a|--arch) arch=$2; shift 2;;
82
+ -i|--hostid) host_id=$2; shift 2;;
83
+ -u|--userdata) userdata=$2; shift 2;;
84
+ -C|--cloud) cloud=1; shift 1;;
85
+ -S|--auth_key) auth_key=$2; shift 2;;
86
+ -L|--no_locales) locales=0; shift 2;;
87
+ -T|--tarball) tarball=$2; shift 2;;
88
+ --) shift 1; break ;;
96
-url1=`ubuntu-cloudimg-query precise daily $arch --format "%{url}\n"`
97
-url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
98
+if [ -n "$tarball" ]; then
101
+ url1=`ubuntu-cloudimg-query precise daily $arch --format "%{url}\n"`
102
+ url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
105
filename=`basename $url2`
107
mkdir -p /var/lock/subsys/
111
if [ ! -f $filename ]; then
116
echo "Extracting rootfs"
117
@@ -215,21 +244,46 @@
119
seed_d=$rootfs/var/lib/cloud/seed/nocloud-net
120
rhostid=$(uuidgen | cut -c -8)
121
- host_id=${hostid:-$rhostid}
123
+ host_id=${hostid:-$rhostid}
126
cat > "$seed_d/meta-data" <<EOF
127
instance_id: lxc-$host_id
130
+ rm $rootfs/etc/hostname
132
+ if [ $locales -eq 1 ]; then
133
+ cp /usr/lib/locale/locale-archive $rootfs/usr/lib/locale/locale-archive
137
+ if [ -n "$auth_key" -a -f "$auth_key" ]; then
138
+ u_path="/home/ubuntu/.ssh"
139
+ root_u_path="$rootfs/$u_path"
140
+ mkdir -p $root_u_path
141
+ cp $auth_key "$root_u_path/authorized_keys"
142
+ chroot $rootfs chown -R ubuntu: "$u_path"
144
+ echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
147
if [ ! -f $userdata ]; then
148
- cp $userdata $seed_d/user-data
149
+ cp $userdata $data_d/user-data
151
- echo "#cloud-config" > $seed_d/user-data
153
if [ -z "$MIRROR" ]; then
154
MIRROR="http://archive.ubuntu.com/ubuntu"
156
- echo "apt-mirror: $MIRROR" >> $seed_d/user-data
158
+ cat > "$seed_d/user-data" <<EOF
160
+output: {all: '| tee -a /var/log/cloud-init-output.log'}
162
+manage_etc_hosts: localhost
163
+locale: $(/usr/bin/locale | awk -F= '/LANG=/ {print$NF}')
168
chroot $rootfs /usr/sbin/usermod -U ubuntu
169
Index: lxc/templates/lxc-ubuntu.in
170
===================================================================
171
--- lxc.orig/templates/lxc-ubuntu.in 2012-02-15 23:54:13.309919426 -0600
172
+++ lxc/templates/lxc-ubuntu.in 2012-02-16 09:51:31.711659121 -0600
174
chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
175
chroot $rootfs useradd --create-home -s /bin/bash -G $group ubuntu
176
echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
177
+ if [ -n "$auth_key" -a -f "$auth_key" ]; then
178
+ u_path="/home/ubuntu/.ssh"
179
+ root_u_path="$rootfs/$u_path"
180
+ mkdir -p $root_u_path
181
+ cp $auth_key "$root_u_path/authorized_keys"
182
+ chroot $rootfs chown -R ubuntu: "$u_path"
184
+ echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
193
+ # if there is exactly one veth network entry, make sure it has an
194
+ # associated hwaddr.
195
+ nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
196
+ if [ $nics -eq 1 ]; then
197
+ grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
198
+lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
202
cat <<EOF >> $path/config
205
@@ -480,16 +498,17 @@
208
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim]
209
- [-F | --flush-cache] [-r|--release <release>]
210
+ [-F | --flush-cache] [-r|--release <release>] [ -S | --auth_key <keyfile>]
211
release: lucid | maverick | natty | oneiric | precise
212
trim: make a minimal (faster, but not upgrade-safe) container
213
bindhome: bind <user>'s home into the container
214
arch: amd64 or i386: defaults to host arch
215
+auth_key: SSH Public key file to inject into container
220
-options=$(getopt -o a:b:hp:r:xn:F -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache -- "$@")
221
+options=$(getopt -o a:b:hp:r:xn:FS: -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key: -- "$@")
222
if [ $? -ne 0 ]; then
226
-b|--bindhome) bindhome=$2; shift 2;;
227
-a|--arch) arch=$2; shift 2;;
228
-x|--trim) trim_container=1; shift 1;;
229
+ -S|--auth_key) auth_key=$2; shift 2;;
230
--) shift 1; break ;;