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@"
28 KNOWN_RELEASES
="precise trusty utopic vivid"
29 skip_arch_check
=${UCTEMPLATE_SKIP_ARCH_CHECK:-0}
31 # Make sure the usual locations are in PATH
32 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
34 if [ -r /etc
/default
/lxc
]; then
39 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
40 [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] ||
{ echo yes; return; }
41 line
=$
(awk '{ print $1 " " $2 " " $3 }' /proc
/self
/uid_map
)
42 [ "$line" = "0 0 4294967295" ] && { echo no
; return; }
47 [ $
(am_in_userns
) = "yes" ] && in_userns
=1
57 if [ $arch = "i386" ]; then
61 # if there is exactly one veth network entry, make sure it has an
63 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
64 if [ $nics -eq 1 ]; then
65 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
68 # Generate the configuration file
69 ## Relocate all the network config entries
70 sed -i -e "/lxc.network/{w ${path}/config-network" -e "d}" $path/config
72 ## Relocate any other config entries
73 sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config
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
81 if [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" ]; then
82 echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.${release}.conf" >> $path/config
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
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
98 ## Re-add the previously removed network config
99 echo "" >> $path/config
100 echo "# Network configuration" >> $path/config
101 cat $path/config-network
>> $path/config
102 rm $path/config-network
104 # Set initial timezone as on host
105 if [ -f /etc
/timezone
]; then
106 cat /etc
/timezone
> $rootfs/etc
/timezone
107 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
108 elif [ -f /etc
/sysconfig
/clock ]; then
109 .
/etc
/sysconfig
/clock
110 echo $ZONE > $rootfs/etc
/timezone
111 chroot
$rootfs dpkg-reconfigure
-f noninteractive tzdata
113 echo "Timezone in container is not configured. Adjust it manually."
116 # rmdir /dev/shm for containers that have /run/shm
117 # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
118 # get bind mounted to the host's /run/shm. So try to rmdir
119 # it, and in case that fails move it out of the way.
120 # NOTE: This can only be removed once 12.04 goes out of support
121 if [ ! -L $rootfs/dev
/shm
] && [ -e $rootfs/dev
/shm
]; then
122 rmdir $rootfs/dev
/shm
2>/dev
/null ||
mv $rootfs/dev
/shm
$rootfs/dev
/shm.bak
123 ln -s /run
/shm
$rootfs/dev
/shm
132 LXC Container configuration for Ubuntu Cloud images.
135 [ -r | --release <release> ]: Release name of container, defaults to host
136 [ --rootfs <path> ]: Path in which rootfs will be placed
137 [ -a | --arch ]: Architecture of container, defaults to host architecture
138 [ -T | --tarball ]: Location of tarball
139 [ -d | --debug ]: Run with 'set -x' to debug errors
140 [ -s | --stream]: Use specified stream rather than 'tryreleased'
142 Additionally, clone hooks can be passed through (ie, --userdata). For those,
144 $CLONE_HOOK_FN --help
149 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
:,mapped-gid
: -- "$@")
150 if [ $?
-ne 0 ]; then
154 eval set -- "$options"
158 # default release is precise, or the systems release if recognized
160 if [ -f /etc
/lsb-release
]; then
162 rels
=$
(ubuntu-distro-info
--supported 2>/dev
/null
) ||
163 rels
="$KNOWN_RELEASES"
165 [ "$DISTRIB_CODENAME" = "$r" ] && release
="$r"
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`
176 if [ "$arch" = "i686" ]; then
178 elif [ "$arch" = "x86_64" ]; then
180 elif [ "$arch" = "armv7l" ]; then
181 # note: arm images don't exist before oneiric; are called armhf in
182 # precise and later; and are not supported by the query, so we don't actually
183 # support them yet (see check later on). When Query2 is available,
184 # we'll use that to enable arm images.
186 elif [ "$arch" = "aarch64" ]; then
188 elif [ "$arch" = "ppc64le" ]; then
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;;
209 -T|
--tarball) tarball
=$2; shift 2;;
210 -d|
--debug) debug
=1; shift 1;;
211 -s|
--stream) stream
=$2; shift 2;;
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;;
218 --mapped-uid) mapped_uid
=$2; shift 2;;
219 --mapped-gid) mapped_gid
=$2; shift 2;;
220 --) shift 1; break ;;
225 cloneargs
=( "--name=$name" "${cloneargs[@]}" )
227 if [ $debug -eq 1 ]; then
231 if [ "$arch" = "i686" ]; then
235 if [ "$skip_arch_check" = "0" ]; then
236 case "$hostarch:$arch" in
237 $arch:$arch) : ;; # the host == container
238 amd64
:i386
) :;; # supported "cross"
239 arm64
:arm
*) :;; # supported "cross"
240 armel
:armhf
) :;; # supported "cross"
241 armhf
:armel
) :;; # supported "cross"
242 *) echo "cannot create '$arch' container on hostarch '$hostarch'";
247 if [ "$stream" != "daily" -a "$stream" != "released" -a "$stream" != "tryreleased" ]; then
248 echo "Only 'daily' and 'released' and 'tryreleased' streams are supported"
252 if [ -z "$path" ]; then
253 echo "'path' parameter is required"
257 if [ "$(id -u)" != "0" ]; then
258 echo "This script should be run as 'root'"
263 config
="$path/config"
264 if [ -z "$rootfs" ]; then
265 if grep -q '^lxc.rootfs' $config 2>/dev
/null
; then
266 rootfs
=$
(awk -F= '/^lxc.rootfs =/{ print $2 }' $config)
272 type ubuntu-cloudimg-query
275 # determine the url, tarball, and directory names
277 # Allow the cache base to be set by environment variable
278 cache
=${LXC_CACHE_PATH:-"$STATE_DIR/cache/lxc"}/cloud-
$release
279 if [ $in_userns -eq 1 ]; then
280 STATE_DIR
="$HOME/.cache/lxc"
281 cache
=${LXC_CACHE_PATH:-"$STATE_DIR"}/cloud-
$release
286 if [ "$stream" = "tryreleased" ]; then
288 ubuntu-cloudimg-query
$release $stream $arch 1>/dev
/null
2>/dev
/null || stream
=daily
291 if [ -n "$tarball" ]; then
294 if ! url1
=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`; then
295 echo "There is no download available for release=$release, stream=$stream, arch=$arch"
296 [ "$stream" = "daily" ] ||
echo "You may try with '--stream=daily'"
299 url2
=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
302 filename
=`basename $url2`
312 umount
-l $cache/$xdir || true
316 # if the release doesn't have a *-rootfs.tar.gz, then create one from the
317 # cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
318 # a tarball from the mounted image.
324 xdir
=`mktemp -d -p .`
325 tarname
=`basename $url`
326 imgname
="$release-*-cloudimg-$arch.img"
327 trap buildcleanup EXIT SIGHUP SIGINT SIGTERM
328 if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
330 echo "Downloading cloud image from $url"
331 wget
$url ||
{ echo "Couldn't find cloud image $url."; exit 1; }
333 echo "Creating new cached cloud image rootfs"
334 tar --wildcards -zxf "$tarname" "$imgname"
335 mount
-o loop
$imgname $xdir
336 (cd $xdir; tar --numeric-owner -cpzf "../$filename" .
)
338 rm -f $tarname $imgname
340 echo "New cloud image cache created"
347 do_extract_rootfs
() {
350 if [ $flushcache -eq 1 ]; then
351 echo "Clearing the cached images"
355 trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM
356 if [ ! -f $filename ]; then
357 wget
$url2 || build_root_tgz
$url1 $filename
364 echo "Extracting container rootfs"
367 if [ $in_userns -eq 1 ]; then
368 tar --anchored --exclude="dev/*" --numeric-owner -xpzf "$cache/$filename"
369 mkdir
-p $rootfs/dev
/pts
/
371 tar --numeric-owner -xpzf "$cache/$filename"
375 if [ -n "$tarball" ]; then
378 mkdir
-p "$STATE_DIR/lock/subsys/"
382 ) 9>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud"
385 copy_configuration
$path $rootfs $name $arch $release
387 "$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs"
389 if [ $mapped_uid -ne -1 ]; then
390 chown
$mapped_uid $path/config
391 chown
-R $mapped_uid $STATE_DIR
392 chown
-R $mapped_uid $cache
394 if [ $mapped_gid -ne -1 ]; then
395 chgrp
$mapped_gid $path/config
396 chgrp
-R $mapped_gid $STATE_DIR
397 chgrp
-R $mapped_gid $cache
400 echo "Container $name created."