]>
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" | |
27 | ||
d1458ac8 SH |
28 | if [ -r /etc/default/lxc ]; then |
29 | . /etc/default/lxc | |
30 | fi | |
31 | ||
1aad9e44 SH |
32 | am_in_userns() { |
33 | [ -e /proc/self/uid_map ] || { echo no; return; } | |
34 | [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || { echo yes; return; } | |
35 | line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map) | |
36 | [ "$line" = "0 0 4294967295" ] && { echo no; return; } | |
37 | echo yes | |
38 | } | |
39 | ||
40 | in_userns=0 | |
41 | [ $(am_in_userns) = "yes" ] && in_userns=1 | |
1aad9e44 | 42 | |
d1458ac8 SH |
43 | copy_configuration() |
44 | { | |
45 | path=$1 | |
46 | rootfs=$2 | |
47 | name=$3 | |
48 | arch=$4 | |
42ff5f0f | 49 | release=$5 |
d1458ac8 SH |
50 | |
51 | if [ $arch = "i386" ]; then | |
52 | arch="i686" | |
53 | fi | |
54 | ||
4759162d SH |
55 | # if there is exactly one veth network entry, make sure it has an |
56 | # associated hwaddr. | |
57 | nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l` | |
58 | if [ $nics -eq 1 ]; then | |
daaf41b3 | 59 | 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 |
60 | fi |
61 | ||
1881820a | 62 | grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config |
d1458ac8 | 63 | cat <<EOF >> $path/config |
daaf41b3 SG |
64 | lxc.mount = $path/fstab |
65 | lxc.pivotdir = lxc_putold | |
d1458ac8 | 66 | |
daaf41b3 | 67 | lxc.devttydir =$ttydir |
d1458ac8 SH |
68 | lxc.tty = 4 |
69 | lxc.pts = 1024 | |
daaf41b3 SG |
70 | |
71 | lxc.utsname = $name | |
d1458ac8 | 72 | lxc.arch = $arch |
eee3ba81 | 73 | lxc.cap.drop = sys_module mac_admin mac_override sys_time |
d1458ac8 | 74 | |
f02ce27d SG |
75 | # When using LXC with apparmor, uncomment the next line to run unconfined: |
76 | #lxc.aa_profile = unconfined | |
9313e1e6 | 77 | |
b85ab798 SH |
78 | # To support container nesting on an Ubuntu host, uncomment next two lines: |
79 | #lxc.aa_profile = lxc-container-default-with-nesting | |
80 | #lxc.hook.mount = /usr/share/lxc/hooks/mountcgroups | |
f02ce27d | 81 | |
65d8ae9c SM |
82 | lxc.hook.clone = ${CLONE_HOOK_FN} |
83 | ||
d1458ac8 SH |
84 | lxc.cgroup.devices.deny = a |
85 | # Allow any mknod (but not using the node) | |
86 | lxc.cgroup.devices.allow = c *:* m | |
87 | lxc.cgroup.devices.allow = b *:* m | |
88 | # /dev/null and zero | |
89 | lxc.cgroup.devices.allow = c 1:3 rwm | |
90 | lxc.cgroup.devices.allow = c 1:5 rwm | |
91 | # consoles | |
92 | lxc.cgroup.devices.allow = c 5:1 rwm | |
93 | lxc.cgroup.devices.allow = c 5:0 rwm | |
d1458ac8 SH |
94 | # /dev/{,u}random |
95 | lxc.cgroup.devices.allow = c 1:9 rwm | |
96 | lxc.cgroup.devices.allow = c 1:8 rwm | |
97 | lxc.cgroup.devices.allow = c 136:* rwm | |
98 | lxc.cgroup.devices.allow = c 5:2 rwm | |
99 | # rtc | |
eee3ba81 | 100 | lxc.cgroup.devices.allow = c 254:0 rm |
a2abaa9e | 101 | # fuse |
d1458ac8 | 102 | lxc.cgroup.devices.allow = c 10:229 rwm |
a2abaa9e | 103 | # tun |
d1458ac8 | 104 | lxc.cgroup.devices.allow = c 10:200 rwm |
a2abaa9e | 105 | # full |
d1458ac8 | 106 | lxc.cgroup.devices.allow = c 1:7 rwm |
a2abaa9e | 107 | # hpet |
d1458ac8 | 108 | lxc.cgroup.devices.allow = c 10:228 rwm |
a2abaa9e | 109 | # kvm |
d1458ac8 SH |
110 | lxc.cgroup.devices.allow = c 10:232 rwm |
111 | EOF | |
112 | ||
113 | cat <<EOF > $path/fstab | |
80a881b2 SH |
114 | proc proc proc nodev,noexec,nosuid 0 0 |
115 | sysfs sys sysfs defaults 0 0 | |
6f259716 SH |
116 | /sys/fs/fuse/connections sys/fs/fuse/connections none bind 0 0 |
117 | /sys/kernel/debug sys/kernel/debug none bind 0 0 | |
118 | /sys/kernel/security sys/kernel/security none bind 0 0 | |
4d7bcfb6 | 119 | /sys/fs/pstore sys/fs/pstore none bind,optional 0 0 |
bf7d76cf | 120 | EOF |
d1458ac8 | 121 | |
1aad9e44 SH |
122 | # unprivileged user can't mknod these. One day we may allow |
123 | # that in the kernel, but not right now. So let's just bind | |
124 | # mount the files from the host. | |
125 | if [ $in_userns -eq 1 ]; then | |
126 | for dev in null tty urandom console; do | |
127 | touch $rootfs/dev/$dev | |
128 | echo "/dev/$dev dev/$dev none bind 0 0" >> $path/fstab | |
129 | done | |
130 | fi | |
131 | ||
542939c3 | 132 | # rmdir /dev/shm for containers that have /run/shm |
42ff5f0f SH |
133 | # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did |
134 | # get bind mounted to the host's /run/shm. So try to rmdir | |
135 | # it, and in case that fails move it out of the way. | |
542939c3 SG |
136 | if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then |
137 | mv $rootfs/dev/shm $rootfs/dev/shm.bak | |
42ff5f0f SH |
138 | ln -s /run/shm $rootfs/dev/shm |
139 | fi | |
140 | ||
d1458ac8 SH |
141 | return 0 |
142 | } | |
143 | ||
144 | usage() | |
145 | { | |
146 | cat <<EOF | |
4759162d SH |
147 | LXC Container configuration for Ubuntu Cloud images. |
148 | ||
149 | Generic Options | |
150 | [ -r | --release <release> ]: Release name of container, defaults to host | |
1897e3bc | 151 | [ --rootfs <path> ]: Path in which rootfs will be placed |
840295ff | 152 | [ -a | --arch ]: Arhcitecture of container, defaults to host architecture |
4759162d | 153 | [ -T | --tarball ]: Location of tarball |
52c8f624 | 154 | [ -d | --debug ]: Run with 'set -x' to debug errors |
427bffc7 | 155 | [ -s | --stream]: Use specified stream rather than 'released' |
4759162d | 156 | |
65d8ae9c SM |
157 | Additionally, clone hooks can be passed through (ie, --userdata). For those, |
158 | see: | |
159 | $CLONE_HOOK_FN --help | |
d1458ac8 SH |
160 | EOF |
161 | return 0 | |
162 | } | |
163 | ||
1897e3bc | 164 | 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: -- "$@") |
d1458ac8 SH |
165 | if [ $? -ne 0 ]; then |
166 | usage $(basename $0) | |
167 | exit 1 | |
168 | fi | |
169 | eval set -- "$options" | |
170 | ||
65d8ae9c SM |
171 | # default release is precise, or the systems release if recognized |
172 | release=precise | |
d1458ac8 SH |
173 | if [ -f /etc/lsb-release ]; then |
174 | . /etc/lsb-release | |
65d8ae9c SM |
175 | rels=$(ubuntu-distro-info --supported 2>/dev/null) || |
176 | rels="lucid natty oneiric precise quantal raring saucy" | |
177 | for r in $rels; do | |
178 | [ "$DISTRIB_CODENAME" = "$r" ] && release="$r" | |
179 | done | |
d1458ac8 SH |
180 | fi |
181 | ||
d1458ac8 SH |
182 | # Code taken from debootstrap |
183 | if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then | |
184 | arch=`/usr/bin/dpkg --print-architecture` | |
185 | elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then | |
186 | arch=`/usr/bin/udpkg --print-architecture` | |
187 | else | |
ed4616b1 | 188 | arch=$(uname -m) |
d1458ac8 SH |
189 | if [ "$arch" = "i686" ]; then |
190 | arch="i386" | |
191 | elif [ "$arch" = "x86_64" ]; then | |
192 | arch="amd64" | |
193 | elif [ "$arch" = "armv7l" ]; then | |
3eecde70 | 194 | # note: arm images don't exist before oneiric; are called armhf in |
b8bced69 | 195 | # precise and later; and are not supported by the query, so we don't actually |
3eecde70 SH |
196 | # support them yet (see check later on). When Query2 is available, |
197 | # we'll use that to enable arm images. | |
8a63c0a9 | 198 | arch="armhf" |
d1458ac8 SH |
199 | fi |
200 | fi | |
201 | ||
52c8f624 | 202 | debug=0 |
d1458ac8 SH |
203 | hostarch=$arch |
204 | cloud=0 | |
4759162d | 205 | locales=1 |
d1458ac8 | 206 | flushcache=0 |
427bffc7 | 207 | stream="released" |
65d8ae9c | 208 | cloneargs=() |
d1458ac8 SH |
209 | while true |
210 | do | |
211 | case "$1" in | |
4759162d SH |
212 | -h|--help) usage $0 && exit 0;; |
213 | -p|--path) path=$2; shift 2;; | |
214 | -n|--name) name=$2; shift 2;; | |
215 | -F|--flush-cache) flushcache=1; shift 1;; | |
216 | -r|--release) release=$2; shift 2;; | |
217 | -a|--arch) arch=$2; shift 2;; | |
4759162d | 218 | -T|--tarball) tarball=$2; shift 2;; |
52c8f624 | 219 | -d|--debug) debug=1; shift 1;; |
427bffc7 | 220 | -s|--stream) stream=$2; shift 2;; |
65d8ae9c SM |
221 | --rootfs) rootfs=$2; shift 2;; |
222 | -L|--no?locales) cloneargs[${#cloneargs[@]}]="--no-locales"; shift 1;; | |
223 | -i|--hostid) cloneargs[${#cloneargs[@]}]="--hostid=$2"; shift 2;; | |
224 | -u|--userdata) cloneargs[${#cloneargs[@]}]="--userdata=$2"; shift 2;; | |
225 | -C|--cloud) cloneargs[${#cloneargs[@]}]="--cloud"; shift 1;; | |
226 | -S|--auth-key) cloneargs[${#cloneargs[@]}]="--auth-key=$2"; shift 2;; | |
4759162d | 227 | --) shift 1; break ;; |
d1458ac8 SH |
228 | *) break ;; |
229 | esac | |
230 | done | |
231 | ||
65d8ae9c SM |
232 | cloneargs=( "--name=$name" "${cloneargs[@]}" ) |
233 | ||
3eecde70 | 234 | if [ $debug -eq 1 ]; then |
52c8f624 SH |
235 | set -x |
236 | fi | |
237 | ||
d1458ac8 SH |
238 | if [ "$arch" == "i686" ]; then |
239 | arch=i386 | |
240 | fi | |
241 | ||
8a63c0a9 SG |
242 | if [ $arch != "i386" -a $arch != "amd64" -a $arch != "armhf" -a $arch != "armel" ]; then |
243 | echo "Only i386, amd64, armel and armhf are supported by the ubuntu cloud template." | |
d1458ac8 SH |
244 | exit 1 |
245 | fi | |
246 | ||
8a63c0a9 SG |
247 | if [ $hostarch != "i386" -a $hostarch != "amd64" -a $hostarch != "armhf" -a $hostarch != "armel" ]; then |
248 | echo "Only i386, amd64, armel and armhf are supported as host." | |
249 | exit 1 | |
250 | fi | |
251 | ||
252 | if [ $hostarch = "amd64" -a $arch != "amd64" -a $arch != "i386" ]; then | |
253 | echo "can't create $arch container on $hostarch" | |
254 | exit 1 | |
255 | fi | |
256 | ||
257 | if [ $hostarch = "i386" -a $arch != "i386" ]; then | |
258 | echo "can't create $arch container on $hostarch" | |
259 | exit 1 | |
260 | fi | |
261 | ||
262 | if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \ | |
263 | [ $arch != "armhf" -a $arch != "armel" ]; then | |
264 | echo "can't create $arch container on $hostarch" | |
d1458ac8 SH |
265 | exit 1 |
266 | fi | |
267 | ||
427bffc7 SH |
268 | if [ "$stream" != "daily" -a "$stream" != "released" ]; then |
269 | echo "Only 'daily' and 'released' streams are supported" | |
270 | exit 1 | |
271 | fi | |
272 | ||
d1458ac8 SH |
273 | if [ -z "$path" ]; then |
274 | echo "'path' parameter is required" | |
275 | exit 1 | |
276 | fi | |
277 | ||
278 | if [ "$(id -u)" != "0" ]; then | |
279 | echo "This script should be run as 'root'" | |
280 | exit 1 | |
281 | fi | |
282 | ||
1881820a SH |
283 | # detect rootfs |
284 | config="$path/config" | |
1897e3bc SH |
285 | if [ -z "$rootfs" ]; then |
286 | if grep -q '^lxc.rootfs' $config 2>/dev/null ; then | |
287 | rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'` | |
288 | else | |
289 | rootfs=$path/rootfs | |
290 | fi | |
1881820a | 291 | fi |
d1458ac8 SH |
292 | |
293 | type ubuntu-cloudimg-query | |
294 | type wget | |
295 | ||
296 | # determine the url, tarball, and directory names | |
297 | # download if needed | |
65d8ae9c | 298 | cache="$STATE_DIR/cache/lxc/cloud-$release" |
d1458ac8 SH |
299 | |
300 | mkdir -p $cache | |
301 | ||
4759162d | 302 | if [ -n "$tarball" ]; then |
b942e672 | 303 | url2="$tarball" |
4759162d | 304 | else |
b942e672 SM |
305 | url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"` |
306 | url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'` | |
4759162d SH |
307 | fi |
308 | ||
d1458ac8 SH |
309 | filename=`basename $url2` |
310 | ||
f1ccde27 SH |
311 | wgetcleanup() |
312 | { | |
b942e672 | 313 | rm -f $filename |
f1ccde27 SH |
314 | } |
315 | ||
3eecde70 SH |
316 | buildcleanup() |
317 | { | |
318 | cd $rootfs | |
319 | umount -l $cache/$xdir || true | |
320 | rm -rf $cache | |
321 | } | |
322 | ||
323 | # if the release doesn't have a *-rootfs.tar.gz, then create one from the | |
324 | # cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating | |
325 | # a tarball from the mounted image. | |
326 | build_root_tgz() | |
327 | { | |
328 | url=$1 | |
329 | filename=$2 | |
330 | ||
331 | xdir=`mktemp -d -p .` | |
332 | tarname=`basename $url` | |
edd3810e | 333 | imgname="$release-*-cloudimg-$arch.img" |
f1ccde27 | 334 | trap buildcleanup EXIT SIGHUP SIGINT SIGTERM |
3eecde70 SH |
335 | if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then |
336 | rm -f $tarname | |
337 | echo "Downloading cloud image from $url" | |
338 | wget $url || { echo "Couldn't find cloud image $url."; exit 1; } | |
339 | fi | |
340 | echo "Creating new cached cloud image rootfs" | |
9c3bc32c | 341 | tar --wildcards -zxf "$tarname" "$imgname" |
3eecde70 | 342 | mount -o loop $imgname $xdir |
9c3bc32c | 343 | (cd $xdir; tar --numeric-owner -cpzf "../$filename" .) |
3eecde70 SH |
344 | umount $xdir |
345 | rm -f $tarname $imgname | |
346 | rmdir $xdir | |
347 | echo "New cloud image cache created" | |
348 | trap EXIT | |
f1ccde27 SH |
349 | trap SIGHUP |
350 | trap SIGINT | |
351 | trap SIGTERM | |
3eecde70 SH |
352 | } |
353 | ||
1aad9e44 | 354 | do_extract_rootfs() { |
d1458ac8 SH |
355 | |
356 | cd $cache | |
357 | if [ $flushcache -eq 1 ]; then | |
358 | echo "Clearing the cached images" | |
359 | rm -f $filename | |
360 | fi | |
361 | ||
f1ccde27 | 362 | trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM |
d1458ac8 | 363 | if [ ! -f $filename ]; then |
b942e672 | 364 | wget $url2 || build_root_tgz $url1 $filename |
d1458ac8 | 365 | fi |
f1ccde27 SH |
366 | trap EXIT |
367 | trap SIGHUP | |
368 | trap SIGINT | |
369 | trap SIGTERM | |
d1458ac8 | 370 | |
3eecde70 | 371 | echo "Extracting container rootfs" |
d1458ac8 SH |
372 | mkdir -p $rootfs |
373 | cd $rootfs | |
9c3bc32c | 374 | tar --numeric-owner -xpzf "$cache/$filename" |
1aad9e44 SH |
375 | } |
376 | ||
377 | if [ -n "$tarball" ]; then | |
378 | do_extract_rootfs | |
379 | else | |
65d8ae9c | 380 | mkdir -p "$STATE_DIR/lock/subsys/" |
1aad9e44 SH |
381 | ( |
382 | flock -x 200 | |
383 | do_extract_rootfs | |
65d8ae9c | 384 | ) 200>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud" |
1aad9e44 | 385 | fi |
d1458ac8 | 386 | |
42ff5f0f | 387 | copy_configuration $path $rootfs $name $arch $release |
d1458ac8 | 388 | |
65d8ae9c SM |
389 | "$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs" |
390 | ||
d1458ac8 SH |
391 | echo "Container $name created." |
392 | exit 0 | |
b942e672 SM |
393 | |
394 | # vi: ts=4 expandtab |