]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-ubuntu-cloud.in
update ubuntu templates to provide macaddr and more
[mirror_lxc.git] / templates / lxc-ubuntu-cloud.in
1 #!/bin/bash
2
3 # template script for generating ubuntu container for LXC based on daily cloud
4 # images
5 #
6 # Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
7 #
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.
11
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.
16
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.
20 #
21
22 set -e
23
24 if [ -r /etc/default/lxc ]; then
25 . /etc/default/lxc
26 fi
27
28 copy_configuration()
29 {
30 path=$1
31 rootfs=$2
32 name=$3
33 arch=$4
34
35 if [ $arch = "i386" ]; then
36 arch="i686"
37 fi
38
39 # if there is exactly one veth network entry, make sure it has an
40 # associated hwaddr.
41 nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
42 if [ $nics -eq 1 ]; then
43 grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
44 lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
45 EOF
46 fi
47
48 cat <<EOF >> $path/config
49 lxc.utsname = $name
50
51 lxc.tty = 4
52 lxc.pts = 1024
53 lxc.rootfs = $rootfs
54 lxc.mount = $path/fstab
55 lxc.arch = $arch
56 lxc.cap.drop = sys_module mac_admin
57
58 lxc.cgroup.devices.deny = a
59 # Allow any mknod (but not using the node)
60 lxc.cgroup.devices.allow = c *:* m
61 lxc.cgroup.devices.allow = b *:* m
62 # /dev/null and zero
63 lxc.cgroup.devices.allow = c 1:3 rwm
64 lxc.cgroup.devices.allow = c 1:5 rwm
65 # consoles
66 lxc.cgroup.devices.allow = c 5:1 rwm
67 lxc.cgroup.devices.allow = c 5:0 rwm
68 #lxc.cgroup.devices.allow = c 4:0 rwm
69 #lxc.cgroup.devices.allow = c 4:1 rwm
70 # /dev/{,u}random
71 lxc.cgroup.devices.allow = c 1:9 rwm
72 lxc.cgroup.devices.allow = c 1:8 rwm
73 lxc.cgroup.devices.allow = c 136:* rwm
74 lxc.cgroup.devices.allow = c 5:2 rwm
75 # rtc
76 lxc.cgroup.devices.allow = c 254:0 rwm
77 #fuse
78 lxc.cgroup.devices.allow = c 10:229 rwm
79 #tun
80 lxc.cgroup.devices.allow = c 10:200 rwm
81 #full
82 lxc.cgroup.devices.allow = c 1:7 rwm
83 #hpet
84 lxc.cgroup.devices.allow = c 10:228 rwm
85 #kvm
86 lxc.cgroup.devices.allow = c 10:232 rwm
87 EOF
88
89 cat <<EOF > $path/fstab
90 proc $rootfs/proc proc nodev,noexec,nosuid 0 0
91 sysfs $rootfs/sys sysfs defaults 0 0
92 EOF
93
94 return 0
95 }
96
97 usage()
98 {
99 cat <<EOF
100 LXC Container configuration for Ubuntu Cloud images.
101
102 Generic Options
103 [ -r | --release <release> ]: Release name of container, defaults to host
104 [ -a | --arch ]: Arhcitecture of container, defaults to host arcitecture
105 [ -C | --cloud ]: Configure container for use with meta-data service, defaults to no
106 [ -T | --tarball ]: Location of tarball
107
108 Options, mutually exclusive of "-C" and "--cloud":
109 [ -i | --hostid ]: HostID for cloud-init, defaults to random string
110 [ -u | --userdata ]: Cloud-init user-data file to configure container on start
111 [ -S | --auth_key ]: SSH Public key file to inject into container
112 [ -L | --nolocales ]: Do not copy host's locales into container
113
114 EOF
115 return 0
116 }
117
118 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: -- "$@")
119 if [ $? -ne 0 ]; then
120 usage $(basename $0)
121 exit 1
122 fi
123 eval set -- "$options"
124
125 release=lucid
126 if [ -f /etc/lsb-release ]; then
127 . /etc/lsb-release
128 case "$DISTRIB_CODENAME" in
129 lucid|maverick|natty|oneiric|precise)
130 release=$DISTRIB_CODENAME
131 ;;
132 esac
133 fi
134
135 arch=$(arch)
136
137 # Code taken from debootstrap
138 if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
139 arch=`/usr/bin/dpkg --print-architecture`
140 elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
141 arch=`/usr/bin/udpkg --print-architecture`
142 else
143 arch=$(arch)
144 if [ "$arch" = "i686" ]; then
145 arch="i386"
146 elif [ "$arch" = "x86_64" ]; then
147 arch="amd64"
148 elif [ "$arch" = "armv7l" ]; then
149 arch="armel"
150 fi
151 fi
152
153 hostarch=$arch
154 cloud=0
155 locales=1
156 flushcache=0
157 while true
158 do
159 case "$1" in
160 -h|--help) usage $0 && exit 0;;
161 -p|--path) path=$2; shift 2;;
162 -n|--name) name=$2; shift 2;;
163 -F|--flush-cache) flushcache=1; shift 1;;
164 -r|--release) release=$2; shift 2;;
165 -a|--arch) arch=$2; shift 2;;
166 -i|--hostid) host_id=$2; shift 2;;
167 -u|--userdata) userdata=$2; shift 2;;
168 -C|--cloud) cloud=1; shift 1;;
169 -S|--auth_key) auth_key=$2; shift 2;;
170 -L|--no_locales) locales=0; shift 2;;
171 -T|--tarball) tarball=$2; shift 2;;
172 --) shift 1; break ;;
173 *) break ;;
174 esac
175 done
176
177 if [ "$arch" == "i686" ]; then
178 arch=i386
179 fi
180
181 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
182 echo "can't create amd64 container on i386"
183 exit 1
184 fi
185
186 if [ $arch != "i386" -a $arch != "amd64" ]; then
187 echo "Only i386 and amd64 are supported by the ubuntu cloud template."
188 exit 1
189 fi
190
191 if [ -z "$path" ]; then
192 echo "'path' parameter is required"
193 exit 1
194 fi
195
196 if [ "$(id -u)" != "0" ]; then
197 echo "This script should be run as 'root'"
198 exit 1
199 fi
200
201 rootfs=$path/rootfs
202
203 type ubuntu-cloudimg-query
204 type wget
205
206 # determine the url, tarball, and directory names
207 # download if needed
208 cache="/var/cache/lxc/cloud-$release"
209
210 mkdir -p $cache
211
212 if [ -n "$tarball" ]; then
213 url2="$tarball"
214 else
215 url1=`ubuntu-cloudimg-query precise daily $arch --format "%{url}\n"`
216 url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
217 fi
218
219 filename=`basename $url2`
220
221 mkdir -p /var/lock/subsys/
222 (
223 flock -n -x 200
224
225 cd $cache
226 if [ $flushcache -eq 1 ]; then
227 echo "Clearing the cached images"
228 rm -f $filename
229 fi
230
231 if [ ! -f $filename ]; then
232 wget $url2
233 fi
234
235 echo "Extracting rootfs"
236 mkdir -p $rootfs
237 cd $rootfs
238 tar -zxf $cache/$filename
239
240
241 if [ $cloud -eq 0 ]; then
242 echo "Configuring for running outside of a cloud environment"
243 echo "If you want to configure for a cloud evironment, please use '-- -C' to create the container"
244
245 seed_d=$rootfs/var/lib/cloud/seed/nocloud-net
246 rhostid=$(uuidgen | cut -c -8)
247 host_id=${hostid:-$rhostid}
248 mkdir -p $seed_d
249
250 cat > "$seed_d/meta-data" <<EOF
251 instance_id: lxc-$host_id
252 EOF
253
254 rm $rootfs/etc/hostname
255
256 if [ $locales -eq 1 ]; then
257 cp /usr/lib/locale/locale-archive $rootfs/usr/lib/locale/locale-archive
258 fi
259
260
261 if [ -n "$auth_key" -a -f "$auth_key" ]; then
262 u_path="/home/ubuntu/.ssh"
263 root_u_path="$rootfs/$u_path"
264 mkdir -p $root_u_path
265 cp $auth_key "$root_u_path/authorized_keys"
266 chroot $rootfs chown -R ubuntu: "$u_path"
267
268 echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
269 fi
270
271 if [ ! -f $userdata ]; then
272 cp $userdata $data_d/user-data
273 else
274
275 if [ -z "$MIRROR" ]; then
276 MIRROR="http://archive.ubuntu.com/ubuntu"
277 fi
278
279 cat > "$seed_d/user-data" <<EOF
280 #cloud-config
281 output: {all: '| tee -a /var/log/cloud-init-output.log'}
282 apt-mirror: $MIRROR
283 manage_etc_hosts: localhost
284 locale: $(/usr/bin/locale | awk -F= '/LANG=/ {print$NF}')
285 EOF
286
287 fi
288
289 chroot $rootfs /usr/sbin/usermod -U ubuntu
290 echo "Please login as user ubuntu with password ubuntu."
291
292 else
293
294 echo "Configured for running in a cloud environment."
295 echo "If you do not have a meta-data service, this container will likely be useless."
296
297 fi
298
299 ) 200>/var/lock/subsys/lxc-ubucloud
300
301 copy_configuration $path $rootfs $name $arch
302
303 echo "Container $name created."
304 exit 0