Fix set -eu and pipefail failures

Fixes problems found by set -eu and pipefail, including:
-Many unset variables
-Commands that can fail under normal circumstances, which breaks
with set -e.  This change swallows those expected errors to allow
our existing error code to handle them.
-The dkms element was not finding Fedora kernel versions correctly.
This may be an issue for other distros too, but since Fedora was
working fine without this functionality I only changed it to print
a warning message rather than failing the build when it happens.
-The ramdisk init script will not be set -eu because if it fails
the result is a kernel panic, which can be tricky to debug.
However, in testing with set -e a few failing commands were found
and have been fixed in this patch.

Change-Id: I44cf98dfc80cfcaec54b88cc83be80a3dbf2cec3
This commit is contained in:
Ben Nemec 2014-04-03 17:49:18 -05:00
parent 66445debb5
commit 381ff6ab1d
15 changed files with 39 additions and 14 deletions

View File

@ -26,9 +26,9 @@ RAMDISK=
if [ -f $TARGET_ROOT/etc/redhat-release ]; then
# Prioritize PAE if present
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep PAE | grep -v debug | head -1)
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep PAE | grep -v debug | head -1 || echo "")
if [ ! $KERNEL ]; then
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep -v debug | head -1)
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep -v debug | head -1 || echo "")
if [ ! $KERNEL ]; then
echo "No suitable kernel found."
exit 1
@ -38,7 +38,7 @@ if [ -f $TARGET_ROOT/etc/redhat-release ]; then
KERNEL=$(basename $KERNEL)
KERNEL_VERSION=`echo $KERNEL | sed 's/vmlinuz-//g'`
RAMDISK=$(basename `ls $BOOTDIR/initramfs-$KERNEL_VERSION.img`)
RAMDISK=$(basename `ls $BOOTDIR/initramfs-$KERNEL_VERSION.img` || echo "")
if [ ! $RAMDISK ]; then
echo "Can't find an initramfs for the $KERNEL_VERSION version of the kernel."
exit 1

View File

@ -25,7 +25,7 @@ SPECIFIED_ELEMS[0]=""
# List of all env vars to set install types
PREFIX="DIB_INSTALLTYPE_"
INSTALL_TYPE_VARS=$(env | grep ^$PREFIX | cut -d'=' -f1)
INSTALL_TYPE_VARS=$(env | grep ^$PREFIX | cut -d'=' -f1 || echo "")
for _install_type_var in $INSTALL_TYPE_VARS; do

View File

@ -23,6 +23,7 @@ set -e
url=$1
dest=$2
time_cond=
mkdir -p $(dirname $dest)
tmp=$(mktemp $(dirname $dest)/.download.XXXXXXXX)

View File

@ -59,7 +59,7 @@ fi
# parallelized later
# Note: -maxdepth 1 ensures only files in the target directory (but not
# subdirectories) are run, which is the way run-parts behaves.
targets=$(find $target_dir -maxdepth 1 -xtype f -executable -printf '%f\n' | grep -E "$allowed_regex" | LANG=C sort -n)
targets=$(find $target_dir -maxdepth 1 -xtype f -executable -printf '%f\n' | grep -E "$allowed_regex" | LANG=C sort -n || echo "")
if [ "$show_list" == "1" ] ; then
for target in $targets ; do

View File

