#!/bin/ash ############################################################## # # COMMON HELPER FUNCTIONS # ######################################################################### # # Function to drop a debug shell with an error message. # # Usage: # drop_shell "This is your error message." # drop_shell() { if [ -n "$MUTED_OUTPUT" ]; then exec 1>&4 2>&5 reset fi [ $# -gt 0 ] && echo "$*" if [ "x$SPLASH" != "x0" ]; then echo "System halted. If you want to spawn a debug shell, select" echo "debug mode in the boot menu." echo "" echo "Press any key to reboot." read -t 1 -s -n 999999 BLA read -n 1 -s BLA echo b > /proc/sysrq-trigger exit 1 fi echo "CTRL + D will continue booting." setsid sh -c 'exec sh /dev/tty1 2>&1' } ######################################################################### # # Helper function to download given FILE_URL from servers in 'slxsrv' # as given through the kernel command line. File will be saved under TARGET_PATH # # Usage: # download FILE_URL TARGET_PATH # # Example: # download "config" "/opt/openslx/config" # # Note: # FILE_URL can have subpath, e.g. "ubuntu-13.04-x64/config" # download() { [ $# -ne 2 ] && echo "Error - 'download' requires 2 arguments, $# given." && return 1 if [ -z "$SLX_KCL_SERVERS" ]; then if ! [ -s "/opt/openslx/config" ]; then echo "Don't have any servers to download from." elif ! . "/opt/openslx/config"; then echo "Error - could not source '/opt/openslx/config'" fi fi local FILE_URL="$1" local TARGET_PATH="$2" # Shuffle server list local SERVERS=$(for SERVER in $SLX_CONFIG_SERVERS $SLX_KCL_SERVERS; do echo "$RANDOM $SERVER"; done | sort -u | sed -r 's/^[0-9]+ //') for TIMEOUT in 1 1 2 END; do for SERVER in $SERVERS; do rm -f -- "${TARGET_PATH}" wget -T 5 -q -O "$TARGET_PATH" "http://${SERVER}/${FILE_URL}" RET=$? if [ "x$RET" != "x0" -o ! -e "$TARGET_PATH" ]; then echo "Error - downloading 'http://$SERVER/$FILE_URL' via wget failed. Exit Code: $RET" usleep 50000 # 50ms else echo "Successfully downloaded 'http://${SERVER}/$FILE_URL'." return 0 fi done [ "$TIMEOUT" = "END" ] && break echo "Trying again in $(($TIMEOUT * 250)) ms..." usleep $(($TIMEOUT * 250000)) done # Max retries reached, no success :-( return 1 } # Add benchmark event to var, including uptime as prefix bench_event() { bench_result="${bench_result}$(cut -f 1 -d ' ' "/proc/uptime") $@ " } # mini tarcopy <3 # tarcopy tarcopy() { [ -d "$1" -a -d "$2" ] || return 1 cd "$1" find . \! -type d > /tmp/tctmp tar -c -p -T /tmp/tctmp | tar -xp -C "$2" rm -f -- /tmp/tctmp cd - &>/dev/null } run_hooks () { [ -d "/inc/${1}.d" ] || return 0 local i for i in "/inc/${1}.d/"*; do [ -f "$i" ] && . "$i" done return 0 }