Skip to content

Commit

Permalink
[Style] Bash Script Clean-Up
Browse files Browse the repository at this point in the history
This PR improves the style of our bash shell scripts through ShellCheck.

Commit log:
* Early termination, return issues
* Revert CI changes, update ONVM script
* Fix docker script
  • Loading branch information
kevindweb authored May 5, 2020
1 parent d0b3c43 commit fe5b253
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 68 deletions.
2 changes: 1 addition & 1 deletion examples/NFD/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ if [ ! -f ../../start_nf.sh ]; then
exit 1
fi

../../start_nf.sh $NF_DIR "$@"
../../start_nf.sh "$NF_DIR" "$@"
6 changes: 3 additions & 3 deletions examples/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ if [ ! -f ../start_nf.sh ]; then
fi

SCRIPT=$(readlink -f "$0")
MANAGER_PATH=$(dirname $(dirname "$SCRIPT"))
MANAGER_PATH=$(dirname "$(dirname "$SCRIPT")")

if [[ -z $(ps ww -u root | grep "$MANAGER_PATH/onvm/onvm_mgr/$RTE_TARGET/onvm_mgr") ]]
if [[ -z $(pgrep -u root -f "$MANAGER_PATH/onvm/onvm_mgr/$RTE_TARGET/onvm_mgr") ]]
then
echo "NF cannot start without a running manager"
exit 1
fi

../start_nf.sh $NF_DIR "$@"
../start_nf.sh "$NF_DIR" "$@"
14 changes: 8 additions & 6 deletions examples/start_nf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
NF_NAME=$1
NF_PATH=$SCRIPTPATH/$NF_NAME
# For NFD NF
# For NFD NF
NF_NAME=${NF_PATH##*/}
BINARY=$NF_PATH/build/app/$NF_NAME
DPDK_BASE_ARGS="-n 3 --proc-type=secondary"
# For simple mode, only used for initial dpdk startup
DEFAULT_CORE_ID=0

if [ ! -f $BINARY ]; then
if [ ! -f "$BINARY" ]; then
echo "ERROR: NF executable not found, $BINARY doesn't exist"
echo "Please verify NF binary name and run script from the NF folder"
exit 1
Expand All @@ -44,7 +44,7 @@ if [ "$1" = "-F" ]
then
config=$2
shift 2
exec sudo $BINARY -F $config "$@"
exec sudo "$BINARY" -F "$config" "$@"
fi

# Check if -- is present, if so parse dpdk/onvm specific args
Expand All @@ -61,8 +61,8 @@ done

# Spaces before $@ are required otherwise it swallows the first arg for some reason
if [[ $dash_dash_cnt -ge 2 ]]; then
DPDK_ARGS="$DPDK_BASE_ARGS $(echo " ""$@" | awk -F "--" '{print $1;}')"
ONVM_ARGS="$(echo " ""$@" | awk -F "--" '{print $2;}')"
DPDK_ARGS="$DPDK_BASE_ARGS $(echo " ""$*" | awk -F "--" '{print $1;}')"
ONVM_ARGS="$(echo " ""$*" | awk -F "--" '{print $2;}')"
# Move to NF arguments
shift ${non_nf_arg_cnt}
if [[ $DPDK_ARGS =~ "-l" && ! $ONVM_ARGS =~ "-m" ]]; then
Expand All @@ -82,4 +82,6 @@ elif [[ $dash_dash_cnt -eq 1 ]]; then
exit 1
fi

exec sudo $BINARY $DPDK_ARGS -- $ONVM_ARGS -- "$@"
# don't mess with variable expansion
# shellcheck disable=SC2086
exec sudo "$BINARY" $DPDK_ARGS -- $ONVM_ARGS -- "$@"
20 changes: 11 additions & 9 deletions onvm/go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ verbosity=1
# Initialize base virtual address to empty.
virt_addr=""

if [[ ! -z $(ps ww -u root | grep "$SCRIPTPATH/onvm_mgr/$RTE_TARGET/onvm_mgr" | grep -v "grep") ]]
if [[ -n $(pgrep -u root -f "$SCRIPTPATH/onvm_mgr/$RTE_TARGET/onvm_mgr") ]]
then
echo "Manager cannot be started while another is running"
exit 1
fi

shift 3

if [ -z $nf_cores ]
if [ -z "$nf_cores" ]
then
usage
fi
Expand All @@ -63,14 +63,14 @@ while getopts "a:r:d:s:t:l:p:z:cv" opt; do
case $opt in
a) virt_addr="--base-virtaddr=$OPTARG";;
r) num_srvc="-r $OPTARG";;
d) def_srvc="-d $optarg";;
d) def_srvc="-d $OPTARG";;
s) stats="-s $OPTARG";;
t) ttl="-t $OPTARG";;
l) packet_limit="-l $OPTARG";;
p) web_port="$OPTARG";;
z) stats_sleep_time="-z $OPTARG";;
c) shared_cpu_flag="-c";;
v) verbosity=$(($verbosity+1));;
v) verbosity=$((verbosity+1));;
\?) echo "Unknown option -$OPTARG" && usage
;;
esac
Expand All @@ -87,23 +87,25 @@ fi

