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

Contents of /trunk/lib/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4074 - (hide annotations) (download)
Tue Oct 10 21:26:08 2006 UTC (6 years, 7 months ago) by lange
File size: 15326 byte(s)
rename action dirinst to dirinstall (closes: #390345)
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 3747 [ "$debug" ] && echo "Test if class $1 is in $classes" >/dev/stderr
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 3747 [ "$debug" ] && echo "ifclass returns $ret" >/dev/stderr
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 lange 3822 # Provides-Var: $task_error
97 lange 3621 # 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 3822 task_error=0 # task can set this variable to indicate an error
120 lange 1300 task_$taskname
121 lange 3822 sndmon "TASKEND $taskname $task_error"
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     define_fai_flags() {
177    
178 lange 1586 local flag
179 lange 1464 # FAI_FLAGS are comma separated, define all flags
180 lange 1683 FAI_FLAGS=${FAI_FLAGS//,/ }
181 lange 3823 echo "FAI_FLAGS: $FAI_FLAGS"
182 lange 1300 for flag in $FAI_FLAGS; do
183     # define this flag as 1
184 lange 3828 eval "flag_$flag=1"
185 lange 1300 done
186 lange 3823 [ "$flag_verbose" ] && verbose=1 # for backward compability
187     [ "$flag_debug" ] && debug=1 # for backward compability
188 lange 1300 }
189     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
190 lange 3621 ### BEGIN SUBROUTINE INFO
191 lange 3848 # Provides-Var: $fai_rundate
192 lange 3831 # Requires-Var: $DOMAIN $do_init_tasks
193 lange 3823 # Suggests-Var: $flag_createvt $flag_sshd
194 lange 3621 # Short-Description: <task desc.>
195     ### END SUBROUTINE INFO
196    
197 lange 1300 task_setup() {
198    
199 lange 1640 # source user specific subroutines
200     [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
201 lange 1300
202 lange 3925 if [ -f /boot/RUNNING_FROM_FAICD ]; then
203     local cdrom=$(mount| awk '/dev.+on \/ /{print $1}')
204     hdparm -d $cdrom | grep -q off 2>/dev/null
205     if [ $? -eq 0 ]; then
206     echo "WARNING: CD-ROM does not use DMA mode. The installation will be sloooow."
207     fi
208     fi
209    
210 lange 1300 define_fai_flags
211    
212 lange 3392 # this may be moved to an external script
213 lange 3831 if [ $do_init_tasks -eq 1 ] ; then
214 lange 3392 # set the system time and date using rdate or/and ntpdate
215     [ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
216 lange 3961 [ "$NTPSRVS_1" ] && ntpdate -b $NTPSRVS
217 lange 3823 [ "$flag_createvt" ] && {
218 lange 2671 # create two virtual terminals; acces via alt-F2 and alt-F3
219     echo "Press ctrl-c to interrupt FAI and to get a shell"
220     openvt -c2 /bin/bash ; openvt -c3 /bin/bash
221     trap 'echo "You can reboot with faireboot";bash' INT QUIT
222     }
223    
224     # start secure shell daemon for remote access
225 lange 3823 [ "$flag_sshd" -a -x /usr/sbin/sshd ] && /usr/sbin/sshd
226 lange 2671 fi
227 lange 3823 unset flag_createvt flag_sshd
228 lange 1300
229     # when did FAI start, using localtime
230 lange 3848 fai_rundate=$(date +'%Y%m%d_%H%M%S')
231 lange 1300 }
232     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
233 lange 3621 ### BEGIN SUBROUTINE INFO
234     # Provides-Var: none
235     # Requires-Var: $FAI_ACTION
236     # Short-Description: call task depending on $FAI_ACTION
237     ### END SUBROUTINE INFO
238    
239 lange 1300 task_action() {
240    
241 lange 1630 if [ -z "$FAI_ACTION" ]; then
242     echo "No action in \$FAI_ACTION defined."
243 lange 2290 sndmon "TASKERROR action 21"
244 lange 2012 task_faiend
245 lange 1630 exit
246     fi
247 lange 1300 echo "FAI_ACTION: $FAI_ACTION"
248     case $FAI_ACTION in
249    
250     install)
251 lange 3831 if [ $do_init_tasks -eq 0 ]; then
252 lange 2757 echo "Cowardly refusing to run a FAI installation on a running system."
253     return
254     fi
255 lange 1690 echo Performing FAI installation. All data may be overwritten!
256 lange 1689 echo -ne "\a"; sleep 1
257     echo -ne "\a"; sleep 1
258 lange 1700 echo -e "\a"; sleep 5
259 lange 1300 task install
260 lange 2108 task faiend
261 lange 1300 ;;
262 lange 4074 dirinstall)
263 lange 3783 echo "Installing into directory $FAI_ROOT"
264 lange 4074 task dirinstall
265 lange 3783 ;;
266 lange 2671 softupdate)
267     echo Performing FAI system update. All data may be overwritten!
268     task softupdate
269     ;;
270 lange 1300 sysinfo)
271     echo Showing system information.
272     task sysinfo
273 lange 2108 task_faiend
274 lange 1300 die Now you have a shell.
275     ;;
276     *)
277     if [ -f $FAI/hooks/$FAI_ACTION ]; then
278     echo "Calling user defined action: $FAI_ACTION"
279     $FAI/hooks/$FAI_ACTION
280     else
281     echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found."
282 lange 2290 sndmon "TASKERROR action 22"
283 lange 1300 task_faiend
284     fi
285     ;;
286     esac
287     }
288     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
289 lange 3621 ### BEGIN SUBROUTINE INFO
290     # Provides-Var: $classes $cfclasses
291     # Requires-Var: $LOGDIR
292     # Suggests-Var: $renewclass
293     # Short-Description: <task desc.>
294     ### END SUBROUTINE INFO
295    
296 lange 1300 task_defclass() {
297    
298 lange 2290 if [ ! -d $FAI/class ]; then
299     sndmon "TASKERROR defclass 21"
300     echo "Directory $FAI/class not found. Following subdirectories are found:"
301 lange 4063 find $FAI -maxdepth 1 -type d -printf "%p\n"
302 lange 2290 die "Aborting."
303     fi
304    
305 lange 1347 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
306 lange 2757 if [ $renewclass -eq 1 ]; then
307     # reevaluate new list of classes
308     fai-class -T $FAI/class $LOGDIR/FAI_CLASSES
309     classes=$(< $LOGDIR/FAI_CLASSES)
310 lange 3796 elif [ -n "$cmdlineclasses" ]; then
311     classes=$cmdlineclasses
312     elif [ ! -f /var/log/fai/FAI_CLASSES ]; then
313 lange 2757 # use classes defined at installation time
314 lange 3819 die "Try to read classes from /var/log/fai/FAI_CLASSES. Failed. Aborting."
315 lange 3796 else
316 lange 2757 classes=$(< /var/log/fai/FAI_CLASSES)
317     fi
318 lange 3806 echo "List of all classes: " $classes
319 lange 1300
320     # define classes as: a.b.c.d for cfengine -D
321     # this doesn't work without echo
322 lange 2259 cfclasses=$(echo $classes)
323 lange 1300 cfclasses=${cfclasses// /.}
324     [ "$debug" ] && echo "cfclasses: $cfclasses"
325     }
326     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
327 lange 3300 _defvar() {
328    
329     local showvar=1 # TODO: new FAI_FLAG or always set when verbose is used
330 lange 3309 [ "$showvar" ] && set -x
331 lange 3300 . $1 </dev/null
332 lange 3309 [ "$showvar" ] && set +x
333 lange 3300 }
334     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
335 lange 1300 task_defvar() {
336    
337 lange 3781 local svar=$LOGDIR/showvar.log
338 lange 1300 cd $FAI/class
339     for class in $classes ; do
340     if [ -f $class.var ]; then
341     [ "$verbose" ] && echo "Executing $class.var"
342 lange 3300 # show only lines with ++, we cannot use a pipe, since it would call
343     # _devfar in a subprocess. Then, variables are not defined
344 lange 3414 _defvar $class.var > $svar 2>&1
345     grep ^++ $svar
346     rm $svar
347 lange 1300 fi
348     done
349    
350     # /fai/class/S* scripts or hooks can write variable definitions
351     # to additonal.var. now source these definitions
352 lange 3300 if [ -f $LOGDIR/additional.var ]; then
353 lange 3414 _defvar $LOGDIR/additional.var > $svar 2>&1
354     grep ^++ $svar
355     rm $svar
356 lange 3300 fi
357 lange 3685 unset class svar
358 lange 3881 # now all variables are defined. Dump them to variables.log
359     set | perl -ne 'print if /^\w\w+=/ and not /^(BASH_VERSINFO|EUID|PPID|SHELLOPTS|UID|rootpw|ROOTPW|HOME|PWD)/' > $LOGDIR/variables.log
360 lange 2516 cd /
361 lange 1300 }
362     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
363     task_mountdisks() {
364    
365 lange 3705 [ ! -f $LOGDIR/fstab ] && die "No $LOGDIR/fstab created."
366 lange 2166 # mount swap space
367     local sd
368     for sd in $SWAPLIST; do
369     swapon $sd && [ "$verbose" ] && echo "Enable swap device $sd"
370     done
371 lange 3705 mount2dir $FAI_ROOT $LOGDIR/fstab
372 lange 1300 }
373     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
374     task_configure() {
375    
376 lange 1761 fai-do-scripts $FAI/scripts
377 lange 1300 }
378     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
379     task_savelog() {
380    
381 lange 3887 [ -d $FAI_ROOT/var/lib/fai ] || mkdir -p $FAI_ROOT/var/lib/fai
382 lange 3780 [ -d $FAI_ROOT/var/log/fai ] || mkdir -p $FAI_ROOT/var/log/fai
383 lange 3887 fai-savelog -l
384 lange 3896 [ -f $LOGDIR/FAI_CLASSES ] && cp -pu $LOGDIR/FAI_CLASSES $FAI_ROOT/var/lib/fai
385     [ -f $LOGDIR/disk_var.sh ] && cp -pu $LOGDIR/disk_var.sh $FAI_ROOT/var/lib/fai
386 lange 1369 fai-savelog -r
387 lange 1300 }
388     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
389     task_faiend() {
390    
391 lange 3925 local dir cdromdevice
392 lange 3831 [ $do_init_tasks -eq 0 ] && exit 0
393 lange 1300 wait_for_jobs
394     echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
395 lange 3946 [ -z "$flag_reboot" ] && : ${flag_reboot:=0}
396 lange 3944 [ -s $LOGDIR.error.log -a "$flag_reboot" -gt "0" ] && sleep 10
397 lange 1300 # reboot without prompting if FAI_FLAG reboot is set
398 lange 3944 [ "$flag_reboot" -lt "1" ] && read
399 lange 1300 echo "Rebooting $HOSTNAME now"
400 lange 2115 sndmon REBOOT
401 lange 1300 cd /
402     sync
403 lange 2012
404 lange 3971 killall -q sshd udevd
405 lange 3925 if [ -f /boot/RUNNING_FROM_FAICD ]; then
406     cat > $target/tmp/rebootCD <<'EOF'
407     #! /bin/bash
408     device=$1
409     eject -m /dev/$device 2>/dev/null >/dev/null
410     echo "Remove CDROM and press <RETURN> to continue reboot"
411     read
412     eject -t /dev/$device 2>/dev/null >/dev/null
413     exec reboot -df
414     EOF
415     chmod +x $target/tmp/rebootCD
416     sync
417     for dir in $(mount | grep $target | awk '{print $3}' | sort -r); do
418     mount -o remount,ro $dir
419     done
420 lange 3934 cdromdevice=$(awk '/ name:/ {print $3}' /proc/sys/dev/cdrom/info)
421 lange 3925 chroot $target /tmp/rebootCD $cdromdevice
422 lange 3937 # never reached, because chroot will reboot the machine
423     die "Internal error when calling /tmp/rebootCD."
424 lange 3925 fi
425 lange 3787 umount $FAI_ROOT/proc
426     umount -ar
427     exec reboot -dfi
428 lange 1300 }
429     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
430     task_install() {
431    
432     > $stamp
433    
434     save_dmesg
435    
436     task partition
437     task mountdisks
438     task extrbase
439     task mirror
440 lange 2612 task debconf
441 lange 2671 task prepareapt
442 lange 1300 task updatebase
443     task instsoft
444     task configure
445     task finish
446     task chboot
447    
448     rm -f $stamp
449     # save again, because new messages could be created
450     save_dmesg
451     task savelog
452    
453     if [ -f $stamp ]; then
454     echo "Error while executing commands in subshell."
455     echo "$stamp was not removed."
456 lange 2290 sndmon "TASKERROR install 21"
457 lange 1300 die "Please look at the log files in $LOGDIR for errors."
458     fi
459     }
460     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
461 lange 4074 task_dirinstall() {
462 lange 3783
463     > $stamp
464    
465 lange 3787 mkdir -p $FAI_ROOT
466 lange 3783 task extrbase
467 lange 3945 [ -f $target/etc/fstab ] || touch $target/etc/fstab
468 lange 3783 task mirror
469     task debconf
470     task prepareapt
471     task updatebase
472     task instsoft
473     task configure
474 lange 3791 task finish
475 lange 3783
476     rm -f $stamp
477 lange 3801 unset LOGUSER # so logfile are not saved to remote
478     task savelog
479 lange 3783
480     if [ -f $stamp ]; then
481     echo "Error while executing commands in subshell."
482     echo "$stamp was not removed."
483     sndmon "TASKERROR install 21"
484     die "Please look at the log files in $LOGDIR for errors."
485     fi
486     }
487     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
488 lange 2671 task_softupdate() {
489    
490 lange 3092 local stamp=/var/run/fai/fai_softupdate_is_running
491    
492 lange 4066 [ -f "$stamp" ] && die "Another fai softupdate is already running. Aborting."
493 lange 2671 > $stamp
494 lange 3896 cp -p /var/lib/fai/disk_var.sh $LOGDIR # use the last disk_var during update
495 lange 2671
496 lange 3903 defnop wait_for_jobs
497 lange 2671 save_dmesg
498    
499     task mirror
500     task debconf
501     task updatebase
502     task instsoft
503     task configure
504     date
505 lange 3791 echo "The $FAI_ACTION took $[$(cut -d . -f 1 /proc/uptime)-$start_seconds] seconds."
506 lange 2671
507     rm -f $stamp
508     # save again, because new messages could be created
509     save_dmesg
510     task savelog
511    
512     if [ -f $stamp ]; then
513     echo "Error while executing commands in subshell."
514     echo "$stamp was not removed."
515     sndmon "TASKERROR softupdate 21"
516     die "Please look at the log files in $LOGDIR for errors."
517     fi
518     umount $FAI_ROOT/fai
519     }
520     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
521    
522 lange 2301 catnc() {
523     # cat but no comment lines
524 lange 2403 egrep -v "^#" $@
525 lange 2301 }
526     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5