/usr/share/bootcd/bootcd-run.lib is in bootcd 5.12.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # bootcd-run.lib
# vim: set filetype=sh :
# version: $Version: $
# copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2007)
# license: GNU General Public License, version 2
# comment: use ANSW to change the next point from external
# description: To understand what to do with this library you can download
# the test programs with wget
#
RUN_INTERACT="no"
LANG=C
LC_ALL=C
IGNORE=""
STDOUT=""
msg()
{
echo "--- MESSAGE ---" | tee -a $ERRLOG >&2
echo "$@" | tee -a $ERRLOG >&2
}
set +u
if [ ! "$ERRLOG" ]; then
ERRLOG=/var/log/bootcd.errlog
touch "$ERRLOG" >/dev/null 2>&1
[ $? -ne 0 ] && ERRLOG=/tmp/bootcd.errlog
msg "ERRLOG was unset. It is set to $ERRLOG now."
fi
set -u
# function dbg prints DEBUG Messages to stderr.
# It is very fast because no if-check has to be done
# functions dbgoff, dbgon, and dbgtoggle switches dbg on or off
dbgoff()
{
eval "dbg() { :; }"
}
if [ "$(type dbg 2>&1| grep "not found")" ]; then
dbgoff
fi
dbgon()
{
eval "dbg() { echo \"DEBUG \$@\" >&2; }"
}
# with option -v dbtoggle is more verbose
dbgtoggle()
{
if [ "$(dbg 2>&1)" ]; then
dbgoff
[ "$1" = "-v" ] && echo "debug off" >&2
else
dbgon
[ "$1" = "-v" ] && echo "debug on" >&2
fi
}
# function run: run given command and output error
# get: $IGNORE -- regexp for error messages, generate no output, no question
# (set from function ignore)
# $STDOUT -- regexp for error messages, only output, no question
# (set from function stdout)
# $ERRORLOG -- write to this file
#
# $RUN_INTERACT -- "yes" meens run from interactiv
# comment: by default, all errors (stdout, error) generates output and questions
#
# Attention: do not try to set global variables in a command called by run !!!
#
run()
{
local ERR C T A
#echo >&2 "IGNORE $IGNORE"
#echo >&2 "STDOUT $STDOUT"
while :
do
> $ERRLOG.tmp
(
eval "$@" 2>&1
ERR=$?
if [ "$ERR" != "0" ]; then
echo "exit=$ERR"
fi
) |
if [ "$IGNORE" != "" ]; then
C="grep -v $IGNORE"
tee $ERRLOG.tmp | tee -a $ERRLOG | eval "$C"
else
tee -a $ERRLOG.tmp | tee -a $ERRLOG
fi
T=`cat $ERRLOG.tmp`
if [ "$IGNORE" != "" ]; then
C="grep -v $IGNORE"
T=`echo "$T" | eval "$C"`
fi
if [ "$STDOUT" != "" ]; then
C="grep -v $STDOUT"
T=`echo "$T" | eval "$C"`
fi
if [ "$T" != "" ]; then
A=""
while [ "$A" != "e" -a "$A" != "r" -a "$A" != "i" -a "$A" != "s" ]
do
echo "--- OUTPUT from <$@> ---" | tee -a $ERRLOG
echo "$T" | tee -a $ERRLOG
if [ "$RUN_INTERACT" = "yes" ]; then
echo -n "--- (e)xit (r)edo (i)gnore (s)witch interactive --- " | tee -a $ERRLOG
else
echo -n "--- (e)xit (r)edo (i)gnore --- " | tee -a $ERRLOG
fi
read A
# Next line handles EOF from a pipe as "(e)xit"
[ $? -ne 0 ] && A="e"
echo "$A" >> $ERRLOG
done
if [ "$A" = "e" ]; then
exit 1
elif [ "$A" = "i" ]; then
break
# switch to interactive mode
elif [ "$A" = "s" -a "$RUN_INTERACT" = "yes" ]; then
echo
echo "switching to interactive mode to point ($ntrctv_ANSW) which failed"
echo
ntrctv_ANSW=$(($ntrctv_ANSW-1))
ntrctv_y=""
break
fi
else
break
fi
done
IGNORE=""
STDOUT=""
}
# function: build stdout for interactive
# get: $1 -- number
# $2 -- stdout string
in_stdout()
{
local nr=$1
shift
eval STDOUT${nr}=\"\$STDOUT${nr} -e \\\"\$@\\\"\"
}
# function: build ignore for interactive
stdout()
{
STDOUT="$STDOUT -e \"$@\""
}
# function: build ignore for interactive
# get: $1 -- number
# $2 -- ignore string
in_ignore()
{
local nr=$1
shift
eval IGNORE${nr}=\"\$IGNORE${nr} -e \\\"\$@\\\"\"
}
## example
#mm=0
#mm=$(($mm+1)); eval IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
#in_ignore $mm "error bla"
#in_ignore $mm "^bluber sumsel .*"
#in_stdout $mm "no error but but a useful comment"
#mm=$(($mm+1)); eval IGNORE${mm}=\"\"
#in_norun $mm
#echo $IGNORE1 -- $IGNORE2
#exit
# function: run interactiv -r without run for this menu point
# get: $1 -- number
# comment: please update norunstring also in interactive!
in_norun()
{
local norunstring="interactive_without_run"
local nr=$1
eval IGNORE${nr}=\"$norunstring\"
eval STDOUT${nr}=\"$norunstring\"
}
ignore()
{
IGNORE="$IGNORE -e \"$@\""
}
warn()
{
if [ $# -gt 0 ]; then
A=""
while [ "$A" != "e" -a "$A" != "i" ]
do
echo "--- WARNING ---" | tee -a $ERRLOG
echo "$@" | tee -a $ERRLOG
echo -n "--- (e)xit (i)gnore --- " | tee -a $ERRLOG
if [ "$SCRIPT" ]; then
A="i"
echo "$A (SCRIPT)"
echo "$A (SCRIPT)" >> $ERRLOG
echo
else
read A
# Next line handles EOF from a pipe as "(e)xit"
[ $? -ne 0 ] && A="e"
echo "$A" >> $ERRLOG
fi
done
if [ "$A" = "e" ]; then
exit 1
fi
fi
}
err()
{
(
echo "--- FATAL ERROR ---"
echo "$@"
) |
if [ "$(set | grep ^ERRLOG=)" ]; then
tee -a $ERRLOG >&2
else
cat >&2
fi
exit 1
}
# create_host_keys <DIR>
# if ssh_host_key, ssh_host_rsa or ... exists in <DIR>
# then recreate it.
create_host_keys()
{
# Now only ssh versions which support Option -t are supported
F=ssh_host_key
if [ -f $1/$F ]; then
echo "Creating OpenSSH_2 $F" >>$ERRLOG
run "rm -f $1/$F $1/$F.pub"
run "ssh-keygen -b 1024 -f $1/$F -N '' -t rsa1 >>$ERRLOG 2>&1"
fi
F=ssh_host_rsa_key
if [ -f $1/$F ]; then
echo "Creating OpenSSH_2 $F" >>$ERRLOG
run "rm -f $1/$F $1/$F.pub"
run "ssh-keygen -b 1024 -f $1/$F -N '' -t rsa >>$ERRLOG 2>&1"
fi
F=ssh_host_dsa_key
if [ -f $1/$F ]; then
echo "Creating OpenSSH_2 $F" >>$ERRLOG
run "rm -f $1/$F $1/$F.pub"
run "ssh-keygen -b 1024 -f $1/$F -N '' -t dsa >>$ERRLOG 2>&1"
fi
}
# syntax: interactive [-y] [-e] [-h <head>] <functioncall with params> ...
# description: interactive evaluates the listed functioncalls. If the first
# argument is not -y an interactive menu will be given. This menu
# can get a header <head> with -h. An extra evaluation of <head>
# and <functioncall with params> can be forced with option -e.
# The <functioncall with params> can contain the special
# character sequences "<-i>" and "<h>". "<-i>" will be translated
# either to "-i" or to "". This can be toggled with the key "i".
#
# -y -- run without interaction
# -e -- evaluate each function call one times more
# -h <head> -- print <head> as menu head
# -r -- run command with "run"
#
# helper functions for "run" (-r given to interactive):
# in_stdout <number> <string> -- ignore stdout+stderr
# in_ignore <number> <string> -- ignore stdout+stderr, don't print
# in_norun <number> -- run menupoint without run!
#
#
interactive()
{
local ntrctv_ANSW ntrctv_A="" ntrctv_i ntrctv_y="" ntrctv_t="-i " ntrctv_head="" ntrctv_e="" ntrctv_run="" ntrctv_c
local ntrctv_cmd
local norunstring="interactive_without_run"
# get all options
while [ $# -ge 1 ]; do
if [ "$1" = "-y" ]; then
ntrctv_y="$1"
ntrctv_t=""
shift
elif [ "$1" = "-e" ]; then
ntrctv_e="$1"
shift
elif [ "$1" = "-h" -a $# -gt 2 ]; then
ntrctv_head="$2"
shift 2
elif [ "$1" = "-r" ]; then
ntrctv_run="yes"
shift 1
else
break
fi
done
# read the function calls
if [ $# -gt 0 ]; then
ntrctv_ANSW="1"
while :; do
if [ ! "$ntrctv_y" ]; then
# print head
if [ "$ntrctv_head" ]; then
ntrctv_c="$ntrctv_head"
# evaluate the head
[ "$ntrctv_e" ] && ntrctv_c="$(eval echo "$ntrctv_c")"
echo "$ntrctv_c"
fi
ntrctv_i=1
# build menu
while [ $ntrctv_i -le $# ]; do
ntrctv_c=$(echo "$ntrctv_i $(eval echo "\${$ntrctv_i}")" | sed "s/<-i>[[:space:]]*/$ntrctv_t/g")
[ "$ntrctv_e" ] && ntrctv_c="$(eval echo "$ntrctv_c")"
echo "$ntrctv_c"
ntrctv_i=$(($ntrctv_i+1))
done
# dbg exists, print flag (do not use declare to be posix konform)
[ "$(type dbg 2>&1| grep "not found")" ] || echo "d toggle dbg() function on and off"
# i flag can be toggled
[ "$(echo "$*"|grep "<-i>")" ] && echo "i toggle -i flag"
# menu point "continue"
[ "$ntrctv_ANSW" != "q" ] && [ $ntrctv_ANSW -lt $# ] && echo "c continue without questions"
echo "q quit"
echo -n "? [$ntrctv_ANSW]"
read ntrctv_A
# Next line handles EOF from a pipe as "q"
[ $? -ne 0 ] && ntrctv_A="q"
[ "$ntrctv_A" -a "$ntrctv_A" != "c" -a "$ntrctv_A" != "i" -a "$ntrctv_A" != "d" ] && ntrctv_ANSW="$ntrctv_A"
fi
if [ "$ntrctv_ANSW" = "q" ]; then
return
elif [ "$ntrctv_A" = "d" ]; then
dbgtoggle -v
elif [ "$ntrctv_A" = "i" ]; then
[ "$ntrctv_t" ] && ntrctv_t="" || ntrctv_t="-i "
elif [ "$(echo "$ntrctv_ANSW" | sed 's/[0-9]//g')" ]; then
ntrctv_ANSW="?"
elif [ "$ntrctv_ANSW" -ge 1 -a "$ntrctv_ANSW" -le $# ]; then
[ "$ntrctv_A" = "c" ] && ntrctv_y="-y"
# build the command and set -i, if needed
ntrctv_cmd="$(echo "$(eval echo "\${${ntrctv_ANSW}}")" | sed -e "s/<-i>/$ntrctv_t/g")"
# eval function call again
[ "$ntrctv_e" ] && ntrctv_cmd="$(eval echo "$ntrctv_cmd")"
# call function
if [ "$ntrctv_run" ]; then
local IGNORE="" STDOUT=""
[ "$(set | grep "^IGNORE${ntrctv_ANSW}=")" ] && eval IGNORE=\"\$IGNORE${ntrctv_ANSW}\"
[ "$(set | grep "^STDOUT${ntrctv_ANSW}=")" ] && eval STDOUT=\"\$STDOUT${ntrctv_ANSW}\"
# run is on but run this menu point without run!
if [ "$IGNORE" = "$STDOUT" -a "$STDOUT" = "$norunstring" ]; then
eval "$ntrctv_cmd"
else
[ "$ntrctv_y" ] && local RUN_INTERACT="yes" || local RUN_INTERACT="no"
run "$ntrctv_cmd"
fi
else
eval "$ntrctv_cmd"
fi
ntrctv_ANSW=$(($ntrctv_ANSW+1))
[ $ntrctv_ANSW -gt $# ] && ntrctv_ANSW=q
fi
done
fi
}
## examples ... comment out and try it
#
#a() { A="called: a"; [ $# -ge 1 ] && A="$A <$1>"; [ $# -ge 2 ] && A="$A <$2>"; [ $# -ge 3 ] && A="$A <$3>"; [ $# -ge 4 ] && A="$A <$4>"; echo "$A" ;}
#interactive a "a -x # with arg and comment" "a -x \"one two\" # 2 args, one with spaces"
#exit 0
#
#interactive -y a "a -x # with arg and comment" "a -x \"one two\" # 2 args, one with spaces"
#exit 0
#
#interactive "a A" "a B" "a C" "a D" "a E" "a F" "a G" "a H" "a I" "a J" "a K"
#exit 0
#
#f="a \"a -x # with arg and comment\" \"a -x \\\"one two\\\" # 2 args, one with spaces\""
#eval "interactive $f"
#exit 0
#
#interactive # should return without doing anything
#exit 0
#
#interactive "a <-i> A" "a B" "a <-i> C"
#exit 0
#
#f1() { IANAME="$IANAME/f1" interactive -e -h "=== \$IANAME \(opts=-e\) n=\<\$n\> ===" "n=$((n+1))" f1; }
#n=""; IANAME="main" interactive -e -h "=== \$IANAME \(opts=-e\) n=\<\$n\> ===" "n=$(($n+1)" f1
#exit 0
#
#interactive -r "sh -c \"exit 0\"" "echo \"error auf stdout\"" "echo \"error auf stderr\" >&2" "sh -c \"exit 1\""
#exit 0
#
#interactive -y -r "sh -c \"exit 0\"" "echo \"error auf stdout\"" "echo \"error auf stderr\" >&2" "sh -c \"exit 1\""
#exit 0
#
# function: ask_user -- ask user
# get: question = string
# default = y|n
# ret: 1 == yes | 0 == no
ask_user()
{
local quest=$1
[ "$2" = "y" -o "$2" = "n" ] && local default=$2 || local default=""
while [ 1 ]; do
echo "$quest"
read a
# Next line handles EOF from a pipe as "default"
[ $? -ne 0 ] && a=""
[ "$a" = "" ] && a=$default
if [ "$a" = "Y" -o "$a" = "y" -o "$a" = "J" -o "$a" = "j" ]; then
# echo "yes"
return 1;
fi
if [ "$a" = "N" -o "$a" = "n" ]; then
# echo "no"
return 0;
fi
echo >&2 "Error: \"$a\" is not a allowed answer!"
done
}
# function: do_dir -- create directory
# get: $1 -- directory
# $2 -- permissions (optional)
do_dir()
{
local dir=$1
local perm=""; [ $# -ge 2 ] && perm=$2
if [ -d "$dir" ]; then
ask_user "Directory \"$dir\" exist's, remove it [Y|n] " "y"
[ $? = 0 ] && err "Can't work without directory \"$dir\"."
rm -rf $dir
fi
mkdir -p $dir
[ "$perm" ] && chmod $perm $dir
echo ""
}
# function: check_tools -- check if tools exist
# use ./ to search in local directory
# get: $1..$n -- tools
check_tools()
{
while [ $# -gt 0 ]; do
echo -n >&2 " check for \"$1\" ..."
if [ "$(echo $1 |cut -c1-2)" = "./" ]; then
[ ! -f "$1" ] && err "Can't find file \"$1\"!"
else
[ ! "$(which $1)" ] && err "Can't find tool \"$1\" in PATH!"
fi
echo >&2 "ok"
shift
done
}
# function: prepare_chroot <workdir> [-i]
# globals: IAHEAD
prepare_chroot()
{
local workdir="$1"
local interact=""; [ $# -ge 2 ] && interact="$2"
local TODO="" mm=0
if [ "$(env | grep "^IAHEAD=")" ]; then
local IAHEAD="$IAHEAD / prepare_chroot"
else
local IAHEAD="prepare_chroot"
fi
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"mount --bind /dev $workdir/dev\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"mount -t proc none $workdir/proc\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"mount -t sysfs none $workdir/sys\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"mount -t devpts none $workdir/dev/pts\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"chroot $workdir sh -c \\\"[ -f /etc/mtab ] && mv /etc/mtab /etc/mtab.bootcd || rm -f /etc/mtab.bootcd\\\"\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"chroot $workdir sh -c \\\"grep -v -e \\\\\\\"^sunrpc\>\\\\\\\" -e \\\\\\\"^rootfs\>\\\\\\\" /proc/mounts >/etc/mtab\\\"\""
# we need selinux mounted to install openssh_server without
# error "chage: Permission denied."
if [ "$(cat /proc/mounts | grep selinux)" ]; then
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"mount -t selinuxfs none $workdir/selinux\""
fi
[ "$interact" ] && interact="" || interact="-y"
eval "interactive -h \"=== \$IAHEAD ===\" $interact -r $TODO"
}
# unprepare_chroot <workdir> [-i]
# globals IAHEAD
unprepare_chroot()
{
local workdir="$1"
local interact=""; [ $# -ge 2 ] && interact="$2"
local TODO="" mm=0
if [ "$(env | grep "^IAHEAD=")" ]; then
local IAHEAD="$IAHEAD / unprepare_chroot"
else
local IAHEAD="unprepare_chroot"
fi
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"chroot $workdir sh -c \\\"[ -f /etc/mtab.bootcd ] && mv /etc/mtab.bootcd /etc/mtab || rm -f /etc/mtab\\\"\""
if [ "$(cat /proc/mounts | grep selinux)" ]; then
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"umount $workdir/selinux\""
fi
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"umount $workdir/dev/pts\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"umount $workdir/sys\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"umount $workdir/proc\""
mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
TODO="$TODO \"umount $workdir/dev\""
[ "$interact" ] && interact="" || interact="-y"
eval "interactive -h \"=== \$IAHEAD ===\" $interact -r $TODO"
}
|