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

Contents of /trunk/lib/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2546 - (hide annotations) (download)
Thu Jan 6 14:09:53 2005 UTC (8 years, 4 months ago) by lange
Original Path: trunk/share/subroutines
File size: 11459 byte(s)
happy new year
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 2546 # (c) 2000-2005 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     echo "$@"
37     exec bash
38     }
39     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40     defnop() {
41    
42     # define given list of subroutine names as dummy function;
43     # this will fake unknown commands
44    
45     local name
46     for name in "$@";do
47     eval "$name () { :;}"
48     done
49     }
50     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51     ifclass() {
52    
53 lange 2102 [ "$debug" ] && echo "Test if class $1 is in $classes"
54 lange 1300 # test if a class is defined
55     local cl
56 lange 1401 local ret=1
57    
58 lange 1300 for cl in $classes; do
59 lange 1401 [ x$cl = x$1 ] && ret=0 && break
60 lange 1300 done
61 lange 2102 [ "$debug" ] && echo "ifclass returns $ret"
62 lange 1401 return $ret
63 lange 1300 }
64     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65     rwmount() {
66    
67     # remount partition read/write
68     mount -o rw,remount $1
69     }
70     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71     save_dmesg() {
72    
73     dmesg > $LOGDIR/dmesg.log
74     }
75     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
76     wait_for_jobs() {
77    
78 lange 1329 # can be an extern script
79 lange 1300 # wait for running (background) jobs to finish (e.g. update-auctex-elisp)
80     local i=0
81 lange 2090 while jobsrunning; do
82 lange 1300 [ $(($i % 3)) -eq 0 ] && echo "Waiting for background jobs to finish."
83     i=$(($i+1))
84     sleep 10
85     done
86     }
87     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88     task() {
89    
90     # hooks are called before a task is called
91     # if a task is skipped, also its hooks are skipped
92     # a hook can set the flag, so the accociated task is skipped
93    
94     local taskname=$1
95    
96     [ -f $LOGDIR/skip.$taskname ] || call_hook $taskname
97    
98     if [ -f $LOGDIR/skip.$taskname ]; then
99     # skip task
100     rm $LOGDIR/skip.$taskname
101     [ "$verbose" ] && echo "Skiping task_$taskname"
102 lange 2095 sndmon "TASKSKIP $taskname"
103 lange 1300 else
104     echo "Calling task_$taskname"
105 lange 2095 sndmon "TASKBEGIN $taskname"
106 lange 2259 terror=0 # task can set this variable to indicate an error
107 lange 1300 task_$taskname
108 lange 2095 sndmon "TASKEND $taskname $terror"
109 lange 1300 fi
110 lange 1586 # since the subroutine is not needed any more, we can undefine it
111     unset task_$taskname
112 lange 1300 }
113     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114     call_hook() {
115    
116     local hook=$1
117     local cl dflag hfile
118     [ "$debug" ] && dflag="-d"
119    
120     for cl in $classes; do
121 lange 1459 hfile=$FAI/hooks/$hook.$cl
122 lange 1300 if [ -x $hfile ]; then
123 lange 1454 echo "Calling hook: $hook.$cl"
124 lange 2390 sndmon "HOOK $hook.$cl"
125 lange 1300 # execute the hook
126     $hfile $dflag
127 lange 1459 check_status $hook.$cl $?
128 lange 1300 fi
129     if [ -x $hfile.source ]; then
130 lange 1453 echo "Source hook: $hook.$cl.source"
131 lange 2390 sndmon "HOOK $hook.$cl.source"
132     # source this hook
133 lange 1300 . $hfile.source $dflag
134 lange 1459 check_status $hook.$cl.source $?
135 lange 1300 fi
136     done
137     }
138     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139     skiptask() {
140    
141     # mark all given tasks, so they will be skipped
142     local tasklist="$@"
143     local task
144    
145     for task in $tasklist; do
146     > $LOGDIR/skip.$task
147     done
148     }
149     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150     get_fai_dir() {
151    
152     # get /fai directory; mount it or get it from a cvs repository
153     if [ -z "$FAI_LOCATION" ]; then
154     [ "$debug" ] && echo "Warning $0: \$FAI_LOCATION not defined."
155     get_fai_cvs
156     else
157     mount $romountopt $FAI_LOCATION $FAI &&
158 lange 1586 echo "Configuration space $FAI mounted from $FAI_LOCATION"
159 lange 1300 fi
160 lange 1888 if [ ! -d $FAI/class ]; then
161 lange 2290 echo "WARNING: directory $FAI/class not found."
162 lange 1888 fi
163 lange 1300 }
164     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
165     get_fai_cvs() {
166    
167     # subroutine which gets $FAI (/fai) configuration directory from
168     # a cvs repository. You can redefine this subroutine if you need
169     # access via ftp, http, or from a database
170    
171     if [ "$FAI_CVSROOT" ] ; then
172     local TAG=""
173     echo "Checking out CVS"
174     [ -n "$FAI_CVSTAG" ] && TAG="-r $FAI_CVSTAG"
175     export FAI_CONFIG_AREA=$FAI
176     export FAI=/tmp/$(basename $FAI_CONFIG_AREA)
177    
178     [ "$debug" ] && echo "\$FAI now points to $FAI"
179    
180 lange 1849 if [ -d "$FAI_CONFIG_AREA" -a -z "$DONTCOPYCVS" ] ; then
181 lange 1300 echo "Config found at $FAI_CONFIG_AREA: Copying"
182     cp -a $FAI_CONFIG_AREA $FAI
183 lange 1849 echo "Updating CVS"
184 lange 1300 fi
185     cd /tmp
186     cvs -q -d"$FAI_CVSROOT" co -P -d $(basename "$FAI") \
187     $TAG $FAI_CVSMODULE > $LOGDIR/cvs.log
188     else
189     echo "Warning $0: Neither \$FAI_LOCATION nor \$FAI_CVSROOT are defined."
190     fi
191 lange 2516 cd /
192 lange 1300 }
193     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194     define_fai_flags() {
195    
196 lange 1586 local flag
197 lange 1464 # FAI_FLAGS are comma separated, define all flags
198 lange 1683 FAI_FLAGS=${FAI_FLAGS//,/ }
199 lange 2347 [ "$verbose" ] && echo "FAI_FLAGS: $FAI_FLAGS"
200 lange 1300 for flag in $FAI_FLAGS; do
201     # define this flag as 1
202     eval "$flag=1"
203     done
204     }
205     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206     task_setup() {
207    
208 lange 1640 # source user specific subroutines
209     [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
210 lange 1300 # set the system time and date using rdate or/and ntpdate
211     [ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
212     [ "$NTPSRVS_1" ] && ntpdate -b -v $NTPSRVS
213    
214     define_fai_flags
215     DNSDOMAIN=$DOMAIN # cfengine 1.5.3 can't use $DOMAIN
216    
217     [ "$createvt" ] && {
218     # 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     [ "$sshd" -a -x /usr/sbin/sshd ] && /usr/sbin/sshd
226    
227     # when did FAI start, using localtime
228 lange 1586 FAI_RUNDATE=$(date +'%Y%m%d_%H%M%S')
229 lange 1300 }
230     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
231     task_action() {
232    
233 lange 1630 if [ -z "$FAI_ACTION" ]; then
234     echo "No action in \$FAI_ACTION defined."
235 lange 2290 sndmon "TASKERROR action 21"
236 lange 2012 task_faiend
237 lange 1630 exit
238     fi
239 lange 1300 echo "FAI_ACTION: $FAI_ACTION"
240     case $FAI_ACTION in
241    
242     install)
243 lange 1690 echo Performing FAI installation. All data may be overwritten!
244 lange 1689 echo -ne "\a"; sleep 1
245     echo -ne "\a"; sleep 1
246 lange 1700 echo -e "\a"; sleep 5
247 lange 1300 task install
248 lange 2108 task faiend
249 lange 1300 ;;
250     sysinfo)
251     echo Showing system information.
252     task sysinfo
253 lange 2108 task_faiend
254 lange 1300 die Now you have a shell.
255     ;;
256     *)
257     if [ -f $FAI/hooks/$FAI_ACTION ]; then
258     echo "Calling user defined action: $FAI_ACTION"
259     $FAI/hooks/$FAI_ACTION
260     else
261     echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found."
262 lange 2290 sndmon "TASKERROR action 22"
263 lange 1300 task_faiend
264     fi
265     ;;
266     esac
267     }
268     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
269     task_defclass() {
270    
271 lange 2290 if [ ! -d $FAI/class ]; then
272     sndmon "TASKERROR defclass 21"
273     echo "Directory $FAI/class not found. Following subdirectories are found:"
274     find $FAI -type d -maxdepth 1 -printf "%p\n"
275     die "Aborting."
276     fi
277    
278 lange 1347 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
279     fai-class $FAI/class $LOGDIR/FAI_CLASSES
280 lange 1640 classes=$(< $LOGDIR/FAI_CLASSES)
281 lange 1300
282     # define classes as: a.b.c.d for cfengine -D
283     # this doesn't work without echo
284 lange 2259 cfclasses=$(echo $classes)
285 lange 1300 cfclasses=${cfclasses// /.}
286     [ "$debug" ] && echo "cfclasses: $cfclasses"
287     }
288     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
289     task_defvar() {
290    
291     cd $FAI/class
292     for class in $classes ; do
293     if [ -f $class.var ]; then
294     [ "$verbose" ] && echo "Executing $class.var"
295     [ "$debug" ] && set -vx
296     . $class.var </dev/null
297     [ "$debug" ] && set +vx
298     fi
299     done
300    
301     # /fai/class/S* scripts or hooks can write variable definitions
302     # to additonal.var. now source these definitions
303     [ "$debug" ] && set -vx
304     [ -f $LOGDIR/additional.var ] && . $LOGDIR/additional.var
305     [ "$debug" ] && set +vx
306 lange 1586 unset class
307     # now all variables are defined. Dump them to variables.sh
308 lange 2481 set | perl -ne 'print if /^\w\w+=/' | egrep -v "^BASH_VERSINFO|^EUID|^PPID|^SHELLOPTS|^UID|^rootpw|^HOME|^PWD" > $LOGDIR/variables.sh
309 lange 2516 cd /
310 lange 1300 }
311     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
312     task_mountdisks() {
313    
314     [ ! -f $LOGDIR/$fstab ] && die "No $LOGDIR/$fstab created."
315 lange 2166 # mount swap space
316     local sd
317     for sd in $SWAPLIST; do
318     swapon $sd && [ "$verbose" ] && echo "Enable swap device $sd"
319     done
320 lange 1300 mount2dir $FAI_ROOT $LOGDIR/$fstab
321     }
322     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
323     task_configure() {
324    
325 lange 1761 fai-do-scripts $FAI/scripts
326 lange 1300 }
327     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
328     task_savelog() {
329    
330 lange 2259 [ -d $target/etc/fai ] || mkdir -p $target/etc/fai
331 lange 1369 fai-savelog -l
332 lange 2540 cd $LOGDIR && cp -p FAI_CLASSES variables.sh $diskvar $target/var/log/fai
333 lange 1369 fai-savelog -r
334 lange 2475 cd /
335 lange 1300 }
336     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
337     task_faiend() {
338    
339     wait_for_jobs
340     echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
341     # reboot without prompting if FAI_FLAG reboot is set
342     [ -z $reboot ] && read
343     echo "Rebooting $HOSTNAME now"
344 lange 2115 sndmon REBOOT
345 lange 1300 cd /
346     sync
347 lange 2012
348 lange 2259 case $(uname -s) in
349 lange 2012 Linux)
350 lange 2532 killall -q sshd
351 lange 2541 umount $target/proc
352 lange 2012 umount -ar
353     exec reboot -dfi
354     ;;
355     esac
356 lange 1300 }
357     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
358     task_backup() {
359    
360 lange 1640 die "Task backup not yet used. But you can use the hook backup."
361 lange 1300 }
362     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
363     task_install() {
364    
365     > $stamp
366    
367     save_dmesg
368    
369     task partition
370     task mountdisks
371     task extrbase
372     task mirror
373     task updatebase
374     task instsoft
375     task configure
376     task finish
377     date
378 lange 2259 echo "The installation took $(cut -d . -f 1 /proc/uptime) seconds."
379 lange 1300 task chboot
380    
381     rm -f $stamp
382     # save again, because new messages could be created
383     save_dmesg
384     task savelog
385    
386     if [ -f $stamp ]; then
387     echo "Error while executing commands in subshell."
388     echo "$stamp was not removed."
389 lange 2290 sndmon "TASKERROR install 21"
390 lange 1300 die "Please look at the log files in $LOGDIR for errors."
391     fi
392     }
393     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
394 lange 2301 catnc() {
395     # cat but no comment lines
396 lange 2403 egrep -v "^#" $@
397 lange 2301 }
398     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5