if [ "${stats}" = "-s web" ]
then
cd ../onvm_web/
cd ../onvm_web/ || usage
if [ -n "${web_port}" ]
then
. start_web_console.sh -p "${web_port}"
else
. start_web_console.sh
fi

cd $ONVM_HOME/onvm
cd "$ONVM_HOME"/onvm || usage
fi

sudo rm -rf /mnt/huge/rtemap_*
sudo $SCRIPTPATH/onvm_mgr/$RTE_TARGET/onvm_mgr -l $cpu -n 4 --proc-type=primary ${virt_addr} -- -p ${ports} -n ${nf_cores} ${num_srvc} ${def_srvc} ${stats} ${stats_sleep_time} ${verbosity_level} ${ttl} ${packet_limit} ${shared_cpu_flag}
# watch out for variable expansion
# shellcheck disable=SC2086
sudo "$SCRIPTPATH"/onvm_mgr/"$RTE_TARGET"/onvm_mgr -l "$cpu" -n 4 --proc-type=primary ${virt_addr} -- -p ${ports} -n ${nf_cores} ${num_srvc} ${def_srvc} ${stats} ${stats_sleep_time} ${verbosity_level} ${ttl} ${packet_limit} ${shared_cpu_flag}

if [ "${stats}" = "-s web" ]
then
echo "Killing web stats running with PIDs: $ONVM_WEB_PID, $ONVM_WEB_PID2"
kill $ONVM_WEB_PID
kill $ONVM_WEB_PID2
kill "$ONVM_WEB_PID"
kill "$ONVM_WEB_PID2"
fi
6 changes: 2 additions & 4 deletions onvm_web/build_web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ cp -r react-app/build ./web-build

