-
Notifications
You must be signed in to change notification settings - Fork 101
/
subroutines
executable file
·1235 lines (1084 loc) · 38.4 KB
/
subroutines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
#*********************************************************************
#
# subroutines -- useful subroutines for FAI
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2000-2017 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
# (c) 2001-2005 by Henning Glawe, glaweh@physik.fu-berlin.de
# Freie Universitaet Berlin
#
#*********************************************************************
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#*********************************************************************
# source this file, then you have these function available in the shell
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
die() {
# echo comment and exit installation
[ -f $LOGDIR/skip.savelog ] || call_hook savelog
[ -f $LOGDIR/skip.savelog ] || task_savelog
echo "$@"
if [ $do_init_tasks -eq 1 ] ; then
exec bash -i
else
exit 99
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
defnop() {
# define given list of subroutine names as dummy function;
# this will fake unknown commands
local name
for name in "$@";do
eval "$name () { :;}"
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: none
# Required-Var: $classes
# Short-Description: test if one or multiple classes are defined
### END SUBROUTINE INFO
ifclass() {
if [ $# -eq 1 ]; then
_ifclass $1
return $?
fi
case $1 in
-a) shift ; _ifclass_and $* ; return $? ;;
-o) shift ; _ifclass_or $* ; return $? ;;
esac
}
_ifclass() {
# check one single class
[ "${debug:-}" ] && echo "Test if class $1 is in ${classes:-}" >&2
# test if a class is defined
local ret
if [[ "${classes:-}" =~ (^|[[:space:]]+)$1($|[[:space:]]+) ]]; then
ret=0
else
ret=1
fi
[ "${debug:-}" ] && echo "ifclass returns $ret" >&2
return $ret
}
_ifclass_and() {
# check if all classes are defined (logical AND)
local cl
for cl in $*; do
ifclass $cl || return 1
done
return 0
}
_ifclass_or() {
# check if one of the classes is defined (logical OR)
local cl
for cl in $*; do
ifclass $cl && return 0
done
return 1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rwmount() {
# remount partition read/write, for interactive use only
mount -o rw,remount $1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
save_dmesg() {
[ $do_init_tasks -eq 0 ] && return
if [ -x /usr/sbin/logtail ] ; then
logtail -f /var/log/kern.log -o /var/lib/fai/kern.log.offset >> $LOGDIR/dmesg.log
else
dmesg > $LOGDIR/dmesg.log
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
umount_csspace() {
# umount config space if accessed via nfs
if [[ $FAI_CONFIG_SRC =~ ^nfs:// ]]; then
mountpoint -q $FAI && umount $FAI
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
jobsrunning() {
# test if jobs are running
ps r | egrep -qv "ps r|TIME COMMAND|rcS"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
umount_target() {
if [ "$target" != '/' ]; then
# do not umount during softupdate
umount $FAI_ROOT/proc $FAI_ROOT/sys $FAI_ROOT/dev/pts $FAI_ROOT/dev 2>/dev/null
for dir in $(mount | grep $target | egrep -v "media/mirror|tmpfs"| awk '{print $3}' | sort -r); do
mountpoint -q $dir && umount $dir
done
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cleanup_devicemapper() {
local vgroup v
# remove device mapper devices
grep -q 'Executing: vgcreate' $LOGDIR/format.log 2>/dev/null
if [ $? -eq 0 ]; then
# write which vg should be removed, PPID is the fai-diskimage process
> /var/run/fai/vgremove.$PPID
vgroup=$(awk '/Executing: vgcreate/ {print $3}' $LOGDIR/format.log)
for v in $vgroup; do
vgchange -an $v
echo vgremove -f $v >> /var/run/fai/vgremove.$PPID
done
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mk_zerofree_list() {
# make a list of devices for zerofree which have an ext2/3/4 file system
local source fstype mpoint v
if [ X$verbose = X1 ]; then
v=-v
fi
while read -r source fstype mpoint ; do
# skip non ext2/3/4 file systems
case $fstype in
ext*) : ;;
*) continue ;;
esac
if [[ $mpoint =~ $target ]]; then
echo "zerofree $v $source" >> /var/run/fai/zerofree.$PPID
fi
done < <(df --output=source,fstype,target)
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
wait_for_jobs() {
# can be an extern script
# wait for running (background) jobs to finish (e.g. update-auctex-elisp)
local i=0
while jobsrunning; do
[ $(($i % 3)) -eq 0 ] && echo "Waiting for background jobs to finish."
(( i += 1 ))
sleep 10
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
stop_fai_installation() {
# this subroutine should directly stop the installation process
sendmon "TASKEND $taskname $task_error"
echo "Error in task $taskname. Code: $task_error" >&2
echo "Traceback: $task_error_func" >&2
die "FATAL ERROR. Installation stopped."
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: $task_error
# Required-Var: $1 $2 $task_error
# Short-Description: save the maximum error code,
# Short-Description: $1 is the error that will be saved unless $2 is zero
### END SUBROUTINE INFO
task_error() {
[ X$2 = X0 ] && return
task_error_func=${FUNCNAME[*]}
task_local_error=$1
[ $1 -gt $task_error ] && task_error=$1
echo $1 > $LOGDIR/task_error
[ $task_error -gt $STOP_ON_ERROR ] && stop_fai_installation
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: $task_error
# Required-Var: $LOGDIR
# Short-Description: call a certain task
### END SUBROUTINE INFO
declare -A taskduration
export duration=0
task() {
# hooks are called before a task is called
# if a task is skipped, also its hooks are skipped
# a hook can set the flag, so the accociated task is skipped
local taskname=$1
shift
local task_local_error=0 # the error code set by every task
[ -f $LOGDIR/skip.$taskname ] || call_hook $taskname "$@"
if [ -f $LOGDIR/skip.$taskname ]; then
# skip task
rm $LOGDIR/skip.$taskname # TODO: remove skip files at the very end
[ X$verbose = X1 ] && echo "Skipping task_$taskname"
sendmon "TASKSKIP $taskname"
else
echo "Calling task_$taskname"
sendmon "TASKBEGIN $taskname"
task_error_func=''
taskduration[$taskname]=$SECONDS
task_$taskname "$@"
[ $duration -eq 1 ] && echo "Duration of task $taskname: " $(($SECONDS-${taskduration[$taskname]}))"s"
sendmon "TASKEND $taskname $task_local_error"
if [ "$task_local_error" -ne 0 ] ; then
echo "Exit code task_$taskname: $task_local_error" >&2
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var:
# Required-Var: $classes $debug
# Short-Description: call a hook, hook.sh can define additional variables
### END SUBROUTINE INFO
call_hook() {
local hook=$1
shift
local cl dflag hfile
[ "$debug" ] && dflag="-d"
for cl in $classes; do
hfile=$FAI/hooks/$hook.$cl
if [ -f $hfile -a ! -x $hfile ]; then
echo "WARNING: Skipping $hfile because it's not executable." >&2
continue
fi
if [ -f $hfile.sh -a ! -x $hfile.sh ]; then
echo "WARNING: Skipping $hfile.sh because it's not executable." >&2
continue
fi
if [ -f $hfile.source ]; then
echo "WARNING: The suffix .source is deprecated. Use .sh instead." >&2
# continue
fi
if [ -x $hfile ]; then
echo "Calling hook: $hook.$cl"
sendmon "HOOK $hook.$cl"
# execute the hook
$hfile $dflag "$@"
check_status $hook.$cl $?
fi
if [ -x $hfile.sh ]; then
echo "Source hook: $hook.$cl.sh"
sendmon "HOOK $hook.$cl.sh"
# source this hook
. $hfile.sh $dflag "$@"
check_status $hook.$cl.sh $?
fi
# deprecated
if [ -x $hfile.source ]; then
echo "Source hook: $hook.$cl.source"
sendmon "HOOK $hook.$cl.source"
# source this hook
. $hfile.source $dflag "$@"
check_status $hook.$cl.source $?
fi
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
skiptask() {
# mark all given tasks, so they will be skipped
local task
for task in "$@"; do
echo > $LOGDIR/skip.$task # create file with size != 0
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
define_fai_flags() {
local flag
# FAI_FLAGS are comma separated, define all flags
FAI_FLAGS=${FAI_FLAGS//,/ }
echo "FAI_FLAGS: $FAI_FLAGS"
for flag in $FAI_FLAGS; do
# define this flag as 1
eval "flag_$flag=1"
done
[ "$flag_verbose" ] && verbose=1 # for backward compability
[ "$flag_debug" ] && debug=1 # for backward compability
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: $fai_rundate
# Requires-Var: $DOMAIN $do_init_tasks
# Suggests-Var: $flag_createvt $flag_sshd
# Short-Description: <task desc.>
### END SUBROUTINE INFO
task_setup() {
# source user specific subroutines
[ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
define_fai_flags
# this may be moved to an external script
if [ $do_init_tasks -eq 1 ] ; then
# set the system time and date using rdate or/and ntpdate
[ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
[ "$NTPSRVS_1" ] && ntpdate -b $NTPSRVS
[ "$flag_createvt" ] && {
# create two virtual terminals; acces via alt-F2 and alt-F3
echo "Press ctrl-c to interrupt FAI and to get a shell"
openvt -c2 /bin/bash ; openvt -c3 /bin/bash
echo "The log files are saved in /tmp/fai" > /dev/tty2
echo "The log files are saved in /tmp/fai" > /dev/tty3
trap 'echo "You can reboot with faireboot"; \
echo "The log files are saved in /tmp/fai"; \
cd /tmp/fai;bash' INT QUIT
}
# start secure shell daemon for remote access
if [ "$flag_sshd" -a -x /usr/sbin/sshd ]; then
pgrep -x sshd >/dev/null || /usr/sbin/sshd
fi
fi
unset flag_createvt flag_sshd
# now you have enough time to make changes to the config space
# only for debugging
if [ -n "$flag_wait" ]; then
echo "Sleeping. Now you may change the config space in $FAI."
echo "Continue after killall sleep."
sleep 50000
fi
# when did FAI start, using localtime
: ${fai_rundate:=$(date +'%Y%m%d_%H%M%S')}
if [ $do_init_tasks -eq 1 ]; then
echo "Starting FAI execution - $fai_rundate"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: none
# Requires-Var: $FAI_ACTION
# Short-Description: call task depending on $FAI_ACTION
### END SUBROUTINE INFO
task_action() {
if [ -z "$FAI_ACTION" ]; then
echo "No action in \$FAI_ACTION defined."
sendmon "TASKERROR action 21"
task_faiend
exit
fi
echo "FAI_ACTION: $FAI_ACTION"
case $FAI_ACTION in
install)
if [ $target = "/" ]; then
echo "Cowardly refusing to run a FAI installation on a running system."
return
fi
echo Performing FAI installation. All data may be overwritten!
if [ $do_init_tasks -eq 1 -a X$flag_menu = X ] ; then
echo -ne "\a"; sleep 1
echo -ne "\a"; sleep 1
echo -e "\a"; sleep 5
fi
task install
task faiend
;;
dirinstall)
task dirinstall
task faiend
;;
softupdate)
task softupdate
task faiend
;;
sysinfo)
task sysinfo
task_faiend
die Now you have a shell.
;;
inventory)
task inventory
task_faiend
die Now you have a shell.
;;
*)
if [ -f $FAI/hooks/$FAI_ACTION ]; then
echo "Calling user defined action: $FAI_ACTION"
$FAI/hooks/$FAI_ACTION
task_faiend
else
echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found." >&2
sendmon "TASKERROR action 22"
task_faiend
fi
;;
esac
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: $classes $cfclasses
# Requires-Var: $LOGDIR
# Suggests-Var: $renewclass
# Short-Description: define FAI classes and store them in several shell variables
### END SUBROUTINE INFO
task_defclass() {
if [ ! -d $FAI/class ]; then
sendmon "TASKERROR defclass 21"
echo "Subdirectory $FAI/class missing in config space. Following subdirectories are found:"
find $FAI -maxdepth 1 -type d -printf "%p\n"
die "Aborting."
fi
# new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
if [ $renewclass -eq 1 ]; then
# reevaluate new list of classes
fai-class -T $FAI/class $LOGDIR/FAI_CLASSES
classes=$(< $LOGDIR/FAI_CLASSES)
elif [ -n "$cmdlineclasses" ]; then
classes=$cmdlineclasses
elif [ ! -f /var/lib/fai/FAI_CLASSES ]; then
# use classes defined at installation time
die "Try to read classes from /var/lib/fai/FAI_CLASSES. Failed. Aborting."
else
classes=$(< /var/lib/fai/FAI_CLASSES)
fi
echo "List of all classes: " ${classes:-}
# define classes as: a.b.c.d for cfengine -D
# this doesn't work without echo
cfclasses=$(echo ${classes:-})
cfclasses=${cfclasses// /.}
[ "${debug:-}" ] && echo "cfclasses: $cfclasses"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
_defvar() {
local showvar=1 # TODO: new FAI_FLAG or always set when verbose is used
[ "$showvar" ] && set -x
. $1 </dev/null
[ "$showvar" ] && set +x
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_defvar() {
local svar=$LOGDIR/showvar.log
local odir=$(pwd)
cd $FAI/class
for class in ${classes:-} ; do
if [ -f $class.var -a -r $class.var ]; then
[ X$verbose = X1 ] && echo "Executing $class.var"
# show only lines with ++, we cannot use a pipe, since it would call
# _devfar in a subprocess. Then, variables are not defined
_defvar $class.var > $svar 2>&1
grep ^++ $svar| sed -e 's/\(.*PW\)=.*$/\1=XXXXXXXXXXXXX/'
rm $svar
fi
done
# /fai/class/* scripts or hooks can write variable definitions
# to additonal.var. now source these definitions
if [ -f $LOGDIR/additional.var -a -r $LOGDIR/additional.var ]; then
echo "Defining variables from additional.var"
_defvar $LOGDIR/additional.var > $svar 2>&1
grep ^++ $svar
rm $svar
fi
unset class svar
# now all variables are defined. Dump them to variables.log, so we can sources them if needed
set | perl -ne 'print if /^\w\w+=/ and not /^(EUID|PPID|SHELLOPTS|UID|rootpw|ROOTPW|HOME|PWD|BASHOPTS)|\(/' > $LOGDIR/variables.log
# another approach is to use this. A slightly different format, but seems to be robust.
# declare -x > $LOGDIR/variables.log
cd $odir
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_mountdisks() {
[ ! -f $LOGDIR/fstab ] && die "No $LOGDIR/fstab created."
# mount swap space
if [ $do_init_tasks -eq 1 ]; then
local sd
for sd in $SWAPLIST; do
swapon -p1 $sd && [ X$verbose = X1 ] && echo "Enable swap device $sd"
done
fi
mount2dir $FAI_ROOT $LOGDIR/fstab
if [ "$?" -ne 0 ]; then
sendmon "TASKERROR mountdisks 885"
task_error 885
fi
if [ $do_init_tasks -eq 1 ] && ! mountpoint -q $target; then
echo "$target is not a mount point. Check your disk_config file."
sendmon "TASKERROR mountdisks 886"
task_error 886
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_configure() {
fai-do-scripts $FAI/scripts
task_error 420 $?
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_tests() {
if [ ! -d $FAI/tests ]; then
return
fi
fai-do-scripts $FAI/tests # always returns 0 atm
# check if any test failed
if [ -f $LOGDIR/test.log ]; then
if grep -q "FAILED with " $LOGDIR/test.log; then
sendmon "TASKERROR tests 312"
task_error 312
return 1
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_savelog() {
mkdir -p $FAI_ROOT/var/{lib,log}/fai
fai-savelog -l
[ -f $LOGDIR/FAI_CLASSES ] && cp -pu $LOGDIR/FAI_CLASSES $FAI_ROOT/var/lib/fai
[ -f $LOGDIR/disk_var.sh ] && cp -pu $LOGDIR/disk_var.sh $FAI_ROOT/var/lib/fai
fai-savelog -r
task_error 410 $?
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Description: Always called as last FAI task
### END SUBROUTINE INFO
task_faiend() {
local dir cdromdevice
wait_for_jobs
: ${flag_reboot:=0}
: ${flag_halt:=0}
# reboot/halt without prompting if FAI_FLAG reboot or halt is set
# wait for keypress if neither flag reboot nor halt is set
if [ $do_init_tasks -eq 1 -a "$flag_reboot" -eq 0 -a "$flag_halt" -eq 0 ]; then
echo "Press <RETURN> to reboot (do not remove your removable medium yet)."
read
fi
sendmon "TASKEND faiend 0"
cd /
mk_zerofree_list
umount_target
cleanup_devicemapper
if [ $do_init_tasks -eq 0 ]; then
echo "Log files are saved in $LOGDIR"
return 0
fi
killall -q sshd systemd-udevd rsyslogd
umount $FAI_ROOT/proc $FAI_ROOT/sys $FAI_ROOT/dev/pts 2>/dev/null
umount -arf 2>/dev/null
echo "Rebooting $HOSTNAME now"
sendmon "TASKEND reboot 0"
# reboot or halt?
if [ "$flag_halt" -gt "0" ]; then
exec halt -dfp
else
exec reboot -df
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_install() {
echo $$ > $stamp
save_dmesg
task partition
task mountdisks
task extrbase
task debconf
task repository
task updatebase
task instsoft
task configure
task tests
task finish
task chboot
rm -f $stamp
save_dmesg # save again, because new messages could be created
task savelog
if [ -f $stamp ]; then
echo "Error while executing commands in subshell." >&2
echo -n "$stamp was not removed. PID of running process: "
cat $stamp
sendmon "TASKERROR install 21"
die "Please look at the log files in $LOGDIR for errors." >&2
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_dirinstall() {
mkdir -p $FAI_ROOT
FAI_ROOT=$(cd $FAI_ROOT;pwd)
xstamp=${FAI_ROOT//\//=}
stamp=/var/run/fai/dirinstall-$xstamp
unset xstamp
clean_exit() {
rm -f $stamp
[ -z "$FAI_ROOT" ] && return
# sometimes mountpoint may be mounted twice, so try to umount them twice
mountpoint -q $FAI_ROOT/proc && umount $FAI_ROOT/proc
mountpoint -q $FAI_ROOT/sys && umount $FAI_ROOT/sys
mountpoint -q $FAI_ROOT/proc && umount $FAI_ROOT/proc
mountpoint -q $FAI_ROOT/sys && umount $FAI_ROOT/sys
mountpoint -q $FAI_ROOT/dev/pts && umount $FAI_ROOT/dev/pts
# sometimes umount $FAI_ROOT/dev fails, because a process is
# still running in the background and accesses /dev
# this occured sometimes when using dirinst and a long package
# list if dhelp.postinst is starting an index process in the
# bg which did not finished until the installation was finished.
mountpoint -q $FAI_ROOT/dev && umount $FAI_ROOT/dev
mkramdisk -au 2>/dev/null
}
trap 'clean_exit' INT QUIT EXIT
[ -f "$stamp" ] && {
echo -n "fai dirinstall into directory $FAI_ROOT already running or was aborted before. PID: "
cat $stamp
echo "You may remove $stamp and try again."
exit 1
}
echo $$ > $stamp
echo "Installing into directory $FAI_ROOT"
task extrbase
[ -f $target/etc/fstab ] || touch $target/etc/fstab
task debconf
task repository
task updatebase
task instsoft
task configure
task tests
task finish
clean_exit
rm -f $stamp
unset LOGUSER # so logfile are not saved to remote
task savelog
if [ -f $stamp ]; then
echo "Error while executing commands in subshell." >&2
echo -n "$stamp was not removed. PID of running process: "
cat $stamp
sendmon "TASKERROR install 21"
die "Please look at the log files in $LOGDIR for errors." >&2
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
task_softupdate() {
echo Performing FAI system update. All data may be overwritten!
stamp=/var/run/fai/fai_softupdate_is_running
[ -f "$stamp" ] && die "Lock file $stamp found. Another fai softupdate is already running. Aborting."
echo $$ > $stamp
trap "rm -f $stamp" INT QUIT EXIT
# the following copy operation is required to make $LOGDIR a reliable source
# for disk_var.sh
# use the last disk_var during update if available
[ -f /var/lib/fai/disk_var.sh ] && cp -p /var/lib/fai/disk_var.sh $LOGDIR
defnop wait_for_jobs
save_dmesg
task debconf
task repository
task updatebase
task instsoft
task configure
task tests
task finish
rm -f $stamp
# save again, because new messages could be created
save_dmesg
task savelog
umount_csspace
if [ -f $stamp ]; then
echo "Error while executing commands in subshell." >&2
echo -n "$stamp was not removed. PID of running process: "
cat $stamp
sendmon "TASKERROR softupdate 21"
die "Please look at the log files in $LOGDIR for errors." >&2
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
catnc() {
# cat but no comment lines
egrep -v "^#" $@
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: $disklist
# Requires-Var:
# Short-Description: create list of available disks
### END SUBROUTINE INFO
set_disk_info() {
# the variable holds a space separated list of devices
disklist=$(fai-disk-info | sort | tr '\n' ' ')
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
eval_cmdline() {
# parse kernel parameters and define variables
local word
echo -n "Kernel currently running: "
uname -rsmo
eval "cmdline=($(</proc/cmdline))"
echo "Kernel parameters: ${cmdline[*]}"
for word in "${cmdline[@]}"; do
if echo "$word" | egrep -q '^[a-zA-Z0-9_]+=' ; then
varname=${word%%=*}
word=${word#*=}
eval $varname=\$word
eval export $varname
fi
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
read_fai_monitor_vars() {
# read variables which are received from the fai-monitor
if [ ! -s /run/initramfs/fai-variables ]; then
return
fi
eval `grep FAI_CONFIG_SRC /run/initramfs/fai-variables`
eval `grep FAI_FLAGS /run/initramfs/fai-variables`
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: $faimond $sendid $SERVER $monserver
# Requires-Var: $LOGDIR $FAI
# Suggests-Var: $monserver $FAI_SENDID $FAI_MONITOR_PORT
# Short-Description: <task desc.>
### END SUBROUTINE INFO
task_confdir() {
if [ $do_init_tasks -eq 1 ] ; then
read_fai_monitor_vars
eval_cmdline
get-boot-info
echo "Reading $LOGDIR/boot.log"
. $LOGDIR/boot.log
unset T170 T171 T172 ROOT_PATH BOOTFILE
printk=${printk:-6}
echo $printk > /proc/sys/kernel/printk
create_resolv_conf
if [ -x /usr/sbin/rsyslogd ]; then
/usr/sbin/rsyslogd
fi
fi
define_fai_flags
if [ -z "$SERVER" ]; then
# since SERVER is not set (by DHCP) we extract it from FAI_CONFIG_SRC
# extract server name from FAI_CONFIG_SRC and delete user@ from string
SERVER=$(expr match "$FAI_CONFIG_SRC" '.*://\([^/]*\)/.*' | sed -e 's/.*@//')
echo "Setting SERVER=$SERVER. Value extracted from FAI_CONFIG_SRC."
fi
# check if monitor server is available
: ${monserver:=$SERVER}
: ${FAI_MONITOR_PORT:=4711}
if [ -z "$monserver" ]; then
echo "No monitor daemon defined."
faimond=0
else
faimond=1
if [ -n "$NIC1" ]; then
MAC=$(< /sys/class/net/$NIC1/address)
PXE="01-"${MAC//:/-}
fi
: ${FAI_SENDID:=host} # set default if undefined
case "$FAI_SENDID" in
host) sendid=$HOSTNAME ;;
mac) sendid=$MAC ;;
pxe) sendid=$PXE ;;
*) sendid=$FAI_SENDID ;;
esac
if sendmon check; then
echo "Monitoring to server $monserver enabled."
sendmon "TASKBEGIN confdir"
else
faimond=0
echo "Can't connect to monserver on $monserver port $FAI_MONITOR_PORT. Monitoring disabled."
fi
fi
local mdir=/lib/modules/`uname -r`
if [ $do_init_tasks -eq 1 ] && [ ! -d $mdir ]; then
echo "ERROR: Kernel modules directory $mdir not available. Only found:"
ls -d /lib/modules/*
echo ""
task_error 790
fi
get-config-dir || {
echo "Problems accessing the config space."
die ""
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: $BOOT_DEVICE $ROOT_PARTITION $BOOT_PARTITION $SWAPLIST
# Requires-Var: $LOGDIR $LOGDIR/disk_var.sh
# Short-Description: partition local hard disks
### END SUBROUTINE INFO
task_partition() {
if [ X$USE_SETUP_STORAGE = X1 ]; then
echo "WARNING: The variable USE_SETUP_STORAGE is not needed any more." >&2
echo "setup-storage is now the default tool for partitioning."
fi
[ ! -s $LOGDIR/disk_var.sh ] && setup-storage -X |& tee $LOGDIR/format.log
# if we've created device mapper devices, do cleanup on error
grep -q 'Executing: vgcreate' $LOGDIR/format.log 2>/dev/null
if [ $? -eq 0 ]; then
trap "umount_target;cleanup_devicemapper" QUIT EXIT
fi
# partitioning tool must create $LOGDIR/disk_var.sh file
if [ ! -s $LOGDIR/disk_var.sh ]; then
local conffile=$(grep "Using config file:" $LOGDIR/format.log | cut -d: -f2 )
if [ -n "$conffile" ]; then
echo ""
echo "This is your disk_config file:"
cat $conffile
fi
echo ""
task_error 710
sendmon "TASKERROR partition 21"
die "Partitioning tool did not create $LOGDIR/disk_var.sh file."
fi
# now define variable for root and boot partition and boot device
. $LOGDIR/disk_var.sh
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: none
# Requires-Var: $NFSROOT
# Suggests-Var:
# Short-Description: <task desc.>
### END SUBROUTINE INFO
call_debootstrap() {
local dversion=$(dpkg-query -Wf '${Version}\n' debootstrap)
echo "Creating base system using debootstrap version $dversion"
# Check if we need cross architecture debootstrap
targetarch=$(expr "$FAI_DEBOOTSTRAP_OPTS" : '.*--arch=\([^[:space:]]*\)' || true)
hostarch1=$(dpkg --print-architecture)
hostarch2=$(dpkg --print-foreign-architectures)
_debootstrap=qemu-debootstrap
if [ -z "$targetarch" ]; then
_debootstrap=debootstrap
else
if [ $targetarch = $hostarch1 -o $targetarch = $hostarch2 ]; then
_debootstrap=debootstrap
fi
fi
if [ $_debootstrap = "qemu-debootstrap" ]; then
if ! type -P $_debootstrap >/dev/null; then
die 1 "qemu-debootstrap not found. Please install the package qemu-user-static."
fi
fi
echo "Calling $_debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $FAI_ROOT $2"
LC_ALL=C $_debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $FAI_ROOT $2
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### BEGIN SUBROUTINE INFO
# Provides-Var: none
# Requires-Var: $FAI_ROOT $do_init_tasks $NFSROOT $LOGDIR
# Suggests-Var: $FAI_DEBOOTSTRAP
# Short-Description: extrace minimal base file into target
### END SUBROUTINE INFO
task_extrbase() {
local fs=$FAI_ROOT/etc/fstab
fetch-basefile
# remember, ftar extracts into $FAI_ROOT by default, so / means $FAI_ROOT/
# copy the base file class based if it exists
[ -d $FAI/basefiles ] && ftar -1v -s $FAI/basefiles /
if [ $? -ne 0 ]; then
if [ $do_init_tasks -eq 1 ]; then
ftar -1v -c base -s /var/tmp /
else
[ -d $NFSROOT/var/tmp ] &&
ftar -1v -c base -s $NFSROOT/var/tmp /
fi
# if no base file was extracted, call debootstrap
if [ ! -d $FAI_ROOT/etc ]; then
echo "No base file found. Calling debootstrap."
[ -z "$FAI_DEBOOTSTRAP" ] && die "\$FAI_DEBOOTSTRAP undefined. Aborting"
call_debootstrap $FAI_DEBOOTSTRAP
task_error 801 $?
fi