Commit 4b6c9a1a authored by Adam Wujek's avatar Adam Wujek 💬

[Feature: 1363] rootfs: add a support to execute custom script at startup

This will allow users to have special configuration (that is not possible to
achieve by just using dot-config) without a need to manually handle custom
script.

The script can be used from /wr/bin/custom_boot_script.sh or downloaded during
the boot. It is not possible to execute a script which was downloaded during
the previous boot. But a script can be written in such way to copy itself to
/wr/bin/custom_boot_script.sh.
Signed-off-by: Adam Wujek's avatarAdam Wujek <adam.wujek@cern.ch>
parent b8b50972
......@@ -275,6 +275,7 @@ config WRS_LOG_OTHER
In the current version following programs uses this option:
--wrs_watchdog
--wrs_auxclk
--wrs_custom_boot_script.sh
This collective entry is to avoid number of entries in dot-config.
The string can be a pathname (e.g. /dev/kmsg) or a <facility>.<level>
spefification like "daemon.debug". An empty strings is used
......@@ -772,6 +773,47 @@ config NIC_THROTTLING_VAL
endmenu
menu "Custom boot script configuration"
config CUSTOM_BOOT_SCRIPT_ENABLED
bool "Execute custom script"
default n
help
Enable execution of a custom script during boot. It can be a local
script placed at /wr/bin/custom_boot_script.sh or downloaded from
a given URL. This script can be used for some exotic configurations
that dot-config does not support. This scipt runs before switching
is enabled.
choice CUSTOM_BOOT_SCRIPT_SOURCE
prompt "Source for a custom boot script"
depends on CUSTOM_BOOT_SCRIPT_ENABLED
default CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL
help
Defines the source of a custom boot script. Local or remote.
config CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL
bool "Use local custom script"
help
Use /wr/bin/custom_boot_script.sh to be executed at boot.
config CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE
bool "Use remote custom script"
help
Use the URL in CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE_URL to download the
custom boot script.
endchoice
config CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE_URL
string "URL of a custom script"
depends on CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE
help
URL to the custom script. HOSTNAME, IPADDR and MACADDR are
substituted before retrieving the file.
Example: "tftp://morgana/custom-script-IPADDR"
endmenu
menu "Developer options"
config MONIT_DISABLE
bool "Disable monit"
......
......@@ -577,8 +577,9 @@ value is changed by the web interface, proper action is taken.
Logging options for the three main WRS processes and other programs.
@t{CONFIG_WRS_LOG_OTHER} is currently used by:
@itemize
@item wrs_watchdog
@item wrs_auxclk
@item @t{wrs_watchdog}
@item @t{wrs_auxclk}
@item @t{wrs_custom_boot_script.sh}
@end itemize
Each value
can be a pathname, to select logging to file (and @t{/dev/kmsg}
......@@ -791,10 +792,27 @@ value is changed by the web interface, proper action is taken.
Limit the Rx bandwidth of the traffic that goes from WR ports to Linux.
Throttling can be enabled to prevent Linux using 100% of the processing
power to receive Ethernet frames coming from WR ports to the CPU.
To enable throttling set @t{CONFIG_NIC_THROTTLING_ENABLED}.
@t{CONFIG_NIC_THROTTLING_VAL} contains maximum allowed bandwidth
in KB/s.
@item CONFIG_CUSTOM_BOOT_SCRIPT_ENABLED
@itemx CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL
@itemx CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE
@itemx CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE_URL
It is possible to run a custom script at boot time. In this case please set @t{CONFIG_CUSTOM_BOOT_SCRIPT_ENABLED}.
To run a script from the local filesystem please set @t{CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL}.
The script at the location @t{/wr/bin/custom_boot_script.sh} will be executed.
As an alternative, you can choose @t{CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE} and be able to
specify an URL (@t{http://}, @t{ftp://} or @t{tftp://}) in @t{CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE_URL} whence
the switch will download the script to be executed at boot time.
The filename in the URL can include @t{HOSTNAME}, @t{IPADDR}
and/or @t{MACADDR}, so the same configuration string can be used to set up a batch
of switches with different configurations.
@item CONFIG_MONIT_DISABLE
Disable monitoring of running processes by monit. Monit by default
......
#!/bin/sh
set -o pipefail
dotconfig=/wr/etc/dot-config
tmpdir=/tmp
tmpscript="$tmpdir"/custom_boot_script.sh
custom_boot_status_file="$tmpdir"/custom_boot_script_status
custom_boot_source_file="$tmpdir"/custom_boot_script_source
custom_boot_source_url_file="$tmpdir"/custom_boot_script_url
print_error() {
echo "$1"
eval echo "$0: $1" $LOGPIPE
}
start() {
echo -n "Executing custom boot script: "
if [ -f $dotconfig ]; then
. $dotconfig
else
echo "$0 unable to source dot-config ($dotconfig)!"
fi
# set log destination
WRS_LOG=$CONFIG_WRS_LOG_OTHER
# if empty turn it to /dev/null
if [ -z $WRS_LOG ]; then
WRS_LOG="/dev/null";
fi
# if a pathname, use it
if echo "$WRS_LOG" | grep / > /dev/null; then
eval LOGPIPE=\" \> $WRS_LOG 2\>\&1 \";
else
# not a pathname: use verbatim
eval LOGPIPE=\" 2\>\&1 \| logger -t wr-switch -p $WRS_LOG\"
fi
# If custom boot script is not enabled, exit
if [ ! "$CONFIG_CUSTOM_BOOT_SCRIPT_ENABLED" = "y" ]; then
echo "disabled"
echo "disabled" > "$custom_boot_status_file"
exit;
fi
if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL" = "y" ]; then
echo "local" > "$custom_boot_source_file"
execute_script=/wr/bin/custom_boot_script.sh
fi
if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE" = "y" ]; then
echo "remote" > "$custom_boot_source_file"
# replace IPADDR and MACADDR, to have a device-specific name
macaddr=$(cat /sys/class/net/eth0/address)
ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d ' -f 1)
if [ -z "$ipaddr" ]; then
eval echo "$0: Warning no IP set!" $LOGPIPE
fi
host_name=`hostname`
URL=$(echo $CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE_URL | \
sed -e s/MACADDR/$macaddr/ -e s/IPADDR/$ipaddr/ -e s/HOSTNAME/$host_name/)
# split the parts, as we need to handle tftp by hand
proto=$(echo $URL | cut -d: -f 1)
host=$(echo $URL | cut -d/ -f 3)
filename=$(echo $URL | cut -d/ -f 4-)
# save URL of custom boot script, to be used by SNMPd
echo "$URL" > "$custom_boot_source_url_file"
rm -f "$tmpscript"
case $proto in
http|ftp)
wget $URL -O "$tmpscript"
;;
tftp)
tftp -g -r "$filename" -l "$tmpscript" "$host"
;;
*)
echo "Invalid URL for custom boot script: \"$URL\"" >& 2
;;
esac
if [ ! -f "$tmpscript" ]; then
print_error "Download error!"
echo "download_error" > "$custom_boot_status_file"
exit
fi
execute_script="$tmpscript"
chmod +x "$execute_script"
fi
if [ -z "$execute_script" ]; then
print_error "Please specify local or remote in dot-config!"
echo "wrong_source" > "$custom_boot_source_file"
echo "wrong_source" > "$custom_boot_status_file"
exit
fi
if [ ! -f "$execute_script" ]; then
print_error "$execute_script not found!"
exit
fi
# redirect output of script to OTHER_LOG
eval "$execute_script" start $LOGPIPE
ret=$?
if [ $ret -eq 0 ]; then
echo "OK"
echo "ok" > "$custom_boot_status_file"
else
echo "Failed"
echo "failed" > "$custom_boot_status_file"
fi
}
stop() {
if [ -f $dotconfig ]; then
. $dotconfig
else
echo "$0 unable to source dot-config ($dotconfig)!"
fi
# set log destination
WRS_LOG=$CONFIG_WRS_LOG_OTHER
# if empty turn it to /dev/null
if [ -z $WRS_LOG ]; then
WRS_LOG="/dev/null";
fi
# if a pathname, use it
if echo "$WRS_LOG" | grep / > /dev/null; then
eval LOGPIPE=\" \> $WRS_LOG 2\>\&1 \";
else
# not a pathname: use verbatim
eval LOGPIPE=\" 2\>\&1 \| logger -t wr-switch -p $WRS_LOG\"
fi
if [ ! "$CONFIG_CUSTOM_BOOT_SCRIPT_ENABLED" = "y" ]; then
exit;
fi
echo -n "Stopping custom boot script: "
if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL" = "y" ]; then
execute_script=/wr/bin/custom_boot_script.sh
fi
if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE" = "y" ]; then
execute_script="$tmpscript"
fi
if [ -z "$execute_script" ]; then
print_error "Please specify local or remote in dot-config!"
exit
fi
if [ ! -f "$execute_script" ]; then
print_error "$execute_script not found!"
exit
fi
# redirect output of script to OTHER_LOG
eval "$execute_script" stop $LOGPIPE
ret=$?
if [ $ret -eq 0 ]; then
echo "OK"
else
echo "Failed"
fi
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
../init.d/wrs_custom_boot_script.sh
\ No newline at end of file
#!/bin/sh
# Adam Wujek, CERN 2016
#
# Example of a custom script to be used at boot time.
start() {
echo "$0: start"
}
stop() {
echo "$0: stop"
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment