]> git.proxmox.com Git - mirror_lxc.git/blame - templates/lxc-ubuntu-cloud.in
Merge pull request #2055 from marcosps/cgfsng_debug
[mirror_lxc.git] / templates / lxc-ubuntu-cloud.in
CommitLineData
d1458ac8
SH
1#!/bin/bash
2
65d8ae9c
SM
3# template script for generating ubuntu container for LXC based on released
4# cloud images.
d1458ac8
SH
5#
6# Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
7#
acbb59f5
SH
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.
d1458ac8 12
acbb59f5
SH
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.
d1458ac8 17
acbb59f5
SH
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
d1458ac8
SH
21
22set -e
23
65d8ae9c
SM
24STATE_DIR="@LOCALSTATEDIR@"
25HOOK_DIR="@LXCHOOKDIR@"
26CLONE_HOOK_FN="$HOOK_DIR/ubuntu-cloud-prep"
f2a95ee1 27LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
0815a592 28KNOWN_RELEASES="precise trusty xenial yakkety zesty"
ad3f14ab 29skip_arch_check=${UCTEMPLATE_SKIP_ARCH_CHECK:-0}
65d8ae9c 30
207bf0e4
SG
31# Make sure the usual locations are in PATH
32export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
33
d1458ac8
SH
34if [ -r /etc/default/lxc ]; then
35 . /etc/default/lxc
36fi
37
1aad9e44
SH
38am_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
46in_userns=0
47[ $(am_in_userns) = "yes" ] && in_userns=1
1aad9e44 48
d1458ac8
SH
49copy_configuration()
50{
51 path=$1
52 rootfs=$2
53 name=$3
54 arch=$4
42ff5f0f 55 release=$5
d1458ac8
SH
56
57 if [ $arch = "i386" ]; then
58 arch="i686"
59 fi
60
4759162d
SH
61 # if there is exactly one veth network entry, make sure it has an
62 # associated hwaddr.
7fa3f2e9 63 nics=`grep -e '^lxc\.net\.0\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
4759162d 64 if [ $nics -eq 1 ]; then
7fa3f2e9 65 grep -q "^lxc.net.0.hwaddr" $path/config || sed -i -e "/^lxc\.net\.0\.type[ \t]*=[ \t]*veth/a lxc.net.0.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config
4759162d
SH
66 fi
67
f2a95ee1 68 # Generate the configuration file
f2a95ee1 69 ## Relocate all the network config entries
7fa3f2e9 70 sed -i -e "/lxc.net.0/{w ${path}/config-network" -e "d}" $path/config
9313e1e6 71
f2a95ee1
SG
72 ## Relocate any other config entries
73 sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
f02ce27d 74
f2a95ee1
SG
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
57d116ab 86 fi
d1458ac8 87
f2a95ee1
SG
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
7a96a068 92 grep -q "^lxc.rootfs.path" $path/config 2>/dev/null || echo "lxc.rootfs.path = $rootfs" >> $path/config
f2a95ee1 93 cat <<EOF >> $path/config
b67771bc 94lxc.uts.name = $name
f2a95ee1 95lxc.arch = $arch
bf7d76cf 96EOF
d1458ac8 97
f2a95ee1
SG
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
1aad9e44 103
0a3673e8
SG
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
17abf278 109 . /etc/sysconfig/clock
0a3673e8
SG
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
542939c3 116 # rmdir /dev/shm for containers that have /run/shm
42ff5f0f
SH
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.
5ff33774 120 # NOTE: This can only be removed once 12.04 goes out of support
68c36a30 121 if [ ! -L $rootfs/dev/shm ] && [ -e $rootfs/dev/shm ]; then
5ff33774 122 rmdir $rootfs/dev/shm 2>/dev/null || mv $rootfs/dev/shm $rootfs/dev/shm.bak
42ff5f0f
SH
123 ln -s /run/shm $rootfs/dev/shm
124 fi
125
d1458ac8
SH
126 return 0
127}
128
129usage()
130{
131 cat <<EOF
4759162d
SH
132LXC Container configuration for Ubuntu Cloud images.
133
134Generic Options
135[ -r | --release <release> ]: Release name of container, defaults to host
1897e3bc 136[ --rootfs <path> ]: Path in which rootfs will be placed
3f5f5d99 137[ -a | --arch ]: Architecture of container, defaults to host architecture
4759162d 138[ -T | --tarball ]: Location of tarball
52c8f624 139[ -d | --debug ]: Run with 'set -x' to debug errors
ad3f14ab 140[ -s | --stream]: Use specified stream rather than 'tryreleased'
4759162d 141
65d8ae9c
SM
142Additionally, clone hooks can be passed through (ie, --userdata). For those,
143see:
144 $CLONE_HOOK_FN --help
d1458ac8
SH
145EOF
146 return 0
147}
148
5d066f24 149options=$(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:,vendordata:,mapped-uid:,mapped-gid: -- "$@")
d1458ac8
SH
150if [ $? -ne 0 ]; then
151 usage $(basename $0)
152 exit 1
153fi
154eval set -- "$options"
155
57d116ab 156mapped_uid=-1
2133f58c 157mapped_gid=-1
f8b2a49c
SG
158# default release is trusty, or the systems release if recognized
159release=trusty
d1458ac8
SH
160if [ -f /etc/lsb-release ]; then
161 . /etc/lsb-release
65d8ae9c 162 rels=$(ubuntu-distro-info --supported 2>/dev/null) ||
ad3f14ab 163 rels="$KNOWN_RELEASES"
65d8ae9c
SM
164 for r in $rels; do
165 [ "$DISTRIB_CODENAME" = "$r" ] && release="$r"
166 done
d1458ac8
SH
167fi
168
d1458ac8
SH
169# Code taken from debootstrap
170if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
171 arch=`/usr/bin/dpkg --print-architecture`
172elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
173 arch=`/usr/bin/udpkg --print-architecture`
174else
ed4616b1 175 arch=$(uname -m)
d1458ac8
SH
176 if [ "$arch" = "i686" ]; then
177 arch="i386"
178 elif [ "$arch" = "x86_64" ]; then
179 arch="amd64"
180 elif [ "$arch" = "armv7l" ]; then
3eecde70 181 # note: arm images don't exist before oneiric; are called armhf in
f8b2a49c 182 # trusty and later; and are not supported by the query, so we don't actually
3eecde70
SH
183 # support them yet (see check later on). When Query2 is available,
184 # we'll use that to enable arm images.
8a63c0a9 185 arch="armhf"
8a3c76b2
SG
186 elif [ "$arch" = "aarch64" ]; then
187 arch="arm64"
188 elif [ "$arch" = "ppc64le" ]; then
189 arch="ppc64el"
d1458ac8
SH
190 fi
191fi
192
52c8f624 193debug=0
d1458ac8
SH
194hostarch=$arch
195cloud=0
4759162d 196locales=1
d1458ac8 197flushcache=0
4b954f12 198stream="tryreleased"
65d8ae9c 199cloneargs=()
d1458ac8
SH
200while true
201do
202 case "$1" in
4a311045 203 -h|--help) usage $0 && exit 1;;
4759162d
SH
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;;
4759162d 209 -T|--tarball) tarball=$2; shift 2;;
52c8f624 210 -d|--debug) debug=1; shift 1;;
427bffc7 211 -s|--stream) stream=$2; shift 2;;
65d8ae9c
SM
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;;
5d066f24 216 -V|--vendordata) cloneargs[${#cloneargs[@]}]="--vendordata=$2"; shift 2;;
65d8ae9c
SM
217 -C|--cloud) cloneargs[${#cloneargs[@]}]="--cloud"; shift 1;;
218 -S|--auth-key) cloneargs[${#cloneargs[@]}]="--auth-key=$2"; shift 2;;
57d116ab 219 --mapped-uid) mapped_uid=$2; shift 2;;
2133f58c 220 --mapped-gid) mapped_gid=$2; shift 2;;
4759162d 221 --) shift 1; break ;;
d1458ac8
SH
222 *) break ;;
223 esac
224done
225
65d8ae9c
SM
226cloneargs=( "--name=$name" "${cloneargs[@]}" )
227
3eecde70 228if [ $debug -eq 1 ]; then
52c8f624
SH
229 set -x
230fi
231
17abf278 232if [ "$arch" = "i686" ]; then
d1458ac8
SH
233 arch=i386
234fi
235
ad3f14ab
SM
236if [ "$skip_arch_check" = "0" ]; then
237 case "$hostarch:$arch" in
238 $arch:$arch) : ;; # the host == container
d4641754
SG
239 amd64:i386) :;; # supported "cross"
240 arm64:arm*) :;; # supported "cross"
241 armel:armhf) :;; # supported "cross"
242 armhf:armel) :;; # supported "cross"
ad3f14ab
SM
243 *) echo "cannot create '$arch' container on hostarch '$hostarch'";
244 exit 1;;
245 esac
d1458ac8
SH
246fi
247
4b954f12
DJL
248if [ "$stream" != "daily" -a "$stream" != "released" -a "$stream" != "tryreleased" ]; then
249 echo "Only 'daily' and 'released' and 'tryreleased' streams are supported"
427bffc7
SH
250 exit 1
251fi
252
d1458ac8
SH
253if [ -z "$path" ]; then
254 echo "'path' parameter is required"
255 exit 1
256fi
257
258if [ "$(id -u)" != "0" ]; then
259 echo "This script should be run as 'root'"
260 exit 1
261fi
262
1881820a
SH
263# detect rootfs
264config="$path/config"
1897e3bc 265if [ -z "$rootfs" ]; then
7a96a068
CB
266 if grep -q '^lxc.rootfs.path' $config 2>/dev/null ; then
267 rootfs=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $config)
1897e3bc
SH
268 else
269 rootfs=$path/rootfs
270 fi
1881820a 271fi
d1458ac8
SH
272
273type ubuntu-cloudimg-query
274type wget
275
276# determine the url, tarball, and directory names
277# download if needed
6dc6f80b
KC
278# Allow the cache base to be set by environment variable
279cache=${LXC_CACHE_PATH:-"$STATE_DIR/cache/lxc"}/cloud-$release
9cde0368 280if [ $in_userns -eq 1 ]; then
6dc6f80b
KC
281 STATE_DIR="$HOME/.cache/lxc"
282 cache=${LXC_CACHE_PATH:-"$STATE_DIR"}/cloud-$release
9cde0368 283fi
d1458ac8
SH
284
285mkdir -p $cache
286
4b954f12
DJL
287if [ "$stream" = "tryreleased" ]; then
288 stream=released
289 ubuntu-cloudimg-query $release $stream $arch 1>/dev/null 2>/dev/null || stream=daily
290fi
291
4759162d 292if [ -n "$tarball" ]; then
b942e672 293 url2="$tarball"
4759162d 294else
ad3f14ab
SM
295 if ! url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`; then
296 echo "There is no download available for release=$release, stream=$stream, arch=$arch"
297 [ "$stream" = "daily" ] || echo "You may try with '--stream=daily'"
4a311045 298 exit 1
ad3f14ab 299 fi
5d58fc90
PHL
300 if [ "$release" = "precise" ] || [ "$release" = "trusty" ]; then
301 url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/' -e 's/.tar.gz/.tar.xz/'`
302 else
303 url2=`echo $url1 | sed -e 's/.tar.gz/.squashfs/'`
304 fi
4759162d
SH
305fi
306
d1458ac8
SH
307filename=`basename $url2`
308
f1ccde27
SH
309wgetcleanup()
310{
b942e672 311 rm -f $filename
f1ccde27
SH
312}
313
1aad9e44 314do_extract_rootfs() {
d1458ac8
SH
315
316 cd $cache
317 if [ $flushcache -eq 1 ]; then
318 echo "Clearing the cached images"
319 rm -f $filename
320 fi
321
f1ccde27 322 trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM
d1458ac8 323 if [ ! -f $filename ]; then
27c278a7 324 wget $url2
d1458ac8 325 fi
f1ccde27
SH
326 trap EXIT
327 trap SIGHUP
328 trap SIGINT
329 trap SIGTERM
d1458ac8 330
3eecde70 331 echo "Extracting container rootfs"
d1458ac8
SH
332 mkdir -p $rootfs
333 cd $rootfs
5d58fc90
PHL
334 if [ "${filename##*.}" = "squashfs" ]; then
335 unsquashfs -n -f -d "$rootfs" "$cache/$filename"
57d116ab 336 else
5d58fc90
PHL
337 if [ $in_userns -eq 1 ]; then
338 tar --anchored --exclude="dev/*" --numeric-owner -xpf "$cache/$filename"
339 mkdir -p $rootfs/dev/pts/
340 else
341 tar --numeric-owner -xpf "$cache/$filename"
342 fi
57d116ab 343 fi
1aad9e44
SH
344}
345
346if [ -n "$tarball" ]; then
347 do_extract_rootfs
348else
65d8ae9c 349 mkdir -p "$STATE_DIR/lock/subsys/"
1aad9e44 350 (
17abf278 351 flock -x 9
1aad9e44 352 do_extract_rootfs
17abf278 353 ) 9>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud"
1aad9e44 354fi
d1458ac8 355
42ff5f0f 356copy_configuration $path $rootfs $name $arch $release
d1458ac8 357
65d8ae9c
SM
358"$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
359
57d116ab 360if [ $mapped_uid -ne -1 ]; then
c01c25fc
SG
361 chown $mapped_uid $path/config
362 chown -R $mapped_uid $STATE_DIR
363 chown -R $mapped_uid $cache
57d116ab 364fi
2133f58c
SH
365if [ $mapped_gid -ne -1 ]; then
366 chgrp $mapped_gid $path/config
367 chgrp -R $mapped_gid $STATE_DIR
368 chgrp -R $mapped_gid $cache
369fi
57d116ab 370
d1458ac8
SH
371echo "Container $name created."
372exit 0
b942e672
SM
373
374# vi: ts=4 expandtab