| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# template script for generating ubuntu container for LXC based on released |
| 4 |
# cloud images. |
| 5 |
# |
| 6 |
# Copyright © 2012 Serge Hallyn <[email protected]> |
| 7 |
# |
| 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. |
| 12 |
|
| 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. |
| 17 |
|
| 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 |
| 21 |
|
| 22 |
set -e |
| 23 |
|
| 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 xenial yakkety zesty" |
| 29 |
skip_arch_check=${UCTEMPLATE_SKIP_ARCH_CHECK:-0} |
| 30 |
|
| 31 |
# Make sure the usual locations are in PATH |
| 32 |
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin |
| 33 |
|
| 34 |
if [ -r /etc/default/lxc ]; then |
| 35 |
. /etc/default/lxc |
| 36 |
fi |
| 37 |
|
| 38 |
am_in_userns() { |
| 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; } |
| 43 |
echo yes |
| 44 |
} |
| 45 |
|
| 46 |
in_userns=0 |
| 47 |
[ $(am_in_userns) = "yes" ] && in_userns=1 |
| 48 |
|
| 49 |
copy_configuration() |
| 50 |
{ |
| 51 |
path=$1 |
| 52 |
rootfs=$2 |
| 53 |
name=$3 |
| 54 |
arch=$4 |
| 55 |
release=$5 |
| 56 |
|
| 57 |
if [ $arch = "i386" ]; then |
| 58 |
arch="i686" |
| 59 |
fi |
| 60 |
|
| 61 |
# if there is exactly one veth network entry, make sure it has an |
| 62 |
# associated hwaddr. |
| 63 |
nics=`grep -e '^lxc\.net\.0\.type[ \t]*=[ \t]*veth' $path/config | wc -l` |
| 64 |
if [ $nics -eq 1 ]; then |
| 65 |
grep -q "^lxc.net.0.hwaddr" $path/config || sed -i -e "/^lxc\.net\.0\.type[ \t]*=[ \t]*veth/a lxc.net.0.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config |
| 66 |
fi |
| 67 |
|
| 68 |
# Generate the configuration file |
| 69 |
## Relocate all the network config entries |
| 70 |
sed -i -e "/lxc.net.0/{w ${path}/config-network" -e "d}" $path/config |
| 71 |
|
| 72 |
## Relocate any other config entries |
| 73 |
sed -i -e "/lxc./{w ${path}/config-auto" -e "d}" $path/config |
| 74 |
|
| 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 |
| 86 |
fi |
| 87 |
|
| 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" $path/config 2>/dev/null || echo "lxc.rootfs.path = $rootfs" >> $path/config |
| 93 |
cat <<EOF >> $path/config |
| 94 |
lxc.uts.name = $name |
| 95 |
lxc.arch = $arch |
| 96 |
EOF |
| 97 |
|
| 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 |
| 103 |
|
| 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 |
| 112 |
else |
| 113 |
echo "Timezone in container is not configured. Adjust it manually." |
| 114 |
fi |
| 115 |
|
| 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 |
| 124 |
fi |
| 125 |
|
| 126 |
return 0 |
| 127 |
} |
| 128 |
|
| 129 |
usage() |
| 130 |
{ |
| 131 |
cat <<EOF |
| 132 |
LXC Container configuration for Ubuntu Cloud images. |
| 133 |
|
| 134 |
Generic Options |
| 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' |
| 141 |
|
| 142 |
Additionally, clone hooks can be passed through (ie, --userdata). For those, |
| 143 |
see: |
| 144 |
$CLONE_HOOK_FN --help |
| 145 |
EOF |
| 146 |
return 0 |
| 147 |
} |
| 148 |
|
| 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:,vendordata:,mapped-uid:,mapped-gid: -- "$@") |
| 150 |
if [ $? -ne 0 ]; then |
| 151 |
usage $(basename $0) |
| 152 |
exit 1 |
| 153 |
fi |
| 154 |
eval set -- "$options" |
| 155 |
|
| 156 |
mapped_uid=-1 |
| 157 |
mapped_gid=-1 |
| 158 |
# default release is trusty, or the systems release if recognized |
| 159 |
release=trusty |
| 160 |
if [ -f /etc/lsb-release ]; then |
| 161 |
. /etc/lsb-release |
| 162 |
rels=$(ubuntu-distro-info --supported 2>/dev/null) || |
| 163 |
rels="$KNOWN_RELEASES" |
| 164 |
for r in $rels; do |
| 165 |
[ "$DISTRIB_CODENAME" = "$r" ] && release="$r" |
| 166 |
done |
| 167 |
fi |
| 168 |
|
| 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 |
| 175 |
arch=$(uname -m) |
| 176 |
if [ "$arch" = "i686" ]; then |
| 177 |
arch="i386" |
| 178 |
elif [ "$arch" = "x86_64" ]; then |
| 179 |
arch="amd64" |
| 180 |
elif [ "$arch" = "armv7l" ]; then |
| 181 |
# note: arm images don't exist before oneiric; are called armhf in |
| 182 |
# trusty 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. |
| 185 |
arch="armhf" |
| 186 |
elif [ "$arch" = "aarch64" ]; then |
| 187 |
arch="arm64" |
| 188 |
elif [ "$arch" = "ppc64le" ]; then |
| 189 |
arch="ppc64el" |
| 190 |
fi |
| 191 |
fi |
| 192 |
|
| 193 |
debug=0 |
| 194 |
hostarch=$arch |
| 195 |
cloud=0 |
| 196 |
locales=1 |
| 197 |
flushcache=0 |
| 198 |
stream="tryreleased" |
| 199 |
cloneargs=() |
| 200 |
while true |
| 201 |
do |
| 202 |
case "$1" in |
| 203 |
-h|--help) usage $0 && exit 1;; |
| 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 |
-V|--vendordata) cloneargs[${#cloneargs[@]}]="--vendordata=$2"; shift 2;; |
| 217 |
-C|--cloud) cloneargs[${#cloneargs[@]}]="--cloud"; shift 1;; |
| 218 |
-S|--auth-key) cloneargs[${#cloneargs[@]}]="--auth-key=$2"; shift 2;; |
| 219 |
--mapped-uid) mapped_uid=$2; shift 2;; |
| 220 |
--mapped-gid) mapped_gid=$2; shift 2;; |
| 221 |
--) shift 1; break ;; |
| 222 |
*) break ;; |
| 223 |
esac |
| 224 |
done |
| 225 |
|
| 226 |
cloneargs=( "--name=$name" "${cloneargs[@]}" ) |
| 227 |
|
| 228 |
if [ $debug -eq 1 ]; then |
| 229 |
set -x |
| 230 |
fi |
| 231 |
|
| 232 |
if [ "$arch" = "i686" ]; then |
| 233 |
arch=i386 |
| 234 |
fi |
| 235 |
|
| 236 |
if [ "$skip_arch_check" = "0" ]; then |
| 237 |
case "$hostarch:$arch" in |
| 238 |
$arch:$arch) : ;; # the host == container |
| 239 |
amd64:i386) :;; # supported "cross" |
| 240 |
arm64:arm*) :;; # supported "cross" |
| 241 |
armel:armhf) :;; # supported "cross" |
| 242 |
armhf:armel) :;; # supported "cross" |
| 243 |
*) echo "cannot create '$arch' container on hostarch '$hostarch'"; |
| 244 |
exit 1;; |
| 245 |
esac |
| 246 |
fi |
| 247 |
|
| 248 |
if [ "$stream" != "daily" -a "$stream" != "released" -a "$stream" != "tryreleased" ]; then |
| 249 |
echo "Only 'daily' and 'released' and 'tryreleased' streams are supported" |
| 250 |
exit 1 |
| 251 |
fi |
| 252 |
|
| 253 |
if [ -z "$path" ]; then |
| 254 |
echo "'path' parameter is required" |
| 255 |
exit 1 |
| 256 |
fi |
| 257 |
|
| 258 |
if [ "$(id -u)" != "0" ]; then |
| 259 |
echo "This script should be run as 'root'" |
| 260 |
exit 1 |
| 261 |
fi |
| 262 |
|
| 263 |
# detect rootfs |
| 264 |
config="$path/config" |
| 265 |
if [ -z "$rootfs" ]; then |
| 266 |
if grep -q '^lxc.rootfs.path' $config 2>/dev/null ; then |
| 267 |
rootfs=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $config) |
| 268 |
else |
| 269 |
rootfs=$path/rootfs |
| 270 |
fi |
| 271 |
fi |
| 272 |
|
| 273 |
type ubuntu-cloudimg-query |
| 274 |
type wget |
| 275 |
|
| 276 |
# determine the url, tarball, and directory names |
| 277 |
# download if needed |
| 278 |
# Allow the cache base to be set by environment variable |
| 279 |
cache=${LXC_CACHE_PATH:-"$STATE_DIR/cache/lxc"}/cloud-$release |
| 280 |
if [ $in_userns -eq 1 ]; then |
| 281 |
STATE_DIR="$HOME/.cache/lxc" |
| 282 |
cache=${LXC_CACHE_PATH:-"$STATE_DIR"}/cloud-$release |
| 283 |
fi |
| 284 |
|
| 285 |
mkdir -p $cache |
| 286 |
|
| 287 |
if [ "$stream" = "tryreleased" ]; then |
| 288 |
stream=released |
| 289 |
ubuntu-cloudimg-query $release $stream $arch 1>/dev/null 2>/dev/null || stream=daily |
| 290 |
fi |
| 291 |
|
| 292 |
if [ -n "$tarball" ]; then |
| 293 |
url2="$tarball" |
| 294 |
else |
| 295 |
if ! url1=`ubuntu-cloudimg-query $release $stream $arch --format "%{url}\n"`; then |
| 296 |
echo "There is no download available for release=$release, stream=$stream, arch=$arch" |
| 297 |
[ "$stream" = "daily" ] || echo "You may try with '--stream=daily'" |
| 298 |
exit 1 |
| 299 |
fi |
| 300 |
if [ "$release" = "precise" ] || [ "$release" = "trusty" ]; then |
| 301 |
url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/' -e 's/.tar.gz/.tar.xz/'` |
| 302 |
else |
| 303 |
url2=`echo $url1 | sed -e 's/.tar.gz/.squashfs/'` |
| 304 |
fi |
| 305 |
fi |
| 306 |
|
| 307 |
filename=`basename $url2` |
| 308 |
|
| 309 |
wgetcleanup() |
| 310 |
{ |
| 311 |
rm -f $filename |
| 312 |
} |
| 313 |
|
| 314 |
do_extract_rootfs() { |
| 315 |
|
| 316 |
cd $cache |
| 317 |
if [ $flushcache -eq 1 ]; then |
| 318 |
echo "Clearing the cached images" |
| 319 |
rm -f $filename |
| 320 |
fi |
| 321 |
|
| 322 |
trap wgetcleanup EXIT SIGHUP SIGINT SIGTERM |
| 323 |
if [ ! -f $filename ]; then |
| 324 |
wget $url2 |
| 325 |
fi |
| 326 |
trap EXIT |
| 327 |
trap SIGHUP |
| 328 |
trap SIGINT |
| 329 |
trap SIGTERM |
| 330 |
|
| 331 |
echo "Extracting container rootfs" |
| 332 |
mkdir -p $rootfs |
| 333 |
cd $rootfs |
| 334 |
if [ "${filename##*.}" = "squashfs" ]; then |
| 335 |
unsquashfs -n -f -d "$rootfs" "$cache/$filename" |
| 336 |
else |
| 337 |
if [ $in_userns -eq 1 ]; then |
| 338 |
tar --anchored --exclude="dev/*" --numeric-owner -xpf "$cache/$filename" |
| 339 |
mkdir -p $rootfs/dev/pts/ |
| 340 |
else |
| 341 |
tar --numeric-owner -xpf "$cache/$filename" |
| 342 |
fi |
| 343 |
fi |
| 344 |
} |
| 345 |
|
| 346 |
if [ -n "$tarball" ]; then |
| 347 |
do_extract_rootfs |
| 348 |
else |
| 349 |
mkdir -p "$STATE_DIR/lock/subsys/" |
| 350 |
( |
| 351 |
flock -x 9 |
| 352 |
do_extract_rootfs |
| 353 |
) 9>"$STATE_DIR/lock/subsys/lxc-ubuntu-cloud" |
| 354 |
fi |
| 355 |
|
| 356 |
copy_configuration $path $rootfs $name $arch $release |
| 357 |
|
| 358 |
"$CLONE_HOOK_FN" "${cloneargs[@]}" "$rootfs" |
| 359 |
|
| 360 |
if [ $mapped_uid -ne -1 ]; then |
| 361 |
chown $mapped_uid $path/config |
| 362 |
chown -R $mapped_uid $STATE_DIR |
| 363 |
chown -R $mapped_uid $cache |
| 364 |
fi |
| 365 |
if [ $mapped_gid -ne -1 ]; then |
| 366 |
chgrp $mapped_gid $path/config |
| 367 |
chgrp -R $mapped_gid $STATE_DIR |
| 368 |
chgrp -R $mapped_gid $cache |
| 369 |
fi |
| 370 |
|
| 371 |
echo "Container $name created." |
| 372 |
exit 0 |
| 373 |
|
| 374 |
# vi: ts=4 expandtab |