@ -3,7 +3,14 @@
# to ensure we have valid modules build for all.
modules=$(dkms status | tr ',:' ' ' | awk '{ print $1 "/" $2 }')
kernels=$(ls /usr/src/linux-headers-*-*-* -d | sed -e 's|/usr/src/linux-headers-||')
kernels=$(ls /usr/src/linux-headers-*-*-* -d | sed -e 's|/usr/src/linux-headers-||' || echo "")
# NOTE(bnemec): On Fedora, the versions can be found in /usr/src/kernels
if [ -z "$kernels" ]; then
kernels=$(ls /usr/src/kernels/* -d | sed -e 's|/usr/src/kernels/||' || echo "")
fi
if [ -z "$kernels" ]; then
echo "Warning: No kernel versions found for DKMS"
fi
__ARCH=$ARCH
unset ARCH

View File

@ -21,8 +21,8 @@ set -e
install_deb_packages () {
DEBIAN_FRONTEND=noninteractive \
http_proxy=$http_proxy https_proxy=$https_proxy \
no_proxy=$no_proxy \
http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
no_proxy=${no_proxy:-} \
apt-get --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
}

View File

@ -5,7 +5,7 @@ set -e
[ -n "$TARGET_ROOT" ]
# If we have a network proxy, use it.
if [ -n "$http_proxy" ] ; then
if [ -n "${http_proxy:-}" ] ; then
sudo dd of=$TARGET_ROOT/etc/apt/apt.conf.d/60img-build-proxy << _EOF_
Acquire::http::Proxy "$http_proxy";
_EOF_

View File

@ -5,6 +5,12 @@
set -e
set -o xtrace
have_apt=
have_yum=
have_zypper=
http_proxy=${http_proxy:-""}
https_proxy=${https_proxy:-""}
if [ -d /etc/apt ] ; then
have_apt=1
fi

View File

@ -60,7 +60,7 @@ else
fi
# udev versions 176 and newer require a different on-disk setup
UDEVD_VERSION=$($UDEVD --version)
UDEVD_VERSION=$(udevadm --version)
if [ "$UDEVD_VERSION" != "" -a $UDEVD_VERSION -gt 175 ]; then
echo "Using new-style udevd setup"

View File

@ -15,16 +15,19 @@ fi
readonly BOOT_INTERFACE
ifconfig lo 127.0.0.1 up
ifconfig "$BOOT_INTERFACE" up
if [ $? -ne 0 ]; then
rv=0
ifconfig "$BOOT_INTERFACE" up || rv=1
if [ rv -ne 0 ]; then
sleep 10
ifconfig "$BOOT_INTERFACE" up
rv=0
ifconfig "$BOOT_INTERFACE" up || rv=1
if [ $? -ne 0 ]; then
err_msg "Failed to ifconfig up $BOOT_INTERFACE"
troubleshoot
fi
fi
ifconfig "$BOOT_INTERFACE" "$BOOT_IP_ADDRESS" netmask "$BOOT_NETMASK"
route del default || true
route add default gw $BOOT_GATEWAY
echo "pinging to boot server $BOOT_SERVER"

View File

@ -17,6 +17,8 @@ if [ ! -d $TARGET_DIR ] ; then
exit 1
fi
BINARY_DEPS=
for _FILE in $(ls ${TARGET_DIR}/binary-deps.d/) ; do
_FILE="${TARGET_DIR}/binary-deps.d/${_FILE}"
if [ -a $_FILE ]; then

View File

@ -20,6 +20,9 @@ LIB_UDEV=$LIB_UDEV_ROOT/lib/udev
INIT="$_LIB/scripts/init"
FUNCTIONS_D="$_LIB/scripts/d"
BUSYBOX=${BUSYBOX:-$(which busybox)}
# NOTE(bnemec): IMAGE_ELEMENT is normally set in disk-image-create, but we're
# not using that to build the image here.
IMAGE_ELEMENT=
mk_build_dir
mkdir -p $TMP_BUILD_DIR/mnt

View File

@ -19,6 +19,7 @@ sudo partprobe $IMAGE_BLOCK_DEVICE
sudo udevadm settle
# If the partition isn't under /dev/loop*p1, create it with kpartx
DM=
if [ ! -e "${IMAGE_BLOCK_DEVICE}p1" ]; then
sudo kpartx -asv $TMP_IMAGE_PATH
DM=${IMAGE_BLOCK_DEVICE/#\/dev/\/dev\/mapper}

View File

@ -17,6 +17,8 @@
set -e
EXTRA_ARGS=
if [ "$1" = "-u" ] ; then
yum -y update
exit 0

View File

@ -110,7 +110,7 @@ function generate_hooks () {
# $1 the break point.
# $2.. what to call if a break is needed
function check_break () {
if echo "$break" | egrep -e "(,|^)$1(,|$)" -q; then
if echo "${break:-}" | egrep -e "(,|^)$1(,|$)" -q; then
echo "Starting debug shell. Exit to resume building." >&2
echo At stage $1 >&2
shift