3 # template script for generating ubuntu container for LXC based on released
6 # Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
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.
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.
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
24 STATE_DIR
="@LOCALSTATEDIR@"
25 HOOK_DIR
="@LXCHOOKDIR@"
26 CLONE_HOOK_FN
="$HOOK_DIR/ubuntu-cloud-prep"
28 if [ -r /etc
/default
/lxc
]; then
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; }
41 [ $
(am_in_userns
) = "yes" ] && in_userns
=1
51 if [ $arch = "i386" ]; then
55 # if there is exactly one veth network entry, make sure it has an
57 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
58 if [ $nics -eq 1 ]; then
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
62 grep -q "^lxc.rootfs" $path/config
2>/dev
/null ||
echo "lxc.rootfs = $rootfs" >> $path/config
63 cat <<EOF >> $path/config
64 lxc.mount = $path/fstab
65 lxc.pivotdir = lxc_putold
67 lxc.devttydir =$ttydir
73 lxc.cap.drop = sys_module mac_admin mac_override sys_time
75 # When using LXC with apparmor, uncomment the next line to run unconfined:
76 #lxc.aa_profile = unconfined
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
82 lxc.hook.clone = ${CLONE_HOOK_FN}
85 # can't write to devices.deny without CAP_SYS_ADMIN in init-user-ns
86 if [ $in_userns -ne 1 ]; then
87 cat <<EOF >> $path/config
88 lxc.cgroup.devices.deny = a
89 # Allow any mknod (but not using the node)
90 lxc.cgroup.devices.allow = c *:* m
91 lxc.cgroup.devices.allow = b *:* m
93 lxc.cgroup.devices.allow = c 1:3 rwm
94 lxc.cgroup.devices.allow = c 1:5 rwm
96 lxc.cgroup.devices.allow = c 5:1 rwm
97 lxc.cgroup.devices.allow = c 5:0 rwm
99 lxc.cgroup.devices.allow = c 1:9 rwm
100 lxc.cgroup.devices.allow = c 1:8 rwm
101 lxc.cgroup.devices.allow = c 136:* rwm
102 lxc.cgroup.devices.allow = c 5:2 rwm
104 lxc.cgroup.devices.allow = c 254:0 rm
106 lxc.cgroup.devices.allow = c 10:229 rwm
108 lxc.cgroup.devices.allow = c 10:200 rwm
110 lxc.cgroup.devices.allow = c 1:7 rwm
112 lxc.cgroup.devices.allow = c 10:228 rwm
114 lxc.cgroup.devices.allow = c 10:232 rwm
118 cat <<EOF > $path/fstab
119 proc proc proc nodev,noexec,nosuid 0 0
120 sysfs sys sysfs defaults 0 0
121 /sys/fs/fuse/connections sys/fs/fuse/connections none bind,optional 0 0
122 /sys/kernel/debug sys/kernel/debug none bind,optional 0 0
123 /sys/kernel/security sys/kernel/security none bind,optional 0 0
124 /sys/fs/pstore sys/fs/pstore none bind,optional 0 0
127 # unprivileged user can't mknod these. One day we may allow
128 # that in the kernel, but not right now. So let's just bind
129 # mount the files from the host.
130 if [ $in_userns -eq 1 ]; then
131 mkdir
-p $rootfs/dev
/pts
132 for dev
in null tty urandom console
; do
133 touch $rootfs/dev
/$dev
134 echo "/dev/$dev dev/$dev none bind 0 0" >> $path/fstab
138 # rmdir /dev/shm for containers that have /run/shm
139 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
140 # get bind mounted to the host's /run/shm. So try to rmdir
141 # it, and in case that fails move it out of the way.
142 if [ ! -L $rootfs/dev
/shm
] && [ -d $rootfs/run
/shm
] && [ -e $rootfs/dev
/shm
]; then
143 mv $rootfs/dev
/shm
$rootfs/dev
/shm.bak
144 ln -s /run
/shm
$rootfs/dev
/shm
153 LXC Container configuration for Ubuntu Cloud images.
156 [ -r | --release <release> ]: Release name of container, defaults to host
157 [ --rootfs <path> ]: Path in which rootfs will be placed
158 [ -a | --arch ]: Arhcitecture of container, defaults to host architecture
159 [ -T | --tarball ]: Location of tarball
160 [ -d | --debug ]: Run with 'set -x' to debug errors
161 [ -s | --stream]: Use specified stream rather than 'released'
163 Additionally, clone hooks can be passed through (ie, --userdata). For those,
165 $CLONE_HOOK_FN --help
170 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
: -- "$@")
171 if [ $?
-ne 0 ]; then
175 eval set -- "$options"
178 # default release is precise, or the systems release if recognized
180 if [ -f /etc
/lsb-release
]; then
182 rels
=$
(ubuntu-distro-info
--supported 2>/dev
/null
) ||
183 rels
="lucid natty oneiric precise quantal raring saucy"
185 [ "$DISTRIB_CODENAME" = "$r" ] && release
="$r"
189 # Code taken from debootstrap
190 if [ -x /usr
/bin
/dpkg
] && /usr
/bin
/dpkg
--print-architecture >/dev
/null
2>&1; then
191 arch
=`/usr/bin/dpkg --print-architecture`
192 elif type udpkg
>/dev
/null
2>&1 && udpkg
--print-architecture >/dev
/null
2>&1; then
193 arch
=`/usr/bin/udpkg --print-architecture`
196 if [ "$arch" = "i686" ]; then
198 elif [ "$arch" = "x86_64" ]; then
200 elif [ "$arch" = "armv7l" ]; then
201 # note: arm images don't exist before oneiric; are called armhf in
202 # precise and later; and are not supported by the query, so we don't actually
203 # support them yet (see check later on). When Query2 is available,
204 # we'll use that to enable arm images.
219 -h|
--help) usage
$0 && exit 0;;
220 -p|
--path) path
=$2; shift 2;;
221 -n|
--name) name
=$2; shift 2;;
222 -F|
--flush-cache) flushcache
=1; shift 1;;
223 -r|
--release) release
=$2; shift 2;;
224 -a|
--arch) arch
=$2; shift 2;;
225 -T|
--tarball) tarball
=$2; shift 2;;
226 -d|
--debug) debug
=1; shift 1;;
227 -s|
--stream) stream
=$2; shift 2;;
228 --rootfs) rootfs
=$2; shift 2;;
229 -L|
--no?locales
) cloneargs
[${#cloneargs[@]}]="--no-locales"; shift 1;;
230 -i|
--hostid) cloneargs
[${#cloneargs[@]}]="--hostid=$2"; shift 2;;
231 -u|
--userdata) cloneargs
[${#cloneargs[@]}]="--userdata=$2"; shift 2;;
232 -C|
--cloud) cloneargs
[${#cloneargs[@]}]="--cloud"; shift 1;;
233 -S|
--auth-key) cloneargs
[${#cloneargs[@]}]="--auth-key=$2"; shift 2;;
234 --mapped-uid) mapped_uid
=$2; shift 2;;
235 --) shift 1; break ;;
240 echo "mapped_uid is .$mapped_uid."
241 cloneargs
=( "--name=$name" "${cloneargs[@]}" )
243 if [ $debug -eq 1 ]; then
247 if [ "$arch" == "i686" ]; then
251 if [ $arch != "i386" -a $arch != "amd64" -a $arch != "armhf" -a $arch != "armel" ]; then
252 echo "Only i386, amd64, armel and armhf are supported by the ubuntu cloud template."
256 if [ $hostarch != "i386" -a $hostarch != "amd64" -a $hostarch != "armhf" -a $hostarch != "armel" ]; then
257 echo "Only i386, amd64, armel and armhf are supported as host."
261 if [ $hostarch = "amd64" -a $arch != "amd64" -a $arch != "i386" ]; then
262 echo "can't create $arch container on $hostarch"
266 if [ $hostarch = "i386" -a $arch != "i386" ]; then
267 echo "can't create $arch container on $hostarch"
271 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
272 [ $arch != "armhf" -a $arch != "armel" ]; then
273 echo "can't create $arch container on $hostarch"
277 if [ "$stream" != "daily" -a "$stream" != "released" ]; then
278 echo "Only 'daily' and 'released' streams are supported"
282 if [ -z "$path" ]; then
283 echo "'path' parameter is required"
287 if [ "$(id -u)" != "0" ]; then
288 echo "This script should be run as 'root'"
293 config
="$path/config"
294 if [ -z "$rootfs" ]; then
295 if grep -q '^lxc.rootfs' $config 2>/dev
/null
; then
296 rootfs
=$
(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
302 type ubuntu-cloudimg-query
305 # determine the url, tarball, and directory names
307 cache
="$STATE_DIR/cache/lxc/cloud-$release"
308 STATE_DIR
="$HOME/.cache/lxc/"
309 cache
="$HOME/.cache/lxc/cloud-$release"
313 if [ -n "$tarball" ]; then
316 url1
=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`
317 url2
=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
320 filename
=`basename $url2`
330 umount
-l $cache/$xdir || true
334 # if the release doesn't have a *-rootfs.tar.gz, then create one from the
335 # cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
336 # a tarball from the mounted image.
342 xdir
=`mktemp -d -p .`
343 tarname
=`basename $url`
344 imgname
="$release-*-cloudimg-$arch.img"
345 trap buildcleanup EXIT SIGHUP SIGINT SIGTERM
346 if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
348 echo "Downloading cloud image from $url"
349 wget
$url ||
{ echo "Couldn't find cloud image $url."; exit 1; }
351 echo "Creating new cached cloud image rootfs"
352 tar --wildcards -zxf "$tarname" "$imgname"
353 mount
-o loop
$imgname $xdir
354 (cd $xdir; tar --numeric-owner -cpzf "../$filename" .
)
356 rm -f $tarname $imgname
358 echo "New cloud image cache created"
365 do_extract_rootfs
() {
368 if [ $flushcache -eq 1 ]; then
369 echo "Clearing the cached images"
373 trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM
374 if [ ! -f $filename ]; then
375 wget
$url2 || build_root_tgz
$url1 $filename
382 echo "Extracting container rootfs"
385 if [ $in_userns -eq 1 ]; then
386 tar --anchored --exclude="dev/*" --numeric-owner -xpzf "$cache/$filename"
388 tar --numeric-owner -xpzf "$cache/$filename"
392 if [ -n "$tarball" ]; then
395 mkdir
-p "$STATE_DIR/lock/subsys/"
399 ) 200>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud"
402 copy_configuration
$path $rootfs $name $arch $release
404 "$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
406 if [ $mapped_uid -ne -1 ]; then
407 chown
$mapped_uid $path/config
408 chown
-R $mapped_uid $STATE_DIR
409 chown
-R $mapped_uid $cache
412 echo "Container $name created."