Debian Codegraph @

templates/lxc-opensuse.in

raw
1 #!/bin/bash
2
3 #
4 # template script for generating OpenSUSE container for LXC
5 #
6
7 #
8 # lxc: linux Container library
9
10 # Authors:
11 # Daniel Lezcano <[email protected]>
12 # Frederic Crozat <[email protected]>
13 # Michael H. Warfield <[email protected]>
14 # Johannes Kastl <[email protected]>
15 # Thomas Lamprecht <[email protected]>
16
17 # This library is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU Lesser General Public
19 # License as published by the Free Software Foundation; either
20 # version 2.1 of the License, or (at your option) any later version.
21
22 # This library is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 # Lesser General Public License for more details.
26
27 # You should have received a copy of the GNU Lesser General Public
28 # License along with this library; if not, write to the Free Software
29 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30
31 # Detect use under userns (unsupported)
32 for arg in "$@"; do
33 [ "$arg" = "--" ] && break
34 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
35 echo "This template can't be used for unprivileged containers." 1>&2
36 echo "You may want to try the \"download\" template instead." 1>&2
37 exit 1
38 fi
39 done
40
41 # Make sure the usual locations are in PATH
42 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
43
44 if [ -x /usr/bin/obs-build ]; then
45 BUILD=/usr/bin/obs-build
46 export BUILD_DIR=/usr/lib/obs-build
47 else
48 BUILD=/usr/bin/build
49 export BUILD_DIR=/usr/lib/build
50 fi
51
52 configure_opensuse()
53 {
54 rootfs=$1
55 hostname=$2
56
57 # set first network adapter as dhcp. This is the most common config.
58 cat <<EOF > $rootfs/etc/sysconfig/network/ifcfg-eth0
59 STARTMODE='auto'
60 BOOTPROTO='dhcp'
61 EOF
62
63 # create empty fstab
64 touch $rootfs/etc/fstab
65
66 # set the hostname
67 cat <<EOF > $rootfs/etc/HOSTNAME
68 $hostname
69 EOF
70 # ensure /etc/hostname is available too
71 ln -s -f HOSTNAME $rootfs/etc/hostname
72
73 # do not use hostname from HOSTNAME variable
74 cat <<EOF >> $rootfs/etc/sysconfig/cron
75 unset HOSTNAME
76 EOF
77
78 # set minimal hosts
79 cat <<EOF > $rootfs/etc/hosts
80 127.0.0.1 localhost $hostname
81 EOF
82
83 # disable yast->bootloader in container
84 cat <<EOF > $rootfs/etc/sysconfig/bootloader
85 LOADER_TYPE=none
86 LOADER_LOCATION=none
87 EOF
88
89 # set /dev/console as securetty
90 cat << EOF >> $rootfs/etc/securetty
91 console
92 EOF
93
94 cat <<EOF >> $rootfs/etc/sysconfig/boot
95 # disable root fsck
96 ROOTFS_FSCK="0"
97 ROOTFS_BLKDEV="/dev/null"
98 EOF
99
100
101 # remove pointless services in a container
102 ln -s /dev/null $rootfs/etc/systemd/system/proc-sys-fs-binfmt_misc.automount
103 ln -s /dev/null $rootfs/etc/systemd/system/console-shell.service
104 ln -s /dev/null $rootfs/etc/systemd/system/systemd-vconsole-setup.service
105 # enable getty and console services
106 sed -e 's/ConditionPathExists=.*//' $rootfs/usr/lib/systemd/system/[email protected] > $rootfs/etc/systemd/system/[email protected]
107 ln -s [email protected] $rootfs/etc/systemd/system/[email protected]
108 mkdir -p $rootfs/etc/systemd/system/getty.target.wants/
109 ln -s ../[email protected] $rootfs/etc/systemd/system/getty.target.wants/[email protected]
110 ln -s -f ../[email protected] $rootfs/etc/systemd/system/getty.target.wants/[email protected]
111 ln -s ../[email protected] $rootfs/etc/systemd/system/getty.target.wants/[email protected]
112 ln -s ../[email protected] $rootfs/etc/systemd/system/getty.target.wants/[email protected]
113 ln -s ../[email protected] $rootfs/etc/systemd/system/getty.target.wants/[email protected]
114
115 touch $rootfs/etc/sysconfig/kernel
116
117 echo "Please change root-password !"
118
119 return 0
120 }
121
122 download_opensuse()
123 {
124 cache=$1
125 arch=$2
126
127 if [ ! -x ${BUILD} ]; then
128 echo "Could not create openSUSE template :"
129 echo "you need to install \"build\" package"
130 return 1
131 fi
132
133 # check the mini opensuse was not already downloaded
134 mkdir -p "$cache/partial-$arch"
135
136 if [ $? -ne 0 ]; then
137 echo "Failed to create '$cache/partial-$arch' directory"
138 return 1
139 fi
140
141 # download a mini opensuse into a cache
142 echo "Downloading opensuse minimal ..."
143 mkdir -p "$cache/partial-$arch-packages"
144
145 oss_repo_url="http://download.opensuse.org/distribution/$DISTRO/repo/oss/"
146 if [[ $DISTRO == "tumbleweed" ]]; then
147 oss_repo_url="http://download.opensuse.org/$DISTRO/repo/oss/"
148 fi
149 zypper --quiet --root $cache/partial-$arch-packages --non-interactive ar "$oss_repo_url" repo-oss || return 1
150
151 # Leap update repos were rearranged
152 if [[ $DISTRO != "tumbleweed" ]]; then
153 update_repo_url="http://download.opensuse.org/update/$DISTRO/oss/"
154 zypper --quiet --root $cache/partial-$arch-packages --non-interactive ar "$update_repo_url" update || return 1
155 fi
156
157 support_packages=(zypper patterns-openSUSE-base lxc ncurses-utils iputils udev netcfg hwinfo insserv-compat module-init-tools openSUSE-release openssh pwdutils sysconfig net-tools iproute2)
158
159 zypper --quiet --root $cache/partial-$arch-packages --non-interactive --gpg-auto-import-keys update || return 1
160 zypper --root $cache/partial-$arch-packages --non-interactive in --no-recommends --auto-agree-with-licenses --download-only zypper lxc patterns-openSUSE-base bash iputils sed tar rsyslog ${support_packages[*]} || return 1
161
162 cat > $cache/partial-$arch-packages/opensuse.conf << EOF
163 Preinstall: aaa_base bash coreutils diffutils
164 Preinstall: filesystem fillup glibc grep insserv-compat perl-base
165 Preinstall: libbz2-1 pam
166 Preinstall: permissions rpm sed tar libz1 libselinux1
167 Preinstall: liblzma5 libcap2 libacl1 libattr1
168 Preinstall: libpopt0 libelf1
169 Preinstall: libpcre1
170
171 RunScripts: aaa_base
172
173 Support: ${support_packages[*]}
174
175 Ignore: rpm:suse-build-key,build-key
176 Ignore: systemd:systemd-presets-branding
177 EOF
178
179 if [[ $DISTRO == "tumbleweed" ]]; then
180 echo "Preinstall: liblua5_3 libncurses6 libreadline7" >> $cache/partial-$arch-packages/opensuse.conf
181 elif [[ $DISTRO == "leap/1"* ]]; then
182 echo "Preinstall: liblua5_3-5 libncurses6 libreadline7" >> $cache/partial-$arch-packages/opensuse.conf
183 elif [[ $DISTRO == "leap/4"* ]]; then
184 echo "Preinstall: liblua5_1 libncurses5 libreadline6" >> $cache/partial-$arch-packages/opensuse.conf
185 echo "Support: rpcbind" >> $cache/partial-$arch-packages/opensuse.conf
186 fi
187
188 if [ "$arch" = "i686" ]; then
189 mkdir -p $cache/partial-$arch-packages/var/cache/zypp/packages/repo-oss/suse/i686/
190 for i in "$cache/partial-$arch-packages/var/cache/zypp/packages/repo-oss/suse/i586/*" ; do
191 ln -s $i $cache/partial-$arch-packages/var/cache/zypp/packages/repo-oss/suse/i686/
192 done
193 mkdir -p $cache/partial-$arch-packages/var/cache/zypp/packages/update/i686
194 for i in "$cache/partial-$arch-packages/var/cache/zypp/packages/update/i586/*" ; do
195 ln -s $i $cache/partial-$arch-packages/var/cache/zypp/packages/update/i686/
196 done
197 fi
198
199 # openSUSE 13.2 has no noarch directory in update
200 [ -d $cache/partial-$arch-packages/var/cache/zypp/packages/update/noarch ] || mkdir -p $cache/partial-$arch-packages/var/cache/zypp/packages/update/noarch
201 if [[ $DISTRO == "leap/4"* ]]; then
202 repos=("--repository" "$cache/partial-$arch-packages/var/cache/zypp/packages/repo-oss/suse/$arch" "--repository" "$cache/partial-$arch-packages/var/cache/zypp/packages/repo-oss/suse/noarch")
203 else
204 # Leap 15 has a different repo structure compared to 42.3
205 repos=("--repository" "$cache/partial-$arch-packages/var/cache/zypp/packages/repo-oss/$arch" "--repository" "$cache/partial-$arch-packages/var/cache/zypp/packages/repo-oss/noarch")
206 fi
207 if [[ $DISTRO != "tumbleweed" ]]; then # tumbleweed has no update repo
208 repos+=("--repository" "$cache/partial-$arch-packages/var/cache/zypp/packages/update/$arch" "--repository" "$cache/partial-$arch-packages/var/cache/zypp/packages/update/noarch")
209 fi
210
211 CLEAN_BUILD=1 BUILD_ARCH="$arch" BUILD_ROOT="$cache/partial-$arch" BUILD_DIST="$cache/partial-$arch-packages/opensuse.conf" PATH="$PATH:$BUILD_DIR" $BUILD_DIR/init_buildsystem --clean --configdir $BUILD_DIR/configs --cachedir $cache/partial-$arch-cache ${repos[*]} || return 1
212
213 chroot $cache/partial-$arch /usr/bin/zypper --quiet --non-interactive ar "$oss_repo_url" repo-oss || return 1
214 if [[ $DISTRO != "tumbleweed" ]]; then
215 chroot $cache/partial-$arch /usr/bin/zypper --quiet --non-interactive ar "$update_repo_url" update || return 1
216 fi
217
218 # really clean the image
219 rm -fr $cache/partial-$arch/{.build,.guessed_dist,.srcfiles*,installed-pkg}
220 rm -fr $cache/partial-$arch/dev
221 # make sure we have a minimal /dev
222 mkdir -p "$cache/partial-$arch/dev"
223 mknod -m 666 $cache/partial-$arch/dev/null c 1 3
224 mknod -m 666 $cache/partial-$arch/dev/zero c 1 5
225 # create mtab symlink
226 rm -f $cache/partial-$arch/etc/mtab
227 ln -sf /proc/self/mounts $cache/partial-$arch/etc/mtab
228
229 # ensure /var/run and /run are symlinked
230 rm -fr $cache/partial-$arch/var/run
231 ln -s -f ../run $cache/partial-$arch/var/run
232 if [ $? -ne 0 ]; then
233 echo "Failed to download the rootfs, aborting."
234 return 1
235 fi
236
237 rm -fr "$cache/partial-$arch-packages"
238 mv "$1/partial-$arch" "$1/rootfs-$arch"
239 echo "Download complete."
240
241 return 0
242 }
243
244 copy_opensuse()
245 {
246 cache=$1
247 arch=$2
248 rootfs=$3
249
250 # make a local copy of the mini opensuse
251 echo "Copying rootfs to $rootfs ..."
252 mkdir -p $rootfs
253 rsync -SHaAX $cache/rootfs-$arch/ $rootfs/ || return 1
254 return 0
255 }
256
257 install_opensuse()
258 {
259 # Allow the cache base to be set by environment variable
260 cache="${LXC_CACHE_PATH:-@LOCALSTATEDIR@/cache/lxc/opensuse/$DISTRO}"
261 rootfs=$1
262 mkdir -p @LOCALSTATEDIR@/lock/subsys/
263 (
264 flock -x 9
265 if [ $? -ne 0 ]; then
266 echo "Cache repository is busy."
267 return 1
268 fi
269
270 echo "Checking cache download in $cache/rootfs-$arch ... "
271 if [ ! -e "$cache/rootfs-$arch" ]; then
272 download_opensuse $cache $arch
273 if [ $? -ne 0 ]; then
274 echo "Failed to download 'opensuse base'"
275 return 1
276 fi
277 fi
278
279 echo "Copy $cache/rootfs-$arch to $rootfs ... "
280 copy_opensuse $cache $arch $rootfs
281 if [ $? -ne 0 ]; then
282 echo "Failed to copy rootfs"
283 return 1
284 fi
285
286 return 0
287 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-opensuse
288
289 return $?
290 }
291
292 # Generate a random hardware (MAC) address composed of FE followed by
293 # 5 random bytes...
294 create_hwaddr()
295 {
296 openssl rand -hex 5 | sed -e 's/\(..\)/:\1/g; s/^/fe/'
297 }
298
299 copy_configuration()
300 {
301 path=$1
302 rootfs=$2
303 name=$3
304
305 grep -q "^lxc.rootfs.path" $path/config 2>/dev/null || echo "
306 lxc.rootfs.path = $rootfs_path
307 " >> $path/config
308
309 # The following code is to create static MAC addresses for each
310 # interface in the container. This code will work for multiple
311 # interfaces in the default config. It will also strip any
312 # hwaddr stanzas out of the default config since we can not share
313 # MAC addresses between containers.
314 #
315 # This code is largely mimiced from the Fedora Template.
316 mv $path/config $path/config.def
317 while read LINE
318 do
319 # This should catch variable expansions from the default config...
320 if expr "${LINE}" : '.*\$' > /dev/null 2>&1
321 then
322 LINE=$(eval "echo \"${LINE}\"")
323 fi
324
325 # There is a tab and a space in the regex bracket below!
326 # Seems that \s doesn't work in brackets.
327 KEY=$(expr "${LINE}" : '\s*\([^ ]*\)\s*=')
328
329 if [[ "${KEY}" != "lxc.net.0.hwaddr" ]]
330 then
331 echo "${LINE}" >> $path/config
332
333 if [[ "${KEY}" == "lxc.net.0.link" ]]
334 then
335 echo "lxc.net.0.hwaddr = $(create_hwaddr)" >> $path/config
336 fi
337 fi
338 done < $path/config.def
339
340 rm -f $path/config.def
341
342 if [ -e "@LXCTEMPLATECONFIG@/opensuse.common.conf" ]; then
343 echo "
344 # Include common configuration
345 lxc.include = @LXCTEMPLATECONFIG@/opensuse.common.conf
346 " >> $path/config
347 fi
348
349 # Append things which require expansion here...
350 cat <<EOF >> $path/config
351 lxc.arch = $arch
352 lxc.uts.name = $name
353
354 lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
355
356 # When using LXC with apparmor, uncomment the next line to run unconfined:
357 lxc.apparmor.profile = unconfined
358
359 # example simple networking setup, uncomment to enable
360 #lxc.net.0.type = $lxc_network_type
361 #lxc.net.0.flags = up
362 #lxc.net.0.link = $lxc_network_link
363 #lxc.net.0.name = eth0
364 # Additional example for veth network type
365 # static MAC address,
366 #lxc.net.0.hwaddr = 00:16:3e:77:52:20
367 # persistent veth device name on host side
368 # Note: This may potentially collide with other containers of same name!
369 #lxc.net.0.veth.pair = v-$name-e0
370
371 EOF
372
373 if [ $? -ne 0 ]; then
374 echo "Failed to add configuration"
375 return 1
376 fi
377
378 return 0
379 }
380
381 clean()
382 {
383 cache="${LXC_CACHE_PATH:-@LOCALSTATEDIR@/cache/lxc/opensuse}"
384
385 if [ ! -e $cache ]; then
386 exit 0
387 fi
388
389 # lock, so we won't purge while someone is creating a repository
390 (
391 flock -x 9
392 if [ $? != 0 ]; then
393 echo "Cache repository is busy."
394 exit 1
395 fi
396
397 echo -n "Purging the download cache..."
398 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
399 exit 0
400 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-opensuse
401 }
402
403 usage()
404 {
405 cat <<EOF
406 $1 -h|--help -p|--path=<path> -r|--release nn.n --clean
407 Please give the release as 13.1, 13.2 etc.
408 If no release is given, openSUSE Leap 42.2 is installed.
409 EOF
410 return 0
411 }
412
413 # Make arch a global. This may become configurable?
414 arch=$(uname -m)
415
416 options=$(getopt -o hp:n:r:c -l help,rootfs:,path:,name:,release:,clean -- "$@")
417 if [ $? -ne 0 ]; then
418 usage $(basename $0)
419 exit 1
420 fi
421 eval set -- "$options"
422
423 while true
424 do
425 case "$1" in
426 -h|--help) usage $0 && exit 0;;
427 -p|--path) path=$2; shift 2;;
428 --rootfs) rootfs=$2; shift 2;;
429 -n|--name) name=$2; shift 2;;
430 -r|--release) DISTRO=$2; shift 2;;
431 -c|--clean) clean=1; shift 1;;
432 --) shift 1; break ;;
433 *) break ;;
434 esac
435 done
436
437 if [ ! -z "$clean" -a -z "$path" ]; then
438 clean || exit 1
439 exit 0
440 fi
441
442 type zypper > /dev/null
443 if [ $? -ne 0 ]; then
444 echo "'zypper' command is missing"
445 exit 1
446 fi
447
448 if [ -z "$path" ]; then
449 echo "'path' parameter is required"
450 exit 1
451 fi
452
453 if grep -q Harlequin /etc/os-release || grep -q Tumbleweed /etc/os-release ; then
454 BVER=`rpm -q --qf '%{version}\n' build`
455 if [ $? -ne 0 -o "$BVER" -lt "20141120" ]; then
456 echo "Building openSUSE containers with your version of the build package is broken. Please install the update to version 20141120 or newer."
457 exit 1
458 fi
459 fi
460
461 if [ -z "$DISTRO" ]; then
462 echo ""
463 echo "No release selected, using openSUSE Leap 15.0"
464 DISTRO="leap/15.0"
465 else
466 echo ""
467 case "$DISTRO" in
468 13.1)
469 echo "Selected openSUSE 13.1"
470 ;;
471
472 13.2)
473 echo "Selected openSUSE 13.2"
474 ;;
475
476 42.1|leap/42.1|421)
477 echo "Selected openSUSE Leap 42.1"
478 DISTRO="leap/42.1"
479 ;;
480
481 42.2|leap/42.2|422)
482 echo "Selected openSUSE Leap 42.2"
483 DISTRO="leap/42.2"
484 ;;
485
486 42.3|leap/42.3|423)
487 echo "Selected openSUSE Leap 42.3"
488 DISTRO="leap/42.3"
489 ;;
490
491 15.0|leap/15.0|150|leap)
492 echo "Selected openSUSE Leap 15.0"
493 DISTRO="leap/15.0"
494 ;;
495
496 tumbleweed|factory)
497 echo "Selected openSUSE Leap Tumbleweed"
498 DISTRO="tumbleweed"
499 ;;
500
501 *)
502 echo "You have chosen an invalid release, quitting..."
503 exit 1
504 ;;
505 esac
506 fi
507
508
509 if [ "$(id -u)" != "0" ]; then
510 echo "This script should be run as 'root'"
511 exit 1
512 fi
513
514 # detect rootfs
515 config="$path/config"
516 if [ -z "$rootfs" ]; then
517 if grep -q '^lxc.rootfs.path' $config 2>/dev/null ; then
518 rootfs=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $config)
519 else
520 rootfs=$path/rootfs
521 fi
522 fi
523
524 install_opensuse $rootfs
525 if [ $? -ne 0 ]; then
526 echo "failed to install opensuse"
527 exit 1
528 fi
529
530 configure_opensuse $rootfs $name
531 if [ $? -ne 0 ]; then
532 echo "failed to configure opensuse for a container"
533 exit 1
534 fi
535
536 copy_configuration $path $rootfs $name
537 if [ $? -ne 0 ]; then
538 echo "failed write configuration file"
539 exit 1
540 fi
541
542 if [ ! -z "$clean" ]; then
543 clean || exit 1
544 exit 0
545 fi