]>
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@" |
ad3f14ab SM |
28 | KNOWN_RELEASES="lucid precise quantal saucy trusty" |
29 | skip_arch_check=${UCTEMPLATE_SKIP_ARCH_CHECK:-0} | |
65d8ae9c | 30 | |
d1458ac8 SH |
31 | if [ -r /etc/default/lxc ]; then |
32 | . /etc/default/lxc | |
33 | fi | |
34 | ||
1aad9e44 SH |
35 | am_in_userns() { |
36 | [ -e /proc/self/uid_map ] || { echo no; return; } | |
37 | [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || { echo yes; return; } | |
38 | line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map) | |
39 | [ "$line" = "0 0 4294967295" ] && { echo no; return; } | |
40 | echo yes | |
41 | } | |
42 | ||
43 | in_userns=0 | |
44 | [ $(am_in_userns) = "yes" ] && in_userns=1 | |
1aad9e44 | 45 | |
d1458ac8 SH |
46 | copy_configuration() |
47 | { | |
48 | path=$1 | |
49 | rootfs=$2 | |
50 | name=$3 | |
51 | arch=$4 | |
42ff5f0f | 52 | release=$5 |
d1458ac8 SH |
53 | |
54 | if [ $arch = "i386" ]; then | |
55 | arch="i686" | |
56 | fi | |
57 | ||
4759162d SH |
58 | # if there is exactly one veth network entry, make sure it has an |
59 | # associated hwaddr. | |
60 | nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l` | |
61 | if [ $nics -eq 1 ]; then | |
daaf41b3 | 62 | 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 |
63 | fi |
64 | ||
f2a95ee1 SG |
65 | # Generate the configuration file |
66 | ## Create the fstab (empty by default) | |
67 | touch $path/fstab | |
d1458ac8 | 68 | |
f2a95ee1 SG |
69 | ## Relocate all the network config entries |
70 | sed -i -e "/lxc.network/{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 | |
92 | grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config | |
93 | cat <<EOF >> $path/config | |
94 | lxc.mount = $path/fstab | |
95 | lxc.utsname = $name | |
96 | lxc.arch = $arch | |
bf7d76cf | 97 | EOF |
d1458ac8 | 98 | |
f2a95ee1 SG |
99 | ## Re-add the previously removed network config |
100 | echo "" >> $path/config | |
101 | echo "# Network configuration" >> $path/config | |
102 | cat $path/config-network >> $path/config | |
103 | rm $path/config-network | |
1aad9e44 | 104 | |
0a3673e8 SG |
105 | # Set initial timezone as on host |
106 | if [ -f /etc/timezone ]; then | |
107 | cat /etc/timezone > $rootfs/etc/timezone | |
108 | chroot $rootfs dpkg-reconfigure -f noninteractive tzdata | |
109 | elif [ -f /etc/sysconfig/clock ]; then | |
17abf278 | 110 | . /etc/sysconfig/clock |
0a3673e8 SG |
111 | echo $ZONE > $rootfs/etc/timezone |
112 | chroot $rootfs dpkg-reconfigure -f noninteractive tzdata | |
113 | else | |
114 | echo "Timezone in container is not configured. Adjust it manually." | |
115 | fi | |
116 | ||
542939c3 | 117 | # rmdir /dev/shm for containers that have /run/shm |
42ff5f0f SH |
118 | # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did |
119 | # get bind mounted to the host's /run/shm. So try to rmdir | |
120 | # it, and in case that fails move it out of the way. | |
5ff33774 | 121 | # NOTE: This can only be removed once 12.04 goes out of support |
542939c3 | 122 | if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then |
5ff33774 | 123 | rmdir $rootfs/dev/shm 2>/dev/null || mv $rootfs/dev/shm $rootfs/dev/shm.bak |
42ff5f0f SH |
124 | ln -s /run/shm $rootfs/dev/shm |
125 | fi | |
126 | ||
d1458ac8 SH |
127 | return 0 |
128 | } | |
129 | ||
130 | usage() | |
131 | { | |
132 | cat <<EOF | |
4759162d SH |
133 | LXC Container configuration for Ubuntu Cloud images. |
134 | ||
135 | Generic Options | |
136 | [ -r | --release <release> ]: Release name of container, defaults to host | |
1897e3bc | 137 | [ --rootfs <path> ]: Path in which rootfs will be placed |
3f5f5d99 | 138 | [ -a | --arch ]: Architecture of container, defaults to host architecture |
4759162d | 139 | [ -T | --tarball ]: Location of tarball |
52c8f624 | 140 | [ -d | --debug ]: Run with 'set -x' to debug errors |
ad3f14ab | 141 | [ -s | --stream]: Use specified stream rather than 'tryreleased' |
4759162d | 142 | |
65d8ae9c SM |
143 | Additionally, clone hooks can be passed through (ie, --userdata). For those, |
144 | see: | |
145 | $CLONE_HOOK_FN --help | |
d1458ac8 SH |
146 | EOF |
147 | return 0 | |
148 | } | |
149 | ||
57d116ab | 150 | 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 |
151 | if [ $? -ne 0 ]; then |
152 | usage $(basename $0) | |
153 | exit 1 | |
154 | fi | |
155 | eval set -- "$options" | |
156 | ||
57d116ab | 157 | mapped_uid=-1 |
65d8ae9c SM |
158 | # default release is precise, or the systems release if recognized |
159 | release=precise | |
d1458ac8 SH |
160 | if [ -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 |
167 | fi |
168 | ||
d1458ac8 SH |
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 | |
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 |
b8bced69 | 182 | # precise 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 |
191 | fi | |
192 | ||
52c8f624 | 193 | debug=0 |
d1458ac8 SH |
194 | hostarch=$arch |
195 | cloud=0 | |
4759162d | 196 | locales=1 |
d1458ac8 | 197 | flushcache=0 |
4b954f12 | 198 | stream="tryreleased" |
65d8ae9c | 199 | cloneargs=() |
d1458ac8 SH |
200 | while true |
201 | do | |
202 | case "$1" in | |
4759162d SH |
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;; | |
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;; | |
216 | -C|--cloud) cloneargs[${#cloneargs[@]}]="--cloud"; shift 1;; | |
217 | -S|--auth-key) cloneargs[${#cloneargs[@]}]="--auth-key=$2"; shift 2;; | |
57d116ab | 218 | --mapped-uid) mapped_uid=$2; shift 2;; |
4759162d | 219 | --) shift 1; break ;; |
d1458ac8 SH |
220 | *) break ;; |
221 | esac | |
222 | done | |
223 | ||
57d116ab | 224 | echo "mapped_uid is .$mapped_uid." |
65d8ae9c SM |
225 | cloneargs=( "--name=$name" "${cloneargs[@]}" ) |
226 | ||
3eecde70 | 227 | if [ $debug -eq 1 ]; then |
52c8f624 SH |
228 | set -x |
229 | fi | |
230 | ||
17abf278 | 231 | if [ "$arch" = "i686" ]; then |
d1458ac8 SH |
232 | arch=i386 |
233 | fi | |
234 | ||
ad3f14ab SM |
235 | if [ "$skip_arch_check" = "0" ]; then |
236 | case "$hostarch:$arch" in | |
237 | $arch:$arch) : ;; # the host == container | |
238 | amd64:i386|arm*:arm*) :;; # supported "cross" | |
239 | *) echo "cannot create '$arch' container on hostarch '$hostarch'"; | |
240 | exit 1;; | |
241 | esac | |
d1458ac8 SH |
242 | fi |
243 | ||
4b954f12 DJL |
244 | if [ "$stream" != "daily" -a "$stream" != "released" -a "$stream" != "tryreleased" ]; then |
245 | echo "Only 'daily' and 'released' and 'tryreleased' streams are supported" | |
427bffc7 SH |
246 | exit 1 |
247 | fi | |
248 | ||
d1458ac8 SH |
249 | if [ -z "$path" ]; then |
250 | echo "'path' parameter is required" | |
251 | exit 1 | |
252 | fi | |
253 | ||
254 | if [ "$(id -u)" != "0" ]; then | |
255 | echo "This script should be run as 'root'" | |
256 | exit 1 | |
257 | fi | |
258 | ||
1881820a SH |
259 | # detect rootfs |
260 | config="$path/config" | |
1897e3bc SH |
261 | if [ -z "$rootfs" ]; then |
262 | if grep -q '^lxc.rootfs' $config 2>/dev/null ; then | |
853d58fd | 263 | rootfs=$(awk -F= '/^lxc.rootfs =/{ print $2 }' $config) |
1897e3bc SH |
264 | else |
265 | rootfs=$path/rootfs | |
266 | fi | |
1881820a | 267 | fi |
d1458ac8 SH |
268 | |
269 | type ubuntu-cloudimg-query | |
270 | type wget | |
271 | ||
272 | # determine the url, tarball, and directory names | |
273 | # download if needed | |
65d8ae9c | 274 | cache="$STATE_DIR/cache/lxc/cloud-$release" |
9cde0368 SG |
275 | if [ $in_userns -eq 1 ]; then |
276 | STATE_DIR="$HOME/.cache/lxc/" | |
277 | cache="$HOME/.cache/lxc/cloud-$release" | |
278 | fi | |
d1458ac8 SH |
279 | |
280 | mkdir -p $cache | |
281 | ||
4b954f12 DJL |
282 | if [ "$stream" = "tryreleased" ]; then |
283 | stream=released | |
284 | ubuntu-cloudimg-query $release $stream $arch 1>/dev/null 2>/dev/null || stream=daily | |
285 | fi | |
286 | ||
4759162d | 287 | if [ -n "$tarball" ]; then |
b942e672 | 288 | url2="$tarball" |
4759162d | 289 | else |
ad3f14ab SM |
290 | if ! url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`; then |
291 | echo "There is no download available for release=$release, stream=$stream, arch=$arch" | |
292 | [ "$stream" = "daily" ] || echo "You may try with '--stream=daily'" | |
293 | exit | |
294 | fi | |
b942e672 | 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" | |
2b142295 | 365 | mkdir -p $rootfs/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 | 375 | ( |
17abf278 | 376 | flock -x 9 |
1aad9e44 | 377 | do_extract_rootfs |
17abf278 | 378 | ) 9>"$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 |