]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-ubuntu-cloud.in
Description: fix handling of non-precise cloud image format
[mirror_lxc.git] / templates / lxc-ubuntu-cloud.in
CommitLineData
d1458ac8
SH
1#!/bin/bash
2
3eecde70 3# template script for generating ubuntu container for LXC based on released cloud
d1458ac8
SH
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
22set -e
23
24if [ -r /etc/default/lxc ]; then
25 . /etc/default/lxc
26fi
27
28copy_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
4759162d
SH
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
44lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
45EOF
46 fi
47
d1458ac8
SH
48 cat <<EOF >> $path/config
49lxc.utsname = $name
50
51lxc.tty = 4
52lxc.pts = 1024
53lxc.rootfs = $rootfs
54lxc.mount = $path/fstab
55lxc.arch = $arch
56lxc.cap.drop = sys_module mac_admin
57
58lxc.cgroup.devices.deny = a
59# Allow any mknod (but not using the node)
60lxc.cgroup.devices.allow = c *:* m
61lxc.cgroup.devices.allow = b *:* m
62# /dev/null and zero
63lxc.cgroup.devices.allow = c 1:3 rwm
64lxc.cgroup.devices.allow = c 1:5 rwm
65# consoles
66lxc.cgroup.devices.allow = c 5:1 rwm
67lxc.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
71lxc.cgroup.devices.allow = c 1:9 rwm
72lxc.cgroup.devices.allow = c 1:8 rwm
73lxc.cgroup.devices.allow = c 136:* rwm
74lxc.cgroup.devices.allow = c 5:2 rwm
75# rtc
76lxc.cgroup.devices.allow = c 254:0 rwm
77#fuse
78lxc.cgroup.devices.allow = c 10:229 rwm
79#tun
80lxc.cgroup.devices.allow = c 10:200 rwm
81#full
82lxc.cgroup.devices.allow = c 1:7 rwm
83#hpet
84lxc.cgroup.devices.allow = c 10:228 rwm
85#kvm
86lxc.cgroup.devices.allow = c 10:232 rwm
87EOF
88
89 cat <<EOF > $path/fstab
90proc $rootfs/proc proc nodev,noexec,nosuid 0 0
91sysfs $rootfs/sys sysfs defaults 0 0
92EOF
93
94 return 0
95}
96
97usage()
98{
99 cat <<EOF
4759162d
SH
100LXC Container configuration for Ubuntu Cloud images.
101
102Generic 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
52c8f624 107[ -d | --debug ]: Run with 'set -x' to debug errors
4759162d
SH
108
109Options, mutually exclusive of "-C" and "--cloud":
110 [ -i | --hostid ]: HostID for cloud-init, defaults to random string
111 [ -u | --userdata ]: Cloud-init user-data file to configure container on start
52c8f624 112 [ -S | --auth-key ]: SSH Public key file to inject into container
4759162d
SH
113 [ -L | --nolocales ]: Do not copy host's locales into container
114
d1458ac8
SH
115EOF
116 return 0
117}
118
52c8f624 119options=$(getopt -o a:hp:r:n:Fi:CLS:T:d -l arch:,help,path:,release:,name:,flush-cache,hostid:,auth-key:,cloud,no_locales,tarball:,debug -- "$@")
d1458ac8
SH
120if [ $? -ne 0 ]; then
121 usage $(basename $0)
122 exit 1
123fi
124eval set -- "$options"
125
126release=lucid
127if [ -f /etc/lsb-release ]; then
128 . /etc/lsb-release
129 case "$DISTRIB_CODENAME" in
130 lucid|maverick|natty|oneiric|precise)
131 release=$DISTRIB_CODENAME
132 ;;
133 esac
134fi
135
136arch=$(arch)
137
138# Code taken from debootstrap
139if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
140 arch=`/usr/bin/dpkg --print-architecture`
141elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
142 arch=`/usr/bin/udpkg --print-architecture`
143else
144 arch=$(arch)
145 if [ "$arch" = "i686" ]; then
146 arch="i386"
147 elif [ "$arch" = "x86_64" ]; then
148 arch="amd64"
149 elif [ "$arch" = "armv7l" ]; then
3eecde70
SH
150 # note: arm images don't exist before oneiric; are called armhf in
151 # precise; and are not supported by the query, so we don't actually
152 # support them yet (see check later on). When Query2 is available,
153 # we'll use that to enable arm images.
d1458ac8
SH
154 arch="armel"
155 fi
156fi
157
52c8f624 158debug=0
d1458ac8
SH
159hostarch=$arch
160cloud=0
4759162d 161locales=1
d1458ac8
SH
162flushcache=0
163while true
164do
165 case "$1" in
4759162d
SH
166 -h|--help) usage $0 && exit 0;;
167 -p|--path) path=$2; shift 2;;
168 -n|--name) name=$2; shift 2;;
169 -F|--flush-cache) flushcache=1; shift 1;;
170 -r|--release) release=$2; shift 2;;
171 -a|--arch) arch=$2; shift 2;;
172 -i|--hostid) host_id=$2; shift 2;;
173 -u|--userdata) userdata=$2; shift 2;;
174 -C|--cloud) cloud=1; shift 1;;
52c8f624 175 -S|--auth-key) auth_key=$2; shift 2;;
4759162d
SH
176 -L|--no_locales) locales=0; shift 2;;
177 -T|--tarball) tarball=$2; shift 2;;
52c8f624 178 -d|--debug) debug=1; shift 1;;
4759162d 179 --) shift 1; break ;;
d1458ac8
SH
180 *) break ;;
181 esac
182done
183
3eecde70 184if [ $debug -eq 1 ]; then
52c8f624
SH
185 set -x
186fi
187
d1458ac8
SH
188if [ "$arch" == "i686" ]; then
189 arch=i386
190fi
191
192if [ $hostarch = "i386" -a $arch = "amd64" ]; then
193 echo "can't create amd64 container on i386"
194 exit 1
195fi
196
197if [ $arch != "i386" -a $arch != "amd64" ]; then
198 echo "Only i386 and amd64 are supported by the ubuntu cloud template."
199 exit 1
200fi
201
202if [ -z "$path" ]; then
203 echo "'path' parameter is required"
204 exit 1
205fi
206
207if [ "$(id -u)" != "0" ]; then
208 echo "This script should be run as 'root'"
209 exit 1
210fi
211
212rootfs=$path/rootfs
213
214type ubuntu-cloudimg-query
215type wget
216
217# determine the url, tarball, and directory names
218# download if needed
219cache="/var/cache/lxc/cloud-$release"
220
221mkdir -p $cache
222
4759162d
SH
223if [ -n "$tarball" ]; then
224 url2="$tarball"
225else
3eecde70 226 url1=`ubuntu-cloudimg-query $release released $arch --format "%{url}\n"`
4759162d
SH
227 url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
228fi
229
d1458ac8
SH
230filename=`basename $url2`
231
3eecde70
SH
232buildcleanup()
233{
234 cd $rootfs
235 umount -l $cache/$xdir || true
236 rm -rf $cache
237}
238
239# if the release doesn't have a *-rootfs.tar.gz, then create one from the
240# cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
241# a tarball from the mounted image.
242build_root_tgz()
243{
244 url=$1
245 filename=$2
246
247 xdir=`mktemp -d -p .`
248 tarname=`basename $url`
249 imgname="`basename $tarname .tar.gz`.img"
250 trap buildcleanup EXIT
251 if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
252 rm -f $tarname
253 echo "Downloading cloud image from $url"
254 wget $url || { echo "Couldn't find cloud image $url."; exit 1; }
255 fi
256 echo "Creating new cached cloud image rootfs"
257 tar zxf $tarname $imgname
258 mount -o loop $imgname $xdir
259 (cd $xdir; tar zcf ../$filename .)
260 umount $xdir
261 rm -f $tarname $imgname
262 rmdir $xdir
263 echo "New cloud image cache created"
264 trap EXIT
265}
266
d1458ac8
SH
267mkdir -p /var/lock/subsys/
268(
269 flock -n -x 200
270
271 cd $cache
272 if [ $flushcache -eq 1 ]; then
273 echo "Clearing the cached images"
274 rm -f $filename
275 fi
276
277 if [ ! -f $filename ]; then
3eecde70 278 wget $url2 || build_root_tgz $url1 $filename
d1458ac8
SH
279 fi
280
3eecde70 281 echo "Extracting container rootfs"
d1458ac8
SH
282 mkdir -p $rootfs
283 cd $rootfs
284 tar -zxf $cache/$filename
285
286
287 if [ $cloud -eq 0 ]; then
288 echo "Configuring for running outside of a cloud environment"
289 echo "If you want to configure for a cloud evironment, please use '-- -C' to create the container"
290
291 seed_d=$rootfs/var/lib/cloud/seed/nocloud-net
292 rhostid=$(uuidgen | cut -c -8)
4759162d
SH
293 host_id=${hostid:-$rhostid}
294 mkdir -p $seed_d
d1458ac8
SH
295
296 cat > "$seed_d/meta-data" <<EOF
297instance_id: lxc-$host_id
298EOF
299
4759162d
SH
300 rm $rootfs/etc/hostname
301
302 if [ $locales -eq 1 ]; then
303 cp /usr/lib/locale/locale-archive $rootfs/usr/lib/locale/locale-archive
304 fi
305
306
307 if [ -n "$auth_key" -a -f "$auth_key" ]; then
308 u_path="/home/ubuntu/.ssh"
309 root_u_path="$rootfs/$u_path"
310 mkdir -p $root_u_path
311 cp $auth_key "$root_u_path/authorized_keys"
312 chroot $rootfs chown -R ubuntu: "$u_path"
313
314 echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
315 fi
316
d1458ac8 317 if [ ! -f $userdata ]; then
4759162d 318 cp $userdata $data_d/user-data
d1458ac8 319 else
4759162d 320
d1458ac8
SH
321 if [ -z "$MIRROR" ]; then
322 MIRROR="http://archive.ubuntu.com/ubuntu"
323 fi
4759162d
SH
324
325 cat > "$seed_d/user-data" <<EOF
326#cloud-config
327output: {all: '| tee -a /var/log/cloud-init-output.log'}
328apt-mirror: $MIRROR
329manage_etc_hosts: localhost
330locale: $(/usr/bin/locale | awk -F= '/LANG=/ {print$NF}')
331EOF
332
d1458ac8
SH
333 fi
334
335 chroot $rootfs /usr/sbin/usermod -U ubuntu
3eecde70 336 echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
d1458ac8
SH
337 echo "Please login as user ubuntu with password ubuntu."
338
339 else
340
341 echo "Configured for running in a cloud environment."
342 echo "If you do not have a meta-data service, this container will likely be useless."
343
344 fi
345
346) 200>/var/lock/subsys/lxc-ubucloud
347
348copy_configuration $path $rootfs $name $arch
349
350echo "Container $name created."
351exit 0