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"
27 LXC_TEMPLATE_CONFIG
="@LXCTEMPLATECONFIG@"
29 if [ -r /etc
/default
/lxc
]; then
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; }
42 [ $
(am_in_userns
) = "yes" ] && in_userns
=1
52 if [ $arch = "i386" ]; then
56 # if there is exactly one veth network entry, make sure it has an
58 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
59 if [ $nics -eq 1 ]; then
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
63 # Generate the configuration file
64 ## Create the fstab (empty by default)
67 ## Relocate all the network config entries
68 sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config
70 ## Relocate any other config entries
71 sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
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
79 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" ]; then
80 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" >> $path/config
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
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
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
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
112 echo "Timezone in container is not configured. Adjust it manually."
115 # rmdir /dev/shm for containers that have /run/shm
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.
119 # NOTE: This can only be removed once 12.04 goes out of support
120 if [ ! -L $rootfs/dev
/shm
] && [ -d $rootfs/run
/shm
] && [ -e $rootfs/dev
/shm
]; then
121 rmdir $rootfs/dev
/shm
2>/dev
/null ||
mv $rootfs/dev
/shm
$rootfs/dev
/shm.bak
122 ln -s /run
/shm
$rootfs/dev
/shm
131 LXC Container configuration for Ubuntu Cloud images.
134 [ -r | --release <release> ]: Release name of container, defaults to host
135 [ --rootfs <path> ]: Path in which rootfs will be placed
136 [ -a | --arch ]: Arhcitecture of container, defaults to host architecture
137 [ -T | --tarball ]: Location of tarball
138 [ -d | --debug ]: Run with 'set -x' to debug errors
139 [ -s | --stream]: Use specified stream rather than 'released'
141 Additionally, clone hooks can be passed through (ie, --userdata). For those,
143 $CLONE_HOOK_FN --help
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
: -- "$@")
149 if [ $?
-ne 0 ]; then
153 eval set -- "$options"
156 # default release is precise, or the systems release if recognized
158 if [ -f /etc
/lsb-release
]; then
160 rels
=$
(ubuntu-distro-info
--supported 2>/dev
/null
) ||
161 rels
="lucid natty oneiric precise quantal raring saucy"
163 [ "$DISTRIB_CODENAME" = "$r" ] && release
="$r"
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`
174 if [ "$arch" = "i686" ]; then
176 elif [ "$arch" = "x86_64" ]; then
178 elif [ "$arch" = "armv7l" ]; then
179 # note: arm images don't exist before oneiric; are called armhf in
180 # precise and later; and are not supported by the query, so we don't actually
181 # support them yet (see check later on). When Query2 is available,
182 # we'll use that to enable arm images.
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;;
203 -T|
--tarball) tarball
=$2; shift 2;;
204 -d|
--debug) debug
=1; shift 1;;
205 -s|
--stream) stream
=$2; shift 2;;
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;;
212 --mapped-uid) mapped_uid
=$2; shift 2;;
213 --) shift 1; break ;;
218 echo "mapped_uid is .$mapped_uid."
219 cloneargs
=( "--name=$name" "${cloneargs[@]}" )
221 if [ $debug -eq 1 ]; then
225 if [ "$arch" == "i686" ]; then
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."
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."
239 if [ $hostarch = "amd64" -a $arch != "amd64" -a $arch != "i386" ]; then
240 echo "can't create $arch container on $hostarch"
244 if [ $hostarch = "i386" -a $arch != "i386" ]; then
245 echo "can't create $arch container on $hostarch"
249 if [ $hostarch = "armhf" -o $hostarch = "armel" ] && \
250 [ $arch != "armhf" -a $arch != "armel" ]; then
251 echo "can't create $arch container on $hostarch"
255 if [ "$stream" != "daily" -a "$stream" != "released" ]; then
256 echo "Only 'daily' and 'released' streams are supported"
260 if [ -z "$path" ]; then
261 echo "'path' parameter is required"
265 if [ "$(id -u)" != "0" ]; then
266 echo "This script should be run as 'root'"
271 config
="$path/config"
272 if [ -z "$rootfs" ]; then
273 if grep -q '^lxc.rootfs' $config 2>/dev
/null
; then
274 rootfs
=$
(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
280 type ubuntu-cloudimg-query
283 # determine the url, tarball, and directory names
285 cache
="$STATE_DIR/cache/lxc/cloud-$release"
286 STATE_DIR
="$HOME/.cache/lxc/"
287 cache
="$HOME/.cache/lxc/cloud-$release"
291 if [ -n "$tarball" ]; then
294 url1
=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`
295 url2
=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
298 filename
=`basename $url2`
308 umount
-l $cache/$xdir || true
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.
320 xdir
=`mktemp -d -p .`
321 tarname
=`basename $url`
322 imgname
="$release-*-cloudimg-$arch.img"
323 trap buildcleanup EXIT SIGHUP SIGINT SIGTERM
324 if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
326 echo "Downloading cloud image from $url"
327 wget
$url ||
{ echo "Couldn't find cloud image $url."; exit 1; }
329 echo "Creating new cached cloud image rootfs"
330 tar --wildcards -zxf "$tarname" "$imgname"
331 mount
-o loop
$imgname $xdir
332 (cd $xdir; tar --numeric-owner -cpzf "../$filename" .
)
334 rm -f $tarname $imgname
336 echo "New cloud image cache created"
343 do_extract_rootfs
() {
346 if [ $flushcache -eq 1 ]; then
347 echo "Clearing the cached images"
351 trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM
352 if [ ! -f $filename ]; then
353 wget
$url2 || build_root_tgz
$url1 $filename
360 echo "Extracting container rootfs"
363 if [ $in_userns -eq 1 ]; then
364 tar --anchored --exclude="dev/*" --numeric-owner -xpzf "$cache/$filename"
365 mkdir
-p $cache/$filename/dev
/pts
/
367 tar --numeric-owner -xpzf "$cache/$filename"
371 if [ -n "$tarball" ]; then
374 mkdir
-p "$STATE_DIR/lock/subsys/"
378 ) 200>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud"
381 copy_configuration
$path $rootfs $name $arch $release
383 "$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
385 if [ $mapped_uid -ne -1 ]; then
386 chown
$mapped_uid $path/config
387 chown
-R $mapped_uid $STATE_DIR
388 chown
-R $mapped_uid $cache
391 echo "Container $name created."