/[fai]/trunk/lib/subroutines
ViewVC logotype

Contents of /trunk/lib/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3902 - (show annotations) (download)
Fri Sep 8 13:54:03 2006 UTC (6 years, 8 months ago) by lange
File size: 14097 byte(s)
umount replaced by trap
1 #! /bin/bash
2
3 # $Id$
4 #*********************************************************************
5 #
6 # subroutines -- useful subroutines for FAI
7 #
8 # This script is part of FAI (Fully Automatic Installation)
9 # (c) 2000-2006 by Thomas Lange, lange@informatik.uni-koeln.de
10 # Universitaet zu Koeln
11 #
12 #*********************************************************************
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
22 #
23 # A copy of the GNU General Public License is available as
24 # `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
25 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
26 # can also obtain it by writing to the Free Software Foundation, Inc.,
27 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #*********************************************************************
29
30 # source this file, then you have these function available in the shell
31
32 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33 die() {
34
35 # echo comment and exit installation
36 task_savelog
37 echo "$@"
38 exec bash
39 }
40 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41 defnop() {
42
43 # define given list of subroutine names as dummy function;
44 # this will fake unknown commands
45
46 local name
47 for name in "$@";do
48 eval "$name () { :;}"
49 done
50 }
51 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52 ### BEGIN SUBROUTINE INFO
53 # Provides-Var: none
54 # Required-Var: $classes
55 # Short-Description: test if class is defined
56 ### END SUBROUTINE INFO
57
58 ifclass() {
59
60 [ "$debug" ] && echo "Test if class $1 is in $classes" >/dev/stderr
61 # test if a class is defined
62 local cl
63 local ret=1
64
65 for cl in $classes; do
66 [ x$cl = x$1 ] && ret=0 && break
67 done
68 [ "$debug" ] && echo "ifclass returns $ret" >/dev/stderr
69 return $ret
70 }
71 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72 rwmount() {
73
74 # remount partition read/write
75 mount -o rw,remount $1
76 }
77 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78 save_dmesg() {
79
80 dmesg > $LOGDIR/dmesg.log
81 }
82 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83 wait_for_jobs() {
84
85 # can be an extern script
86 # wait for running (background) jobs to finish (e.g. update-auctex-elisp)
87 local i=0
88 while jobsrunning; do
89 [ $(($i % 3)) -eq 0 ] && echo "Waiting for background jobs to finish."
90 (( i += 1 ))
91 sleep 10
92 done
93 }
94 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95 ### BEGIN SUBROUTINE INFO
96 # Provides-Var: $task_error
97 # Required-Var: $LOGDIR
98 # Short-Description: call a certain task
99 ### END SUBROUTINE INFO
100
101 task() {
102
103 # hooks are called before a task is called
104 # if a task is skipped, also its hooks are skipped
105 # a hook can set the flag, so the accociated task is skipped
106
107 local taskname=$1
108
109 [ -f $LOGDIR/skip.$taskname ] || call_hook $taskname
110
111 if [ -f $LOGDIR/skip.$taskname ]; then
112 # skip task
113 rm $LOGDIR/skip.$taskname # TODO: remove skip files at the very end
114 [ "$verbose" ] && echo "Skiping task_$taskname"
115 sndmon "TASKSKIP $taskname"
116 else
117 echo "Calling task_$taskname"
118 sndmon "TASKBEGIN $taskname"
119 task_error=0 # task can set this variable to indicate an error
120 task_$taskname
121 sndmon "TASKEND $taskname $task_error"
122 fi
123 # since the subroutine is not needed any more, we can undefine it
124 unset task_$taskname
125 }
126 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127 ### BEGIN SUBROUTINE INFO
128 # Provides-Var:
129 # Required-Var: $classes $debug
130 # Short-Description: call a hook, hook.source can define additional variables
131 ### END SUBROUTINE INFO
132
133 call_hook() {
134
135 local hook=$1
136 local cl dflag hfile
137 [ "$debug" ] && dflag="-d"
138
139 for cl in $classes; do
140 hfile=$FAI/hooks/$hook.$cl
141 if [ -f $hfile -a ! -x $hfile ]; then
142 echo "WARNING: Skipping $hfile execustion because it's not executable."
143 continue
144 fi
145 if [ -f $hfile.source -a ! -x $hfile.source ]; then
146 echo "WARNING: Skipping $hfile.source execustion because it's not executable."
147 continue
148 fi
149 if [ -x $hfile ]; then
150 echo "Calling hook: $hook.$cl"
151 sndmon "HOOK $hook.$cl"
152 # execute the hook
153 $hfile $dflag
154 check_status $hook.$cl $?
155 fi
156 if [ -x $hfile.source ]; then
157 echo "Source hook: $hook.$cl.source"
158 sndmon "HOOK $hook.$cl.source"
159 # source this hook
160 . $hfile.source $dflag
161 check_status $hook.$cl.source $?
162 fi
163 done
164 }
165 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
166 skiptask() {
167
168 # mark all given tasks, so they will be skipped
169 local task
170
171 for task in "$@"; do
172 > $LOGDIR/skip.$task
173 done
174 }
175 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176 define_fai_flags() {
177
178 local flag
179 # FAI_FLAGS are comma separated, define all flags
180 FAI_FLAGS=${FAI_FLAGS//,/ }
181 echo "FAI_FLAGS: $FAI_FLAGS"
182 for flag in $FAI_FLAGS; do
183 # define this flag as 1
184 eval "flag_$flag=1"
185 done
186 [ "$flag_verbose" ] && verbose=1 # for backward compability
187 [ "$flag_debug" ] && debug=1 # for backward compability
188 }
189 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
190 ### BEGIN SUBROUTINE INFO
191 # Provides-Var: $fai_rundate
192 # Requires-Var: $DOMAIN $do_init_tasks
193 # Suggests-Var: $flag_createvt $flag_sshd
194 # Short-Description: <task desc.>
195 ### END SUBROUTINE INFO
196
197 task_setup() {
198
199 # source user specific subroutines
200 [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
201
202 define_fai_flags
203
204 # this may be moved to an external script
205 if [ $do_init_tasks -eq 1 ] ; then
206 # set the system time and date using rdate or/and ntpdate
207 [ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
208 [ "$NTPSRVS_1" ] && ntpdate -b -v $NTPSRVS
209 [ "$flag_createvt" ] && {
210 # create two virtual terminals; acces via alt-F2 and alt-F3
211 echo "Press ctrl-c to interrupt FAI and to get a shell"
212 openvt -c2 /bin/bash ; openvt -c3 /bin/bash
213 trap 'echo "You can reboot with faireboot";bash' INT QUIT
214 }
215
216 # start secure shell daemon for remote access
217 [ "$flag_sshd" -a -x /usr/sbin/sshd ] && /usr/sbin/sshd
218 fi
219 unset flag_createvt flag_sshd
220
221 # when did FAI start, using localtime
222 fai_rundate=$(date +'%Y%m%d_%H%M%S')
223 }
224 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
225 ### BEGIN SUBROUTINE INFO
226 # Provides-Var: none
227 # Requires-Var: $FAI_ACTION
228 # Short-Description: call task depending on $FAI_ACTION
229 ### END SUBROUTINE INFO
230
231 task_action() {
232
233 if [ -z "$FAI_ACTION" ]; then
234 echo "No action in \$FAI_ACTION defined."
235 sndmon "TASKERROR action 21"
236 task_faiend
237 exit
238 fi
239 echo "FAI_ACTION: $FAI_ACTION"
240 case $FAI_ACTION in
241
242 install)
243 if [ $do_init_tasks -eq 0 ]; then
244 echo "Cowardly refusing to run a FAI installation on a running system."
245 return
246 fi
247 echo Performing FAI installation. All data may be overwritten!
248 echo -ne "\a"; sleep 1
249 echo -ne "\a"; sleep 1
250 echo -e "\a"; sleep 5
251 task install
252 task faiend
253 ;;
254 dirinst)
255 echo "Installing into directory $FAI_ROOT"
256 task dirinst
257 ;;
258 softupdate)
259 echo Performing FAI system update. All data may be overwritten!
260 task softupdate
261 ;;
262 sysinfo)
263 echo Showing system information.
264 task sysinfo
265 task_faiend
266 die Now you have a shell.
267 ;;
268 *)
269 if [ -f $FAI/hooks/$FAI_ACTION ]; then
270 echo "Calling user defined action: $FAI_ACTION"
271 $FAI/hooks/$FAI_ACTION
272 else
273 echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found."
274 sndmon "TASKERROR action 22"
275 task_faiend
276 fi
277 ;;
278 esac
279 }
280 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
281 ### BEGIN SUBROUTINE INFO
282 # Provides-Var: $classes $cfclasses
283 # Requires-Var: $LOGDIR
284 # Suggests-Var: $renewclass
285 # Short-Description: <task desc.>
286 ### END SUBROUTINE INFO
287
288 task_defclass() {
289
290 if [ ! -d $FAI/class ]; then
291 sndmon "TASKERROR defclass 21"
292 echo "Directory $FAI/class not found. Following subdirectories are found:"
293 find $FAI -type d -maxdepth 1 -printf "%p\n"
294 die "Aborting."
295 fi
296
297 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
298 if [ $renewclass -eq 1 ]; then
299 # reevaluate new list of classes
300 fai-class -T $FAI/class $LOGDIR/FAI_CLASSES
301 classes=$(< $LOGDIR/FAI_CLASSES)
302 elif [ -n "$cmdlineclasses" ]; then
303 classes=$cmdlineclasses
304 elif [ ! -f /var/log/fai/FAI_CLASSES ]; then
305 # use classes defined at installation time
306 die "Try to read classes from /var/log/fai/FAI_CLASSES. Failed. Aborting."
307 else
308 classes=$(< /var/log/fai/FAI_CLASSES)
309 fi
310 echo "List of all classes: " $classes
311
312 # define classes as: a.b.c.d for cfengine -D
313 # this doesn't work without echo
314 cfclasses=$(echo $classes)
315 cfclasses=${cfclasses// /.}
316 [ "$debug" ] && echo "cfclasses: $cfclasses"
317 }
318 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
319 _defvar() {
320
321 local showvar=1 # TODO: new FAI_FLAG or always set when verbose is used
322 [ "$showvar" ] && set -x
323 . $1 </dev/null
324 [ "$showvar" ] && set +x
325 }
326 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
327 task_defvar() {
328
329 local svar=$LOGDIR/showvar.log
330 cd $FAI/class
331 for class in $classes ; do
332 if [ -f $class.var ]; then
333 [ "$verbose" ] && echo "Executing $class.var"
334 # show only lines with ++, we cannot use a pipe, since it would call
335 # _devfar in a subprocess. Then, variables are not defined
336 _defvar $class.var > $svar 2>&1
337 grep ^++ $svar
338 rm $svar
339 fi
340 done
341
342 # /fai/class/S* scripts or hooks can write variable definitions
343 # to additonal.var. now source these definitions
344 if [ -f $LOGDIR/additional.var ]; then
345 _defvar $LOGDIR/additional.var > $svar 2>&1
346 grep ^++ $svar
347 rm $svar
348 fi
349 unset class svar
350 # now all variables are defined. Dump them to variables.log
351 set | perl -ne 'print if /^\w\w+=/ and not /^(BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID|rootpw|ROOTPW|HOME|PWD)/' > $LOGDIR/variables.log
352 cd /
353 }
354 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
355 task_mountdisks() {
356
357 [ ! -f $LOGDIR/fstab ] && die "No $LOGDIR/fstab created."
358 # mount swap space
359 local sd
360 for sd in $SWAPLIST; do
361 swapon $sd && [ "$verbose" ] && echo "Enable swap device $sd"
362 done
363 mount2dir $FAI_ROOT $LOGDIR/fstab
364 }
365 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
366 task_configure() {
367
368 fai-do-scripts $FAI/scripts
369 }
370 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
371 task_savelog() {
372
373 [ -d $FAI_ROOT/var/lib/fai ] || mkdir -p $FAI_ROOT/var/lib/fai
374 [ -d $FAI_ROOT/var/log/fai ] || mkdir -p $FAI_ROOT/var/log/fai
375 fai-savelog -l
376 [ -f $LOGDIR/FAI_CLASSES ] && cp -pu $LOGDIR/FAI_CLASSES $FAI_ROOT/var/lib/fai
377 [ -f $LOGDIR/disk_var.sh ] && cp -pu $LOGDIR/disk_var.sh $FAI_ROOT/var/lib/fai
378 fai-savelog -r
379 }
380 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
381 task_faiend() {
382
383 [ $do_init_tasks -eq 0 ] && exit 0
384 wait_for_jobs
385 echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
386 # reboot without prompting if FAI_FLAG reboot is set
387 [ -z $flag_reboot ] && read
388 echo "Rebooting $HOSTNAME now"
389 sndmon REBOOT
390 cd /
391 sync
392
393 killall -q sshd
394 umount $FAI_ROOT/proc
395 umount -ar
396 exec reboot -dfi
397 }
398 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
399 task_install() {
400
401 > $stamp
402
403 save_dmesg
404
405 task partition
406 task mountdisks
407 task extrbase
408 task mirror
409 task debconf
410 task prepareapt
411 task updatebase
412 task instsoft
413 task configure
414 task finish
415 task chboot
416
417 rm -f $stamp
418 # save again, because new messages could be created
419 save_dmesg
420 task savelog
421
422 if [ -f $stamp ]; then
423 echo "Error while executing commands in subshell."
424 echo "$stamp was not removed."
425 sndmon "TASKERROR install 21"
426 die "Please look at the log files in $LOGDIR for errors."
427 fi
428 }
429 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
430 task_dirinst() {
431
432 > $stamp
433
434 mkdir -p $FAI_ROOT
435 task extrbase
436 task mirror
437 task debconf
438 task prepareapt
439 task updatebase
440 task instsoft
441 task configure
442 task finish
443
444 rm -f $stamp
445 unset LOGUSER # so logfile are not saved to remote
446 task savelog
447
448 if [ -f $stamp ]; then
449 echo "Error while executing commands in subshell."
450 echo "$stamp was not removed."
451 sndmon "TASKERROR install 21"
452 die "Please look at the log files in $LOGDIR for errors."
453 fi
454 }
455 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
456 task_softupdate() {
457
458 local stamp=/var/run/fai/fai_softupdate_is_running
459
460 > $stamp
461 cp -p /var/lib/fai/disk_var.sh $LOGDIR # use the last disk_var during update
462
463 save_dmesg
464
465 task mirror
466 task debconf
467 task updatebase
468 task instsoft
469 task configure
470 date
471 echo "The $FAI_ACTION took $[$(cut -d . -f 1 /proc/uptime)-$start_seconds] seconds."
472
473 rm -f $stamp
474 # save again, because new messages could be created
475 save_dmesg
476 task savelog
477
478 if [ -f $stamp ]; then
479 echo "Error while executing commands in subshell."
480 echo "$stamp was not removed."
481 sndmon "TASKERROR softupdate 21"
482 die "Please look at the log files in $LOGDIR for errors."
483 fi
484 umount $FAI_ROOT/fai
485 }
486 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
487
488 catnc() {
489 # cat but no comment lines
490 egrep -v "^#" $@
491 }
492 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5