Debian Codegraph @

templates/lxc-plamo.in

raw
1 #!/bin/bash -eu
2
3 #
4 # template script for generating Plamo Linux container for LXC
5 #
6
7 #
8 # lxc: linux Container library
9
10 # Authors:
11 # KATOH Yasufumi <[email protected]>
12 # TAMUKI Shoichi <[email protected]>
13
14 # This library is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU Lesser General Public
16 # License as published by the Free Software Foundation; either
17 # version 2.1 of the License, or (at your option) any later version.
18
19 # This library is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # Lesser General Public License for more details.
23
24 # You should have received a copy of the GNU Lesser General Public
25 # License along with this library; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27
28 # ref. https://github.com/Ponce/lxc-slackware/blob/master/lxc-slackware
29 # lxc-ubuntu script
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 [ -r /etc/default/lxc ] && . /etc/default/lxc
45
46 download_plamo() {
47 # check the mini plamo was not already downloaded
48 if ! mkdir -p $ptcache ; then
49 echo "Failed to create '$ptcache' directory."
50 return 1
51 fi
52 # download a mini plamo into a cache
53 echo "Downloading Plamo-$release minimal..."
54 cd $ptcache
55 case $DLSCHEME in
56 http|https) depth=2 ;;
57 ftp) depth=3 ;;
58 esac
59 rej=${IGNOREPKGS%% *} ; [ -n "$rej" ] && rej="$rej-*"
60 if [ `echo $IGNOREPKGS | wc -w` -gt 1 ] ; then
61 for p in ${IGNOREPKGS#* } ; do rej="$rej,$p-*" ; done
62 fi
63 for i in $CATEGORIES ; do
64 wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz,.tzst -R "$rej" \
65 -I $MIRRORPATH/Plamo-$release/$arch/plamo/$i \
66 -X $MIRRORPATH/Plamo-$release/$arch/plamo/$i/old \
67 $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/plamo/$i
68 if [ $? -ne 0 ] ; then
69 echo "Failed to download the rootfs, aborting."
70 return 1
71 fi
72 done
73 for i in $EXTRACTGRS ; do
74 wget -nv -e robots=off -r -l $depth -nd -A .tgz,.txz,.tzst -R "$rej" \
75 -I $MIRRORPATH/Plamo-$release/$arch/contrib/$i \
76 -X $MIRRORPATH/Plamo-$release/$arch/contrib/$i/old \
77 $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/contrib/$i
78 if [ $? -ne 0 ] ; then
79 echo "Failed to download the rootfs, aborting."
80 return 1
81 fi
82 done
83 for p in $ADDONPKGS ; do
84 wget -nv -e robots=off -r -l $depth -nd -A "`basename $p`-*" \
85 -I $MIRRORPATH/Plamo-$release/$arch/`dirname $p` \
86 -X $MIRRORPATH/Plamo-$release/$arch/`dirname $p`/old \
87 $DLSCHEME://$MIRRORSRV$MIRRORPATH/Plamo-$release/$arch/`dirname $p`
88 if [ $? -ne 0 ] ; then
89 echo "Failed to download the rootfs, aborting."
90 return 1
91 fi
92 done
93 mv $ptcache $dlcache
94 echo "Download complete."
95 return 0
96 }
97
98 copy_plamo() {
99 # make a local copy of the mini plamo
100 echo "Copying $rtcache to $rootfs..."
101 mkdir -p $rootfs
102 find $rtcache -mindepth 1 -maxdepth 1 -exec cp -a {} $rootfs \; || return 1
103 return 0
104 }
105
106 install_plamo() {
107 mkdir -p @LOCALSTATEDIR@/lock/subsys
108 (
109 if ! flock -n 9 ; then
110 echo "Cache repository is busy."
111 return 1
112 fi
113 echo "Checking cache download in $dlcache..."
114 if [ ! -d $dlcache ] ; then
115 if ! download_plamo ; then
116 echo "Failed to download plamo $release base packages."
117 return 1
118 fi
119 fi
120 # install "installpkg" command temporarily with static linked tar
121 # command into the lxc cache directory to keep the original uid/
122 # gid of files/directories.
123 echo "Installing 'installpkg' command into $dlcache/sbin..."
124
125 if [ $majorver -ge 7 ]; then
126 pkgtool="pkgtools"
127 pkgtool=$( cd $dlcache ; ls "$pkgtool"* )
128 pkgtool=${pkgtool%%-*}
129 pkgtool=${pkgtool:0:-1}
130 else
131 pkgtool="hdsetup"
132 LANG=C
133 fi
134
135 ( cd $dlcache ; tar xpf "$pkgtool"*-*.t*z* ; rm -rf tmp usr var )
136 if [ $pkgtool = "pkgtools" ]; then
137 ( cd $dlcache/sbin
138 mv installer_new installer
139 pkg_scripts="installpkg installpkg2 installpkg2.mes makepkg updatepkg removepkg"
140 for s in $pkg_scripts
141 do
142 mv new_"$s" $s
143 done
144 cd installer
145 ln -sf zstd-* zstd
146 )
147 fi
148
149 sed -i "/ldconfig/!s@/sbin@$dlcache&@g" $dlcache/sbin/installpkg*
150 PATH=$dlcache/sbin:$PATH
151 echo "Installing packages to $rtcache..."
152 if [ ! -d $rtcache ] ; then
153 mkdir -p $rtcache
154 for p in `ls -cr $dlcache/*.t*z*` ; do
155 installpkg -root $rtcache -priority ADD $p
156 done
157 fi
158 echo "Copy $rtcache to $rootfs..."
159 if ! copy_plamo ; then
160 echo "Failed to copy rootfs."
161 return 1
162 fi
163 return 0
164 ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
165 }
166
167 configure_plamo6() {
168 # suppress log level output for udev
169 sed -i 's/="err"/=0/' $rootfs/etc/udev/udev.conf
170 # /etc/fstab
171 cat <<- "EOF" > $rootfs/etc/fstab
172 none /proc proc defaults 0 0
173 none /sys sysfs defaults 0 0
174 none /dev tmpfs defaults 0 0
175 none /tmp tmpfs defaults 0 0
176 none /dev/pts devpts gid=5,mode=620 0 0
177 none /proc/bus/usb usbfs noauto 0 0
178 none /var/lib/nfs/rpc_pipefs rpc_pipefs defaults 0 0
179 EOF
180 # /etc/inittab
181 cat <<- "EOF" | patch $rootfs/etc/inittab
182 32,33c32,33
183 < # What to do when power fails (shutdown to single user).
184 < pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
185 ---
186 > # What to do when power fails (shutdown).
187 > pf::powerfail:/sbin/shutdown -h +0 "THE POWER IS FAILING"
188 47a48
189 > 1:1235:respawn:/sbin/agetty 38400 console
190 52,53d52
191 < c5:1235:respawn:/sbin/agetty 38400 tty5 linux
192 < c6:12345:respawn:/sbin/agetty 38400 tty6 linux
193 EOF
194 # configure the network using the dhcp
195 echo "DHCP" > $rootfs/var/run/inet1-scheme
196
197 # delete unnecessary process from rc.S
198 ed - $rootfs/etc/rc.d/rc.S <<- "EOF"
199 /^mount -w -n -t proc/;/^mkdir \/dev\/shm/-1d
200 /^mknod \/dev\/null/;/^# Clean \/etc\/mtab/-2d
201 /^# copy the rules/;/^# Set the hostname/-1d
202 /^# Check the integrity/;/^# Clean up temporary/-1d
203 w
204 EOF
205
206 # delete unnecessary process from rc.M
207 ed - $rootfs/etc/rc.d/rc.M <<- "EOF"
208 /^# Screen blanks/;/^# Initialize ip6tables/-1d
209 /^# Initialize sysctl/;/^echo "Starting services/-1d
210 /^sync/;/^# All done/-1d
211 w
212 EOF
213
214 # delete unnecessary process from rc.6
215 ed - $rootfs/etc/rc.d/rc.6 <<- "EOF"
216 /^# Save system time/;/^# Unmount any remote filesystems/-1d
217 /^# Turn off swap/;/^# See if this is a powerfail situation/-1d
218 w
219 EOF
220
221 # /etc/rc.d/rc.inet1.tradnet
222 head -n-93 $rootfs/sbin/netconfig.tradnet > /tmp/netconfig.rconly
223 cat <<- EOF >> /tmp/netconfig.rconly
224 PCMCIA=n
225 RC=$rootfs/etc/rc.d/rc.inet1.tradnet
226 IFCONFIG=sbin/ifconfig
227 ROUTE=sbin/route
228 INET1SCHEME=var/run/inet1-scheme
229 IPADDR=127.0.0.1
230 NETWORK=127.0.0.0
231 DHCPCD=usr/sbin/dhclient
232 LOOPBACK=y
233 make_config_file
234 EOF
235 rm -f $rootfs/etc/rc.d/rc.inet1.tradnet
236 sh /tmp/netconfig.rconly
237 rm -f /tmp/netconfig.rconly
238 sed -i '/cmdline/s/if/& false \&\&/' $rootfs/etc/rc.d/rc.inet1.tradnet
239 # /etc/rc.d/rc.inet2
240 sed -i '/rpc.mountd/s/^/#/' $rootfs/etc/rc.d/rc.inet2
241 sed -i '/modprobe/s/^/#/' $rootfs/etc/rc.d/rc.inet2
242 # configure to start only the minimum of service
243 chmod 644 $rootfs/etc/rc.d/init.d/saslauthd
244 chmod 644 $rootfs/etc/rc.d/init.d/open-iscsi
245 rm -f $rootfs/etc/rc.d/init.d/postfix
246 rm -f $rootfs/var/log/initpkg/shadow
247 return 0
248 }
249
250 configure_plamo7() {
251 # fstab
252 cat <<- "EOF" >> $rootfs/etc/fstab
253 proc /proc proc defaults 0 0
254 tmpfs /run tmpfs defaults 0 0
255 sysfs /sys sysfs defaults 0 0
256 tmpfs /tmp tmpfs defaults 0 0
257 devpts /dev/pts devpts gid=5,mode=620 0 0
258 usbfs /proc/bus/usb usbfs noauto 0 0
259 EOF
260
261 # inittab
262 sed -i -e '/^1:2345/i c:1235:respawn:/sbin/agetty console 38400'\
263 -e '/^ca:/a pf::powerfail:/sbin/shutdown -h +0 "THE POWER IS FAILING"' \
264 -e '/^4:2345/d' \
265 -e '/^5:2345/d' \
266 -e '/^6:2345/d' $rootfs/etc/inittab
267
268 # tweak init script on startup
269 remove_init_S="S05modules S10eudev S20swap S30checkfs S35setclock S50eudev_retry S70console"
270 for f in $remove_init_S
271 do
272 rm -f $rootfs/etc/rc.d/rcS.d/"$f"
273 done
274
275 # remove init script in runlevel3
276 remove_init="rc3.d/S30sshd
277 rc6.d/K30sshd rc6.d/K35setclock rc6.d/S65swap rc6.d/S70mountfs
278 rc0.d/K30sshd rc0.d/K35setclock rc0.d/S65swap rc0.d/S70mountfs"
279 for f in $remove_init
280 do
281 rm -f $rootfs/etc/rc.d/"$f"
282 done
283
284 # Tweak rc script
285 sed -i -e '/wait_for_user/d' \
286 -e '/Press Enter to/d' \
287 $rootfs/etc/rc.d/init.d/rc
288
289 # network
290 cat <<- "EOF" > $rootfs/etc/sysconfig/ifconfig.eth0
291 ONBOOT="yes"
292 IFACE="eth0"
293 SERVICE="dhclient"
294 EOF
295
296 # remove initpkg that do not execute on containers
297 noexec="shadow netconfig7 eudev openssh"
298 for f in $noexec
299 do
300 rm -f $rootfs/var/log/initpkg/"$f"
301 done
302 }
303
304 configure_plamo() {
305 # set the hostname
306 echo "$name" > $rootfs/etc/HOSTNAME
307 # set minimal hosts
308 echo "127.0.0.1 localhost $name" > $rootfs/etc/hosts
309 # localtime (JST)
310 ln -s ../usr/share/zoneinfo/Asia/Tokyo $rootfs/etc/localtime
311 # disable pam_loginuid.so in /etc/pam.d/login
312 sed -i '/pam_loginuid/s/^/#/' $rootfs/etc/pam.d/login
313 if [ $majorver -ge 7 ]; then
314 configure_plamo7
315 else
316 configure_plamo6
317 fi
318 }
319
320 copy_configuration() {
321 ret=0
322 cat <<- EOF >> $path/config || let ret++
323 lxc.uts.name = $name
324 lxc.arch = $arch
325 EOF
326 if [ -f "@LXCTEMPLATECONFIG@/plamo.common.conf" ] ; then
327 cat <<- "EOF" >> $path/config || let ret++
328
329 lxc.include = @LXCTEMPLATECONFIG@/plamo.common.conf
330 EOF
331 fi
332 if [ $ret -ne 0 ] ; then
333 echo "Failed to add configuration."
334 return 1
335 fi
336 return 0
337 }
338
339 post_process() {
340 # nothing do in Plamo Linux
341 true
342 }
343
344 do_bindhome() {
345 # bind-mount the user's path into the container's /home
346 h=`getent passwd $bindhome | cut -d: -f6`
347 mkdir -p $rootfs/$h
348 echo "lxc.mount.entry = $h $rootfs/$h none bind 0 0" >> $path/config
349 # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
350 if ! pwd=`getent passwd $bindhome` ; then
351 echo "Warning: failed to copy password entry for $bindhome."
352 else
353 echo $pwd >> $rootfs/etc/passwd
354 fi
355 echo `getent shadow $bindhome` >> $rootfs/etc/shadow
356 }
357
358 cleanup() {
359 [ -d $dlcache -a -d $rtcache ] || return 0
360 # lock, so we won't purge while someone is creating a repository
361 (
362 if ! flock -n 9 ; then
363 echo "Cache repository is busy."
364 return 1
365 fi
366 echo "Purging the download cache..."
367 rm -rf --one-file-system $dlcache $rtcache || return 1
368 echo "Done."
369 return 0
370 ) 9> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
371 }
372
373 usage() {
374 cat <<- EOF
375 $prog [-h|--help] -p|--path=<path> -n|--name=<name> --rootfs=<rootfs>
376 [-c|--clean] [-r|--release=<release>] [-a|--arch=<arch>]
377 [-b|--bindhome=<user>]
378 release: $release
379 arch: x86 or x86_64: defaults to host arch
380 bindhome: bind <user>'s home into the container
381 EOF
382 }
383
384 prog=`basename $0`
385 path="" ; name="" ; rootfs=""
386 clean=0
387 release=${release:-7.x}
388 arch=`uname -m | sed 's/i.86/x86/'` ; hostarch=$arch
389 bindhome=""
390 sopts=hp:n:cr:a:b:
391 lopts=help,path:,name:,rootfs:,clean,release:,arch:,bindhome:
392 if ! options=`getopt -o $sopts -l $lopts -- "$@"` ; then
393 usage
394 exit 1
395 fi
396 eval set -- "$options"
397 while true ; do
398 case "$1" in
399 -h|--help) usage && exit 0 ;;
400 -p|--path) path=$2 ; shift 2 ;;
401 -n|--name) name=$2 ; shift 2 ;;
402 --rootfs) rootfs=$2 ; shift 2 ;;
403 -c|--clean) clean=1 ; shift 1 ;;
404 -r|--release) release=$2 ; shift 2 ;;
405 -a|--arch) arch=$2 ; shift 2 ;;
406 -b|--bindhome) bindhome=$2 ; shift 2 ;;
407 --) shift 1 ; break ;;
408 *) break ;;
409 esac
410 done
411 if [ $clean -eq 1 -a -z "$path" ] ; then
412 cleanup || exit 1
413 exit 0
414 fi
415 if [ $hostarch == "x86" -a $arch == "x86_64" ] ; then
416 echo "Can't create x86_64 container on x86."
417 exit 1
418 fi
419 if [ -z "$path" ] ; then
420 echo "'path' parameter is required."
421 exit 1
422 fi
423 if [ -z "$name" ] ; then
424 echo "'name' parameter is required."
425 exit 1
426 fi
427 if [ `id -u` -ne 0 ] ; then
428 echo "This script should be run as 'root'."
429 exit 1
430 fi
431
432 majorver=${release%.*}
433 if [ $majorver -ge 7 ]; then
434 CATEGORIES=${CATEGORIES-"00_base"}
435 ADDONPKGS=${ADDONPKGS-""}
436 else
437 CATEGORIES=${CATEGORIES-"00_base 01_minimum"}
438 ADDONPKGS=${ADDONPKGS-"`echo contrib/Hamradio/{morse,qrq}`"}
439 fi
440
441 DLSCHEME=${DLSCHEME:-"https"}
442 MIRRORSRV=${MIRRORSRV:-"repository.plamolinux.org"}
443 MIRRORPATH=${MIRRORPATH:-"/pub/linux/Plamo"}
444 CATEGORIES=${CATEGORIES-"00_base 01_minimum"}
445 EXTRACTGRS=${EXTRACTGRS-""}
446 IGNOREPKGS=${IGNOREPKGS-"grub kernel lilo linux_firmware microcode_ctl
447 linux_firmwares cpufreqd cpufrequtils gpm ntp kmod kmscon"}
448 ADDONPKGS=${ADDONPKGS-"`echo contrib/Hamradio/{morse,qrq}`"}
449
450 cache="${LXC_CACHE_PATH:-@LOCALSTATEDIR@/cache/lxc}"
451 ptcache=$cache/partial-${prog##*-}-$release-$arch
452 dlcache=$cache/cache-${prog##*-}-$release-$arch
453 rtcache=$cache/rootfs-${prog##*-}-$release-$arch
454 if [ -z "$rootfs" ] ; then
455 if grep -q "^lxc.rootfs.path" $path/config ; then
456 rootfs=`awk -F= '/^lxc.rootfs.path =/{ print $2 }' $path/config`
457 else
458 rootfs=$path/rootfs
459 fi
460 fi
461 if ! install_plamo ; then
462 echo "Failed to install plamo $release."
463 exit 1
464 fi
465 if ! configure_plamo ; then
466 echo "Failed to configure plamo $release for a container."
467 exit 1
468 fi
469 if ! copy_configuration ; then
470 echo "Failed to write configuration file."
471 exit 1
472 fi
473 post_process
474 if [ -n "$bindhome" ] ; then
475 do_bindhome
476 fi
477 if [ $clean -eq 1 ] ; then
478 cleanup || exit 1
479 exit 0
480 fi