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

Contents of /trunk/lib/subroutines

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1981 - (show annotations) (download)
Thu Jul 24 13:05:50 2003 UTC (9 years, 10 months ago) by lange
Original Path: trunk/share/subroutines
File size: 11073 byte(s)
copy some files to $target/etc/fai
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-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 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 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 # can be an extern script
90 # 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 # since the subroutine is not needed any more, we can undefine it
118 unset task_$taskname
119 }
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 hfile=$FAI/hooks/$hook.$cl
129 if [ -x $hfile ]; then
130 echo "Calling hook: $hook.$cl"
131 # execute the hook
132 $hfile $dflag
133 check_status $hook.$cl $?
134 fi
135 if [ -x $hfile.source ]; then
136 echo "Source hook: $hook.$cl.source"
137 # source this hook
138 . $hfile.source $dflag
139 check_status $hook.$cl.source $?
140 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 echo "Configuration space $FAI mounted from $FAI_LOCATION"
164 fi
165 if [ ! -d $FAI/class ]; then
166 die "$FAI/class not found. Aborting."
167 fi
168 }
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 if [ -d "$FAI_CONFIG_AREA" -a -z "$DONTCOPYCVS" ] ; then
186 echo "Config found at $FAI_CONFIG_AREA: Copying"
187 cp -a $FAI_CONFIG_AREA $FAI
188 echo "Updating CVS"
189 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 local flag
201 # FAI_FLAGS are comma separated, define all flags
202 FAI_FLAGS=${FAI_FLAGS//,/ }
203 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 # source user specific subroutines
213 [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
214 # 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 FAI_RUNDATE=$(date +'%Y%m%d_%H%M%S')
233
234 cat >> $rcsfaivar <<-EOM
235 FAI_RUNDATE=$FAI_RUNDATE
236 LOGDIR=$LOGDIR
237 EOM
238 }
239 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
240 task_action() {
241
242 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 echo "FAI_ACTION: $FAI_ACTION"
248 echo "FAI_ACTION=$FAI_ACTION" >> $rcsfaivar
249 case $FAI_ACTION in
250
251 install)
252 echo Performing FAI installation. All data may be overwritten!
253 echo -ne "\a"; sleep 1
254 echo -ne "\a"; sleep 1
255 echo -e "\a"; sleep 5
256 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 local class
278
279 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
280 fai-class $FAI/class $LOGDIR/FAI_CLASSES
281 classes=$(< $LOGDIR/FAI_CLASSES)
282
283 # also define all classes in reverse order ($revclasses)
284 # for class in $classes ; do
285 # revclasses="$class $revclasses"
286 # done
287
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 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 }
316 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
317 task_mountdisks() {
318
319 [ ! -f $LOGDIR/$fstab ] && die "No $LOGDIR/$fstab created."
320 if islinux; then
321 # mount swap space
322 local sd
323 for sd in $SWAPLIST; do
324 swapon $sd && [ "$verbose" ] && echo "Enable swap device $sd"
325 done
326 fi
327 mount2dir $FAI_ROOT $LOGDIR/$fstab
328 }
329 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
330 task_configure() {
331
332 fai-do-scripts $FAI/scripts
333 }
334 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
335 task_savelog() {
336
337 fai-savelog -l
338 fai-savelog -r
339 [ -d $target/etc/fai ] || mkdir -p $target/etc/fai
340 cd $LOGDIR && cp -p FAI_CLASSES variables.sh $diskvar $target/etc/fai
341 }
342 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
343 task_faiend() {
344
345 wait_for_jobs
346 echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
347 # reboot without prompting if FAI_FLAG reboot is set
348 [ -z $reboot ] && read
349 echo "Rebooting $HOSTNAME now"
350 cd /
351 sync
352 umount -ar
353 exec reboot -dfi
354 }
355 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
356 task_backup() {
357
358 die "Task backup not yet used. But you can use the hook backup."
359 }
360 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
361 task_install() {
362
363 > $stamp
364
365 save_dmesg
366
367 task partition
368 task mountdisks
369 task extrbase
370 task mirror
371 task updatebase
372 task instsoft
373 task configure
374 task finish
375 date
376 echo -e "FAI finished.\a"
377 task chboot
378
379 rm -f $stamp
380 # save again, because new messages could be created
381 save_dmesg
382 task savelog
383
384 if [ -f $stamp ]; then
385 echo "Error while executing commands in subshell."
386 echo "$stamp was not removed."
387 die "Please look at the log files in $LOGDIR for errors."
388 fi
389 task faiend
390 }
391 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5