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

Contents of /trunk/share/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1453 - (show annotations) (download)
Wed Jan 15 14:27:41 2003 UTC (10 years, 5 months ago) by lange
File size: 13576 byte(s)
remove -n from echo
1 #! /bin/sh
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-2002 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 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 [ "$debug" ] && set +vx
65 # test if a class is defined
66 local cl
67 local ret=1
68
69 for cl in $classes; do
70 [ x$cl = x$1 ] && ret=0 && break
71 done
72 [ "$debug" ] && set -vx
73 return $ret
74 }
75 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
76 create_resolv_conf() {
77
78 # can be an extern script
79 # create a resolv.conf using the DHCP or BOOTP information
80 if [ "$DNSSRVS" ]; then
81 [ "$DOMAIN" ] && echo "domain $DOMAIN" >/tmp/etc/resolv.conf
82 for dnshost in $DNSSRVS ; do
83 echo "nameserver $dnshost" >>/tmp/etc/resolv.conf
84 done
85 fi
86 }
87 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88 rwmount() {
89
90 # remount partition read/write
91 mount -o rw,remount $1
92 }
93 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94 save_dmesg() {
95
96 dmesg > $LOGDIR/dmesg.log
97 }
98 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99 wait_for_jobs() {
100
101 # can be an extern script
102 # wait for running (background) jobs to finish (e.g. update-auctex-elisp)
103 local i=0
104 while (jobsrunning); do
105 [ $(($i % 3)) -eq 0 ] && echo "Waiting for background jobs to finish."
106 i=$(($i+1))
107 sleep 10
108 done
109 }
110 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 task() {
112
113 # hooks are called before a task is called
114 # if a task is skipped, also its hooks are skipped
115 # a hook can set the flag, so the accociated task is skipped
116
117 local taskname=$1
118
119 [ -f $LOGDIR/skip.$taskname ] || call_hook $taskname
120
121 if [ -f $LOGDIR/skip.$taskname ]; then
122 # skip task
123 rm $LOGDIR/skip.$taskname
124 [ "$verbose" ] && echo "Skiping task_$taskname"
125 else
126 echo "Calling task_$taskname"
127 task_$taskname
128 fi
129 }
130 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131 call_hook() {
132
133 local hook=$1
134 local cl dflag hfile
135 [ "$debug" ] && dflag="-d"
136
137 for cl in $classes; do
138 hfile=$FAI/hooks/${hook}.$cl
139 if [ -x $hfile ]; then
140 echo "Call hook: $hook.$cl"
141 # execute the hook
142 $hfile $dflag
143 check_status $hfile $?
144 fi
145 if [ -x $hfile.source ]; then
146 echo "Source hook: $hook.$cl.source"
147 # source this hook
148 . $hfile.source $dflag
149 check_status $hfile.source $?
150 fi
151 done
152 }
153 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154 skiptask() {
155
156 # mark all given tasks, so they will be skipped
157 local tasklist="$@"
158 local task
159
160 for task in $tasklist; do
161 > $LOGDIR/skip.$task
162 done
163 }
164 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
165 get_fai_dir() {
166
167 # get /fai directory; mount it or get it from a cvs repository
168 if [ -z "$FAI_LOCATION" ]; then
169 [ "$debug" ] && echo "Warning $0: \$FAI_LOCATION not defined."
170 get_fai_cvs
171 else
172 mount $romountopt $FAI_LOCATION $FAI &&
173 echo "$FAI mounted from $FAI_LOCATION"
174 fi
175 # source user specific subroutines
176 [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
177 }
178 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179 get_fai_cvs() {
180
181 # subroutine which gets $FAI (/fai) configuration directory from
182 # a cvs repository. You can redefine this subroutine if you need
183 # access via ftp, http, or from a database
184
185 if [ "$FAI_CVSROOT" ] ; then
186 local TAG=""
187 echo "Checking out CVS"
188 [ -n "$FAI_CVSTAG" ] && TAG="-r $FAI_CVSTAG"
189 export FAI_CONFIG_AREA=$FAI
190 export FAI=/tmp/$(basename $FAI_CONFIG_AREA)
191
192 [ "$debug" ] && echo "\$FAI now points to $FAI"
193
194 if [ -d "$FAI_CONFIG_AREA" ] ; then
195 echo "Config found at $FAI_CONFIG_AREA: Copying"
196 cp -a $FAI_CONFIG_AREA $FAI
197 fi
198 cd /tmp
199 cvs -q -d"$FAI_CVSROOT" co -P -d $(basename "$FAI") \
200 $TAG $FAI_CVSMODULE > $LOGDIR/cvs.log
201 else
202 echo "Warning $0: Neither \$FAI_LOCATION nor \$FAI_CVSROOT are defined."
203 fi
204 }
205 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206 define_fai_flags() {
207
208 # FAI_FLAGS are now comma separated, define all flags
209 FAI_FLAGS=`echo $FAI_FLAGS | sed -e 's/,/ /g'`
210 for flag in $FAI_FLAGS; do
211 # define this flag as 1
212 eval "$flag=1"
213 [ "$verbose" ] && echo "FAI_FLAGS: $flag=1"
214 done
215 }
216 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217 task_setup() {
218
219 local flag
220 # set the system time and date using rdate or/and ntpdate
221 [ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
222 [ "$NTPSRVS_1" ] && ntpdate -b -v $NTPSRVS
223
224 define_fai_flags
225
226 DNSDOMAIN=$DOMAIN # cfengine 1.5.3 can't use $DOMAIN
227 devnull=/dev/null
228 [ "$debug" ] && devnull=/dev/console
229
230 # not yet used, maybe use a hook for that
231 # if [ "$FAI_EXTRA_MOUNT" ]; then
232 # mount $romountopt -n $FAI_EXTRA_MOUNT && echo "$FAI_EXTRA_MOUNT mounted"
233 # fi
234
235 [ "$createvt" ] && {
236 # create two virtual terminals; acces via alt-F2 and alt-F3
237 echo "Press ctrl-c to interrupt FAI and to get a shell"
238 openvt -c2 /bin/bash ; openvt -c3 /bin/bash
239 trap 'echo "You can reboot with faireboot";bash' INT QUIT
240 }
241
242 # start secure shell daemon for remote access
243 [ "$sshd" -a -x /usr/sbin/sshd ] && /usr/sbin/sshd
244 disk_info # get and define all information of local disks
245
246 # when did FAI start, using localtime
247 RUNDATE=$(date +'%Y%m%d_%H%M%S')
248
249 cat >> $rcsfaivar <<-EOM
250 RUNDATE=$RUNDATE
251 FAI_ACTION=$FAI_ACTION
252 LOGDIR=$LOGDIR
253 EOM
254 }
255 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
256 task_action() {
257
258 echo "FAI_ACTION: $FAI_ACTION"
259 case $FAI_ACTION in
260
261 install)
262 echo Performing FAI installation. All data may be overwritten!
263 task install
264 ;;
265 sysinfo)
266 echo Showing system information.
267 task sysinfo
268 die Now you have a shell.
269 ;;
270 backup)
271 echo Doing backup of multiple partitions.
272 task backup
273 ;;
274 *)
275 if [ -f $FAI/hooks/$FAI_ACTION ]; then
276 echo "Calling user defined action: $FAI_ACTION"
277 $FAI/hooks/$FAI_ACTION
278 else
279 echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found."
280 task_faiend
281 fi
282 ;;
283 esac
284 }
285 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
286 task_defclass() {
287
288 local class
289
290 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
291 fai-class $FAI/class $LOGDIR/FAI_CLASSES
292
293 classes=`cat $LOGDIR/FAI_CLASSES`
294
295 # also define all classes in reverse order ($revclasses)
296 for class in $classes ; do
297 revclasses="$class $revclasses"
298 done
299
300 # define classes as: a.b.c.d for cfengine -D
301 # this doesn't work without echo
302 cfclasses=`echo $classes`
303 cfclasses=${cfclasses// /.}
304 [ "$debug" ] && echo "cfclasses: $cfclasses"
305 }
306 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
307 task_defvar() {
308
309 local class
310 cd $FAI/class
311 for class in $classes ; do
312 if [ -f $class.var ]; then
313 [ "$verbose" ] && echo "Executing $class.var"
314 [ "$debug" ] && set -vx
315 . $class.var </dev/null
316 [ "$debug" ] && set +vx
317 fi
318 done
319
320 # /fai/class/S* scripts or hooks can write variable definitions
321 # to additonal.var. now source these definitions
322 [ "$debug" ] && set -vx
323 [ -f $LOGDIR/additional.var ] && . $LOGDIR/additional.var
324 [ "$debug" ] && set +vx
325 }
326 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
327 task_mountdisks() {
328
329 [ ! -f $LOGDIR/$fstab ] && die "No $LOGDIR/$fstab created."
330 if islinux; then
331 # mount swap space
332 local sd vflag
333 [ "$verbose" ] && vflag=-v
334 for sd in $SWAPLIST; do
335 swapon $vflag $sd
336 done
337 fi
338 mount2dir $FAI_ROOT $LOGDIR/$fstab
339 }
340 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
341 task_configure() {
342
343 # execute all scripts that match the name of a class.
344 # If class is a directory, execute all $class/S[0-9]* scripts in
345 # it, but do not execute files ending in ~
346
347 local class f
348
349 cd $FAI/scripts
350 for class in $classes ; do
351 [ -x $class -a -f $class ] && do_script $class
352 if [ -d $class ]; then
353 for f in `ls $class/S[0-9]*[^~]` ; do
354 [ -x $f -a -f $f ] && do_script $f
355 done
356 fi
357 done
358 }
359 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
360 do_script() {
361
362 # execute scripts and save their outpu in log files
363 # cfengine, shell, perl and expect scripts are known types
364 local shelldebug file filetype
365
366 file=$1
367
368 cd $FAI/scripts
369 filetype=`file $file`
370 shelldebug=
371 case $filetype in
372 *"Bourne shell script"*)
373 [ "$debug" ] && shelldebug="sh -x" ;;
374 *"Bourne-Again shell script"*)
375 [ "$debug" ] && shelldebug="bash -x" ;;
376 esac
377
378 case $filetype in
379
380 *"executable shell script"*|*"/bash script"*|*"Bourne shell script"*|*"Bourne-Again shell script"*)
381 echo "Executing $shelldebug shell: $file"
382 echo "===== shell: $file =====" >> $LOGDIR/shell.log 2>&1
383 $shelldebug ./$file >> $LOGDIR/shell.log 2>&1
384 check_status $file $?
385 ;;
386
387 *"cfengine script"*)
388 echo "Executing cfengine: $file"
389 echo "===== cfengine: $file =====" >> $LOGDIR/cfengine.log 2>&1
390 ./$file --no-lock -v -f $file -D${cfclasses} >> $LOGDIR/cfengine.log 2>&1
391 check_status $file $?
392 ;;
393
394 *"perl script"*)
395 echo "Executing perl: $file"
396 echo "===== perl: $file =====" >> $LOGDIR/perl.log 2>&1
397 ./$file >> $LOGDIR/perl.log 2>&1
398 check_status $file $?
399 ;;
400
401 *"expect script"*)
402 echo "Executing expect: $file"
403 echo "===== expect: $file =====" >> $LOGDIR/expect.log 2>&1
404 ./$file >> $LOGDIR/expect.log 2>&1
405 check_status $file $?
406 ;;
407
408 *) echo "File $file has unsupported type $filetype." ;;
409 esac
410 }
411 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
412 task_savelog() {
413
414 fai-savelog -l
415 fai-savelog -r
416 }
417 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
418 task_faiend() {
419
420 wait_for_jobs
421 echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
422 # reboot without prompting if FAI_FLAG reboot is set
423 [ -z $reboot ] && read
424 echo "Rebooting $HOSTNAME now"
425 cd /
426 sync
427 umount -ar
428 exec reboot -dfi
429 }
430 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
431 task_backup() {
432
433 die "Task backup not yet used. But you can use the hook backup."
434 # NOT YET USED. IT'S ONLY AN IDEA
435
436 # FAI_BACKUP_LIST contains backup options
437 # proposed format: "hda1 hda3 hda12 sda2 '0 0 1 1'"
438 # list of devices list of backup levels
439 # this subroutine is not yet tested, not yet used
440 # It's only an idea how a backup could be made
441 # local partition
442 # fai-mount-disk
443 # for partition in $SAVE_DEVICES ; do
444 # tar cf - $FAI_ROOT/$partition | $FAI_REMOTESH -l $LOGUSER $SERVER dd if= of=$HOSTNAME.$partition.tar
445 # dump 0f $LOGUSER@$SERVER:backup/$HOSTNAME/$partition.dmp $FAI_ROOT/$partition
446 # done
447 }
448 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
449 task_install() {
450
451 > $stamp
452
453 save_dmesg
454 load_keymap_consolechars
455
456 task partition
457 task mountdisks
458 task extrbase
459 task mirror
460 task updatebase
461 task instsoft
462 task configure
463 task finish
464 date
465 echo -e "FAI finished.\a"
466 task chboot
467
468 rm -f $stamp
469 # save again, because new messages could be created
470 save_dmesg
471 task savelog
472
473 if [ -f $stamp ]; then
474 echo "Error while executing commands in subshell."
475 echo "$stamp was not removed."
476 die "Please look at the log files in $LOGDIR for errors."
477 fi
478 task faiend
479 }
480 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5