From c9c0dffa05c00428595b5ddebbeeb396418d3f65 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 21 Aug 2013 13:43:56 +0200 Subject: [remote] Renamed functions to functions.inc; make setup-addons not fail if no addons are configured; try random server order when downloading --- remote/rootfs/rootfs-stage31/data/etc/functions | 90 --------------------- .../rootfs/rootfs-stage31/data/etc/functions.inc | 92 ++++++++++++++++++++++ remote/rootfs/rootfs-stage31/data/init | 4 +- .../opt/openslx/scripts/systemd-setup_slx_addons | 10 ++- 4 files changed, 100 insertions(+), 96 deletions(-) delete mode 100644 remote/rootfs/rootfs-stage31/data/etc/functions create mode 100644 remote/rootfs/rootfs-stage31/data/etc/functions.inc diff --git a/remote/rootfs/rootfs-stage31/data/etc/functions b/remote/rootfs/rootfs-stage31/data/etc/functions deleted file mode 100644 index 37bb7d89..00000000 --- a/remote/rootfs/rootfs-stage31/data/etc/functions +++ /dev/null @@ -1,90 +0,0 @@ -######################################################################### -# -# COMMON HELPER FUNCTIONS -# - -######################################################################### -# -# Function to drop a debug shell with an error message. -# -# Usage: -# drop_shell "This is your error message." -# -drop_shell() { - [ $# -gt 0 ] && echo $@ - echo "CTRL + D will continue booting." - setsid sh -c 'exec sh /dev/tty1 2>&1' -} - -######################################################################### -# -# Helper function to parse parameter named OPTION from the kernel command line (/proc/cmdline) -# -# Usage: -# read_from_cmdline OPTION -# -read_from_cmdline(){ - [ $# -ne 1 ] && echo "Error - 'read_from_cmdline' requires 1 argument, $# given." && exit 1 - - local OPTION="$1" - local VALUE="" - - VALUE="$(grep -o -E "${OPTION}=\S+" /proc/cmdline | cut -c $((${#OPTION}+2))-)" - if [ -z "$VALUE" ]; then - echo "Error - '${OPTION}=' not found in command line, or empty" - return 1 - else - echo "$VALUE" - fi -} - -######################################################################### -# -# 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 [ -e "${FUTURE_ROOT}/opt/openslx/config" ]; then - . "${FUTURE_ROOT}/opt/openslx/config" || echo "Error - could not source '${FUTURE_ROOT}/opt/openslx/config'" - else - # read SLX_KCL_SERVERS from cmdline since we didn't get it from the config file. - SLX_KCL_SERVERS="$(read_from_cmdline "slxsrv" | tr ',' ' ')" - echo "SLX_KCL_SERVERS='$SLX_KCL_SERVERS'" >> "${FUTURE_ROOT}/opt/openslx/config" - fi - - local FILE_URL="$1" - local TARGET_PATH="$2" - - for TIMEOUT in 1 2 3 4 5; do - for SERVER in $SLX_CONFIG_SERVERS $SLX_KCL_SERVERS; do - wget -T 5 -q -O "$TARGET_PATH" "http://${SERVER}/${FILE_URL}" - RET=$? - if [ "x$RET" != "x0" ]; then - echo "Error - downloading 'http://$SERVER/$FILE_URL' via wget failed. Exit Code: $RET" - echo "Trying again in $(($TIMEOUT * 500)) ms..." - [ $TIMEOUT -eq 5 ] && drop_shell "Max timeouts reached. Dropping shell..." - usleep $(($TIMEOUT * 500000)) - else - [ "x$DEBUG" != "x" ] && echo "Successfully downloaded 'http://${SERVER}/$FILE_URL'." - return 0 - fi - done - done - - # check if it actually worked - if [ ! -e $TARGET_PATH ]; then - echo "Error - $TARGET_PATH does not exists. Downloading '$FILE_URL' from '$SERVER_LIST' failed." - return 1 - fi -} diff --git a/remote/rootfs/rootfs-stage31/data/etc/functions.inc b/remote/rootfs/rootfs-stage31/data/etc/functions.inc new file mode 100644 index 00000000..362943ae --- /dev/null +++ b/remote/rootfs/rootfs-stage31/data/etc/functions.inc @@ -0,0 +1,92 @@ +######################################################################### +# +# COMMON HELPER FUNCTIONS +# + +######################################################################### +# +# Function to drop a debug shell with an error message. +# +# Usage: +# drop_shell "This is your error message." +# +drop_shell() { + [ $# -gt 0 ] && echo $@ + echo "CTRL + D will continue booting." + setsid sh -c 'exec sh /dev/tty1 2>&1' +} + +######################################################################### +# +# Helper function to parse parameter named OPTION from the kernel command line (/proc/cmdline) +# +# Usage: +# read_from_cmdline OPTION +# +read_from_cmdline(){ + [ $# -ne 1 ] && echo "Error - 'read_from_cmdline' requires 1 argument, $# given." && exit 1 + + local OPTION="$1" + local VALUE="" + + VALUE="$(grep -o -E "${OPTION}=\S+" /proc/cmdline | cut -c $((${#OPTION}+2))-)" + if [ -z "$VALUE" ]; then + echo "Error - '${OPTION}=' not found in command line, or empty" + return 1 + else + echo "$VALUE" + fi +} + +######################################################################### +# +# 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 [ -e "${FUTURE_ROOT}/opt/openslx/config" ]; then + . "${FUTURE_ROOT}/opt/openslx/config" || echo "Error - could not source '${FUTURE_ROOT}/opt/openslx/config'" + else + # read SLX_KCL_SERVERS from cmdline since we didn't get it from the config file. + SLX_KCL_SERVERS="$(read_from_cmdline "slxsrv" | tr ',' ' ')" + echo "SLX_KCL_SERVERS='$SLX_KCL_SERVERS'" >> "${FUTURE_ROOT}/opt/openslx/config" + fi + + local FILE_URL="$1" + local TARGET_PATH="$2" + + 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 2 3 4 5; do + for SERVER in $SERVERS; do + wget -T 5 -q -O "$TARGET_PATH" "http://${SERVER}/${FILE_URL}" + RET=$? + if [ "x$RET" != "x0" ]; then + echo "Error - downloading 'http://$SERVER/$FILE_URL' via wget failed. Exit Code: $RET" + echo "Trying again in $(($TIMEOUT * 500)) ms..." + [ $TIMEOUT -eq 5 ] && drop_shell "Max timeouts reached. Dropping shell..." + usleep $(($TIMEOUT * 500000)) + else + [ "x$DEBUG" != "x" ] && echo "Successfully downloaded 'http://${SERVER}/$FILE_URL'." + return 0 + fi + done + done + + # check if it actually worked + if [ ! -e $TARGET_PATH ]; then + echo "Error - $TARGET_PATH does not exists. Downloading '$FILE_URL' from '$SERVER_LIST' failed." + return 1 + fi +} diff --git a/remote/rootfs/rootfs-stage31/data/init b/remote/rootfs/rootfs-stage31/data/init index 35e898b5..db4ef484 100755 --- a/remote/rootfs/rootfs-stage31/data/init +++ b/remote/rootfs/rootfs-stage31/data/init @@ -17,7 +17,7 @@ export PATH=/bin:/sbin:/usr/bin:/usr/sbin export LD_LIBRARY_PATH=/usr/lib64 # import common functions -. /etc/functions +. /etc/functions.inc # mount the important standard directories mount -n -t tmpfs -o 'mode=755' run "/run" @@ -91,7 +91,7 @@ fi # copy files needed for stage3.2 to FUTURE_ROOT [ $DEBUG -ge 1 ] && echo "Copying busybox etc. to stage32..." -tar -cp "/bin" "/sbin" "/usr/bin" "/usr/sbin" "/etc/functions" | tar -xp -C "${FUTURE_ROOT}/opt/openslx/" +tar -cp "/bin" "/sbin" "/usr/bin" "/usr/sbin" "/etc/functions.inc" | tar -xp -C "${FUTURE_ROOT}/opt/openslx/" #echo 'blacklist pcspkr' >> /mnt/etc/modprobe.d/blacklist.conf # one last debug shell if activated diff --git a/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_slx_addons b/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_slx_addons index a3350424..70a09924 100755 --- a/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_slx_addons +++ b/remote/rootfs/rootfs-stage32/data/opt/openslx/scripts/systemd-setup_slx_addons @@ -6,12 +6,14 @@ # # -. /opt/openslx/etc/functions +. /opt/openslx/config || { echo "Could not source config!"; exit 23; } +[ -z "${SLX_ADDONS}" ] && { echo "No addons configured. Nothing to do :-)."; exit 0; } + +. /opt/openslx/etc/functions.inc || { echo "functions.inc not found!"; exit 1337; } + export FUTURE_ROOT="/" # read openslx config -. /opt/openslx/config -[ -z "${SLX_ADDONS}" ] && { echo "SLX_ADDONS is not set in /opt/openslx/config." && exit 1; } if [ -z "${SLX_CONFIG_SERVERS}" ]; then echo "SLX_CONFIG_SERVERS is not set in /opt/openslx/config. Will only try the base servers from the cmdline." if [ -z "${SLX_KCL_SERVERS}" ]; then @@ -31,7 +33,7 @@ for ADDON in ${SLX_ADDONS}; do download "${SLX_BASE_PATH}/${ADDON}.sqfs" "${ADDON_TARGET_PATH}" # now mount it to $SLX_MNT/ - ADDON_MOUNT_POINT="${SLX_BASE_MNT}/$(basename $(echo ${ADDON})|awk -F "." '{print $1}')" + ADDON_MOUNT_POINT="${SLX_BASE_MNT}/$(basename $(echo ${ADDON}) | awk -F "." '{print $1}')" mkdir -p "$ADDON_MOUNT_POINT" echo "Mounting ${ADDON_TARGET_PATH} to ${ADDON_MOUNT_POINT}" mount -t squashfs "$ADDON_TARGET_PATH" ${ADDON_MOUNT_POINT} || { echo "Failed to mount." && exit 1; } -- cgit v1.2.3-55-g7522