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