Debian Codegraph @

templates/lxc-openmandriva.in

raw ◀ ▶
1 Debian patch touch this file
1 #!/bin/bash
2
3 #
4 # template script for generating openmandriva container for LXC
5 #
6
7 #
8 # lxc: linux Container library
9
10 # Authors:
11 # Alexander Stefanov <[email protected]>
12 # Vokhmin Alexey V <[email protected]>
13 # Bernhard Rosenkraenzer <[email protected]>
14
15 # This library is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU Lesser General Public
17 # License as published by the Free Software Foundation; either
18 # version 2.1 of the License, or (at your option) any later version.
19
20 # This library is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 # Lesser General Public License for more details.
24
25 # You should have received a copy of the GNU Lesser General Public
26 # License along with this library; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #
29
30 # Detect use under userns (unsupported)
31 for arg in "$@"; do
32 [ "$arg" = "--" ] && break
33 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
34 echo "This template can't be used for unprivileged containers." 1>&2
35 echo "You may want to try the \"download\" template instead." 1>&2
36 exit 1
37 fi
38 done
39
40 # Make sure the usual locations are in PATH
41 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
42
43 #Configurations
44 #distro=cooker
45 hostarch=$(uname -m)
46 # Allow the cache base to be set by environment variable
47 cache_base="${LXC_CACHE_PATH:-@LOCALSTATEDIR@/cache/lxc/openmandriva/$arch}"
48 default_path=@LXCPATH@
49 default_profile=default
50 lxc_network_type=veth
51 lxc_network_link=lxcbr0
52
53 # is this openmandriva?
54 grep -q OpenMandriva /etc/os-release && is_openmandriva=true
55
56 configure_openmandriva()
57 {
58 # set the hostname
59 echo "${utsname}" > ${rootfs_path}/etc/hostname
60
61 # set minimal hosts
62 cat <<EOF > $rootfs_path/etc/hosts
63 127.0.0.1 localhost.localdomain localhost $utsname
64 ::1 localhost6.localdomain6 localhost6
65 EOF
66 }
67
68 populate_dev()
69 {
70 echo -n "Create devices in /dev/"
71 dev_path="${rootfs_path}/dev"
72 rm -rf $dev_path
73 mkdir -p $dev_path
74 mknod -m 666 ${dev_path}/null c 1 3
75 mknod -m 666 ${dev_path}/zero c 1 5
76 mknod -m 666 ${dev_path}/random c 1 8
77 mknod -m 666 ${dev_path}/urandom c 1 9
78 mkdir -m 755 ${dev_path}/pts
79 mkdir -m 1777 ${dev_path}/shm
80 mknod -m 666 ${dev_path}/tty c 5 0
81 mknod -m 666 ${dev_path}/tty0 c 4 0
82 mknod -m 666 ${dev_path}/tty1 c 4 1
83 mknod -m 666 ${dev_path}/tty2 c 4 2
84 mknod -m 666 ${dev_path}/tty3 c 4 3
85 mknod -m 666 ${dev_path}/tty4 c 4 4
86 mknod -m 666 ${dev_path}/tty5 c 4 5
87 mknod -m 666 ${dev_path}/tty6 c 4 6
88 mknod -m 600 ${dev_path}/console c 5 1
89 mknod -m 666 ${dev_path}/full c 1 7
90 mknod -m 600 ${dev_path}/initctl p
91 mknod -m 666 ${dev_path}/ptmx c 5 2
92 mkdir -m 755 ${dev_path}/net
93 mknod -m 666 ${dev_path}/net/tun c 10 200
94 }
95
96 set_guest_root_password()
97 {
98 [ -z "$root_password" ] && return # pass is empty, abort
99
100 echo " - setting guest root password.."
101 echo "root passwd is: $root_password"
102 echo "root:$root_password" | chroot "$rootfs_path" chpasswd
103 echo "done."
104 }
105
106 create_chroot_openmandriva()
107 {
108 # check the mini openmandriva was not already downloaded
109 INSTALL_ROOT=$cache/cache
110 mkdir -p $INSTALL_ROOT
111 if [ $? -ne 0 ]; then
112 echo "Failed to create '$INSTALL_ROOT' directory"
113 return 1
114 fi
115
116 # Poor man's version of "dnf install --installroot=" --
117 # need to get the repository configs from rpm packages
118 # without having the repositories configured...
119 PKGS=http://abf-downloads.rosalinux.ru/$release/repository/$arch/main/release/
120 curl -s -L $PKGS |grep '^<a' |cut -d'"' -f2 >PACKAGES
121 PRE_PACKAGES="rosa-repos"
122 for i in $PRE_PACKAGES; do
123 P=`grep "^$i-[0-9].*" PACKAGES`
124 if [ "$?" != "0" ]; then
125 echo "Can't find $i package for target" >&2
126 exit 1
127 fi
128 curl -O $PKGS/$P
129 rpm -r $INSTALL_ROOT -Uvh --ignorearch --nodeps $P
130 done
131
132 # package list to install
133 PKG_LIST="basesystem-minimal locales locales-en systemd dnf networkmanager kbd"
134 # download a mini openmandriva into a cache
135 echo "Downloading openmandriva minimal ..."
136 DNF="/usr/bin/dnf -y install --nogpgcheck --installroot=$INSTALL_ROOT --releasever=$release --forcearch=$arch $PKG_LIST"
137 echo $DNF
138 $DNF
139 # We're splitting the old loop into two loops plus a directory retrival.
140 # First loop... Try and retrive a mirror list with retries and a slight
141 # delay between attempts...
142 if [ $? -ne 0 ]; then
143 echo "Failed to download the rootfs, aborting."
144 return 1
145 fi
146
147 mv "$INSTALL_ROOT" "$cache/rootfs"
148 echo "Download complete."
149
150 return 0
151 }
152
153 copy_openmandriva()
154 {
155
156 echo -n "Copying rootfs to $rootfs_path ..."
157 mkdir -p $rootfs_path
158 rsync -SHaAX $cache/rootfs/ $rootfs_path/
159 return 0
160 }
161
162 update_openmandriva()
163 {
164 echo "automated update in progress..."
165 dnf -y distro-sync --installroot=$cache/rootfs
166 }
167
168 configure_openmandriva_systemd()
169 {
170 chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/proc-sys-fs-binfmt_misc.automount
171 chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/systemd-udevd.service
172 chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/systemd-udevd-control.socket
173 chroot ${rootfs_path} ln -s /dev/null /etc/systemd/system/systemd-udevd-kernel.socket
174 # remove numlock service
175 # KDGKBLED: Inappropriate ioctl for device
176 rm -f ${rootfs_path}/etc/systemd/system/[email protected]/enable-numlock.conf
177
178 unlink ${rootfs_path}/etc/systemd/system/default.target
179 chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
180 sed -i 's!ConditionPathExists=/dev/tty0!ConditionPathExists=|/dev/tty0\nConditionVirtualization=|lxc!' \
181 ${rootfs_path}/lib/systemd/system/getty\@.service
182 }
183
184
185 install_openmandriva()
186 {
187 mkdir -p @LOCALSTATEDIR@/lock/subsys/
188 (
189 flock -x 9
190 if [ $? -ne 0 ]; then
191 echo "Cache repository is busy."
192 return 1
193 fi
194
195 echo "Checking cache download in $cache/rootfs ... "
196 if [ ! -e "$cache/rootfs" ]; then
197 echo $cache/rootfs
198 create_chroot_openmandriva
199 if [ $? -ne 0 ]; then
200 echo "Failed to download 'openmandriva basesystem-minimal'"
201 return 1
202 fi
203 else
204 echo "Cache found. Updating..."
205 update_openmandriva
206 if [ $? -ne 0 ]; then
207 echo "Failed to update 'openmandriva base', continuing with last known good cache"
208 else
209 echo "Update finished"
210 fi
211 fi
212
213 echo "Copy $cache/rootfs to $rootfs_path ... "
214 copy_openmandriva
215 if [ $? -ne 0 ]; then
216 echo "Failed to copy rootfs"
217 return 1
218 fi
219 return 0
220 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-openmandriva
221
222 return $?
223 }
224
225 copy_configuration()
226 {
227
228 mkdir -p $config_path
229 grep -q "^lxc.rootfs.path" $config_path/config 2>/dev/null || echo "lxc.rootfs.path = $rootfs_path" >> $config_path/config
230 cat <<EOF >> $config_path/config
231 lxc.uts.name = $name
232 lxc.tty.max = 4
233 lxc.pty.max = 1024
234 lxc.cap.drop = sys_module mac_admin mac_override sys_time
235 lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
236
237 # When using LXC with apparmor, uncomment the next line to run unconfined:
238 #lxc.apparmor.profile = unconfined
239
240 #networking
241 lxc.net.0.type = $lxc_network_type
242 lxc.net.0.flags = up
243 lxc.net.0.link = $lxc_network_link
244 lxc.net.0.name = eth0
245 lxc.net.0.mtu = 1500
246 EOF
247 if [ ! -z ${ipv4} ]; then
248 cat <<EOF >> $config_path/config
249 lxc.net.0.ipv4.address = $ipv4
250 EOF
251 fi
252 if [ ! -z ${gw} ]; then
253 cat <<EOF >> $config_path/config
254 lxc.net.0.ipv4.gateway = $gw
255 EOF
256 fi
257 if [ ! -z ${ipv6} ]; then
258 cat <<EOF >> $config_path/config
259 lxc.net.0.ipv6.address = $ipv6
260 EOF
261 fi
262 if [ ! -z ${gw6} ]; then
263 cat <<EOF >> $config_path/config
264 lxc.net.0.ipv6.gateway = $gw6
265 EOF
266 fi
267 cat <<EOF >> $config_path/config
268 #cgroups
•269 lxc.cgroup2.devices.deny = a
270 # /dev/null and zero
•271 lxc.cgroup2.devices.allow = c 1:3 rwm
272 lxc.cgroup2.devices.allow = c 1:5 rwm
273 # consoles
•274 lxc.cgroup2.devices.allow = c 5:1 rwm
275 lxc.cgroup2.devices.allow = c 5:0 rwm
276 lxc.cgroup2.devices.allow = c 4:0 rwm
277 lxc.cgroup2.devices.allow = c 4:1 rwm
278 # /dev/{,u}random
•279 lxc.cgroup2.devices.allow = c 1:9 rwm
280 lxc.cgroup2.devices.allow = c 1:8 rwm
281 lxc.cgroup2.devices.allow = c 136:* rwm
282 lxc.cgroup2.devices.allow = c 5:2 rwm
283 # rtc
•284 lxc.cgroup2.devices.allow = c 10:135 rwm
285 EOF
286
287 if [ $? -ne 0 ]; then
288 echo "Failed to add configuration"
289 return 1
290 fi
291
292 return 0
293 }
294
295 clean()
296 {
297
298 if [ ! -e $cache ]; then
299 exit 0
300 fi
301
302 # lock, so we won't purge while someone is creating a repository
303 (
304 flock -x 9
305 if [ $? != 0 ]; then
306 echo "Cache repository is busy."
307 exit 1
308 fi
309
310 echo -n "Purging the download cache for OpenMandriva-$release..."
311 rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
312 exit 0
313 ) 9>@LOCALSTATEDIR@/lock/subsys/lxc-openmandriva
314 }
315
316 usage()
317 {
318 cat <<EOF
319 usage:
320 $1 -n|--name=<container_name>
321 [-p|--path=<path>] [-c|--clean] [-R|--release=<openmandriva2013.0/rosa2012.1/cooker/ release>]
322 [-4|--ipv4=<ipv4 address>] [-6|--ipv6=<ipv6 address>]
323 [-g|--gw=<gw address>] [-d|--dns=<dns address>]
324 [-P|--profile=<name of the profile>] [--rootfs=<path>]
325 [-A|--arch=<arch of the container>]
326 [-h|--help]
327 Mandatory args:
328 -n,--name container name, used to as an identifier for that container from now on
329 Optional args:
330 -p,--path path to where the container rootfs will be created, defaults to @LXCPATH@. The container config will go under @LXCPATH@ in that case
331 -c,--clean clean the cache
332 -R,--release openmandriva2013.0/cooker/rosa2012.1 release for the new container. if the host is OpenMandriva, then it will default to the host's release.
333 -4,--ipv4 specify the ipv4 address to assign to the virtualized interface, eg. 192.168.1.123/24
334 -6,--ipv6 specify the ipv6 address to assign to the virtualized interface, eg. 2003:db8:1:0:214:1234:fe0b:3596/64
335 -g,--gw specify the default gw, eg. 192.168.1.1
336 -G,--gw6 specify the default gw, eg. 2003:db8:1:0:214:1234:fe0b:3596
337 -d,--dns specify the DNS server, eg. 192.168.1.2
338 -P,--profile Profile name is the file name in /etc/lxc/profiles contained packages name for install to cache.
339 -A,--arch Define what arch the container will be [i586,x86_64,armv7l,armv7hl]
340 ---rootfs rootfs path
341 -h,--help print this help
342 EOF
343 return 0
344 }
345
346 options=$(getopt -o hp:n:P:cR:4:6:g:d:A -l help,rootfs:,path:,name:,profile:,clean:,release:,ipv4:,ipv6:,gw:,dns:,arch: -- "$@")
347 if [ $? -ne 0 ]; then
348 usage $(basename $0)
349 exit 1
350 fi
351 eval set -- "$options"
352
353 release=${release:-"cooker"}
354
355 while true
356 do
357 case "$1" in
358 -h|--help) usage $0 && exit 0;;
359 -p|--path) path=$2; shift 2;;
360 --rootfs) rootfs_path=$2; shift 2;;
361 -n|--name) name=$2; shift 2;;
362 -P|--profile) profile=$2; shift 2;;
363 -c|--clean) clean=1; shift 1;;
364 -R|--release) release=$2; shift 2;;
365 -A|--arch) arch=$2; shift 2;;
366 -4|--ipv4) ipv4=$2; shift 2;;
367 -6|--ipv6) ipv6=$2; shift 2;;
368 -g|--gw) gw=$2; shift 2;;
369 -d|--dns) dns=$2; shift 2;;
370 --) shift 1; break ;;
371 *) break ;;
372 esac
373 done
374
375 arch=${arch:-$hostarch}
376
377 if [ ! -z "$clean" -a -z "$path" ]; then
378 clean || exit 1
379 exit 0
380 fi
381
382 if [ -z "${utsname}" ]; then
383 utsname=${name}
384 fi
385
386 type urpmi >/dev/null 2>&1
387 if [ $? -ne 0 ]; then
388 echo "'urpmi' command is missing"
389 exit 1
390 fi
391
392 if [ -z "$path" ]; then
393 path=$default_path
394 fi
395
396 if [ -z "$profile" ]; then
397 profile=$default_profile
398 fi
399
400 if [ $hostarch = "i686" -a $arch = "x86_64" ]; then
401 echo "can't create x86_64 container on i686"
402 exit 1
403 fi
404
405 if [ -z "$ipv4" -a -z "$ipv6" ]; then
406 BOOTPROTO="dhcp"
407 else
408 BOOTPROTO="static"
409 fi
410
411 if [ "$(id -u)" != "0" ]; then
412 echo "This script should be run as 'root'"
413 exit 1
414 fi
415
416 # check for 'lxc.rootfs.path' passed in through default config by lxc-create
417 if [ -z "$rootfs_path" ]; then
418 if grep -q '^lxc.rootfs.path' $path/config 2>/dev/null ; then
419 rootfs_path=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $path/config)
420 else
421 rootfs_path=$path/$name/rootfs
422 fi
423 fi
424
425 config_path=$default_path/$name
426 cache=$cache_base/$release/$arch/$profile
427
428 if [ ! -f $config_path/config ]; then
429 echo "A container with that name exists, chose a different name"
430 exit 1
431 fi
432
433 install_openmandriva
434 if [ $? -ne 0 ]; then
435 echo "failed to install openmandriva"
436 exit 1
437 fi
438
439 configure_openmandriva
440 if [ $? -ne 0 ]; then
441 echo "failed to configure openmandriva for a container"
442 exit 1
443 fi
444
445 # If the systemd configuration directory exists - set it up for what we need.
446 if [ -d ${rootfs_path}/etc/systemd/system ]
447 then
448 configure_openmandriva_systemd
449 fi
450
451 populate_dev
452 if [ $? -ne 0 ]; then
453 echo "failed to populated /dev/ devices"
454 exit 1
455 fi
456
457 set_guest_root_password
458 if [ $? -ne 0 ]; then
459 echo "failed to configure password for chroot"
460 exit 1
461 fi
462
463 copy_configuration
464 if [ $? -ne 0 ]; then
465 echo "failed write configuration file"
466 exit 1
467 fi
468
469 if [ ! -z "$clean" ]; then
470 clean || exit 1
471 exit 0
472 fi
473 echo "container rootfs and config created"