Remove use of 'which'.

Instead, either use the bash built-in of type to ensure it exists. Since
which is an external dep, things can fail oddly in a constrained
environment.

Also add a dib-lint test for this.

Change-Id: I645029f5b5bfe1198c89ce10fd3246be8636e8af
Signed-off-by: Jesse Keating <omgjlk@us.ibm.com>
This commit is contained in:
Jesse Keating 2017-05-18 11:09:32 -07:00
parent 94ab9e2e7e
commit 84d10dce57
10 changed files with 32 additions and 15 deletions

View File

@ -209,6 +209,23 @@ for i in $(find $ELEMENTS_DIR -type f \
fi fi
fi fi
# check that which calls are not used. It is not built in and is missing
# from some constrained environments
if ! excluded which; then
while read LINE
do
if [[ $LINE =~ "which " ]]; then
# Don't match:
# - explicitly ignored
# - commented
if [[ $LINE =~ (dib-lint: which|^#) ]]; then
continue
fi
error "$i : potential use of which\n -- $LINE"
fi
done < $i
fi
done done
echo "Checking indents..." echo "Checking indents..."

View File

@ -60,9 +60,9 @@ function install_grub2 {
# XXX: grub-probe on the nbd0/loop0 device returns nothing - workaround, manually # XXX: grub-probe on the nbd0/loop0 device returns nothing - workaround, manually
# specify modules. https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1073731 # specify modules. https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1073731
GRUBNAME=$(which grub-install) || echo "trying grub2-install" GRUBNAME=$(type -p grub-install) || echo "trying grub2-install"
if [ -z "$GRUBNAME" ]; then if [ -z "$GRUBNAME" ]; then
GRUBNAME=$(which grub2-install) GRUBNAME=$(type -p grub2-install)
fi fi
# If no GRUB2 is found, fallback to extlinux # If no GRUB2 is found, fallback to extlinux
@ -136,7 +136,7 @@ function install_grub2 {
echo 'GRUB_GFXPAYLOAD_LINUX=text' >>/etc/default/grub echo 'GRUB_GFXPAYLOAD_LINUX=text' >>/etc/default/grub
echo 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200 no_timer_check"' >>/etc/default/grub echo 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200 no_timer_check"' >>/etc/default/grub
echo 'GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >>/etc/default/grub echo 'GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >>/etc/default/grub
if which grub2-mkconfig >/dev/null; then if type grub2-mkconfig >/dev/null; then
GRUB_MKCONFIG="grub2-mkconfig -o $GRUB_CFG" GRUB_MKCONFIG="grub2-mkconfig -o $GRUB_CFG"
else else
GRUB_MKCONFIG="grub-mkconfig -o $GRUB_CFG" GRUB_MKCONFIG="grub-mkconfig -o $GRUB_CFG"

View File

@ -46,7 +46,7 @@ mv "$TMP_MOUNT_PATH/init" "$MODULE_PATH/80deploy-ramdisk/init.sh"
# been released for all of our supported platforms we can remove this. Until then # been released for all of our supported platforms we can remove this. Until then
# this makes --include work correctly and will be a noop if we're running a fixed # this makes --include work correctly and will be a noop if we're running a fixed
# Dracut version. # Dracut version.
sed -i 's|cp --reflink=auto --sparse=auto -fa -t "$s" "$i"$|cp --reflink=auto --sparse=auto -fa -t "${initdir}/${tgt}" "$i"|g' $(which dracut) sed -i 's|cp --reflink=auto --sparse=auto -fa -t "$s" "$i"$|cp --reflink=auto --sparse=auto -fa -t "${initdir}/${tgt}" "$i"|g' $(type -p dracut)
# Notes on the options passed to Dracut: # Notes on the options passed to Dracut:
# -N: Do not build a host-specific ramdisk. We want to be able to run this ramdisk # -N: Do not build a host-specific ramdisk. We want to be able to run this ramdisk

View File

@ -20,7 +20,7 @@ function pxe_mac() {
# boot interface with no problems. # boot interface with no problems.
_mac="${BASH_REMATCH[1]//-/:}" _mac="${BASH_REMATCH[1]//-/:}"
_mac="${_mac#*:}" _mac="${_mac#*:}"
elif [[ -d /sys/firmware/efi ]] && which efibootmgr &>/dev/null; then elif [[ -d /sys/firmware/efi ]] && type efibootmgr &>/dev/null; then
# Likewise, if we booted via the network while running in UEFI mode, and # Likewise, if we booted via the network while running in UEFI mode, and
# efibootmgr is installed, we can determine the MAC address of the nic we # efibootmgr is installed, we can determine the MAC address of the nic we
# booted from. It would be good to have code that can also do this using # booted from. It would be good to have code that can also do this using

View File

@ -32,7 +32,7 @@ for _FILE in $(ls ${TARGET_DIR}/binary-deps.d/) ; do
done done
for _BIN in $BINARY_DEPS ; do for _BIN in $BINARY_DEPS ; do
_LOCATION=$(which "$_BIN" || echo "") _LOCATION=$(type -p "$_BIN" || echo "")
if [ -z "$_LOCATION" ]; then if [ -z "$_LOCATION" ]; then
echo "$_BIN is not found in PATH. Please ensure your elements install it" echo "$_BIN is not found in PATH. Please ensure your elements install it"
exit 1 exit 1

View File

@ -33,7 +33,7 @@ FIRMWARE_DIR=$MODULE_ROOT/lib/firmware
LIB_UDEV=$LIB_UDEV_ROOT/lib/udev LIB_UDEV=$LIB_UDEV_ROOT/lib/udev
INIT="$_LIB/scripts/init" INIT="$_LIB/scripts/init"
FUNCTIONS_D="$_LIB/scripts/d" FUNCTIONS_D="$_LIB/scripts/d"
BUSYBOX=${BUSYBOX:-$(which busybox)} BUSYBOX=${BUSYBOX:-$(type -p busybox)}
# NOTE(bnemec): IMAGE_ELEMENT is normally set in disk-image-create, but we're # NOTE(bnemec): IMAGE_ELEMENT is normally set in disk-image-create, but we're
# not using that to build the image here. # not using that to build the image here.
IMAGE_ELEMENT= IMAGE_ELEMENT=

View File

@ -6,7 +6,7 @@ fi
set -eu set -eu
set -o pipefail set -o pipefail
SETFILES=$(which setfiles || true) SETFILES=$(type -p setfiles || true)
if [ -e /etc/selinux/targeted/contexts/files/file_contexts -a -x "${SETFILES}" ]; then if [ -e /etc/selinux/targeted/contexts/files/file_contexts -a -x "${SETFILES}" ]; then
# get all mounpoints in the system # get all mounpoints in the system
IFS='|' read -ra SPLIT_MOUNTS <<< "$DIB_MOUNTPOINTS" IFS='|' read -ra SPLIT_MOUNTS <<< "$DIB_MOUNTPOINTS"

View File

@ -208,7 +208,7 @@ fi
for X in ${!IMAGE_TYPES[@]}; do for X in ${!IMAGE_TYPES[@]}; do
case "${IMAGE_TYPES[$X]}" in case "${IMAGE_TYPES[$X]}" in
qcow2) qcow2)
if [ -z "$(which qemu-img)" ]; then if ! type qemu-img > /dev/null 2>&1; then
echo "qcow2 output format specified but qemu-img executable not found." echo "qcow2 output format specified but qemu-img executable not found."
exit 1 exit 1
fi fi
@ -218,19 +218,19 @@ for X in ${!IMAGE_TYPES[@]}; do
IMAGE_TYPES+=('tar') IMAGE_TYPES+=('tar')
;; ;;
vhd) vhd)
if [ -z "$(which vhd-util)" ]; then if ! type vhd-util > /dev/null 2>&1; then
echo "vhd output format specified but no vhd-util executable found." echo "vhd output format specified but no vhd-util executable found."
exit 1 exit 1
fi fi
;; ;;
squashfs) squashfs)
if [ -z "$(which mksquashfs)" ]; then if ! type mksquashfs > /dev/null 2>&1; then
echo "squashfs output format specified but no mksquashfs executable found." echo "squashfs output format specified but no mksquashfs executable found."
exit 1 exit 1
fi fi
;; ;;
docker) docker)
if [ -z "$(which docker)" ]; then if ! type docker > /dev/null 2>&1; then
echo "docker output format specified but no docker executable found." echo "docker output format specified but no docker executable found."
exit 1 exit 1
fi fi
@ -244,7 +244,7 @@ done
# NOTE: fstrim is on most all recent systems. It is provided by the util-linux # NOTE: fstrim is on most all recent systems. It is provided by the util-linux
# package. # package.
if [[ -z "$(which fstrim)" ]]; then if ! type fstrim > /dev/null 2>&1; then
echo "fstrim utility is not found. This is provided by util-linux package" echo "fstrim utility is not found. This is provided by util-linux package"
echo "Please check your PATH variable is set correctly" echo "Please check your PATH variable is set correctly"
exit 1 exit 1

View File

@ -163,7 +163,7 @@ function populate_lib () {
if busybox_list | grep -v "^ip$" | grep "^$i\$" >/dev/null; then if busybox_list | grep -v "^ip$" | grep "^$i\$" >/dev/null; then
continue continue
fi fi
path=`which $i 2>/dev/null` || path=$i path=`type -p $i 2>/dev/null` || path=$i
if ! [ -x "$path" ]; then if ! [ -x "$path" ]; then
echo "$i is not found in PATH" 2>&1 echo "$i is not found in PATH" 2>&1
exit 1 exit 1

View File

@ -61,7 +61,7 @@ function build_test_image() {
test_formats="tar tgz squashfs raw qcow2 docker aci" test_formats="tar tgz squashfs raw qcow2 docker aci"
for binary in qemu-img docker mksquashfs; do for binary in qemu-img docker mksquashfs; do
if [ -z "$(which $binary)" ]; then if [ -z "$(type $binary)" ]; then
echo "Warning: No $binary binary found, cowardly refusing to run tests." echo "Warning: No $binary binary found, cowardly refusing to run tests."
exit 1 exit 1
fi fi