From cc7f97b966da5d28e0cdba6fad93b2b433a23e1f Mon Sep 17 00:00:00 2001 From: Brian C. Lane Date: Mar 31 2022 18:49:10 +0000 Subject: functions: Add useful bash functions like strstr Also adds basic status functions used by the existing scripts. --- diff --git a/libexec/livesys/functions b/libexec/livesys/functions new file mode 100644 index 0000000..606e1bc --- /dev/null +++ b/libexec/livesys/functions @@ -0,0 +1,86 @@ +# Functions to be used with livesys-* scripts + +# returns OK if $1 contains $2 +strstr() { + [ "${1#*$2*}" = "$1" ] && return 1 + return 0 +} + +# Run some action. Log its output. +action() { + local STRING rc + + STRING=$1 + echo -n "$STRING " + shift + "$@" && success $"$STRING" || failure $"$STRING" + rc=$? + echo + return $rc +} + +# verbose ->> very (very!) old bootup look (prior to RHL-6.0?) +# color ->> default bootup look +# other ->> default bootup look without ANSI colors or positioning +BOOTUP=color +# Column to start "[ OK ]" label in: +RES_COL=60 +# terminal sequence to move to that column: +MOVE_TO_COL="echo -en \\033[${RES_COL}G" +# Terminal sequence to set color to a 'success' (bright green): +SETCOLOR_SUCCESS="echo -en \\033[1;32m" +# Terminal sequence to set color to a 'failure' (bright red): +SETCOLOR_FAILURE="echo -en \\033[1;31m" +# Terminal sequence to set color to a 'warning' (bright yellow): +SETCOLOR_WARNING="echo -en \\033[1;33m" +# Terminal sequence to reset to the default color: +SETCOLOR_NORMAL="echo -en \\033[0;39m" + +# NOTE: /dev/ttyS* is serial console. "not a tty" is such as +# /dev/null associated when executed under systemd service units. +if LANG=C tty | grep -q -e '\(/dev/ttyS\|not a tty\)'; then + BOOTUP=serial + MOVE_TO_COL= + SETCOLOR_SUCCESS= + SETCOLOR_FAILURE= + SETCOLOR_WARNING= + SETCOLOR_NORMAL= +fi + +# Log that something succeeded +success() { + [ "$BOOTUP" != "verbose" ] && echo_success + return 0 +} + +# Log that something failed +failure() { + local rc=$? + [ "$BOOTUP" != "verbose" ] && echo_failure + [ -x /bin/plymouth ] && /bin/plymouth --details + return $rc +} + +echo_success() { + [ "$BOOTUP" = "color" ] && $MOVE_TO_COL + echo -n "[" + [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS + echo -n $" OK " + [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL + echo -n "]" + echo -ne "\r" + return 0 +} + +echo_failure() { + [ "$BOOTUP" = "color" ] && $MOVE_TO_COL + echo -n "[" + [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE + echo -n $"FAILED" + [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL + echo -n "]" + echo -ne "\r" + return 1 +} + + diff --git a/libexec/livesys/livesys-late b/libexec/livesys/livesys-late index 8a0360b..239088b 100755 --- a/libexec/livesys/livesys-late +++ b/libexec/livesys/livesys-late @@ -3,6 +3,8 @@ # live: Late init script for live image # SPDX-License-Identifier: GPL-3.0-or-later +. /usr/libexec/livesys/functions + if ! strstr "`cat /proc/cmdline`" rd.live.image || [ -e /.liveimg-late-configured ] ; then exit 0 fi diff --git a/libexec/livesys/livesys-main b/libexec/livesys/livesys-main index 0620b89..507a8b5 100755 --- a/libexec/livesys/livesys-main +++ b/libexec/livesys/livesys-main @@ -4,6 +4,8 @@ # SPDX-License-Identifier: GPL-3.0-or-later # +. /usr/libexec/livesys/functions + if ! strstr "`cat /proc/cmdline`" rd.live.image; then exit 0 fi