]>
Commit | Line | Data |
---|---|---|
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 | |
22 | set -e | |
23 | ||
65d8ae9c SM |
24 | STATE_DIR="@LOCALSTATEDIR@" |
25 | HOOK_DIR="@LXCHOOKDIR@" | |
26 | CLONE_HOOK_FN="$HOOK_DIR/ubuntu-cloud-prep" | |
f2a95ee1 | 27 | LXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@" |
65d8ae9c | 28 | |
d1458ac8 SH |
29 | if [ -r /etc/default/lxc ]; then |
30 | . /etc/default/lxc | |
31 | fi | |
32 | ||
1aad9e44 SH |
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 | |
1aad9e44 | 43 | |
d1458ac8 SH |
44 | copy_configuration() |
45 | { | |
46 | path=$1 | |
47 | rootfs=$2 | |
48 | name=$3 | |
49 | arch=$4 | |
42ff5f0f | 50 | release=$5 |
d1458ac8 SH |
51 | |
52 | if [ $arch = "i386" ]; then | |
53 | arch="i686" | |
54 | fi | |
55 | ||
4759162d SH |
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 | |
daaf41b3 | 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 |
4759162d SH |
61 | fi |
62 | ||
f2a95ee1 SG |
63 | # Generate the configuration file |
64 | ## Create the fstab (empty by default) | |
65 | touch $path/fstab | |
d1458ac8 | 66 | |
f2a95ee1 SG |
67 | ## Relocate all the network config entries |
68 | sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config | |
9313e1e6 | 69 | |
f2a95ee1 SG |
70 | ## Relocate any other config entries |
71 | sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config | |
f02ce27d | 72 | |
f2a95ee1 SG |
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 | |
57d116ab | 84 | fi |
d1458ac8 | 85 | |
f2a95ee1 SG |
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 | |
bf7d76cf | 95 | EOF |
d1458ac8 | 96 | |
f2a95ee1 SG |
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 | |
1aad9e44 | 102 | |
0a3673e8 SG |
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 | ||
542939c3 | 115 | # rmdir /dev/shm for containers that have /run/shm |
42ff5f0f SH |
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. | |
5ff33774 | 119 | # NOTE: This can only be removed once 12.04 goes out of support |
542939c3 | 120 | if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then |
5ff33774 | 121 | rmdir $rootfs/dev/shm 2>/dev/null || mv $rootfs/dev/shm $rootfs/dev/shm.bak |
42ff5f0f SH |
122 | ln -s /run/shm $rootfs/dev/shm |
123 | fi | |
124 | ||
d1458ac8 SH |
125 | return 0 |
126 | } | |
127 | ||
128 | usage() | |
129 | { | |
130 | cat <<EOF | |
4759162d SH |
131 | LXC Container configuration for Ubuntu Cloud images. |
132 | ||
133 | Generic Options | |
134 | [ -r | --release <release> ]: Release name of container, defaults to host | |
1897e3bc | 135 | [ --rootfs <path> ]: Path in which rootfs will be placed |
840295ff | 136 | [ -a | --arch ]: Arhcitecture of container, defaults to host architecture |
4759162d | 137 | [ -T | --tarball ]: Location of tarball |
52c8f624 | 138 | [ -d | --debug ]: Run with 'set -x' to debug errors |
427bffc7 | 139 | [ -s | --stream]: Use specified stream rather than 'released' |
4759162d | 140 | |
65d8ae9c SM |
141 | Additionally, clone hooks can be passed through (ie, --userdata). For those, |
142 | see: | |
143 | $CLONE_HOOK_FN --help | |
d1458ac8 SH |
144 | EOF |
145 | return 0 | |
146 | } | |
147 | ||
57d116ab | 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: -- "$@") |
d1458ac8 SH |
149 | if [ $? -ne 0 ]; then |
150 | usage $(basename $0) | |
151 | exit 1 | |
152 | fi | |
153 | eval set -- "$options" | |
154 | ||
57d116ab | 155 | mapped_uid=-1 |
65d8ae9c SM |
156 | # default release is precise, or the systems release if recognized |
157 | release=precise | |
d1458ac8 SH |
158 | if [ -f /etc/lsb-release ]; then |
159 | . /etc/lsb-release | |
65d8ae9c SM |
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 | |
d1458ac8 SH |
165 | fi |
166 | ||
d1458ac8 SH |
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 | |
ed4616b1 | 173 | arch=$(uname -m) |
d1458ac8 SH |
174 | if [ "$arch" = "i686" ]; then |
175 | arch="i386" | |
176 | elif [ "$arch" = "x86_64" ]; then | |
177 | arch="amd64" | |
178 | elif [ "$arch" = "armv7l" ]; then | |
3eecde70 | 179 | # note: arm images don't exist before oneiric; are called armhf in |
b8bced69 | 180 | # precise and later; and are not supported by the query, so we don't actually |
3eecde70 SH |
181 | # support them yet (see check later on). When Query2 is available, |
182 | # we'll use that to enable arm images. | |
8a63c0a9 | 183 | arch="armhf" |
d1458ac8 SH |
184 | fi |
185 | fi | |
186 | ||
52c8f624 | 187 | debug=0 |
d1458ac8 SH |
188 | hostarch=$arch |
189 | cloud=0 | |
4759162d | 190 | locales=1 |
d1458ac8 | 191 | flushcache=0 |
427bffc7 | 192 | stream="released" |
65d8ae9c | 193 | cloneargs=() |
d1458ac8 SH |
194 | while true |
195 | do | |
196 | case "$1" in | |
4759162d SH |
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;; | |
4759162d | 203 | -T|--tarball) tarball=$2; shift 2;; |
52c8f624 | 204 | -d|--debug) debug=1; shift 1;; |
427bffc7 | 205 | -s|--stream) stream=$2; shift 2;; |
65d8ae9c SM |
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;; | |
57d116ab | 212 | --mapped-uid) mapped_uid=$2; shift 2;; |
4759162d | 213 | --) shift 1; break ;; |
d1458ac8 SH |
214 | *) break ;; |
215 | esac | |
216 | done | |
217 | ||
57d116ab | 218 | echo "mapped_uid is .$mapped_uid." |
65d8ae9c SM |
219 | cloneargs=( "--name=$name" "${cloneargs[@]}" ) |
220 | ||
3eecde70 | 221 | if [ $debug -eq 1 ]; then |
52c8f624 SH |
222 | set -x |
223 | fi | |
224 | ||
d1458ac8 SH |
225 | if [ "$arch" == "i686" ]; then |
226 | arch=i386 | |
227 | fi | |
228 | ||
8a63c0a9 SG |
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." | |
d1458ac8 SH |
231 | exit 1 |
232 | fi | |
233 | ||
8a63c0a9 SG |
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" | |
d1458ac8 SH |
252 | exit 1 |
253 | fi | |
254 | ||
427bffc7 SH |
255 | if [ "$stream" != "daily" -a "$stream" != "released" ]; then |
256 | echo "Only 'daily' and 'released' streams are supported" | |
257 | exit 1 | |
258 | fi | |
259 | ||
d1458ac8 SH |
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 | ||
1881820a SH |
270 | # detect rootfs |
271 | config="$path/config" | |
1897e3bc SH |
272 | if [ -z "$rootfs" ]; then |
273 | if grep -q '^lxc.rootfs' $config 2>/dev/null ; then | |
853d58fd | 274 | rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config) |
1897e3bc SH |
275 | else |
276 | rootfs=$path/rootfs | |
277 | fi | |
1881820a | 278 | fi |
d1458ac8 SH |
279 | |
280 | type ubuntu-cloudimg-query | |
281 | type wget | |
282 | ||
283 | # determine the url, tarball, and directory names | |
284 | # download if needed | |
65d8ae9c | 285 | cache="$STATE_DIR/cache/lxc/cloud-$release" |
57d116ab SH |
286 | STATE_DIR="$HOME/.cache/lxc/" |
287 | cache="$HOME/.cache/lxc/cloud-$release" | |
d1458ac8 SH |
288 | |
289 | mkdir -p $cache | |
290 | ||
4759162d | 291 | if [ -n "$tarball" ]; then |
b942e672 | 292 | url2="$tarball" |
4759162d | 293 | else |
b942e672 SM |
294 | url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"` |
295 | url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'` | |
4759162d SH |
296 | fi |
297 | ||
d1458ac8 SH |
298 | filename=`basename $url2` |
299 | ||
f1ccde27 SH |
300 | wgetcleanup() |
301 | { | |
b942e672 | 302 | rm -f $filename |
f1ccde27 SH |
303 | } |
304 | ||
3eecde70 SH |
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` | |
edd3810e | 322 | imgname="$release-*-cloudimg-$arch.img" |
f1ccde27 | 323 | trap buildcleanup EXIT SIGHUP SIGINT SIGTERM |
3eecde70 SH |
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" | |
9c3bc32c | 330 | tar --wildcards -zxf "$tarname" "$imgname" |
3eecde70 | 331 | mount -o loop $imgname $xdir |
9c3bc32c | 332 | (cd $xdir; tar --numeric-owner -cpzf "../$filename" .) |
3eecde70 SH |
333 | umount $xdir |
334 | rm -f $tarname $imgname | |
335 | rmdir $xdir | |
336 | echo "New cloud image cache created" | |
337 | trap EXIT | |
f1ccde27 SH |
338 | trap SIGHUP |
339 | trap SIGINT | |
340 | trap SIGTERM | |
3eecde70 SH |
341 | } |
342 | ||
1aad9e44 | 343 | do_extract_rootfs() { |
d1458ac8 SH |
344 | |
345 | cd $cache | |
346 | if [ $flushcache -eq 1 ]; then | |
347 | echo "Clearing the cached images" | |
348 | rm -f $filename | |
349 | fi | |
350 | ||
f1ccde27 | 351 | trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM |
d1458ac8 | 352 | if [ ! -f $filename ]; then |
b942e672 | 353 | wget $url2 || build_root_tgz $url1 $filename |
d1458ac8 | 354 | fi |
f1ccde27 SH |
355 | trap EXIT |
356 | trap SIGHUP | |
357 | trap SIGINT | |
358 | trap SIGTERM | |
d1458ac8 | 359 | |
3eecde70 | 360 | echo "Extracting container rootfs" |
d1458ac8 SH |
361 | mkdir -p $rootfs |
362 | cd $rootfs | |
57d116ab SH |
363 | if [ $in_userns -eq 1 ]; then |
364 | tar --anchored --exclude="dev/*" --numeric-owner -xpzf "$cache/$filename" | |
f2a95ee1 | 365 | mkdir -p $cache/$filename/dev/pts/ |
57d116ab SH |
366 | else |
367 | tar --numeric-owner -xpzf "$cache/$filename" | |
368 | fi | |
1aad9e44 SH |
369 | } |
370 | ||
371 | if [ -n "$tarball" ]; then | |
372 | do_extract_rootfs | |
373 | else | |
65d8ae9c | 374 | mkdir -p "$STATE_DIR/lock/subsys/" |
1aad9e44 SH |
375 | ( |
376 | flock -x 200 | |
377 | do_extract_rootfs | |
65d8ae9c | 378 | ) 200>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud" |
1aad9e44 | 379 | fi |
d1458ac8 | 380 | |
42ff5f0f | 381 | copy_configuration $path $rootfs $name $arch $release |
d1458ac8 | 382 | |
65d8ae9c SM |
383 | "$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs" |
384 | ||
57d116ab | 385 | if [ $mapped_uid -ne -1 ]; then |
c01c25fc SG |
386 | chown $mapped_uid $path/config |
387 | chown -R $mapped_uid $STATE_DIR | |
388 | chown -R $mapped_uid $cache | |
57d116ab SH |
389 | fi |
390 | ||
d1458ac8 SH |
391 | echo "Container $name created." |
392 | exit 0 | |
b942e672 SM |
393 | |
394 | # vi: ts=4 expandtab |