| 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-2005 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 |
task_savelog
|
| 37 |
echo "$@"
|
| 38 |
exec bash
|
| 39 |
}
|
| 40 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 41 |
defnop() {
|
| 42 |
|
| 43 |
# define given list of subroutine names as dummy function;
|
| 44 |
# this will fake unknown commands
|
| 45 |
|
| 46 |
local name
|
| 47 |
for name in "$@";do
|
| 48 |
eval "$name () { :;}"
|
| 49 |
done
|
| 50 |
}
|
| 51 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 52 |
ifclass() {
|
| 53 |
|
| 54 |
[ "$debug" ] && echo "Test if class $1 is in $classes"
|
| 55 |
# test if a class is defined
|
| 56 |
local cl
|
| 57 |
local ret=1
|
| 58 |
|
| 59 |
for cl in $classes; do
|
| 60 |
[ x$cl = x$1 ] && ret=0 && break
|
| 61 |
done
|
| 62 |
[ "$debug" ] && echo "ifclass returns $ret"
|
| 63 |
return $ret
|
| 64 |
}
|
| 65 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 66 |
rwmount() {
|
| 67 |
|
| 68 |
# remount partition read/write
|
| 69 |
mount -o rw,remount $1
|
| 70 |
}
|
| 71 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 72 |
save_dmesg() {
|
| 73 |
|
| 74 |
dmesg > $LOGDIR/dmesg.log
|
| 75 |
}
|
| 76 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 77 |
wait_for_jobs() {
|
| 78 |
|
| 79 |
# can be an extern script
|
| 80 |
# wait for running (background) jobs to finish (e.g. update-auctex-elisp)
|
| 81 |
local i=0
|
| 82 |
while jobsrunning; do
|
| 83 |
[ $(($i % 3)) -eq 0 ] && echo "Waiting for background jobs to finish."
|
| 84 |
i=$(($i+1))
|
| 85 |
sleep 10
|
| 86 |
done
|
| 87 |
}
|
| 88 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 89 |
task() {
|
| 90 |
|
| 91 |
# hooks are called before a task is called
|
| 92 |
# if a task is skipped, also its hooks are skipped
|
| 93 |
# a hook can set the flag, so the accociated task is skipped
|
| 94 |
|
| 95 |
local taskname=$1
|
| 96 |
|
| 97 |
[ -f $LOGDIR/skip.$taskname ] || call_hook $taskname
|
| 98 |
|
| 99 |
if [ -f $LOGDIR/skip.$taskname ]; then
|
| 100 |
# skip task
|
| 101 |
rm $LOGDIR/skip.$taskname
|
| 102 |
[ "$verbose" ] && echo "Skiping task_$taskname"
|
| 103 |
sndmon "TASKSKIP $taskname"
|
| 104 |
else
|
| 105 |
echo "Calling task_$taskname"
|
| 106 |
sndmon "TASKBEGIN $taskname"
|
| 107 |
terror=0 # task can set this variable to indicate an error
|
| 108 |
task_$taskname
|
| 109 |
sndmon "TASKEND $taskname $terror"
|
| 110 |
fi
|
| 111 |
# since the subroutine is not needed any more, we can undefine it
|
| 112 |
unset task_$taskname
|
| 113 |
}
|
| 114 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 115 |
call_hook() {
|
| 116 |
|
| 117 |
local hook=$1
|
| 118 |
local cl dflag hfile
|
| 119 |
[ "$debug" ] && dflag="-d"
|
| 120 |
|
| 121 |
for cl in $classes; do
|
| 122 |
hfile=$FAI/hooks/$hook.$cl
|
| 123 |
if [ -f $hfile -a ! -x $hfile ]; then
|
| 124 |
echo "WARNING: Skipping $hfile execustion because it's not executable."
|
| 125 |
continue
|
| 126 |
fi
|
| 127 |
if [ -f $hfile.source -a ! -x $hfile.source ]; then
|
| 128 |
echo "WARNING: Skipping $hfile.source execustion because it's not executable."
|
| 129 |
continue
|
| 130 |
fi
|
| 131 |
if [ -x $hfile ]; then
|
| 132 |
echo "Calling hook: $hook.$cl"
|
| 133 |
sndmon "HOOK $hook.$cl"
|
| 134 |
# execute the hook
|
| 135 |
$hfile $dflag
|
| 136 |
check_status $hook.$cl $?
|
| 137 |
fi
|
| 138 |
if [ -x $hfile.source ]; then
|
| 139 |
echo "Source hook: $hook.$cl.source"
|
| 140 |
sndmon "HOOK $hook.$cl.source"
|
| 141 |
# source this hook
|
| 142 |
. $hfile.source $dflag
|
| 143 |
check_status $hook.$cl.source $?
|
| 144 |
fi
|
| 145 |
done
|
| 146 |
}
|
| 147 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 148 |
skiptask() {
|
| 149 |
|
| 150 |
# mark all given tasks, so they will be skipped
|
| 151 |
local tasklist="$@"
|
| 152 |
local task
|
| 153 |
|
| 154 |
for task in $tasklist; do
|
| 155 |
> $LOGDIR/skip.$task
|
| 156 |
done
|
| 157 |
}
|
| 158 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 159 |
get_fai_dir() {
|
| 160 |
|
| 161 |
# get /fai directory; mount it or get it from a cvs repository
|
| 162 |
if [ -z "$FAI_LOCATION" ]; then
|
| 163 |
get_fai_cvs
|
| 164 |
else
|
| 165 |
mount $romountopt $FAI_LOCATION $FAI &&
|
| 166 |
echo "Configuration space $FAI mounted from $FAI_LOCATION"
|
| 167 |
fi
|
| 168 |
ln -s $FAI $rundir/current_config
|
| 169 |
if [ ! -d $FAI/class ]; then
|
| 170 |
echo "WARNING: directory $FAI/class not found."
|
| 171 |
fi
|
| 172 |
}
|
| 173 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 174 |
get_fai_cvs() {
|
| 175 |
|
| 176 |
# subroutine which gets $FAI (/fai) configuration directory from
|
| 177 |
# a cvs repository. You can redefine this subroutine if you need
|
| 178 |
# access via ftp, http, or from a database
|
| 179 |
|
| 180 |
if [ "$FAI_CVSROOT" ] ; then
|
| 181 |
local TAG=""
|
| 182 |
[ -n "$FAI_CVSTAG" ] && TAG="-r $FAI_CVSTAG"
|
| 183 |
export FAI_CONFIG_AREA=$FAI_ROOT$FAI
|
| 184 |
export FAI=$(mktemp -t -d fai-config.XXXXXX)
|
| 185 |
|
| 186 |
[ "$debug" ] && echo "\$FAI now points to $FAI"
|
| 187 |
|
| 188 |
if [ -d "$FAI_CONFIG_AREA/CVS" -a -z "$FORCE" ] ; then
|
| 189 |
echo "Config found at $FAI_CONFIG_AREA: Copying"
|
| 190 |
cp -a $FAI_CONFIG_AREA/. $FAI
|
| 191 |
echo "Updating CVS"
|
| 192 |
cd $FAI
|
| 193 |
cvs -q -d"$FAI_CVSROOT" up -P $TAG -d -C > $LOGDIR/cvs.log
|
| 194 |
else
|
| 195 |
echo "Checking out CVS"
|
| 196 |
cd /tmp
|
| 197 |
cvs -q -d"$FAI_CVSROOT" co -P -d $(basename "$FAI") \
|
| 198 |
$TAG $FAI_CVSMODULE > $LOGDIR/cvs.log
|
| 199 |
fi
|
| 200 |
else
|
| 201 |
echo "Warning $0: Neither \$FAI_LOCATION nor \$FAI_CVSROOT are defined."
|
| 202 |
fi
|
| 203 |
cd /
|
| 204 |
}
|
| 205 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 206 |
define_fai_flags() {
|
| 207 |
|
| 208 |
local flag
|
| 209 |
# FAI_FLAGS are comma separated, define all flags
|
| 210 |
FAI_FLAGS=${FAI_FLAGS//,/ }
|
| 211 |
[ "$verbose" ] && echo "FAI_FLAGS: $FAI_FLAGS"
|
| 212 |
for flag in $FAI_FLAGS; do
|
| 213 |
# define this flag as 1
|
| 214 |
eval "$flag=1"
|
| 215 |
done
|
| 216 |
}
|
| 217 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 218 |
task_setup() {
|
| 219 |
|
| 220 |
# source user specific subroutines
|
| 221 |
[ -f $FAI/hooks/subroutines ] && . $FAI/hooks/subroutines
|
| 222 |
if [ $DO_INIT_TASKS -eq 1 ]; then
|
| 223 |
# set the system time and date using rdate or/and ntpdate
|
| 224 |
[ "$TIMESRVS_1" ] && rdate $TIMESRVS_1
|
| 225 |
[ "$NTPSRVS_1" ] && ntpdate -b -v $NTPSRVS
|
| 226 |
fi
|
| 227 |
|
| 228 |
define_fai_flags
|
| 229 |
DNSDOMAIN=$DOMAIN # cfengine 1.5.3 can't use $DOMAIN
|
| 230 |
|
| 231 |
if [ $DO_INIT_TASKS -eq 1 ] ; then
|
| 232 |
[ "$createvt" ] && {
|
| 233 |
# create two virtual terminals; acces via alt-F2 and alt-F3
|
| 234 |
echo "Press ctrl-c to interrupt FAI and to get a shell"
|
| 235 |
openvt -c2 /bin/bash ; openvt -c3 /bin/bash
|
| 236 |
trap 'echo "You can reboot with faireboot";bash' INT QUIT
|
| 237 |
}
|
| 238 |
|
| 239 |
# start secure shell daemon for remote access
|
| 240 |
[ "$sshd" -a -x /usr/sbin/sshd ] && /usr/sbin/sshd
|
| 241 |
fi
|
| 242 |
|
| 243 |
# when did FAI start, using localtime
|
| 244 |
FAI_RUNDATE=$(date +'%Y%m%d_%H%M%S')
|
| 245 |
}
|
| 246 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 247 |
task_action() {
|
| 248 |
|
| 249 |
if [ -z "$FAI_ACTION" ]; then
|
| 250 |
echo "No action in \$FAI_ACTION defined."
|
| 251 |
sndmon "TASKERROR action 21"
|
| 252 |
task_faiend
|
| 253 |
exit
|
| 254 |
fi
|
| 255 |
echo "FAI_ACTION: $FAI_ACTION"
|
| 256 |
case $FAI_ACTION in
|
| 257 |
|
| 258 |
install)
|
| 259 |
if [ $DO_INIT_TASKS -eq 0 ]; then
|
| 260 |
echo "Cowardly refusing to run a FAI installation on a running system."
|
| 261 |
return
|
| 262 |
fi
|
| 263 |
echo Performing FAI installation. All data may be overwritten!
|
| 264 |
echo -ne "\a"; sleep 1
|
| 265 |
echo -ne "\a"; sleep 1
|
| 266 |
echo -e "\a"; sleep 5
|
| 267 |
task install
|
| 268 |
task faiend
|
| 269 |
;;
|
| 270 |
softupdate)
|
| 271 |
echo Performing FAI system update. All data may be overwritten!
|
| 272 |
task softupdate
|
| 273 |
;;
|
| 274 |
sysinfo)
|
| 275 |
echo Showing system information.
|
| 276 |
task sysinfo
|
| 277 |
task_faiend
|
| 278 |
die Now you have a shell.
|
| 279 |
;;
|
| 280 |
*)
|
| 281 |
if [ -f $FAI/hooks/$FAI_ACTION ]; then
|
| 282 |
echo "Calling user defined action: $FAI_ACTION"
|
| 283 |
$FAI/hooks/$FAI_ACTION
|
| 284 |
else
|
| 285 |
echo "ERROR: User defined action $FAI/hooks/$FAI_ACTION not found."
|
| 286 |
sndmon "TASKERROR action 22"
|
| 287 |
task_faiend
|
| 288 |
fi
|
| 289 |
;;
|
| 290 |
esac
|
| 291 |
}
|
| 292 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 293 |
task_defclass() {
|
| 294 |
|
| 295 |
if [ ! -d $FAI/class ]; then
|
| 296 |
sndmon "TASKERROR defclass 21"
|
| 297 |
echo "Directory $FAI/class not found. Following subdirectories are found:"
|
| 298 |
find $FAI -type d -maxdepth 1 -printf "%p\n"
|
| 299 |
die "Aborting."
|
| 300 |
fi
|
| 301 |
|
| 302 |
# new script for defining classes; variables imported: $LOGDIR, $verbose, $debug
|
| 303 |
if [ $renewclass -eq 1 ]; then
|
| 304 |
# reevaluate new list of classes
|
| 305 |
fai-class -T $FAI/class $LOGDIR/FAI_CLASSES
|
| 306 |
classes=$(< $LOGDIR/FAI_CLASSES)
|
| 307 |
else
|
| 308 |
# use classes defined at installation time
|
| 309 |
if [ ! -f /var/log/fai/FAI_CLASSES ]; then
|
| 310 |
die "Try to read classes from /var/log/fai/FAI_CLASSES. Failed. Aborting."
|
| 311 |
fi
|
| 312 |
classes=$(< /var/log/fai/FAI_CLASSES)
|
| 313 |
fi
|
| 314 |
|
| 315 |
# define classes as: a.b.c.d for cfengine -D
|
| 316 |
# this doesn't work without echo
|
| 317 |
cfclasses=$(echo $classes)
|
| 318 |
cfclasses=${cfclasses// /.}
|
| 319 |
[ "$debug" ] && echo "cfclasses: $cfclasses"
|
| 320 |
}
|
| 321 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 322 |
task_defvar() {
|
| 323 |
|
| 324 |
cd $FAI/class
|
| 325 |
for class in $classes ; do
|
| 326 |
if [ -f $class.var ]; then
|
| 327 |
[ "$verbose" ] && echo "Executing $class.var"
|
| 328 |
[ "$debug" ] && set -vx
|
| 329 |
. $class.var </dev/null
|
| 330 |
[ "$debug" ] && set +vx
|
| 331 |
fi
|
| 332 |
done
|
| 333 |
|
| 334 |
# /fai/class/S* scripts or hooks can write variable definitions
|
| 335 |
# to additonal.var. now source these definitions
|
| 336 |
[ "$debug" ] && set -vx
|
| 337 |
[ -f $LOGDIR/additional.var ] && . $LOGDIR/additional.var
|
| 338 |
[ "$debug" ] && set +vx
|
| 339 |
unset class
|
| 340 |
# now all variables are defined. Dump them to variables.sh
|
| 341 |
set | perl -ne 'print if /^\w\w+=/' | egrep -v "^BASH_VERSINFO|^EUID|^PPID|^SHELLOPTS|^UID|^rootpw|^HOME|^PWD" > $LOGDIR/variables.sh
|
| 342 |
cd /
|
| 343 |
}
|
| 344 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 345 |
task_mountdisks() {
|
| 346 |
|
| 347 |
[ ! -f $LOGDIR/$fstab ] && die "No $LOGDIR/$fstab created."
|
| 348 |
# mount swap space
|
| 349 |
local sd
|
| 350 |
for sd in $SWAPLIST; do
|
| 351 |
swapon $sd && [ "$verbose" ] && echo "Enable swap device $sd"
|
| 352 |
done
|
| 353 |
mount2dir $FAI_ROOT $LOGDIR/$fstab
|
| 354 |
}
|
| 355 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 356 |
task_configure() {
|
| 357 |
|
| 358 |
fai-do-scripts $FAI/scripts
|
| 359 |
}
|
| 360 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 361 |
task_savelog() {
|
| 362 |
|
| 363 |
[ -d $target/var/log/fai ] || mkdir -p $target/var/log/fai
|
| 364 |
[ -d $target/var/log/fai ] && fai-savelog -l
|
| 365 |
cd $LOGDIR && cp -p FAI_CLASSES variables.sh $diskvar $target/var/log/fai
|
| 366 |
fai-savelog -r
|
| 367 |
cd /
|
| 368 |
}
|
| 369 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 370 |
task_faiend() {
|
| 371 |
|
| 372 |
[ $DO_INIT_TASKS -eq 0 ] && exit 0
|
| 373 |
wait_for_jobs
|
| 374 |
echo "Press <RETURN> to reboot or ctrl-c to execute a shell"
|
| 375 |
# reboot without prompting if FAI_FLAG reboot is set
|
| 376 |
[ -z $reboot ] && read
|
| 377 |
echo "Rebooting $HOSTNAME now"
|
| 378 |
sndmon REBOOT
|
| 379 |
cd /
|
| 380 |
sync
|
| 381 |
|
| 382 |
case $(uname -s) in
|
| 383 |
Linux)
|
| 384 |
killall -q sshd
|
| 385 |
umount $target/proc
|
| 386 |
umount -ar
|
| 387 |
exec reboot -dfi
|
| 388 |
;;
|
| 389 |
esac
|
| 390 |
}
|
| 391 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 392 |
task_backup() {
|
| 393 |
|
| 394 |
die "Task backup not yet used. But you can use the hook backup."
|
| 395 |
}
|
| 396 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 397 |
task_install() {
|
| 398 |
|
| 399 |
> $stamp
|
| 400 |
|
| 401 |
save_dmesg
|
| 402 |
|
| 403 |
task partition
|
| 404 |
task mountdisks
|
| 405 |
task extrbase
|
| 406 |
task mirror
|
| 407 |
task debconf
|
| 408 |
task prepareapt
|
| 409 |
task updatebase
|
| 410 |
task instsoft
|
| 411 |
task configure
|
| 412 |
task finish
|
| 413 |
task chboot
|
| 414 |
|
| 415 |
rm -f $stamp
|
| 416 |
# save again, because new messages could be created
|
| 417 |
save_dmesg
|
| 418 |
task savelog
|
| 419 |
|
| 420 |
if [ -f $stamp ]; then
|
| 421 |
echo "Error while executing commands in subshell."
|
| 422 |
echo "$stamp was not removed."
|
| 423 |
sndmon "TASKERROR install 21"
|
| 424 |
die "Please look at the log files in $LOGDIR for errors."
|
| 425 |
fi
|
| 426 |
}
|
| 427 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 428 |
task_softupdate() {
|
| 429 |
|
| 430 |
local stamp=/var/run/fai/fai_softupdate_is_running
|
| 431 |
|
| 432 |
> $stamp
|
| 433 |
|
| 434 |
save_dmesg
|
| 435 |
|
| 436 |
local start_seconds=$(cut -d . -f 1 /proc/uptime)
|
| 437 |
task mirror
|
| 438 |
task debconf
|
| 439 |
task prepareapt
|
| 440 |
task updatebase
|
| 441 |
task instsoft
|
| 442 |
task configure
|
| 443 |
date
|
| 444 |
echo "The update took $[$(cut -d . -f 1 /proc/uptime)-$start_seconds] seconds."
|
| 445 |
|
| 446 |
rm -f $stamp
|
| 447 |
# save again, because new messages could be created
|
| 448 |
save_dmesg
|
| 449 |
task savelog
|
| 450 |
|
| 451 |
if [ -f $stamp ]; then
|
| 452 |
echo "Error while executing commands in subshell."
|
| 453 |
echo "$stamp was not removed."
|
| 454 |
sndmon "TASKERROR softupdate 21"
|
| 455 |
die "Please look at the log files in $LOGDIR for errors."
|
| 456 |
fi
|
| 457 |
umount $FAI_ROOT/fai
|
| 458 |
}
|
| 459 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 460 |
|
| 461 |
catnc() {
|
| 462 |
# cat but no comment lines
|
| 463 |
egrep -v "^#" $@
|
| 464 |
}
|
| 465 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|