echo "Minifying web files"
for css in ./web-build/static/css/*.css; do
uglifycss --output $css $css
uglifycss --output "$css" "$css"
done

for js in $(find ./web-build -name '*.js'); do
uglifyjs --output $js $js
done
find ./web-build -name "*.js" -exec uglifyjs --output {} {} \;
13 changes: 6 additions & 7 deletions onvm_web/start_web_console.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ while getopts "p:" opt; do
esac
done


# Start ONVM web stats console at http://localhost:<port num>
echo -n "Starting openNetVM Web Stats Console at http://localhost:"
echo $web_port
echo "$web_port"

is_web_port_in_use=$(sudo netstat -tulpn | grep LISTEN | grep ":$web_port")
if [[ "$is_web_port_in_use" != "" ]]
then
echo "[ERROR] Web port $web_port is in use"
echo $is_web_port_in_use
echo "$is_web_port_in_use"
echo "[ERROR] Web stats failed to start"
exit 1
fi
Expand All @@ -70,15 +69,15 @@ is_data_port_in_use=$(sudo netstat -tulpn | grep LISTEN | grep ":8000")
if [[ "$is_data_port_in_use" != "" ]]
then
echo "[ERROR] Port 8000 is in use"
echo $is_data_port_in_use
echo "$is_data_port_in_use"
echo "[ERROR] Web stats failed to start"
exit 1
fi

cd $ONVM_HOME/onvm_web
cd "$ONVM_HOME"/onvm_web || usage
nohup python cors_server.py 8000 &
export ONVM_WEB_PID=$!

cd $ONVM_HOME/onvm_web/web-build
nohup python -m SimpleHTTPServer $web_port &
cd "$ONVM_HOME"/onvm_web/web-build || usage
nohup python -m SimpleHTTPServer "$web_port" &
export ONVM_WEB_PID2=$!
24 changes: 13 additions & 11 deletions scripts/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ while getopts :d:c:D:h:o:n: OPTION ; do
h) HUGE=${OPTARG} ;;
o) ONVM=${OPTARG} ;;
n) NAME=${OPTARG} ;;
\?) echo "Unknown option -$OPTARG" && exit 1
esac
done

DEVICES=()
MODE=""

if [[ "${NAME}" == "" ]] || [[ "${HUGE}" == "" ]] || [[ "${ONVM}" == "" ]] ; then
echo -e "sudo ./docker.sh -h HUGEPAGES -o ONVM -n NAME [-D DEVICES] [-d DIRECTORY] [-c COMMAND]\n"
Expand All @@ -65,35 +65,37 @@ for DEV in ${RAW_DEVICES} ; do
done

if [[ "${DIR}" != "" ]] ; then
DIR="--volume=${DIR}:/$(basename ${DIR})"
DIR="--volume=${DIR}:/$(basename "${DIR}")"
fi

#shellcheck disable=SC2086
if [[ "${CMD}" == "" ]] ; then
sudo docker run \
--interactive --tty \
--privileged \
--name=${NAME} \
--name="${NAME}" \
--hostname="${NAME}" \
--network bridge \
--volume=/var/run:/var/run \
--volume=${HUGE}:${HUGE} \
--volume=${ONVM}:/openNetVM \
--volume="${HUGE}":"${HUGE}" \
--volume="${ONVM}":/openNetVM \
${DIR} \
${DEVICES[@]} \
"${DEVICES[@]}" \
sdnfv/opennetvm \
/bin/bash
else
sudo docker run \
--detach=true \
--privileged \
--name=${NAME} \
--name="${NAME}" \
--hostname="${NAME}" \
--network bridge \
--volume=/var/run:/var/run \
--volume=${HUGE}:${HUGE} \
--volume=${ONVM}:/openNetVM \
--volume="${HUGE}":"${HUGE}" \
--volume="${ONVM}":/openNetVM \
${DIR} \
${DEVICES[@]} \
"${DEVICES[@]}" \
sdnfv/opennetvm \
/bin/bash -c "${CMD}"
fi


7 changes: 3 additions & 4 deletions scripts/dpdk_helper_scripts.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

HUGEPGSZ=`cat /proc/meminfo | grep Hugepagesize | cut -d : -f 2 | tr -d ' '`
HUGEPGSZ=$(grep Hugepagesize /proc/meminfo | cut -d : -f 2 | tr -d ' ')

#
# Unloads igb_uio.ko.
Expand Down Expand Up @@ -75,7 +75,6 @@ set_numa_pages()

echo > .echo_tmp
for d in /sys/devices/system/node/node? ; do
node=$(basename $d)
echo "echo $hp_count > $d/hugepages/hugepages-${HUGEPGSZ}/nr_hugepages" >> .echo_tmp
done
echo "Reserving hugepages"
Expand All @@ -84,8 +83,8 @@ set_numa_pages()

create_mnt_huge

hp_free=$(cat /proc/meminfo | grep HugePages_Free | awk '{print $2}')
if [ $hp_free == "0" ]; then
hp_free=$(grep HugePages_Free /proc/meminfo | awk '{print $2}')
if [ "$hp_free" == "0" ]; then
echo "Coudn't set up huge pages. Did you try turning it off and on again?"
exit 1
else
Expand Down
22 changes: 12 additions & 10 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fi
sudo -v

# Ensure we're working relative to the onvm root directory
if [ $(basename $(pwd)) == "scripts" ]; then
if [ "$(basename "$(pwd)")" == "scripts" ]; then
cd ..
fi

Expand All @@ -83,33 +83,35 @@ if [ -z "$ONVM_HOME" ]; then
fi

# Source DPDK helper functions
. $ONVM_HOME/scripts/dpdk_helper_scripts.sh
. "$ONVM_HOME"/scripts/dpdk_helper_scripts.sh

set +e
remove_igb_uio_module
set -e

# Compile dpdk
cd $RTE_SDK
cd "$RTE_SDK"
echo "Compiling and installing dpdk in $RTE_SDK"

# Adding ldflags.txt output for mTCP compatibility
if grep "ldflags.txt" $RTE_SDK/mk/rte.app.mk > /dev/null
if grep "ldflags.txt" "$RTE_SDK"/mk/rte.app.mk > /dev/null
then
:
else
sed -i -e 's/O_TO_EXE_STR =/\$(shell if [ \! -d \${RTE_SDK}\/\${RTE_TARGET}\/lib ]\; then mkdir -p \${RTE_SDK}\/\${RTE_TARGET}\/lib\; fi)\nLINKER_FLAGS = \$(call linkerprefix,\$(LDLIBS))\n\$(shell echo \${LINKER_FLAGS} \> \${RTE_SDK}\/\${RTE_TARGET}\/lib\/ldflags\.txt)\nO_TO_EXE_STR =/g' $RTE_SDK/mk/rte.app.mk
# want to use single quotes for sed operation
# shellcheck disable=SC2016
sed -i -e 's/O_TO_EXE_STR =/\$(shell if [ \! -d \${RTE_SDK}\/\${RTE_TARGET}\/lib ]\; then mkdir -p \${RTE_SDK}\/\${RTE_TARGET}\/lib\; fi)\nLINKER_FLAGS = \$(call linkerprefix,\$(LDLIBS))\n\$(shell echo \${LINKER_FLAGS} \> \${RTE_SDK}\/\${RTE_TARGET}\/lib\/ldflags\.txt)\nO_TO_EXE_STR =/g' "$RTE_SDK"/mk/rte.app.mk
fi

sleep 1
make config T=$RTE_TARGET
make T=$RTE_TARGET -j 8
make install T=$RTE_TARGET -j 8
make config T="$RTE_TARGET"
make T="$RTE_TARGET" -j 8
make install T="$RTE_TARGET" -j 8

# Refresh sudo
sudo -v

cd $start_dir
cd "$start_dir"

# Setup/Check for free HugePages if the user wants to
if [ -z "$ONVM_SKIP_HUGEPAGES" ]; then
Expand All @@ -118,7 +120,7 @@ fi

grep -m 1 "huge" /etc/fstab | cat
# Only add to /etc/fstab if user wants it
if [ ${PIPESTATUS[0]} != 0 ] && [ -z "$ONVM_SKIP_FSTAB" ]; then
if [ "${PIPESTATUS[0]}" != 0 ] && [ -z "$ONVM_SKIP_FSTAB" ]; then
echo "Adding huge fs to /etc/fstab"
sleep 1
sudo sh -c "echo \"huge /mnt/huge hugetlbfs defaults 0 0\" >> /etc/fstab"
Expand Down
4 changes: 2 additions & 2 deletions scripts/no_hyperthread.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ CPUS_TO_SKIP=" $(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list

# Disable Hyperthreading
for CPU_PATH in /sys/devices/system/cpu/cpu[0-9]*; do
CPU="$(echo $CPU_PATH | tr -cd "0-9")"
CPU="$(echo "$CPU_PATH" | tr -cd "0-9")"
echo "$CPUS_TO_SKIP" | grep " $CPU " > /dev/null
if [ $? -ne 0 ]; then
echo 0 > $CPU_PATH/online
echo 0 > "$CPU_PATH"/online
fi
done

Expand Down
Loading

0 comments on commit fe5b253

Please sign in to comment.