shellcheck: fix issues and format with beautysh
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ib06266cbe243a3e70a4c468c0d4d2c4d2b1f1914
diff --git a/firstboot/first-boot-set-hostname.sh b/firstboot/first-boot-set-hostname.sh
index eaabb0a..c238962 100644
--- a/firstboot/first-boot-set-hostname.sh
+++ b/firstboot/first-boot-set-hostname.sh
@@ -1,4 +1,4 @@
-#!/bin/sh -eu
+#!/bin/bash -eu
show_error() {
if [ -n "${JOURNAL_STREAM-}" ]; then
@@ -17,24 +17,21 @@
INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset'
BMC_SN=''
BMC_ITEM_PATH=$(busctl --no-pager --verbose call \
- ${MAPPER_IFACE} \
- ${MAPPER_PATH} \
- ${MAPPER_IFACE} \
- GetSubTree sias \
- ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \
- 2>/dev/null | grep ${INVENTORY_PATH} || true)
+ ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
+ GetSubTree sias \
+ ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \
+ 2>/dev/null | grep ${INVENTORY_PATH} || true)
# ' STRING "/xyz/openbmc_project/inventory/system/chassis/bmc";'
BMC_ITEM_PATH=${BMC_ITEM_PATH#*\"}
BMC_ITEM_PATH=${BMC_ITEM_PATH%\"*}
BMC_ITEM_SERVICE=$(mapper get-service \
- ${BMC_ITEM_PATH} 2>/dev/null || true)
+ "${BMC_ITEM_PATH}" 2>/dev/null || true)
if [[ -n "${BMC_ITEM_SERVICE}" ]]; then
- BMC_SN=$(busctl get-property ${BMC_ITEM_SERVICE} \
- ${BMC_ITEM_PATH} \
- ${INV_ASSET_IFACE} SerialNumber)
+ BMC_SN=$(busctl get-property "${BMC_ITEM_SERVICE}" \
+ "${BMC_ITEM_PATH}" "${INV_ASSET_IFACE}" SerialNumber)
# 's "002B0DH1000"'
BMC_SN=${BMC_SN#*\"}
BMC_SN=${BMC_SN%\"*}
@@ -47,34 +44,31 @@
NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface'
NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
- ${MAPPER_IFACE} \
- ${MAPPER_PATH} \
- ${MAPPER_IFACE} \
- GetSubTree sias \
- ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
- 2>/dev/null | grep ${INVENTORY_PATH} || true)
+ ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
+ GetSubTree sias \
+ ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+ 2>/dev/null | grep ${INVENTORY_PATH} || true)
NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
- NETWORK_ITEM_OBJ=$(mapper get-service ${NETWORK_ITEM_PATH} 2>/dev/null || true)
+ NETWORK_ITEM_OBJ=$(mapper get-service "${NETWORK_ITEM_PATH}" 2>/dev/null || true)
if [[ -z "${NETWORK_ITEM_OBJ}" ]]; then
show_error 'No Ethernet interface found in the Inventory. Unique hostname not set!'
exit 1
fi
- MAC_ADDR=$(busctl get-property ${NETWORK_ITEM_OBJ} \
- ${NETWORK_ITEM_PATH} \
- ${NETWORK_ITEM_IFACE} MACAddress)
+ MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_OBJ}" \
+ "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress)
# 's "54:52:01:02:03:04"'
MAC_ADDR=${MAC_ADDR#*\"}
MAC_ADDR=${MAC_ADDR%\"*}
- hostnamectl set-hostname $(hostname)-${MAC_ADDR}
+ hostnamectl set-hostname "$(hostname)-${MAC_ADDR}"
else
- hostnamectl set-hostname $(hostname)-${BMC_SN}
+ hostnamectl set-hostname "$(hostname)-${BMC_SN}"
fi
}
diff --git a/firstboot/first-boot-set-mac.sh b/firstboot/first-boot-set-mac.sh
index 63a4ad7..1a1ae22 100644
--- a/firstboot/first-boot-set-mac.sh
+++ b/firstboot/first-boot-set-mac.sh
@@ -1,4 +1,4 @@
-#!/bin/sh -eu
+#!/bin/bash -eu
show_error() {
if [ -n "${JOURNAL_STREAM-}" ]; then
@@ -17,31 +17,25 @@
# Get the NETWORK ITEM count
NETWORK_ITEM_PATH_COUNT=$(busctl --no-pager --verbose call \
- ${MAPPER_IFACE} \
- ${MAPPER_PATH} \
- ${MAPPER_IFACE} \
- GetSubTree sias \
- ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
- 2>/dev/null | grep ${INVENTORY_PATH} | wc -l || true)
+ ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
+ GetSubTree sias \
+ ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+ 2>/dev/null | grep -c "${INVENTORY_PATH}" || true)
- if [ $NETWORK_ITEM_PATH_COUNT -gt 1 ]; then
+ if [ "$NETWORK_ITEM_PATH_COUNT" -gt 1 ]; then
# If there are more than 2 NETOWRK ITEM and path must contain $1
# for finding the right NETWORK ITEM
NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
- ${MAPPER_IFACE} \
- ${MAPPER_PATH} \
- ${MAPPER_IFACE} \
- GetSubTree sias \
- ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
- 2>/dev/null | grep ${INVENTORY_PATH} | grep $1 || true)
+ ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
+ GetSubTree sias \
+ ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+ 2>/dev/null | grep ${INVENTORY_PATH} | grep "$1" || true)
else
NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
- ${MAPPER_IFACE} \
- ${MAPPER_PATH} \
- ${MAPPER_IFACE} \
- GetSubTree sias \
- ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
- 2>/dev/null | grep ${INVENTORY_PATH} || true)
+ ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
+ GetSubTree sias \
+ ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+ 2>/dev/null | grep ${INVENTORY_PATH} || true)
fi
# ' STRING "/xyz/openbmc_project/inventory/system/chassis/ethernet";'
@@ -49,16 +43,15 @@
NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
NETWORK_ITEM_SERVICE=$(mapper get-service \
- ${NETWORK_ITEM_PATH} 2>/dev/null || true)
+ "${NETWORK_ITEM_PATH}" 2>/dev/null || true)
if [[ -z "${NETWORK_ITEM_SERVICE}" ]]; then
show_error 'No Ethernet interface found in the Inventory. Is VPD EEPROM empty?'
return
fi
- MAC_ADDR=$(busctl get-property ${NETWORK_ITEM_SERVICE} \
- ${NETWORK_ITEM_PATH} \
- ${NETWORK_ITEM_IFACE} MACAddress)
+ MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_SERVICE}" \
+ "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress)
# 's "54:52:01:02:03:04"'
MAC_ADDR=${MAC_ADDR#*\"}
@@ -66,9 +59,9 @@
if [[ -n "${MAC_ADDR}" ]]; then
busctl set-property xyz.openbmc_project.Network \
- /xyz/openbmc_project/network/$1 \
- xyz.openbmc_project.Network.MACAddress \
- MACAddress s ${MAC_ADDR}
+ "/xyz/openbmc_project/network/$1" \
+ xyz.openbmc_project.Network.MACAddress \
+ MACAddress s "${MAC_ADDR}"
fi
}
@@ -77,7 +70,7 @@
exit 1
fi
-sync_mac $1
+sync_mac "$1"
# Prevent start at next boot time
mkdir -p "/var/lib/first-boot-set-mac"
diff --git a/http-redirect/tests b/http-redirect/tests
index 870f0fa..318313e 100755
--- a/http-redirect/tests
+++ b/http-redirect/tests
@@ -7,6 +7,7 @@
# TRACEFILE="/dev/tty"
# create temp files
+# shellcheck disable=SC2064
rm -f $TMPFILE $URLFILE && touch $TMPFILE &&
trap "rm ./$TMPFILE ./$URLFILE" 0 || exit 2
echo "/over/the/rainbow /over/the/rainbow" > $URLFILE
@@ -30,12 +31,12 @@
if grep -is "$expect" $TMPFILE
then
- echo PASS $test
+ echo "PASS $test"
else
- echo FAIL $test
- echo "Expected to find >'$expect'< in :"
- cat $TMPFILE
- false
+ echo "FAIL $test"
+ echo "Expected to find >'$expect'< in :"
+ cat $TMPFILE
+ false
fi
test="2 Test no absolute-path in URI"
@@ -48,12 +49,12 @@
if grep -is "$expect" $TMPFILE
then
- echo PASS $test
+ echo "PASS $test"
else
- echo FAIL $test
- echo "Expected to find >'$expect'< in :"
- cat $TMPFILE
- false
+ echo "FAIL $test"
+ echo "Expected to find >'$expect'< in :"
+ cat $TMPFILE
+ false
fi
@@ -67,12 +68,12 @@
if grep -is "$expect" $TMPFILE
then
- echo PASS $test
+ echo "PASS $test"
else
- echo FAIL $test
- echo "Expected to find >'$expect'< in :"
- cat $TMPFILE
- false
+ echo "FAIL $test"
+ echo "Expected to find >'$expect'< in :"
+ cat $TMPFILE
+ false
fi
@@ -92,12 +93,12 @@
if grep -is "$expect" $TMPFILE
then
- echo PASS $test
+ echo "PASS $test"
else
- echo FAIL $test
- echo "Expected to find >'$expect'< in :"
- cat $TMPFILE
- false
+ echo "FAIL $test"
+ echo "Expected to find >'$expect'< in :"
+ cat $TMPFILE
+ false
fi
diff --git a/usb-ctrl/usb-ctrl b/usb-ctrl/usb-ctrl
index 7cc02c0..3a1f2f9 100644
--- a/usb-ctrl/usb-ctrl
+++ b/usb-ctrl/usb-ctrl
@@ -1,77 +1,79 @@
-#!/bin/sh
+#!/bin/bash
+set -e
setup_image()
{
- set -x
- local storage="$1"
- local sz_mb="$2"
- # create the backing store
- dd if=/dev/zero of="$storage" bs=1M seek="$sz_mb" count=0 2>/dev/null
- # this shows up as 23FC-F676 in /dev/disk/by-uuid
- local diskid=0x23FCF676
- mkdosfs -n 'OPENBMC-FW' -i $diskid -I "$storage" >/dev/null 2>&1
+ set -x
+ local storage="$1"
+ local sz_mb="$2"
+ # create the backing store
+ dd if=/dev/zero of="$storage" bs=1M seek="$sz_mb" count=0 2>/dev/null
+ # this shows up as 23FC-F676 in /dev/disk/by-uuid
+ local diskid=0x23FCF676
+ mkdosfs -n 'OPENBMC-FW' -i $diskid -I "$storage" >/dev/null 2>&1
}
mount_image()
{
- set -x
- local storage="$1"
- local stormnt="$2"
- mkdir -p $stormnt || exit 1
- mount -o loop -t vfat "$storage" "$stormnt"
+ set -x
+ local storage="$1"
+ local stormnt="$2"
+ mkdir -p "$stormnt" || exit 1
+ mount -o loop -t vfat "$storage" "$stormnt"
}
cleanup_image()
{
- set -x
- local storage="$1"
- local stormnt="$2"
- umount -f "$stormnt"
- rm -f "$storage"
- rmdir "$stormnt"
+ set -x
+ local storage="$1"
+ local stormnt="$2"
+ umount -f "$stormnt"
+ rm -f "$storage"
+ rmdir "$stormnt"
}
network()
{
- set -x
- local type="$1"
- local name="$2"
- local on="$3"
- local bmc_mac="$4"
- local host_mac="$5"
- if [ "$on" = "on" ]; then
- usb_insert "${name}" "${type}" "${bmc_mac}" "${host_mac}"
- elif [ "$on" = "off" ]; then
- usb_eject "${name}" "${type}"
- else
- echo "Unknown ${type} command"
- usage
- fi
+ set -x
+ local type="$1"
+ local name="$2"
+ local on="$3"
+ local bmc_mac="$4"
+ local host_mac="$5"
+ if [ "$on" = "on" ]; then
+ usb_insert "${name}" "${type}" "${bmc_mac}" "${host_mac}"
+ elif [ "$on" = "off" ]; then
+ usb_eject "${name}" "${type}"
+ else
+ echo "Unknown ${type} command"
+ usage
+ fi
}
GADGET_BASE=/sys/kernel/config/usb_gadget
which_dev()
{
- local in_use="$(cat $GADGET_BASE/*/UDC)"
- cd /sys/class/udc
- for D in *; do
- case "$in_use" in
- *"$D"*) ;;
- *) echo "$D"; return 0;;
- esac
- done
- return 1
+ local in_use
+ in_use="$(cat $GADGET_BASE/*/UDC)"
+ cd /sys/class/udc
+ for D in *; do
+ case "$in_use" in
+ *"$D"*) ;;
+ *) echo "$D"; return 0 ;;
+ esac
+ done
+ return 1
}
usb_ms_insert()
{
- usb_insert "$1" mass_storage "$2" "$3"
+ usb_insert "$1" mass_storage "$2" "$3"
}
usb_ms_eject()
{
- usb_eject "$1" mass_storage
+ usb_eject "$1" mass_storage
}
## $1: device syspath to provide usb-gadget configure,
@@ -81,49 +83,49 @@
## if $2 not specified or illegal, then using 'usb-ro' as default
usb_set_interface_type()
{
- local usb_gadget_syspath="$1"
- local interface_type="${2:-'usb-ro'}"
+ local usb_gadget_syspath="$1"
+ local interface_type="${2:-'usb-ro'}"
- # defining target variables to configure interface type
- local removable=
- local ro=
- local cdrom=
+ # defining target variables to configure interface type
+ local removable=
+ local ro=
+ local cdrom=
- if [ ! -d "${usb_gadget_syspath}" ]; then
- echo "Device syspath ${usb_gadget_syspath} does not exist" >&2
- return 1
- fi
+ if [ ! -d "${usb_gadget_syspath}" ]; then
+ echo "Device syspath ${usb_gadget_syspath} does not exist" >&2
+ return 1
+ fi
- case "${interface_type}" in
- hdd)
- removable=0
- ro=0
- cdrom=0
- ;;
- usb)
- removable=1
- ro=0
- cdrom=0
- ;;
+ case "${interface_type}" in
+ hdd)
+ removable=0
+ ro=0
+ cdrom=0
+ ;;
+ usb)
+ removable=1
+ ro=0
+ cdrom=0
+ ;;
- cdrom)
- removable=1
- ro=1
- cdrom=1
- ;;
- usb-ro)
- removable=1
- ro=1
- cdrom=0
- ;;
- *)
- echo "Unknown mass-storage interface type '${interface_type}' specified" >&2
- echo "Configuring interface type as 'usb-ro'" >&2
- removable=1
- ro=1
- cdrom=0
- ;;
- esac
+ cdrom)
+ removable=1
+ ro=1
+ cdrom=1
+ ;;
+ usb-ro)
+ removable=1
+ ro=1
+ cdrom=0
+ ;;
+ *)
+ echo "Unknown mass-storage interface type '${interface_type}' specified" >&2
+ echo "Configuring interface type as 'usb-ro'" >&2
+ removable=1
+ ro=1
+ cdrom=0
+ ;;
+ esac
echo $removable > "${usb_gadget_syspath}/removable"
echo $ro > "${usb_gadget_syspath}/ro"
@@ -143,112 +145,115 @@
## it is the optional MAC address for the Host (default a random MAC)
usb_insert()
{
- local name="$1"
- local dev_type="$2"
- local storage="$3"
- local intf_type="$4"
- local bmc_mac="$3"
- local host_mac="$4"
+ local name="$1"
+ local dev_type="$2"
+ local storage="$3"
+ local intf_type="$4"
+ local bmc_mac="$3"
+ local host_mac="$4"
- if [ -d "$GADGET_BASE/${name}" ]; then
- echo "Device ${name} already exists" >&2
- return 1
- fi
- mkdir "$GADGET_BASE/${name}"
- cd "$GADGET_BASE/${name}"
+ if [ -d "$GADGET_BASE/${name}" ]; then
+ echo "Device ${name} already exists" >&2
+ return 1
+ fi
+ mkdir "$GADGET_BASE/${name}"
+ cd "$GADGET_BASE/${name}"
- echo 0x1d6b > idVendor # Linux Foundation
- echo 0x0105 > idProduct # FunctionFS Gadget
- mkdir strings/0x409
- local machineid=$(cat /etc/machine-id)
- local data="OpenBMC USB gadget device serial number"
- local serial=$( echo -n "${machineid}${data}${machineid}" | \
- sha256sum | cut -b 0-12 )
- echo $serial > strings/0x409/serialnumber
- echo OpenBMC > strings/0x409/manufacturer
- echo "OpenBMC USB Device" > strings/0x409/product
+ echo 0x1d6b > idVendor # Linux Foundation
+ echo 0x0105 > idProduct # FunctionFS Gadget
+ mkdir strings/0x409
+ local machineid
+ machineid=$(cat /etc/machine-id)
+ local data="OpenBMC USB gadget device serial number"
+ local serial
+ serial=$( echo -n "${machineid}${data}${machineid}" | \
+ sha256sum | cut -b 0-12 )
+ echo "$serial" > strings/0x409/serialnumber
+ echo "OpenBMC" > strings/0x409/manufacturer
+ echo "OpenBMC USB Device" > strings/0x409/product
- gadget_function="functions/${dev_type}.${name}"
- mkdir configs/c.1
- mkdir "${gadget_function}"
- case "${dev_type}" in
- mass_storage)
- # usb_set_interface_type handles default and unknown type
- usb_set_interface_type "${gadget_function}/lun.0" "${intf_type}"
- echo "${storage}" > "${gadget_function}/lun.0/file"
- ;;
- ecm|eem|rndis)
- echo "${bmc_mac}" > "${gadget_function}/dev_addr"
- echo "${host_mac}" > "${gadget_function}/host_addr"
- ;;
- esac
- mkdir configs/c.1/strings/0x409
+ gadget_function="functions/${dev_type}.${name}"
+ mkdir configs/c.1
+ mkdir "${gadget_function}"
+ case "${dev_type}" in
+ mass_storage)
+ # usb_set_interface_type handles default and unknown type
+ usb_set_interface_type "${gadget_function}/lun.0" "${intf_type}"
+ echo "${storage}" > "${gadget_function}/lun.0/file"
+ ;;
+ ecm|eem|rndis)
+ echo "${bmc_mac}" > "${gadget_function}/dev_addr"
+ echo "${host_mac}" > "${gadget_function}/host_addr"
+ ;;
+ esac
+ mkdir configs/c.1/strings/0x409
- echo "Conf 1" > configs/c.1/strings/0x409/configuration
- echo 120 > configs/c.1/MaxPower
- ln -s "${gadget_function}" configs/c.1
- local dev=$(which_dev)
- echo $dev > UDC
+ echo "Conf 1" > configs/c.1/strings/0x409/configuration
+ echo 120 > configs/c.1/MaxPower
+ ln -s "${gadget_function}" configs/c.1
+ local dev
+ dev=$(which_dev)
+ echo "$dev" > UDC
}
## $1: device name, e.g. usb0, usb1
## $2: device type defined in kernel, e.g. mass_storage, ecm, eem, rndis
usb_eject()
{
- local name="$1"
- local dev_type="$2"
+ local name="$1"
+ local dev_type="$2"
- echo '' > "$GADGET_BASE/${name}/UDC"
+ echo '' > "$GADGET_BASE/${name}/UDC"
- rm -f "$GADGET_BASE/${name}/configs/c.1/${dev_type}.${name}"
- rmdir "$GADGET_BASE/${name}/configs/c.1/strings/0x409"
- rmdir "$GADGET_BASE/${name}/configs/c.1"
- rmdir "$GADGET_BASE/${name}/functions/${dev_type}.${name}"
- rmdir "$GADGET_BASE/${name}/strings/0x409"
- rmdir "$GADGET_BASE/${name}"
+ rm -f "$GADGET_BASE/${name}/configs/c.1/${dev_type}.${name}"
+ rmdir "$GADGET_BASE/${name}/configs/c.1/strings/0x409"
+ rmdir "$GADGET_BASE/${name}/configs/c.1"
+ rmdir "$GADGET_BASE/${name}/functions/${dev_type}.${name}"
+ rmdir "$GADGET_BASE/${name}/strings/0x409"
+ rmdir "$GADGET_BASE/${name}"
}
usage()
{
- echo "Usage: $0 <action> ..."
- echo " $0 setup <file> <sizeMB>"
- echo " $0 insert <name> <file> [<type=usb|usb-ro|hdd|cdrom>]"
- echo " $0 eject <name>"
- echo " $0 mount <file> <mnt>"
- echo " $0 cleanup <file> <mnt>"
- echo " $0 ecm <name> <on|off> [<bmc-mac-address|.> [<host-mac-address>]]"
- echo " $0 eem <name> <on|off> [<bmc-mac-address|.> [<host-mac-address>]]"
- echo " $0 rndis <name> <on|off> [<bmc-mac-address|.> [<host-mac-address>]]"
- exit 1
+ echo "Usage: $0 <action> ..."
+ echo " $0 setup <file> <sizeMB>"
+ echo " $0 insert <name> <file> [<type=usb|usb-ro|hdd|cdrom>]"
+ echo " $0 eject <name>"
+ echo " $0 mount <file> <mnt>"
+ echo " $0 cleanup <file> <mnt>"
+ echo " $0 ecm <name> <on|off> [<bmc-mac-address|.> [<host-mac-address>]]"
+ echo " $0 eem <name> <on|off> [<bmc-mac-address|.> [<host-mac-address>]]"
+ echo " $0 rndis <name> <on|off> [<bmc-mac-address|.> [<host-mac-address>]]"
+ exit 1
}
-echo "$#: $0 $@"
+echo "$#: $0 $*"
case "$1" in
- insert)
- shift
- usb_ms_insert "$@"
- ;;
- eject)
- shift
- usb_ms_eject "$@"
- ;;
- setup)
- shift
- setup_image "$@"
- ;;
- mount)
- shift
- mount_image "$@"
- ;;
- cleanup)
- shift
- cleanup_image "$@"
- ;;
- ecm|eem|rndis)
- network "$@"
- ;;
- *)
- usage
- ;;
+ insert)
+ shift
+ usb_ms_insert "$@"
+ ;;
+ eject)
+ shift
+ usb_ms_eject "$@"
+ ;;
+ setup)
+ shift
+ setup_image "$@"
+ ;;
+ mount)
+ shift
+ mount_image "$@"
+ ;;
+ cleanup)
+ shift
+ cleanup_image "$@"
+ ;;
+ ecm|eem|rndis)
+ network "$@"
+ ;;
+ *)
+ usage
+ ;;
esac
exit $?