Debian Codegraph @

lib/subroutines

raw
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # subroutines -- useful subroutines for FAI
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2000-2026 by Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 # (c) 2001-2005 by Henning Glawe, [email protected]
11 # Freie Universitaet Berlin
12 #
13 #*********************************************************************
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
23 #
24 # A copy of the GNU General Public License is available as
25 # `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
26 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
27 # can also obtain it by writing to the Free Software Foundation, Inc.,
28 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29 #*********************************************************************
30
31 # source this file, then you have these function available in the shell
32
33 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34 die() {
35
36 # echo comment and exit installation
37 [ -f $LOGDIR/skip.savelog ] || call_hook savelog
38 [ -f $LOGDIR/skip.savelog ] || task_savelog
39 echo "$*"
40 if inside_nfsroot; then
41 exec bash -i
42 else
43 /usr/lib/fai/fai-abort
44 fi
45 }
46 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47 defnop() {
48
49 # define given list of subroutine names as dummy function;
50 # this will fake unknown commands
51
52 local name
53 for name in "$@";do
54 eval "$name () { :;}"
55 done
56 }
57 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58 mount-fai-data() {
59
60 # if we have a partition MY-DATA mount it
61 if [ -e /dev/disk/by-label/MY-DATA ]; then
62 mkdir -p /media/data
63 mount /dev/disk/by-label/MY-DATA /media/data &&
64 echo "Partition MY-DATA found and mounted"
65 fi
66 }
67 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68 install_pkgs() {
69
70 # install all packages found in the subdirecories pkgs/<CLASSNAME>
71
72 local c pkgs dirs pdir ttdir
73 local debdir=$1
74
75 if [ ! -d $debdir ]; then
76 return
77 fi
78
79 pdir=/tmp/pkgs
80 ttdir=${target}$pdir
81 mkdir $ttdir
82 mount --bind $debdir $ttdir
83
84 for c in $classes; do
85 if [ -d $ttdir/$c ]; then
86 dirs+=" $c"
87 fi
88 done
89
90 if [ -z "$dirs" ]; then
91 umount $ttdir
92 return
93 fi
94
95 pkgs=$(cd $ttdir;find $dirs -name \*.deb)
96 if [ -n "$pkgs" ]; then
97 echo "Installing .deb files from $debdir"
98 $ROOTCMD bash -c "cd $pdir; dpkg -R -i $dirs"
99 fi
100
101 pkgs=$(cd $ttdir;find $dirs -name \*.rpm)
102 if [ -n "$pkgs" ]; then
103 echo "Installing .rpm files from $debdir"
104 $ROOTCMD bash -c "cd $pdir; rpm -i $pkgs"
105 fi
106 umount $ttdir
107 }
108 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109 rwmount() {
110
111 # remount partition read/write, for interactive use only
112 mount -o rw,remount $1
113 }
114 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115 rw_target() {
116
117 # make $target and mounted subdirectories read-write
118 local dir
119 for dir in $(mount | grep "on $target" | grep -E -v "media/mirror|tmpfs"| awk '{print $3}'); do
120 rwmount $dir
121 done
122 }
123 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124 ctam() {
125
126 # chroot /target mount
127 # mount pseudo filesystems into /target
128 if [ -z "$target" -o "$target" = "/" ]; then
129 echo "ERROR: \$target is not set (correctly)."
130 return
131 fi
132 if [ -f $target/proc/cpuinfo ] ; then
133 echo "Filesystems already mounted to $target."
134 return
135 fi
136
137 mount -t proc proc $target/proc
138 mount -t sysfs sysfs $target/sys
139 mount --bind /dev $target/dev
140 mount --make-private $target/dev
141 mount -t devpts devpts $target/dev/pts
142 mount --make-private $target/dev/pts
143
144 if [ "$FAI_ACTION" = "softupdate" -o "$FAI_ACTION" = "dirinstall" ]; then
145 : # do not mount efivars for these actions
146 else
147 if [ -d /sys/firmware/efi ]; then
148 mount --bind /sys/firmware/efi/efivars $target/sys/firmware/efi/efivars
149 fi
150 fi
151 }
152 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153 ctau() {
154
155 # chroot /target umount
156 # umount pseudo filesystems from /target
157 if [ -z "$target" -o "$target" = "/" ]; then
158 echo "ERROR: \$target is not set (correctly)."
159 return
160 fi
161 if [ ! -f $target/proc/cpuinfo ] ; then
162 echo "Filesystems already umounted from $target."
163 return
164 fi
165
166 for f in /dev/pts /dev /proc /sys/firmware/efi/efivars /sys; do
167 mountpoint -q ${target}$f && umount ${target}$f
168 done
169 }
170 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171 ctai() {
172
173 # chroot into target
174 if [ -z "$target" -o "$target" = "/" ]; then
175 echo "ERROR: \$target is not set (correctly)."
176 return
177 fi
178 if [ ! -f $target/bin/bash ]; then
179 echo "ERROR: Cannot chroot into $target."
180 return
181 fi
182 chroot $target bash
183 }
184 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
185 save_dmesg() {
186
187 inside_nfsroot || return
188 if [ -x /usr/sbin/logtail ] ; then
189 logtail -f /var/log/kern.log -o /var/lib/fai/kern.log.offset >> $LOGDIR/dmesg.log
190 else
191 dmesg > $LOGDIR/dmesg.log
192 fi
193 }
194 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
195 umount_csspace() {
196
197 # umount config space if accessed via nfs
198 if [[ $FAI_CONFIG_SRC =~ ^nfs:// ]]; then
199 mountpoint -q $FAI && umount $FAI
200 fi
201 if [[ $FAI_CONFIG_SRC =~ ^https?:// ]]; then
202 umount /var/lib/fai
203 fi
204 }
205 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206 umount_target() {
207
208 if [ "$target" != '/' ]; then
209 # do not umount during softupdate
210 umount $target/proc $target/sys/firmware/efi/efivars $target/sys $target/dev/pts $target/dev 2>/dev/null
211 for dir in $(mount | grep $target | grep -E -v "media/mirror"| awk '{print $3}' | sort -r); do
212 if [ "$dir" = "$target" ]; then
213 # do not umount an empty target directory
214 [ -z "$(ls -A "$target")" ] && return
215 fi
216 mountpoint -q $dir && umount $dir
217 done
218 fi
219 }
220 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
221 cleanup_devicemapper() {
222
223 local vgroup v
224 # remove device mapper devices
225 grep -qs 'Executing: vgcreate' $LOGDIR/format.log
226 if [ $? -eq 0 ]; then
227
228 # write which vg should be removed, PPID is the fai-diskimage process
229 > /var/run/fai/vgremove.$PPID
230 vgroup=$(awk '/Executing: vgcreate/ {print $3}' $LOGDIR/format.log)
231 for v in $vgroup; do
232 echo blkdeactivate /dev/$v/* >> /var/run/fai/vgremove.$PPID
233 done
234 fi
235 }
236 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
237 mk_zerofree_list() {
238
239 # make a list of devices for zerofree which have an ext2/3/4 file system
240 local source fstype mpoint v
241
242 if [ X$verbose = X1 ]; then
243 v=-v
244 fi
245 while read -r source fstype mpoint ; do
246
247 # skip non ext2/3/4 file systems
248 case $fstype in
249 ext*) : ;;
250 *) continue ;;
251 esac
252
253 if [[ $mpoint =~ $target ]]; then
254 echo "zerofree $v $source" >> /var/run/fai/zerofree.$PPID
255 fi
256
257 done < <(df --output=source,fstype,target)
258 }
259 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
260 stop_fai_installation() {
261
262 # this subroutine should directly stop the installation process
263 sendmon "TASKEND $taskname $task_error"
264 echo "Error in task $taskname. Code: $task_error" >&2
265 echo "Traceback: $task_error_func" >&2
266 die "FATAL ERROR. Installation stopped."
267 }
268 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
269 ### BEGIN SUBROUTINE INFO
270 # Provides-Var: $task_error
271 # Required-Var: $1 $2 $task_error
272 # Short-Description: save the maximum error code,
273 # Short-Description: $1 is the error that will be saved unless $2 is zero
274 # Short-Description: if $3 is set to 1 only $task_error is set but the
275 # Short-Description: installation is not stopped
276 ### END SUBROUTINE INFO
277
278 task_error() {
279
280 local current
281 [ X$2 = X0 ] && return
282 task_error_func=${FUNCNAME[*]}
283
284 # save max error for current task
285 task_local_error=$1
286 current=$(< $LOGDIR/task_local_error)
287 if [ $task_local_error -gt $current ]; then
288 echo $task_local_error > $LOGDIR/task_local_error
289 fi
290
291 # save global max task error
292 task_error=$(< $LOGDIR/task_error)
293 if [ $task_local_error -gt $task_error ]; then
294 task_error=$task_local_error
295 echo $task_local_error > $LOGDIR/task_error
296 fi
297 if [ X$3 = X1 ]; then
298 :
299 else
300 [ $task_error -gt $STOP_ON_ERROR ] && stop_fai_installation
301 fi
302 }
303 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
304 ### BEGIN SUBROUTINE INFO
305 # Provides-Var: $task_error
306 # Required-Var: $LOGDIR
307 # Short-Description: call a certain task
308 ### END SUBROUTINE INFO
309
310 declare -A taskduration
311 export duration=0
312 task() {
313
314 # hooks are called before a task is called
315 # if a task is skipped, also its hooks are skipped
316 # a hook can set the flag, so the accociated task is skipped
317
318 local taskname=$1
319 shift
320 local task_local_error=0 # the error code set by every task
321
322 echo 0 > $LOGDIR/task_local_error
323 [ -f $LOGDIR/skip.$taskname ] || call_hook $taskname "$@"
324
325 if [ -f $LOGDIR/skip.$taskname ]; then
326 # skip task
327 rm $LOGDIR/skip.$taskname # TODO: remove skip files at the very end
328 [ X$verbose = X1 ] && echo "Skipping task_$taskname"
329 sendmon "TASKSKIP $taskname"
330 else
331 echo "Calling task_$taskname"
332 sendmon "TASKBEGIN $taskname"
333 task_error_func=''
334 taskduration[$taskname]=$SECONDS
335 task_$taskname "$@"
336 [ $duration -eq 1 ] && echo "Duration of task $taskname: " $((SECONDS-${taskduration[$taskname]}))"s"
337 task_local_error=$(< $LOGDIR/task_local_error)
338 sendmon "TASKEND $taskname $task_local_error"
339 if [ "$task_local_error" -ne 0 ] ; then
340 echo "Exit code task_$taskname: $task_local_error" >&2
341 fi
342 fi
343 }
344 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
345 ### BEGIN SUBROUTINE INFO
346 # Provides-Var:
347 # Required-Var: $classes $debug
348 # Short-Description: call a hook, hook.sh can define additional variables
349 ### END SUBROUTINE INFO
350
351 call_hook() {
352
353 local hook=$1
354 shift
355
356 local cl dflag hfile
357 [ "$debug" ] && dflag="-d"
358
359 for cl in $classes; do
360 hfile=$FAI/hooks/$hook.$cl
361 if [ -f $hfile -a ! -x $hfile ]; then
362 echo "WARNING: Skipping $hfile because it's not executable." >&2
363 continue
364 fi
365 if [ -f $hfile.sh -a ! -x $hfile.sh ]; then
366 echo "WARNING: Skipping $hfile.sh because it's not executable." >&2
367 continue
368 fi
369 if [ -f $hfile.source ]; then
370 echo "ERROR: The suffix .source is deprecated. Use .sh instead." >&2
371 # continue
372 fi
373 if [ -x $hfile.sh ]; then
374 echo "Source hook: $hook.$cl.sh"
375 sendmon "HOOK $hook.$cl.sh"
376 # source this hook
377 . $hfile.sh $dflag "$@"
378 check_status $hook.$cl.sh $?
379 fi
380 if [ -x $hfile ]; then
381 echo "Calling hook: $hook.$cl"
382 sendmon "HOOK $hook.$cl"
383 # execute the hook
384 $hfile $dflag "$@"
385 check_status $hook.$cl $?
386 fi
387 done
388 }
389 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
390 skiptask() {
391
392 # mark all given tasks, so they will be skipped
393 local task
394
395 for task in "$@"; do
396 echo > $LOGDIR/skip.$task # create file with size != 0
397 done
398 }
399 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
400 define_fai_flags() {
401
402 local flag
403 # FAI_FLAGS are comma separated, define all flags
404 FAI_FLAGS=${FAI_FLAGS//,/ }
405 echo "FAI_FLAGS: $FAI_FLAGS"
406 for flag in $FAI_FLAGS; do
407 # define this flag as 1
408 eval "flag_$flag=1"
409 done
410 [ "$flag_verbose" ] && verbose=1 # for backward compability
411 [ "$flag_debug" ] && debug=1 # for backward compability
412 }
413 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
414 ### BEGIN SUBROUTINE INFO
415 # Provides-Var: $fai_rundate
416 # Requires-Var:
417 # Suggests-Var: $flag_createvt $flag_sshd
418 # Short-Description: <task desc.>
419 ### END SUBROUTINE INFO
420
421 task_setup() {
422
423 # source user specific subroutines
424 [ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
425
426 define_fai_flags
427
428 # this may be moved to an external script
429 if inside_nfsroot; then
430 # set the system time and date using rdate or/and ntpdate
431 [ "$TIMESRVS_1" ] && [ -f /bin/rdate ] && rdate $TIMESRVS_1
432 [ -n "$NTPSRVS" ] && ntpdate -b $NTPSRVS
433 # Save the updated time to hardware, so when udevadm trigger
434 # triggers a re-read of the time from rtc, it isn't reset
435 hwclock --systohc
436 [ "$flag_createvt" ] && {
437 # create two virtual terminals; acces via alt-F2 and alt-F3
438 echo "Press ctrl-c to interrupt FAI and to get a shell"
439 openvt -c2 /bin/bash ; openvt -c3 /bin/bash
440 echo "The log files are saved in /tmp/fai" > /dev/tty2
441 echo "The log files are saved in /tmp/fai" > /dev/tty3
442 trap 'echo "You can reboot with faireboot"; \
443 echo "The log files are saved in /tmp/fai"; \
444 cd /tmp/fai;bash' INT QUIT
445 }
446
447 systemctl start dbus
448 # start secure shell daemon for remote access
449 if [ "$flag_sshd" -a -x /usr/sbin/sshd ]; then
450 pgrep -x sshd >/dev/null || /usr/sbin/sshd
451 fi
452
453 udevadm trigger
454 fi
455 unset flag_createvt flag_sshd
456
457 # now you have enough time to make changes to the config space
458 # only for debugging
459 if [ -n "$flag_wait" ]; then
460 echo "Sleeping. Now you may change the config space in $FAI."
461 echo "Continue after killall sleep."
462 sleep 50000
463 fi
464
465 # when did FAI start, using localtime
466 : ${fai_rundate:=$(date +'%Y%m%d_%H%M%S')}
467 if inside_nfsroot; then
468 echo "Starting FAI execution - $fai_rundate"
469 fi
470 }
471 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
472 ### BEGIN SUBROUTINE INFO
473 # Provides-Var: none
474 # Requires-Var: $FAI_ACTION
475 # Short-Description: call task depending on $FAI_ACTION
476 ### END SUBROUTINE INFO
477
478 task_action() {
479
480 if [ -z "$FAI_ACTION" ]; then
481 echo "No action in \$FAI_ACTION defined."
482 sendmon "TASKERROR action 21"
483 task_faiend
484 exit
485 fi
486
487 echo "FAI_ACTION: $FAI_ACTION"
488 case $FAI_ACTION in
489 install)
490 if [ $target = "/" ]; then
491 echo "Cowardly refusing to run a FAI installation on a running system."
492 return
493 fi
494 echo Performing FAI installation. All data may be overwritten!
495 if inside_nfsroot && [ X$flag_menu = X ] ; then
496 echo -ne "\a"; sleep 1
497 echo -ne "\a"; sleep 1
498 echo -e "\a"; sleep 5
499 fi
500 task install
501 task faiend
502 ;;
503 dirinstall)
504 task dirinstall
505 task faiend
506 ;;
507 softupdate)
508 task softupdate
509 task faiend
510 ;;
511 sysinfo)
512 task sysinfo
513 task_faiend
514 die Now you have a shell.
515 ;;
516 inventory)
517 task inventory
518 task_faiend
519 die Now you have a shell.
520 ;;
521 *)
522 if [ -f $FAI/hooks/$FAI_ACTION ]; then
523 echo "Calling user defined action: $FAI_ACTION"
524 $FAI/hooks/$FAI_ACTION
525 task_faiend
526 else
527 echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found." >&2
528 sendmon "TASKERROR action 22"
529 task_faiend
530 fi
531 ;;
532 esac
533 }
534 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
535 ### BEGIN SUBROUTINE INFO
536 # Provides-Var: $classes $cfclasses
537 # Requires-Var: $LOGDIR
538 # Suggests-Var: $renewclass
539 # Short-Description: define FAI classes and store them in several shell variables
540 ### END SUBROUTINE INFO
541
542 task_defclass() {
543
544 if [ ! -d $FAI/class ]; then
545 sendmon "TASKERROR defclass 21"
546 echo "Subdirectory $FAI/class missing in config space. Following subdirectories are found:"
547 find $FAI -maxdepth 1 -type d -printf "%p\n"
548 die "Aborting."
549 fi
550
551 # new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
552 if [ $renewclass -eq 1 ]; then
553 # reevaluate new list of classes
554 fai-class -T $FAI/class $LOGDIR/FAI_CLASSES
555 # check task error again
556 local lerr=$(< $LOGDIR/task_local_error)
557 task_error $lerr
558
559 classes=$(< $LOGDIR/FAI_CLASSES)
560 elif [ -n "$cmdlineclasses" ]; then
561 classes=$cmdlineclasses
562 elif [ ! -f /var/lib/fai/FAI_CLASSES ]; then
563 # use classes defined at installation time
564 die "Try to read classes from /var/lib/fai/FAI_CLASSES. Failed. Aborting."
565 else
566 classes=$(< /var/lib/fai/FAI_CLASSES)
567 fi
568 echo "List of all classes: " ${classes:-}
569
570 # define classes as: a.b.c.d for cfengine -D
571 # this doesn't work without echo
572 cfclasses=$(echo ${classes:-})
573 cfclasses=${cfclasses// /.}
574 [ "${debug:-}" ] && echo "cfclasses: $cfclasses"
575 }
576 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
577 _defvar() {
578
579 local showvar=1 # TODO: new FAI_FLAG or always set when verbose is used
580 [ "$showvar" ] && set -x
581 . $1 </dev/null
582 [ "$showvar" ] && set +x
583 }
584 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
585 task_defvar() {
586
587 local svar=$LOGDIR/showvar.log
588 local odir=$(pwd)
589 cd $FAI/class
590 for class in ${classes:-} ; do
591 if [ -f $class.var -a -r $class.var ]; then
592 [ X$verbose = X1 ] && echo "Executing $class.var"
593 # show only lines with ++, we cannot use a pipe, since it would call
594 # _devfar in a subprocess. Then, variables are not defined
595 _defvar $class.var > $svar 2>&1
596 grep -P '^\+\+ \w+=' $svar| sed -e 's/\(.*PW\)=.*$/\1=XXXXXXXXXXXXX/'
597 rm $svar
598 fi
599 done
600
601 # /fai/class/* scripts or hooks can write variable definitions
602 # to additonal.var. now source these definitions
603 if [ -f $LOGDIR/additional.var -a -r $LOGDIR/additional.var ]; then
604 echo "Defining variables from additional.var"
605 _defvar $LOGDIR/additional.var > $svar 2>&1
606 grep ^++ $svar
607 rm $svar
608 fi
609 cd $odir
610 unset class svar odir
611 # now all variables are defined. Dump them to variables.log, so we can sources them if needed
612 set | perl -ne 'print if /^\w\w+=/ and not /^(EUID|PPID|SHELLOPTS|UID|rootpw|ROOTPW|USERPW|HOME|PWD|BASHOPTS)|\(/' > $LOGDIR/variables.log
613 # another approach is to use this. A slightly different format, but seems to be robust.
614 # declare -x > $LOGDIR/variables.log
615 }
616 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
617 task_mountdisks() {
618
619 [ ! -f $LOGDIR/fstab ] && die "No $LOGDIR/fstab created."
620 # mount swap space
621 if inside_nfsroot; then
622 local sd
623 for sd in $SWAPLIST; do
624 swapon -p1 $sd && [ X$verbose = X1 ] && echo "Enable swap device $sd"
625 done
626 fi
627 mount2dir $FAI_ROOT $LOGDIR/fstab
628 if [ "$?" -ne 0 ]; then
629 sendmon "TASKERROR mountdisks 885"
630 task_error 885
631 fi
632 if inside_nfsroot && ! mountpoint -q $target; then
633 echo "$target is not a mount point. Check your disk_config file."
634 sendmon "TASKERROR mountdisks 886"
635 task_error 886
636 fi
637 }
638 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
639 task_configure() {
640
641 fai-do-scripts $FAI/scripts
642 task_error 420 $?
643 }
644 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
645 task_tests() {
646
647 if [ ! -d $FAI/tests ]; then
648 return
649 fi
650 fai-do-scripts $FAI/tests # always returns 0 atm
651 # check if any test failed
652 if [ -f $LOGDIR/test.log ]; then
653 if grep -q "FAILED with " $LOGDIR/test.log; then
654 sendmon "TASKERROR tests 312"
655 task_error 312
656 return 1
657 fi
658 fi
659 }
660 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
661 task_savelog() {
662
663 if [ -d /media/data/ ]; then
664 mkdir -p /media/data/logs
665 fai-savelog -l /media/data/logs
666 fi
667 if [ -w $FAI_ROOT/var/log ]; then
668 mkdir -p $FAI_ROOT/var/{lib,log}/fai
669 fai-savelog -l
670 [ -f $LOGDIR/FAI_CLASSES ] && cp -pu $LOGDIR/FAI_CLASSES $FAI_ROOT/var/lib/fai
671 [ -f $LOGDIR/disk_var.sh ] && cp -pu $LOGDIR/disk_var.sh $FAI_ROOT/var/lib/fai
672 fi
673 fai-savelog -r
674 }
675 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
676 ### BEGIN SUBROUTINE INFO
677 # Description: Always called as last FAI task
678 ### END SUBROUTINE INFO
679 task_faiend() {
680
681 : ${flag_reboot:=0}
682 : ${flag_halt:=0}
683
684 # reboot/halt without prompting if FAI_FLAG reboot or halt is set
685 # wait for keypress if neither flag reboot nor halt is set
686 if inside_nfsroot && [ "$flag_reboot" -eq 0 ] && [ "$flag_halt" -eq 0 ]; then
687 echo "Press <RETURN> to reboot (do not remove your removable medium yet)."
688 read
689 fi
690
691 sendmon "TASKEND faiend 0"
692
693 cd /
694 mk_zerofree_list
695 umount_target
696 cleanup_devicemapper
697 if ! inside_nfsroot; then
698 echo "Log files are saved in $LOGDIR"
699 return 0
700 fi
701 killall -q sshd systemd-udevd
702 umount $FAI_ROOT/proc $FAI_ROOT/sys/firmware/efi/efivars $FAI_ROOT/sys $FAI_ROOT/dev/pts 2>/dev/null
703 umount -arf 2>/dev/null
704
705 echo "Rebooting $HOSTNAME now"
706 sendmon "TASKEND reboot 0"
707 # reboot or halt?
708 if [ "$flag_halt" -gt "0" ]; then
709 exec halt -dfp
710 else
711 exec reboot -df
712 fi
713 }
714 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
715 task_install() {
716
717 echo $$ > $stamp
718
719 save_dmesg
720
721 task partition
722 task mountdisks
723 task extrbase
724 task debconf
725 task repository
726 task updatebase
727 task instsoft
728 task configure
729 task tests
730 task finish
731 task chboot
732
733 rm -f $stamp
734 save_dmesg # save again, because new messages could be created
735 task savelog
736
737 if [ -f $stamp ]; then
738 echo "Error while executing commands in subshell." >&2
739 echo -n "$stamp was not removed. PID of running process: "
740 cat $stamp
741 sendmon "TASKERROR install 21"
742 die "Please look at the log files in $LOGDIR for errors." >&2
743 fi
744 }
745 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
746 task_dirinstall() {
747
748 mkdir -p $FAI_ROOT
749 FAI_ROOT=$(cd $FAI_ROOT;pwd)
750 stamp=/var/run/fai/dirinstall-${FAI_ROOT//\//=}
751 clean_exit() {
752 rm -f $stamp
753 [ -z "$FAI_ROOT" ] && return
754 # sometimes mountpoint may be mounted twice, so try to umount them twice
755 mountpoint -q $FAI_ROOT/proc && umount $FAI_ROOT/proc
756 mountpoint -q $FAI_ROOT/sys && umount $FAI_ROOT/sys
757 mountpoint -q $FAI_ROOT/proc && umount $FAI_ROOT/proc
758 mountpoint -q $FAI_ROOT/sys && umount $FAI_ROOT/sys
759 mountpoint -q $FAI_ROOT/dev/pts && umount $FAI_ROOT/dev/pts
760 mountpoint -q $FAI_ROOT/run/udev && umount $FAI_ROOT/run/udev
761 # sometimes umount $FAI_ROOT/dev fails, because a process is
762 # still running in the background and accesses /dev
763 # this occured sometimes when using dirinst and a long package
764 # list if dhelp.postinst is starting an index process in the
765 # bg which did not finished until the installation was finished.
766 mountpoint -q $FAI_ROOT/dev && umount $FAI_ROOT/dev
767 mkramdisk -au 2>/dev/null
768 }
769 trap 'clean_exit' INT QUIT EXIT
770
771
772
773 [ -f "$stamp" ] && {
774 echo -n "fai dirinstall into directory $FAI_ROOT already running or was aborted before. PID: "
775 cat $stamp
776 echo "You may remove $stamp and try again."
777 exit 1
778 }
779
780 echo $$ > $stamp
781 echo "Installing into directory $FAI_ROOT"
782 task extrbase
783 [ -f $target/etc/fstab ] || touch $target/etc/fstab
784 task debconf
785 task repository
786 task updatebase
787 task instsoft
788 task configure
789 task tests
790 task finish
791 clean_exit
792
793 echo -n "Size of file system created: "; du -sh $FAI_ROOT
794 rm -f $stamp
795 unset LOGUSER # so logfile are not saved to remote
796 task savelog
797
798 if [ -f $stamp ]; then
799 echo "Error while executing commands in subshell." >&2
800 echo -n "$stamp was not removed. PID of running process: "
801 cat $stamp
802 sendmon "TASKERROR install 21"
803 die "Please look at the log files in $LOGDIR for errors." >&2
804 fi
805 }
806 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
807 task_softupdate() {
808
809 echo Performing FAI system update. All data may be overwritten!
810 stamp=/var/run/fai/fai_softupdate_is_running
811 [ -f "$stamp" ] && die "Lock file $stamp found. Another fai softupdate is already running. Aborting."
812 echo $$ > $stamp
813 trap "rm -f $stamp" INT QUIT EXIT
814
815 # the following copy operation is required to make $LOGDIR a reliable source
816 # for disk_var.sh
817 # use the last disk_var during update if available
818 [ -f /var/lib/fai/disk_var.sh ] && cp -p /var/lib/fai/disk_var.sh $LOGDIR
819
820 save_dmesg
821
822 task debconf
823 task repository
824 task updatebase
825 task instsoft
826 task configure
827 task tests
828 task finish
829
830 rm -f $stamp
831 # save again, because new messages could be created
832 save_dmesg
833 task savelog
834 umount_csspace
835
836 if [ -f $stamp ]; then
837 echo "Error while executing commands in subshell." >&2
838 echo -n "$stamp was not removed. PID of running process: "
839 cat $stamp
840 sendmon "TASKERROR softupdate 21"
841 die "Please look at the log files in $LOGDIR for errors." >&2
842 fi
843 }
844 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
845
846 catnc() {
847 # cat but no comment lines
848 grep -E -v "^#" $@
849 }
850 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
851 ### BEGIN SUBROUTINE INFO
852 # Provides-Var: $disklist
853 # Requires-Var:
854 # Short-Description: create list of available disks
855 ### END SUBROUTINE INFO
856
857 set_disk_info() {
858
859 # the variable holds a space separated list of devices
860 disklist=$(fai-disk-info | sort | tr '\n' ' ')
861 }
862 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
863 eval_cmdline() {
864
865 # parse kernel parameters and define variables
866 local word cmdline
867
868 echo "Kernel currently running: "
869 uname -rsmo
870 cmdline="$(</proc/cmdline)"
871 echo "Kernel parameters: $cmdline"
872 for word in $cmdline; do
873 if echo "$word" | grep -E -q '^[a-zA-Z0-9_]+=' ; then
874 eval "export $word"
875 fi
876 done
877 }
878 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
879 read_fai_monitor_vars() {
880
881 # read variables which are received from the fai-monitor
882 if [ ! -s /run/initramfs/fai-variables ]; then
883 return
884 fi
885
886 eval $(grep FAI_CONFIG_SRC /run/initramfs/fai-variables)
887 eval $(grep FAI_FLAGS /run/initramfs/fai-variables)
888 }
889 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
890 ### BEGIN SUBROUTINE INFO
891 # Provides-Var: $faimond $sendid $SERVER $monserver
892 # Requires-Var: $LOGDIR $FAI
893 # Suggests-Var: $monserver $FAI_SENDID $FAI_MONITOR_PORT
894 # Short-Description: <task desc.>
895 ### END SUBROUTINE INFO
896
897 task_confdir() {
898
899 if inside_nfsroot; then
900
901 read_fai_monitor_vars
902 eval_cmdline
903 mokutil --sb-state |& grep enabled
904
905 get-boot-info
906 echo "Reading $LOGDIR/boot.log"
907 . $LOGDIR/boot.log
908 unset T170 T171 T172 ROOT_PATH BOOTFILE
909
910 printk=${printk:-6}
911 echo $printk > /proc/sys/kernel/printk
912 create_resolv_conf
913 fi
914 define_fai_flags
915
916 if [ -z "$SERVER" ]; then
917 # since SERVER is not set (by DHCP) we extract it from FAI_CONFIG_SRC
918 # extract server name from FAI_CONFIG_SRC and delete user@ from string
919 SERVER=$(expr match "$FAI_CONFIG_SRC" '.*://\([^/]*\)/.*' | sed -e 's/.*@//')
920 if [ -n "$SERVER" ]; then
921 echo "Setting SERVER=$SERVER. Value extracted from FAI_CONFIG_SRC."
922 else
923 SERVER=$(awk '{if($2 == "/run/rootfsbase") print $1;}' /proc/mounts | grep :/ | cut -d ':' -f 1)
924 if [ -n "$SERVER" ]; then
925 echo "Setting SERVER=$SERVER. Value extracted from NFSROOT."
926 elif [ "$FAI_CONFIG_SRC" = "detect://" ]; then
927 :
928 else
929 echo "NO SERVER FOUND FOR FAI_CONFIG_SRC"
930 fi
931 fi
932 fi
933
934 # check if monitor server is available
935 : ${monserver:=$SERVER}
936 : ${FAI_MONITOR_PORT:=4711}
937
938 if [ -z "$monserver" ]; then
939 echo "No monitor daemon defined."
940 faimond=0
941 else
942 faimond=1
943 if [ -n "$NIC1" ]; then
944 MAC=$(< /sys/class/net/$NIC1/address)
945 PXE="01-"${MAC//:/-}
946 fi
947
948 : ${FAI_SENDID:=host} # set default if undefined
949 case "$FAI_SENDID" in
950 host) sendid=$HOSTNAME ;;
951 mac) sendid=$MAC ;;
952 pxe) sendid=$PXE ;;
953 *) sendid=$FAI_SENDID ;;
954 esac
955
956 if sendmon check; then
957 echo "Monitoring to server $monserver enabled."
958 sendmon "TASKBEGIN confdir"
959 else
960 faimond=0
961 echo "Can't connect to monserver on $monserver port $FAI_MONITOR_PORT. Monitoring disabled."
962 fi
963 fi
964
965 local mdir=/lib/modules/$(uname -r)
966 if inside_nfsroot && [ ! -d $mdir ]; then
967 echo "ERROR: The running kernel does not match the kernel modules inside the nfsroot."
968 echo "ERROR: Kernel modules directory $mdir not available. Only found:"
969 ls -d /lib/modules/*
970 echo ""
971 task_error 790
972 fi
973
974 get-config-dir || {
975 echo "Problems accessing the config space."
976 die ""
977 }
978 }
979 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
980 ### BEGIN SUBROUTINE INFO
981 # Provides-Var: $BOOT_DEVICE $ROOT_PARTITION $BOOT_PARTITION $SWAPLIST
982 # Requires-Var: $LOGDIR $LOGDIR/disk_var.sh
983 # Short-Description: partition local hard disks
984 ### END SUBROUTINE INFO
985
986 task_partition() {
987
988 [ ! -s $LOGDIR/disk_var.sh ] && setup-storage -X |& tee $LOGDIR/format.log
989
990 # if we've created device mapper devices, do cleanup on error
991 grep -qs 'Executing: vgcreate' $LOGDIR/format.log
992 if [ $? -eq 0 ]; then
993 trap "umount_target;cleanup_devicemapper" QUIT EXIT
994 fi
995
996 # partitioning tool must create $LOGDIR/disk_var.sh file
997 if [ ! -s $LOGDIR/disk_var.sh ]; then
998 local conffile=$(grep "Using config file:" $LOGDIR/format.log | cut -d: -f2 )
999 if [ -n "$conffile" ]; then
1000 echo ""
1001 echo "This is your disk_config file:"
1002 cat $conffile
1003 fi
1004 echo ""
1005 task_error 710
1006 sendmon "TASKERROR partition 21"
1007 die "Partitioning tool did not create $LOGDIR/disk_var.sh file."
1008 fi
1009 # now define variable for root and boot partition and boot device
1010 . $LOGDIR/disk_var.sh
1011 }
1012 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1013 ### BEGIN SUBROUTINE INFO
1014 # Provides-Var: none
1015 # Requires-Var: $NFSROOT
1016 # Suggests-Var:
1017 # Short-Description: <task desc.>
1018 ### END SUBROUTINE INFO
1019
1020 call_debootstrap() {
1021
1022 local dversion=$(dpkg-query -Wf '${Version}\n' debootstrap)
1023 echo "Creating base system using debootstrap version $dversion"
1024
1025 echo "Calling debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $FAI_ROOT $2 $3"
1026 LC_ALL=C debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $FAI_ROOT $2 $3
1027 }
1028 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1029 ### BEGIN SUBROUTINE INFO
1030 # Provides-Var: none
1031 # Requires-Var: $FAI_ROOT $NFSROOT $LOGDIR
1032 # Suggests-Var: $FAI_DEBOOTSTRAP
1033 # Short-Description: extrace minimal base file into target
1034 ### END SUBROUTINE INFO
1035
1036 task_extrbase() {
1037
1038 local fs=$FAI_ROOT/etc/fstab
1039
1040 fetch-basefile
1041 # remember, ftar extracts into $FAI_ROOT by default, so / means $FAI_ROOT/
1042 # copy the base file class based if it exists
1043 [ -d $FAI/basefiles ] && ftar -1v -s $FAI/basefiles /
1044 if [ $? -ne 0 ]; then
1045 if inside_nfsroot; then
1046 ftar -1v -c base -s /var/tmp /
1047 else
1048 [ -d $NFSROOT/var/tmp ] &&
1049 ftar -1v -c base -s $NFSROOT/var/tmp /
1050 fi
1051
1052 # if no base file was extracted, call debootstrap
1053 if [ ! -d $FAI_ROOT/etc ]; then
1054 echo "No base file found. Calling debootstrap."
1055 [ -z "$FAI_DEBOOTSTRAP" ] && die "\$FAI_DEBOOTSTRAP undefined. Aborting"
1056 call_debootstrap $FAI_DEBOOTSTRAP
1057 task_error 801 $?
1058 fi
1059 fi
1060
1061 # now we can copy fstab
1062 [ -f $LOGDIR/fstab ] && cp -S.old -bp $LOGDIR/fstab $fs
1063 # copy crypttab, if setup-storage created one
1064 [ -f $LOGDIR/crypttab ] && cp -p $LOGDIR/crypttab $FAI_ROOT/etc/crypttab
1065 # make /var/lib/dpkg a ramdisk
1066 mkramdisk -a
1067 }
1068 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1069 ### BEGIN SUBROUTINE INFO
1070 # Provides-Var:
1071 # Requires-Var: $FAI_ROOT $MNTPOINT $romountopt
1072 # Suggests-Var: $FAI_DEBMIRROR $debug
1073 # Short-Description: mount Debian mirror via NFS
1074 ### END SUBROUTINE INFO
1075
1076 mount_mirror() {
1077
1078 # mount debian mirror directory
1079 [ "$FAI_DEBMIRROR" ] || return # nothing to do
1080 mkdir -p ${FAI_ROOT}${MNTPOINT}
1081 if mount $romountopt $FAI_DEBMIRROR ${FAI_ROOT}${MNTPOINT}; then
1082 [ "$debug" ] && echo "Mirror mounted from $FAI_DEBMIRROR to ${FAI_ROOT}${MNTPOINT}"
1083 else
1084 sendmon "TASKERROR repository $?"
1085 die "Can't mount $FAI_DEBMIRROR"
1086 fi
1087 }
1088 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1089 task_debconf () {
1090
1091 if [ ! -d $FAI/debconf ]; then
1092 echo "Can't find debconf directory $FAI/debconf. Skipping preseeding."
1093 task_error 2
1094 return
1095 fi
1096 fai-debconf $FAI/debconf
1097 }
1098 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1099 ### BEGIN SUBROUTINE INFO
1100 # Provides-Var: none
1101 # Requires-Var: $classes $FAI $FAI_ROOT $FAI_ETC_DIR $FAI_ALLOW_UNSIGNED
1102 # Suggests-Var:
1103 # Short-Description: prepare access to package repository
1104 ### END SUBROUTINE INFO
1105
1106 task_repository () {
1107
1108 local v
1109 if [ X$verbose = X1 ]; then
1110 v=-v
1111 fi
1112
1113 inside_nfsroot && FAI_ETC_DIR=/etc
1114
1115 # some generale network files are needed on the client
1116 # resolv.conf is needed, /etc/hosts is useful in /target
1117 # use a file from the config space or from /etc inside the nfsroot
1118 if ! fcopy -BM $v /etc/resolv.conf ; then
1119 if [ -d "$FAI_ROOT/run/systemd/resolve" ]; then
1120 cp $v /etc/resolv.conf "$FAI_ROOT/run/systemd/resolve/stub-resolv.conf"
1121 fi
1122 if [ "$(readlink -f /etc/resolv.conf)" = "$(readlink -f $FAI_ROOT/etc/resolv.conf)" ] ; then
1123 :
1124 # no need for copy since both are pointing to the same file
1125 else
1126 [ -f /etc/resolv.conf ] && cp $v /etc/resolv.conf $FAI_ROOT/etc
1127 fi
1128 fi
1129 if ! fcopy -M $v /etc/hosts ; then
1130 [ X$FAI_ROOT = X/ ] || cp $v -bS.orig /etc/hosts $FAI_ROOT/etc
1131 fi
1132
1133 # apt specific things
1134 if ! fcopy -SBM $v /etc/apt/sources.list; then
1135 [ -f $FAI_ETC_DIR/apt/sources.list ] && cp $v $FAI_ETC_DIR/apt/sources.list $FAI_ROOT/etc/apt
1136 fi
1137 if ! fcopy -BM $v /etc/apt/preferences; then
1138 [ -f $FAI_ETC_DIR/apt/preferences ] && cp $v $FAI_ETC_DIR/apt/preferences $FAI_ROOT/etc/apt
1139 fi
1140 fcopy -SBMir /etc/apt # copy all other apt config files from the config space
1141
1142 if [ X$FAI_ALLOW_UNSIGNED = X1 ]; then
1143 cat <<EOF > $FAI_ROOT/etc/apt/apt.conf.d/10fai
1144 APT::Get::AllowUnauthenticated "true";
1145 Acquire::AllowInsecureRepositories "true";
1146 Aptitude::CmdLine::Ignore-Trust-Violations yes;
1147 EOF
1148 else
1149 rm -f $FAI_ROOT/etc/apt/apt.conf.d/10fai
1150 fi
1151
1152 # add apt keys for all classes
1153 for keyfile in ${classes:-}; do
1154 if [ -f $FAI/package_config/$keyfile.gpg ]; then
1155 echo "Copying APT key $keyfile.gpg to target"
1156 cp -v --preserve=timestamp $FAI/package_config/$keyfile.gpg $FAI_ROOT/etc/apt/trusted.gpg.d/
1157 fi
1158 done
1159
1160 # mount Debian mirror via NFS if needed
1161 mount_mirror
1162 }
1163 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1164 task_updatebase() {
1165
1166 # maybe the base system is not up to date
1167
1168 if [ X$verbose = X1 ]; then
1169 updatebase </dev/null |& tee -a $LOGDIR/software.log
1170 task_error 474 ${PIPESTATUS[0]}
1171 else
1172 updatebase </dev/null >> $LOGDIR/software.log 2>&1
1173 task_error 474 $?
1174 fi
1175 }
1176 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1177 task_instsoft() {
1178
1179 if [ X$verbose = X1 ]; then
1180 echo "Installing software may take a while"
1181 fi
1182 if [ "$debug" ]; then
1183 install_packages | tee -a $LOGDIR/software.log
1184 task_error 471 ${PIPESTATUS[0]}
1185 elif [ X$verbose = X1 ]; then
1186 install_packages </dev/null |& tee -a $LOGDIR/software.log
1187 task_error 471 ${PIPESTATUS[0]}
1188 else
1189 install_packages </dev/null >> $LOGDIR/software.log 2>&1
1190 task_error 471 $?
1191 fi
1192 # This almost indicates an error
1193 grep -E "^E:" $LOGDIR/software.log && task_error 472
1194 grep "Couldn't find any package whose name or description matched" $LOGDIR/software.log && task_error 321
1195 grep -q "E: Sub-process /usr/bin/dpkg returned an error code" $LOGDIR/software.log && task_error 620
1196
1197 install_pkgs $FAI/pkgs |& tee -a $LOGDIR/software.log
1198 install_pkgs /media/data/pkgs |& tee -a $LOGDIR/software.log
1199 }
1200 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1201 task_finish() {
1202
1203 mkramdisk -au # umount ramdisk
1204 if inside_nfsroot; then
1205 # show some local information
1206 df -PTh | grep -E ':|^/|^Filesystem'
1207 # show rx and tx bytes of network device
1208 grep . /sys/class/net/*/statistics/*x_bytes | perl -ane 'm#/sys/class/net/(.+)/statistics/(.+):(\d+)# && ($3) && ($1 ne lo) && printf "%s %s %.2f Mbytes\n",$1,$2,$3/1000000 '
1209 swapoff -a
1210 else
1211 df -PTh | grep -E "^Filesystem|$target"
1212 fi
1213
1214 # undo fake of all programs made by fai
1215 fai-divert -R
1216 rm -f $FAI_ROOT/etc/apt/apt.conf.d/{10,90}fai
1217 echo -n "FAI finished at: ";date
1218 echo "The $FAI_ACTION took $(($(cut -d . -f 1 /proc/uptime) - start_seconds)) seconds."
1219 }
1220 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1221 task_chboot() {
1222
1223 # the whole subroutine may be an externel script
1224
1225 [ -z "$LOGUSER" ] && return # silently return from subroutine
1226
1227 # do not execute for fai-diskimage
1228 if [[ $FAI_ROOT =~ '/fai-diskimage.' ]]; then
1229 return
1230 fi
1231
1232 local frsh remotesh hostname
1233 local doexit=0
1234 read hostname < /proc/sys/kernel/hostname
1235 local ipaddr=$(grep IPADDR $LOGDIR/boot.log | cut -d= -f2 | sed "s/'//g")
1236 local nexttest=$(grep -E -s ^NEXTTEST= $LOGDIR/test.log | cut -d= -f2)
1237
1238 case "$FAI_LOGPROTO" in
1239 ftp) remotesh=ssh ;;
1240 ssh) remotesh=ssh ;;
1241 rsh) remotesh=rsh ;;
1242 none) return ;;
1243 *) echo "ERROR: Unknown value for \$FAI_LOGPROTO." ;;
1244 esac
1245 frsh="$remotesh -l $LOGUSER ${SERVER}"
1246
1247 if [ -z "$SERVER" ] ; then
1248 echo "SERVER not defined. Can't change network boot configuration"
1249 task_error 2
1250 doexit=1
1251 fi
1252 [ $doexit -eq 1 ] && return
1253
1254 # change boot device (local disk or network) when using PXE
1255 # first test if rsh to server works
1256 $frsh true >/dev/null 2>&1
1257 if [ $? -ne 0 ]; then
1258 task_error 3
1259 echo "WARNING: $frsh failed. Can't call fai-chboot on the install server." >&2
1260 else
1261 if [ -n "$nexttest" ]; then
1262 # for test sequences, we want the system to reinstall immediately with
1263 # with different class setup
1264 $frsh /usr/sbin/fai-chboot -k ADDCLASSES=$nexttest -FIv $ipaddr $MAC
1265 else
1266 # remove pxe config, so host will use default and boot from local disk
1267 $frsh /usr/sbin/fai-chboot -vd $ipaddr $MAC
1268 fi
1269 fi
1270 }
1271 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1272 sendmon() {
1273
1274 # send message to monitor daemon
1275 echo "$*" >> $LOGDIR/fai-monitor.log
1276 [ "$faimond" -eq 0 ] && return 0
1277 echo "$sendid $*" | nc -w 8 $monserver $FAI_MONITOR_PORT 2>/dev/null
1278 return $?
1279 }
1280 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1281 all_disks_and_size() {
1282
1283 # print a list of devices and their block size
1284 ( grep -E 'md[0-9]{3,}$' /proc/partitions;
1285 grep -E ' etherd/e[[:digit:]]+\.[[:digit:]]+\b| i2o/hd.+\b| cciss/c[[:digit:]]+d[[:digit:]]+\b| ida/c[[:digit:]]+d[[:digit:]]+\b| rd/c[[:digit:]]+d[[:digit:]]+\b| fio.\b| hd.\b| sd[a-z]{1,2}\b|/disc\b| vd.\b| xvd.\b| nvme[[:digit:]]+n[[:digit:]]+$| mmcblk[[:digit:]]+$' /proc/partitions ) | awk '{ print $4 " " $3 }'
1286 }
1287 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1288 disks_by_id() {
1289
1290 # list all disks by ID and their link to the device name
1291 (
1292 cd /dev/disk/by-id
1293 find -type l -printf "%f %l\n" | \
1294 grep -Pv '^md|-part\d|^wwn-|^nvme-eui|^nvme-nvme|^nvme\S+_\d ' | \
1295 grep -E '^ata|^nvme|^usb'
1296 )
1297 }
1298 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1299 checkdisk() {
1300
1301 # read lines with device name and size and check if device is a disk
1302 # $1 can be a device name that will be ignored. Used for the device
1303 # name of the USB stick if we boot from it
1304
1305 local igndev=$1 # a device to ignore
1306 local isdisk
1307 while read device blocks; do
1308 isdisk=1
1309 # old way of detecting disks: [ $(cat /sys/block/$device/removable) -eq 1 ] && isdisk=0
1310 [ $(stat -c %G /dev/$device) = "disk" ] || isdisk=0
1311
1312 # if set, ignore this device
1313 [ "$device" = "$igndev" ] && isdisk=0
1314 if [ $isdisk -eq 1 ]; then
1315 echo "$device"
1316 fi
1317 done
1318 }
1319 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1320 once_only() {
1321
1322 # read from stdin and echo lines only once, ignore if the same line occurs again
1323 local line
1324 local -A seen
1325 while read line; do
1326 if [ X${seen["$line"]} = X ]; then
1327 echo $line
1328 fi
1329 seen["$line"]=1
1330 done
1331 }
1332 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1333 all_disks_by_size() {
1334 all_disks_and_size | sort -nr -k2 | checkdisk $FAI_BOOTSTICK
1335 }
1336 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1337 largestdisk() {
1338 all_disks_and_size | sort -nr -k2 | checkdisk $FAI_BOOTSTICK | head -1
1339 }
1340 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1341 smallestdisk() {
1342 all_disks_and_size | sort -n -k2 | checkdisk $FAI_BOOTSTICK | head -1
1343 }
1344 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1345 matchdisks() {
1346 # matchdisks PATTERN PATTERN ....
1347 # create an ordered list of disks matching the patterns provided
1348 # pay attention that the pattern do not match multiple times
1349 grep_disks "$@" | sed -e 's#.*/##g'
1350 }
1351 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1352 notmatchdisks() {
1353 grepv_disks $1 | sed -e 's#.*/##g'
1354 }
1355 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1356 grep_disks() {
1357 if [ -z "$*" ]; then
1358 set -- '.'
1359 fi
1360 for p in "$@"; do
1361 disks_by_id | grep -E $p
1362 done
1363 }
1364 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1365 grepv_disks() {
1366 if [ -z "$1" ]; then
1367 set -- '.'
1368 fi
1369 disks_by_id | grep -E -v $1
1370 }
1371 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1372 set_bootstick() {
1373
1374 # determine the device of the USB stick we boot from
1375 # sets the variable FAI_BOOTSTICK
1376
1377 FAI_BOOTSTICK=$(mount | awk '/run\/initramfs\/live/ {print $1}')
1378 FAI_BOOTSTICK=${FAI_BOOTSTICK#/dev/}
1379 FAI_BOOTSTICK=${FAI_BOOTSTICK%%[[:digit:]]*}
1380
1381 # use disk label FAI_CD to detect the USB stick
1382 if [ -z "$FAI_BOOTSTICK" ] && [ -L /dev/disk/by-label/FAI_CD ]; then
1383 FAI_BOOTSTICK=$(readlink /dev/disk/by-label/FAI_CD | sed -e 's#.*/##g')
1384 FAI_BOOTSTICK=${FAI_BOOTSTICK%%[[:digit:]]*}
1385 fi
1386 }