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

Contents of /trunk/share/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2408 - (show annotations) (download)
Tue Aug 3 23:08:16 2004 UTC (8 years, 9 months ago) by lange
File size: 11363 byte(s)
do not dump readonly variables to variables.sh
1 #! /bin/bash
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-2003 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 ifclass() {
52
53 [ "$debug" ] && echo "Test if class $1 is in $classes"
54 # test if a class is defined
55 local cl
56 local ret=1
57
58 for cl in $classes; do
59 [ x$cl = x$1 ] && ret=0 && break
60 done
61 [ "$debug" ] && echo "ifclass returns $ret"
62 return $ret
63 }
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 # can be an extern script
79 # wait for running (background) jobs to finish (e.g. update-auctex-elisp)
80 local i=0
81 while jobsrunning; do
82 [ $(($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 sndmon "TASKSKIP $taskname"
103 else
104 echo "Calling task_$taskname"
105 sndmon "TASKBEGIN $taskname"
106 terror=0 # task can set this variable to indicate an error
107 task_$taskname
108 sndmon "TASKEND $taskname $terror"
109 fi
110 # since the subroutine is not needed any more, we can undefine it
111 unset task_$taskname
112 }
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 hfile=$FAI/hooks/$hook.$cl
122 if [ -x $hfile ]; then
123 echo "Calling hook: $hook.$cl"
124 sndmon "HOOK $hook.$cl"
125 # execute the hook
126 $hfile $dflag
127 check_status $hook.$cl $?
128 fi
129 if [ -x $hfile.source ]; then
130 echo "Source hook: $hook.$cl.source"
131 sndmon "HOOK $hook.$cl.source"
132 # source this hook
133 . $hfile.source $dflag
134 check_status $hook.$cl.source $?
135 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 echo "Configuration space $FAI mounted from $FAI_LOCATION"
159 fi
160 if [ ! -d $FAI/class ]; then
161 echo "WARNING: directory $FAI/class not found."
162 fi
163 }
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 if [ -d "$FAI_CONFIG_AREA" -a -z "$DONTCOPYCVS" ] ; then
181 echo "Config found at $FAI_CONFIG_AREA: Copying"
182 cp -a $FAI_CONFIG_AREA $FAI
183 echo "Updating CVS"
184 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 }
192 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
193 define_fai_flags() {
194
195 local flag
196 # FAI_FLAGS are comma separated, define all flags
197 FAI_FLAGS=${FAI_FLAGS//,/ }
198 [ "$verbose" ] && echo "FAI_FLAGS: $FAI_FLAGS"
199 for flag in $FAI_FLAGS; do
200 # define this flag as 1
201 eval "$flag=1"
202 done
203 }
204 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
205 task_setup() {
206
207 # source user specific subroutines
208 [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
209 # set the system time and date using rdate or/and ntpdate
210 [ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
211 [ "$NTPSRVS_1" ] && ntpdate -b -v $NTPSRVS
212
213 define_fai_flags
214 DNSDOMAIN=$DOMAIN # cfengine 1.5.3 can't use $DOMAIN
215
216 [ "$createvt" ] && {
217 # create two virtual terminals; acces via alt-F2 and alt-F3
218 echo "Press ctrl-c to interrupt FAI and to get a shell"
219 openvt -c2 /bin/bash ; openvt -c3 /bin/bash
220 trap 'echo "You can reboot with faireboot";bash' INT QUIT
221 }
222
223 # start secure shell daemon for remote access
224 [ "$sshd" -a -x /usr/sbin/sshd ] && /usr/sbin/sshd
225
226 # when did FAI start, using localtime
227 FAI_RUNDATE=$(date +'%Y%m%d_%H%M%S')
228 }
229 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
230 task_action() {
231
232 if [ -z "$FAI_ACTION" ]; then
233 echo "No action in \$FAI_ACTION defined."
234 sndmon "TASKERROR action 21"
235 task_faiend
236 exit
237 fi
238 echo "FAI_ACTION: $FAI_ACTION"
239 case $FAI_ACTION in
240
241 install)
242 echo Performing FAI installation. All data may be overwritten!
243 echo -ne "\a"; sleep 1
244 echo -ne "\a"; sleep 1
245 echo -e "\a"; sleep 5
246 task install
247 task faiend
248 ;;
249 sysinfo)
250 echo Showing system information.
251 task sysinfo
252 task_faiend
253 die Now you have a shell.
254 ;;
255 *)
256 if [ -f $FAI/hooks/$FAI_ACTION ]; then
257 echo "Calling user defined action: $FAI_ACTION"
258 $FAI/hooks/$FAI_ACTION
259 else
260 echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found."
261 sndmon "TASKERROR action 22"
262 task_faiend
263 fi
264 ;;
265 esac
266 }
267 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
268 task_defclass() {
269
270 if [ ! -d $FAI/class ]; then
271 sndmon "TASKERROR defclass 21"
272 echo "Directory $FAI/class not found. Following subdirectories are found:"
273 find $FAI -type d -maxdepth 1 -printf "%p\n"
274 die "Aborting."
275 fi
276
277 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
278 fai-class $FAI/class $LOGDIR/FAI_CLASSES
279 classes=$(< $LOGDIR/FAI_CLASSES)
280
281 # define classes as: a.b.c.d for cfengine -D
282 # this doesn't work without echo
283 cfclasses=$(echo $classes)
284 cfclasses=${cfclasses// /.}
285 [ "$debug" ] && echo "cfclasses: $cfclasses"
286 }
287 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
288 task_defvar() {
289
290 cd $FAI/class
291 for class in $classes ; do
292 if [ -f $class.var ]; then
293 [ "$verbose" ] && echo "Executing $class.var"
294 [ "$debug" ] && set -vx
295 . $class.var </dev/null
296 [ "$debug" ] && set +vx
297 fi
298 done
299
300 # /fai/class/S* scripts or hooks can write variable definitions
301 # to additonal.var. now source these definitions
302 [ "$debug" ] && set -vx
303 [ -f $LOGDIR/additional.var ] && . $LOGDIR/additional.var
304 [ "$debug" ] && set +vx
305 unset class
306 # now all variables are defined. Dump them to variables.sh
307 set | perl -ne 'print if /^\w\w+=/' | egrep -v "^BASH_VERSINFO|^EUID|^PPID|^SHELLOPTS|^UID" > $LOGDIR/variables.sh
308 }
309 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
310 task_mountdisks() {
311
312 [ ! -f $LOGDIR/$fstab ] && die "No $LOGDIR/$fstab created."
313 # mount swap space
314 local sd
315 for sd in $SWAPLIST; do
316 swapon $sd && [ "$verbose" ] && echo "Enable swap device $sd"
317 done
318 mount2dir $FAI_ROOT $LOGDIR/$fstab
319 }
320 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
321 task_configure() {
322
323 fai-do-scripts $FAI/scripts
324 }
325 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
326 task_savelog() {
327
328 [ -d $target/etc/fai ] || mkdir -p $target/etc/fai
329 cd $LOGDIR && cp -p FAI_CLASSES variables.sh $diskvar $target/etc/fai
330 fai-savelog -l
331 fai-savelog -r
332 }
333 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
334 task_faiend() {
335
336 wait_for_jobs
337 echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
338 # reboot without prompting if FAI_FLAG reboot is set
339 [ -z $reboot ] && read
340 echo "Rebooting $HOSTNAME now"
341 sndmon REBOOT
342 cd /
343 sync
344
345 case $(uname -s) in
346 Linux)
347 umount -ar
348 exec reboot -dfi
349 ;;
350 esac
351 }
352 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
353 task_backup() {
354
355 die "Task backup not yet used. But you can use the hook backup."
356 }
357 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
358 task_install() {
359
360 > $stamp
361
362 save_dmesg
363
364 task partition
365 task mountdisks
366 task extrbase
367 task mirror
368 task updatebase
369 task instsoft
370 task configure
371 task finish
372 date
373 echo "The installation took $(cut -d . -f 1 /proc/uptime) seconds."
374 task chboot
375
376 rm -f $stamp
377 # save again, because new messages could be created
378 save_dmesg
379 task savelog
380
381 if [ -f $stamp ]; then
382 echo "Error while executing commands in subshell."
383 echo "$stamp was not removed."
384 sndmon "TASKERROR install 21"
385 die "Please look at the log files in $LOGDIR for errors."
386 fi
387 }
388 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
389 catnc() {
390 # cat but no comment lines
391 egrep -v "^#" $@
392 }
393 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5