]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-ubuntu-cloud.in
Move some common Ubuntu config
[mirror_lxc.git] / templates / lxc-ubuntu-cloud.in
1 #!/bin/bash
2
3 # template script for generating ubuntu container for LXC based on released
4 # cloud images.
5 #
6 # Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
22 set -e
23
24 STATE_DIR="@LOCALSTATEDIR@"
25 HOOK_DIR="@LXCHOOKDIR@"
26 CLONE_HOOK_FN="$HOOK_DIR/ubuntu-cloud-prep"
27 LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
28
29 if [ -r /etc/default/lxc ]; then
30 . /etc/default/lxc
31 fi
32
33 am_in_userns() {
34 [ -e /proc/self/uid_map ] || { echo no; return; }
35 [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || { echo yes; return; }
36 line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map)
37 [ "$line" = "0 0 4294967295" ] && { echo no; return; }
38 echo yes
39 }
40
41 in_userns=0
42 [ $(am_in_userns) = "yes" ] && in_userns=1
43
44 copy_configuration()
45 {
46 path=$1
47 rootfs=$2
48 name=$3
49 arch=$4
50 release=$5
51
52 if [ $arch = "i386" ]; then
53 arch="i686"
54 fi
55
56 # if there is exactly one veth network entry, make sure it has an
57 # associated hwaddr.
58 nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
59 if [ $nics -eq 1 ]; then
60 grep -q "^lxc.network.hwaddr" $path/config || sed -i -e "/^lxc\.network\.type[ \t]*=[ \t]*veth/a lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config
61 fi
62
63 # Generate the configuration file
64 ## Create the fstab (empty by default)
65 touch $path/fstab
66
67 ## Relocate all the network config entries
68 sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config
69
70 ## Relocate any other config entries
71 sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
72
73 ## Add all the includes
74 echo "" >> $path/config
75 echo "# Common configuration" >> $path/config
76 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.common.conf" ]; then
77 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.common.conf" >> $path/config
78 fi
79 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" ]; then
80 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" >> $path/config
81 fi
82 if [ $in_userns -eq 1 ] && [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.userns.conf" ]; then
83 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.userns.conf" >> $path/config
84 fi
85
86 ## Add the container-specific config
87 echo "" >> $path/config
88 echo "# Container specific configuration" >> $path/config
89 [ -e "$path/config-auto" ] && cat $path/config-auto >> $path/config && rm $path/config-auto
90 grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
91 cat <<EOF >> $path/config
92 lxc.mount = $path/fstab
93 lxc.utsname = $name
94 lxc.arch = $arch
95 EOF
96
97 ## Re-add the previously removed network config
98 echo "" >> $path/config
99 echo "# Network configuration" >> $path/config
100 cat $path/config-network >> $path/config
101 rm $path/config-network
102
103 # Set initial timezone as on host
104 if [ -f /etc/timezone ]; then
105 cat /etc/timezone > $rootfs/etc/timezone
106 chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
107 elif [ -f /etc/sysconfig/clock ]; then
108 source /etc/sysconfig/clock
109 echo $ZONE > $rootfs/etc/timezone
110 chroot $rootfs dpkg-reconfigure -f noninteractive tzdata
111 else
112 echo "Timezone in container is not configured. Adjust it manually."
113 fi
114
115 # rmdir /dev/shm for containers that have /run/shm
116 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
117 # get bind mounted to the host's /run/shm. So try to rmdir
118 # it, and in case that fails move it out of the way.
119 # NOTE: This can only be removed once 12.04 goes out of support
120 if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then
121 rmdir $rootfs/dev/shm 2>/dev/null || mv $rootfs/dev/shm $rootfs/dev/shm.bak
122 ln -s /run/shm $rootfs/dev/shm
123 fi
124
125 return 0
126 }
127
128 usage()
129 {
130 cat <<EOF
131 LXC Container configuration for Ubuntu Cloud images.
132
133 Generic Options
134 [ -r | --release <release> ]: Release name of container, defaults to host
135 [ --rootfs <path> ]: Path in which rootfs will be placed
136 [ -a | --arch ]: Arhcitecture of container, defaults to host architecture
137 [ -T | --tarball ]: Location of tarball
138 [ -d | --debug ]: Run with 'set -x' to debug errors
139 [ -s | --stream]: Use specified stream rather than 'released'
140
141 Additionally, clone hooks can be passed through (ie, --userdata). For those,
142 see:
143 $CLONE_HOOK_FN --help
144 EOF
145 return 0
146 }
147
148 options=$(getopt -o a:hp:r:n:Fi:CLS:T:ds:u: -l arch:,help,rootfs:,path:,release:,name:,flush-cache,hostid:,auth-key:,cloud,no_locales,tarball:,debug,stream:,userdata:,mapped-uid: -- "$@")
149 if [ $? -ne 0 ]; then
150 usage $(basename $0)
151 exit 1
152 fi
153 eval set -- "$options"
154
155 mapped_uid=-1
156 # default release is precise, or the systems release if recognized
157 release=precise
158 if [ -f /etc/lsb-release ]; then
159 . /etc/lsb-release
160 rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
161 rels="lucid natty oneiric precise quantal raring saucy"
162 for r in $rels; do
163 [ "$DISTRIB_CODENAME" = "$r" ] && release="$r"
164 done
165 fi
166
167 # Code taken from debootstrap
168 if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
169 arch=`/usr/bin/dpkg --print-architecture`
170 elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
171 arch=`/usr/bin/udpkg --print-architecture`
172 else
173 arch=$(uname -m)
174 if [ "$arch" = "i686" ]; then
175 arch="i386"
176 elif [ "$arch" = "x86_64" ]; then
177 arch="amd64"
178 elif [ "$arch" = "armv7l" ]; then
179 # note: arm images don't exist before oneiric; are called armhf in
180 # precise and later; and are not supported by the query, so we don't actually
181 # support them yet (see check later on). When Query2 is available,
182 # we'll use that to enable arm images.
183 arch="armhf"
184 fi
185 fi
186
187 debug=0
188 hostarch=$arch
189 cloud=0
190 locales=1
191 flushcache=0
192 stream="released"
193 cloneargs=()
194 while true
195 do
196 case "$1" in
197 -h|--help) usage $0 && exit 0;;
198 -p|--path) path=$2; shift 2;;
199 -n|--name) name=$2; shift 2;;
200 -F|--flush-cache) flushcache=1; shift 1;;
201 -r|--release) release=$2; shift 2;;
202 -a|--arch) arch=$2; shift 2;;
203 -T|--tarball) tarball=$2; shift 2;;
204 -d|--debug) debug=1; shift 1;;
205 -s|--stream) stream=$2; shift 2;;
206 --rootfs) rootfs=$2; shift 2;;
207 -L|--no?locales) cloneargs[${#cloneargs[@]}]="--no-locales"; shift 1;;
208 -i|--hostid) cloneargs[${#cloneargs[@]}]="--hostid=$2"; shift 2;;
209 -u|--userdata) cloneargs[${#cloneargs[@]}]="--userdata=$2"; shift 2;;
210 -C|--cloud) cloneargs[${#cloneargs[@]}]="--cloud"; shift 1;;
211 -S|--auth-key) cloneargs[${#cloneargs[@]}]="--auth-key=$2"; shift 2;;
212 --mapped-uid) mapped_uid=$2; shift 2;;
213 --) shift 1; break ;;
214 *) break ;;
215 esac
216 done
217
218 echo "mapped_uid is .$mapped_uid."
219 cloneargs=( "--name=$name" "${cloneargs[@]}" )
220
221 if [ $debug -eq 1 ]; then
222 set -x
223 fi
224
225 if [ "$arch" == "i686" ]; then
226 arch=i386
227 fi
228
229 if [ $arch != "i386" -a $arch != "amd64" -a $arch != "armhf" -a $arch != "armel" ]; then
230 echo "Only i386, amd64, armel and armhf are supported by the ubuntu cloud template."
231 exit 1
232 fi
233
234 if [ $hostarch != "i386" -a $hostarch != "amd64" -a $hostarch != "armhf" -a $hostarch != "armel" ]; then
235 echo "Only i386, amd64, armel and armhf are supported as host."
236 exit 1
237 fi
238
239 if [ $hostarch = "amd64" -a $arch != "amd64" -a $arch != "i386" ]; then
240 echo "can't create $arch container on $hostarch"
241 exit 1
242 fi
243
244 if [ $hostarch = "i386" -a $arch != "i386" ]; then
245 echo "can't create $arch container on $hostarch"
246 exit 1
247 fi
248
249 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
250 [ $arch != "armhf" -a $arch != "armel" ]; then
251 echo "can't create $arch container on $hostarch"
252 exit 1
253 fi
254
255 if [ "$stream" != "daily" -a "$stream" != "released" ]; then
256 echo "Only 'daily' and 'released' streams are supported"
257 exit 1
258 fi
259
260 if [ -z "$path" ]; then
261 echo "'path' parameter is required"
262 exit 1
263 fi
264
265 if [ "$(id -u)" != "0" ]; then
266 echo "This script should be run as 'root'"
267 exit 1
268 fi
269
270 # detect rootfs
271 config="$path/config"
272 if [ -z "$rootfs" ]; then
273 if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
274 rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
275 else
276 rootfs=$path/rootfs
277 fi
278 fi
279
280 type ubuntu-cloudimg-query
281 type wget
282
283 # determine the url, tarball, and directory names
284 # download if needed
285 cache="$STATE_DIR/cache/lxc/cloud-$release"
286 STATE_DIR="$HOME/.cache/lxc/"
287 cache="$HOME/.cache/lxc/cloud-$release"
288
289 mkdir -p $cache
290
291 if [ -n "$tarball" ]; then
292 url2="$tarball"
293 else
294 url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`
295 url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
296 fi
297
298 filename=`basename $url2`
299
300 wgetcleanup()
301 {
302 rm -f $filename
303 }
304
305 buildcleanup()
306 {
307 cd $rootfs
308 umount -l $cache/$xdir || true
309 rm -rf $cache
310 }
311
312 # if the release doesn't have a *-rootfs.tar.gz, then create one from the
313 # cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
314 # a tarball from the mounted image.
315 build_root_tgz()
316 {
317 url=$1
318 filename=$2
319
320 xdir=`mktemp -d -p .`
321 tarname=`basename $url`
322 imgname="$release-*-cloudimg-$arch.img"
323 trap buildcleanup EXIT SIGHUP SIGINT SIGTERM
324 if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
325 rm -f $tarname
326 echo "Downloading cloud image from $url"
327 wget $url || { echo "Couldn't find cloud image $url."; exit 1; }
328 fi
329 echo "Creating new cached cloud image rootfs"
330 tar --wildcards -zxf "$tarname" "$imgname"
331 mount -o loop $imgname $xdir
332 (cd $xdir; tar --numeric-owner -cpzf "../$filename" .)
333 umount $xdir
334 rm -f $tarname $imgname
335 rmdir $xdir
336 echo "New cloud image cache created"
337 trap EXIT
338 trap SIGHUP
339 trap SIGINT
340 trap SIGTERM
341 }
342
343 do_extract_rootfs() {
344
345 cd $cache
346 if [ $flushcache -eq 1 ]; then
347 echo "Clearing the cached images"
348 rm -f $filename
349 fi
350
351 trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM
352 if [ ! -f $filename ]; then
353 wget $url2 || build_root_tgz $url1 $filename
354 fi
355 trap EXIT
356 trap SIGHUP
357 trap SIGINT
358 trap SIGTERM
359
360 echo "Extracting container rootfs"
361 mkdir -p $rootfs
362 cd $rootfs
363 if [ $in_userns -eq 1 ]; then
364 tar --anchored --exclude="dev/*" --numeric-owner -xpzf "$cache/$filename"
365 mkdir -p $cache/$filename/dev/pts/
366 else
367 tar --numeric-owner -xpzf "$cache/$filename"
368 fi
369 }
370
371 if [ -n "$tarball" ]; then
372 do_extract_rootfs
373 else
374 mkdir -p "$STATE_DIR/lock/subsys/"
375 (
376 flock -x 200
377 do_extract_rootfs
378 ) 200>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud"
379 fi
380
381 copy_configuration $path $rootfs $name $arch $release
382
383 "$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
384
385 if [ $mapped_uid -ne -1 ]; then
386 chown $mapped_uid $path/config
387 chown -R $mapped_uid $STATE_DIR
388 chown -R $mapped_uid $cache
389 fi
390
391 echo "Container $name created."
392 exit 0
393
394 # vi: ts=4 expandtab