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

Contents of /trunk/lib/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3685 - (hide annotations) (download)
Tue Jul 25 16:53:46 2006 UTC (6 years, 9 months ago) by lange
File size: 15108 byte(s)
undefine local variable svar before printing all variables
1 lange 2102 #! /bin/bash
2 lange 1370
3 lange 1300 # $Id$
4     #*********************************************************************
5     #
6     # subroutines -- useful subroutines for FAI
7     #
8     # This script is part of FAI (Fully Automatic Installation)
9 lange 3300 # (c) 2000-2006 by Thomas Lange, lange@informatik.uni-koeln.de
10 lange 1300 # 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 lange 2600 task_savelog
37 lange 1300 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 lange 3621 ### 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 lange 1300 ifclass() {
59    
60 lange 2102 [ "$debug" ] && echo "Test if class $1 is in $classes"
61 lange 1300 # test if a class is defined
62     local cl
63 lange 1401 local ret=1
64    
65 lange 1300 for cl in $classes; do
66 lange 1401 [ x$cl = x$1 ] && ret=0 && break
67 lange 1300 done
68 lange 2102 [ "$debug" ] && echo "ifclass returns $ret"
69 lange 1401 return $ret
70 lange 1300 }
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 lange 1329 # can be an extern script
86 lange 1300 # wait for running (background) jobs to finish (e.g. update-auctex-elisp)
87     local i=0
88 lange 2090 while jobsrunning; do
89 lange 1300 [ $(($i % 3)) -eq 0 ] && echo "Waiting for background jobs to finish."
90 lange 3635 (( i += 1 ))
91 lange 1300 sleep 10
92     done
93     }
94     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95 lange 3621 ### BEGIN SUBROUTINE INFO
96     # Provides-Var: $terror
97     # Required-Var: $LOGDIR
98     # Short-Description: call a certain task
99     ### END SUBROUTINE INFO
100    
101 lange 1300 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 lange 3415 rm $LOGDIR/skip.$taskname # TODO: remove skip files at the very end
114 lange 1300 [ "$verbose" ] && echo "Skiping task_$taskname"
115 lange 2095 sndmon "TASKSKIP $taskname"
116 lange 1300 else
117     echo "Calling task_$taskname"
118 lange 2095 sndmon "TASKBEGIN $taskname"
119 lange 2259 terror=0 # task can set this variable to indicate an error
120 lange 1300 task_$taskname
121 lange 2095 sndmon "TASKEND $taskname $terror"
122 lange 1300 fi
123 lange 1586 # since the subroutine is not needed any more, we can undefine it
124     unset task_$taskname
125 lange 1300 }
126     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127 lange 3621 ### 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 lange 1300 call_hook() {
134    
135     local hook=$1
136     local cl dflag hfile
137     [ "$debug" ] && dflag="-d"
138    
139     for cl in $classes; do
140 lange 1459 hfile=$FAI/hooks/$hook.$cl
141 lange 3071 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 lange 1300 if [ -x $hfile ]; then
150 lange 1454 echo "Calling hook: $hook.$cl"
151 lange 2390 sndmon "HOOK $hook.$cl"
152 lange 1300 # execute the hook
153     $hfile $dflag
154 lange 1459 check_status $hook.$cl $?
155 lange 1300 fi
156     if [ -x $hfile.source ]; then
157 lange 1453 echo "Source hook: $hook.$cl.source"
158 lange 2390 sndmon "HOOK $hook.$cl.source"
159     # source this hook
160 lange 1300 . $hfile.source $dflag
161 lange 1459 check_status $hook.$cl.source $?
162 lange 1300 fi
163     done
164     }
165     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
166     skiptask() {
167    
168     # mark all given tasks, so they will be skipped
169     local task
170    
171 lange 3415 for task in "$@"; do
172 lange 1300 > $LOGDIR/skip.$task
173     done
174     }
175     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176     get_fai_dir() {
177    
178     # get /fai directory; mount it or get it from a cvs repository
179 lange 3340
180 lange 3517 [ -f /boot/RUNNING_FROM_FAICD ] && mkrw -s 100m $FAI
181 lange 3530 # now you have enough time to make changes to the config space
182     if [ -n "$fl_wait" ]; then
183     echo "Sleeping. Now you may change the config space in $FAI."
184     echo "Continue after killall sleep."
185     sleep 50000
186     fi
187    
188 lange 1300 if [ -z "$FAI_LOCATION" ]; then
189     get_fai_cvs
190     else
191     mount $romountopt $FAI_LOCATION $FAI &&
192 lange 1586 echo "Configuration space $FAI mounted from $FAI_LOCATION"
193 lange 1300 fi
194 lange 2671 ln -s $FAI $rundir/current_config
195 lange 1888 if [ ! -d $FAI/class ]; then
196 lange 2290 echo "WARNING: directory $FAI/class not found."
197 lange 1888 fi
198 lange 1300 }
199     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
200     get_fai_cvs() {
201    
202     # subroutine which gets $FAI (/fai) configuration directory from
203     # a cvs repository. You can redefine this subroutine if you need
204     # access via ftp, http, or from a database
205    
206     if [ "$FAI_CVSROOT" ] ; then
207     local TAG=""
208     [ -n "$FAI_CVSTAG" ] && TAG="-r $FAI_CVSTAG"
209 lange 2671 export FAI_CONFIG_AREA=$FAI_ROOT$FAI
210     export FAI=$(mktemp -t -d fai-config.XXXXXX)
211 lange 1300
212     [ "$debug" ] && echo "\$FAI now points to $FAI"
213    
214 lange 2671 if [ -d "$FAI_CONFIG_AREA/CVS" -a -z "$FORCE" ] ; then
215 lange 1300 echo "Config found at $FAI_CONFIG_AREA: Copying"
216 lange 2671 cp -a $FAI_CONFIG_AREA/. $FAI
217 lange 1849 echo "Updating CVS"
218 lange 2671 cd $FAI
219     cvs -q -d"$FAI_CVSROOT" up -P $TAG -d -C > $LOGDIR/cvs.log
220     else
221     echo "Checking out CVS"
222     cd /tmp
223     cvs -q -d"$FAI_CVSROOT" co -P -d $(basename "$FAI") \
224     $TAG $FAI_CVSMODULE > $LOGDIR/cvs.log
225 lange 1300 fi
226     else
227     echo "Warning $0: Neither \$FAI_LOCATION nor \$FAI_CVSROOT are defined."
228     fi
229 lange 2516 cd /
230 lange 1300 }
231     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
232     define_fai_flags() {
233    
234 lange 1586 local flag
235 lange 1464 # FAI_FLAGS are comma separated, define all flags
236 lange 1683 FAI_FLAGS=${FAI_FLAGS//,/ }
237 lange 2347 [ "$verbose" ] && echo "FAI_FLAGS: $FAI_FLAGS"
238 lange 1300 for flag in $FAI_FLAGS; do
239     # define this flag as 1
240     eval "$flag=1"
241     done
242     }
243     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
244 lange 3621 ### BEGIN SUBROUTINE INFO
245     # Provides-Var: $DNSDOMAIN $FAI_RUNDATE
246     # Requires-Var: $DOMAIN $DO_INIT_TASKS
247     # Suggests-Var: $createvt $sshd
248     # Short-Description: <task desc.>
249     ### END SUBROUTINE INFO
250    
251 lange 1300 task_setup() {
252    
253 lange 1640 # source user specific subroutines
254     [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
255 lange 1300
256     define_fai_flags
257     DNSDOMAIN=$DOMAIN # cfengine 1.5.3 can't use $DOMAIN
258    
259 lange 3392 # this may be moved to an external script
260 lange 2757 if [ $DO_INIT_TASKS -eq 1 ] ; then
261 lange 3392 # set the system time and date using rdate or/and ntpdate
262     [ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
263     [ "$NTPSRVS_1" ] && ntpdate -b -v $NTPSRVS
264 lange 2671 [ "$createvt" ] && {
265     # create two virtual terminals; acces via alt-F2 and alt-F3
266     echo "Press ctrl-c to interrupt FAI and to get a shell"
267     openvt -c2 /bin/bash ; openvt -c3 /bin/bash
268     trap 'echo "You can reboot with faireboot";bash' INT QUIT
269     }
270    
271     # start secure shell daemon for remote access
272     [ "$sshd" -a -x /usr/sbin/sshd ] && /usr/sbin/sshd
273     fi
274 lange 1300
275     # when did FAI start, using localtime
276 lange 1586 FAI_RUNDATE=$(date +'%Y%m%d_%H%M%S')
277 lange 1300 }
278     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
279 lange 3621 ### BEGIN SUBROUTINE INFO
280     # Provides-Var: none
281     # Requires-Var: $FAI_ACTION
282     # Short-Description: call task depending on $FAI_ACTION
283     ### END SUBROUTINE INFO
284    
285 lange 1300 task_action() {
286    
287 lange 1630 if [ -z "$FAI_ACTION" ]; then
288     echo "No action in \$FAI_ACTION defined."
289 lange 2290 sndmon "TASKERROR action 21"
290 lange 2012 task_faiend
291 lange 1630 exit
292     fi
293 lange 1300 echo "FAI_ACTION: $FAI_ACTION"
294     case $FAI_ACTION in
295    
296     install)
297 lange 2757 if [ $DO_INIT_TASKS -eq 0 ]; then
298     echo "Cowardly refusing to run a FAI installation on a running system."
299     return
300     fi
301 lange 1690 echo Performing FAI installation. All data may be overwritten!
302 lange 1689 echo -ne "\a"; sleep 1
303     echo -ne "\a"; sleep 1
304 lange 1700 echo -e "\a"; sleep 5
305 lange 1300 task install
306 lange 2108 task faiend
307 lange 1300 ;;
308 lange 2671 softupdate)
309     echo Performing FAI system update. All data may be overwritten!
310     task softupdate
311     ;;
312 lange 1300 sysinfo)
313     echo Showing system information.
314     task sysinfo
315 lange 2108 task_faiend
316 lange 1300 die Now you have a shell.
317     ;;
318     *)
319     if [ -f $FAI/hooks/$FAI_ACTION ]; then
320     echo "Calling user defined action: $FAI_ACTION"
321     $FAI/hooks/$FAI_ACTION
322     else
323     echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found."
324 lange 2290 sndmon "TASKERROR action 22"
325 lange 1300 task_faiend
326     fi
327     ;;
328     esac
329     }
330     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
331 lange 3621 ### BEGIN SUBROUTINE INFO
332     # Provides-Var: $classes $cfclasses
333     # Requires-Var: $LOGDIR
334     # Suggests-Var: $renewclass
335     # Short-Description: <task desc.>
336     ### END SUBROUTINE INFO
337    
338 lange 1300 task_defclass() {
339    
340 lange 2290 if [ ! -d $FAI/class ]; then
341     sndmon "TASKERROR defclass 21"
342     echo "Directory $FAI/class not found. Following subdirectories are found:"
343     find $FAI -type d -maxdepth 1 -printf "%p\n"
344     die "Aborting."
345     fi
346    
347 lange 1347 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
348 lange 2757 if [ $renewclass -eq 1 ]; then
349     # reevaluate new list of classes
350     fai-class -T $FAI/class $LOGDIR/FAI_CLASSES
351     classes=$(< $LOGDIR/FAI_CLASSES)
352     else
353     # use classes defined at installation time
354     if [ ! -f /var/log/fai/FAI_CLASSES ]; then
355     die "Try to read classes from /var/log/fai/FAI_CLASSES. Failed. Aborting."
356     fi
357     classes=$(< /var/log/fai/FAI_CLASSES)
358     fi
359 lange 1300
360     # define classes as: a.b.c.d for cfengine -D
361     # this doesn't work without echo
362 lange 2259 cfclasses=$(echo $classes)
363 lange 1300 cfclasses=${cfclasses// /.}
364     [ "$debug" ] && echo "cfclasses: $cfclasses"
365     }
366     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
367 lange 3300 _defvar() {
368    
369     local showvar=1 # TODO: new FAI_FLAG or always set when verbose is used
370 lange 3309 [ "$showvar" ] && set -x
371 lange 3300 . $1 </dev/null
372 lange 3309 [ "$showvar" ] && set +x
373 lange 3300 }
374     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
375 lange 1300 task_defvar() {
376    
377 lange 3423 local svar=/var/log/fai/current/showvar.log
378 lange 1300 cd $FAI/class
379     for class in $classes ; do
380     if [ -f $class.var ]; then
381     [ "$verbose" ] && echo "Executing $class.var"
382 lange 3300 # show only lines with ++, we cannot use a pipe, since it would call
383     # _devfar in a subprocess. Then, variables are not defined
384 lange 3414 _defvar $class.var > $svar 2>&1
385     grep ^++ $svar
386     rm $svar
387 lange 1300 fi
388     done
389    
390     # /fai/class/S* scripts or hooks can write variable definitions
391     # to additonal.var. now source these definitions
392 lange 3300 if [ -f $LOGDIR/additional.var ]; then
393 lange 3414 _defvar $LOGDIR/additional.var > $svar 2>&1
394     grep ^++ $svar
395     rm $svar
396 lange 3300 fi
397 lange 3685 unset class svar
398 lange 1586 # now all variables are defined. Dump them to variables.sh
399 lange 2481 set | perl -ne 'print if /^\w\w+=/' | egrep -v "^BASH_VERSINFO|^EUID|^PPID|^SHELLOPTS|^UID|^rootpw|^HOME|^PWD" > $LOGDIR/variables.sh
400 lange 2516 cd /
401 lange 1300 }
402     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
403     task_mountdisks() {
404    
405     [ ! -f $LOGDIR/$fstab ] && die "No $LOGDIR/$fstab created."
406 lange 2166 # mount swap space
407     local sd
408     for sd in $SWAPLIST; do
409     swapon $sd && [ "$verbose" ] && echo "Enable swap device $sd"
410     done
411 lange 1300 mount2dir $FAI_ROOT $LOGDIR/$fstab
412     }
413     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
414     task_configure() {
415    
416 lange 1761 fai-do-scripts $FAI/scripts
417 lange 1300 }
418     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
419     task_savelog() {
420    
421 lange 2600 [ -d $target/var/log/fai ] || mkdir -p $target/var/log/fai
422     [ -d $target/var/log/fai ] && fai-savelog -l
423 lange 3335 [ -f $LOGDIR/FAI_CLASSES ] && cp -p $LOGDIR/FAI_CLASSES $target/var/log/fai
424     [ -f $LOGDIR/variables.sh ] && cp -p $LOGDIR/variables.sh $target/var/log/fai
425     [ "$diskvar" != "/var/log/fai/disk_var.sh" ] && cp -p $diskvar $target/var/log/fai
426 lange 1369 fai-savelog -r
427 lange 1300 }
428     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
429     task_faiend() {
430    
431 lange 2757 [ $DO_INIT_TASKS -eq 0 ] && exit 0
432 lange 1300 wait_for_jobs
433     echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
434     # reboot without prompting if FAI_FLAG reboot is set
435     [ -z $reboot ] && read
436     echo "Rebooting $HOSTNAME now"
437 lange 2115 sndmon REBOOT
438 lange 1300 cd /
439     sync
440 lange 2012
441 lange 2259 case $(uname -s) in
442 lange 2012 Linux)
443 lange 2532 killall -q sshd
444 lange 2541 umount $target/proc
445 lange 2012 umount -ar
446     exec reboot -dfi
447     ;;
448     esac
449 lange 1300 }
450     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
451     task_install() {
452    
453     > $stamp
454    
455     save_dmesg
456    
457     task partition
458     task mountdisks
459     task extrbase
460     task mirror
461 lange 2612 task debconf
462 lange 2671 task prepareapt
463 lange 1300 task updatebase
464     task instsoft
465     task configure
466     task finish
467     task chboot
468    
469     rm -f $stamp
470     # save again, because new messages could be created
471     save_dmesg
472     task savelog
473    
474     if [ -f $stamp ]; then
475     echo "Error while executing commands in subshell."
476     echo "$stamp was not removed."
477 lange 2290 sndmon "TASKERROR install 21"
478 lange 1300 die "Please look at the log files in $LOGDIR for errors."
479     fi
480     }
481     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
482 lange 2671 task_softupdate() {
483    
484 lange 3092 local stamp=/var/run/fai/fai_softupdate_is_running
485    
486 lange 2671 > $stamp
487 lange 3335 diskvar=/var/log/disk_var.sh
488 lange 2671
489     save_dmesg
490    
491     local start_seconds=$(cut -d . -f 1 /proc/uptime)
492     task mirror
493     task debconf
494     task prepareapt
495     task updatebase
496     task instsoft
497     task configure
498     date
499     echo "The update took $[$(cut -d . -f 1 /proc/uptime)-$start_seconds] seconds."
500    
501     rm -f $stamp
502     # save again, because new messages could be created
503     save_dmesg
504     task savelog
505    
506     if [ -f $stamp ]; then
507     echo "Error while executing commands in subshell."
508     echo "$stamp was not removed."
509     sndmon "TASKERROR softupdate 21"
510     die "Please look at the log files in $LOGDIR for errors."
511     fi
512     umount $FAI_ROOT/fai
513     }
514     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
515    
516 lange 2301 catnc() {
517     # cat but no comment lines
518 lange 2403 egrep -v "^#" $@
519 lange 2301 }
520     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5