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

Contents of /trunk/share/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5