From 381ff6ab1d51af630ee4e8e11feff40bead0a273 Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Thu, 3 Apr 2014 17:49:18 -0500 Subject: [PATCH] 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 --- .../baremetal/cleanup.d/99-extract-kernel-and-ramdisk | 6 +++--- elements/base/extra-data.d/99-enable-install-types | 2 +- elements/cache-url/bin/cache-url | 1 + elements/dib-run-parts/bin/dib-run-parts | 2 +- elements/dkms/post-install.d/99-dkms | 9 ++++++++- elements/dpkg/bin/install-packages | 4 ++-- elements/dpkg/root.d/50-build-with-http-cache | 2 +- elements/local-config/pre-install.d/02-proxy-settings | 6 ++++++ elements/ramdisk/extra-data.d/scripts/init | 2 +- elements/ramdisk/init.d/02-start-network | 9 ++++++--- elements/ramdisk/post-install.d/01-ensure-binaries | 2 ++ elements/ramdisk/post-install.d/99-build-ramdisk | 3 +++ elements/vm/block-device.d/10-partition | 1 + elements/yum/bin/install-packages | 2 ++ lib/common-functions | 2 +- 15 files changed, 39 insertions(+), 14 deletions(-) diff --git a/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk b/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk index 764741b00..a0fc97d6a 100755 --- a/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk +++ b/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk @@ -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 diff --git a/elements/base/extra-data.d/99-enable-install-types b/elements/base/extra-data.d/99-enable-install-types index d1e097d94..ee5e66653 100755 --- a/elements/base/extra-data.d/99-enable-install-types +++ b/elements/base/extra-data.d/99-enable-install-types @@ -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 diff --git a/elements/cache-url/bin/cache-url b/elements/cache-url/bin/cache-url index 25ef9a36f..132c2704e 100755 --- a/elements/cache-url/bin/cache-url +++ b/elements/cache-url/bin/cache-url @@ -23,6 +23,7 @@ set -e url=$1 dest=$2 +time_cond= mkdir -p $(dirname $dest) tmp=$(mktemp $(dirname $dest)/.download.XXXXXXXX) diff --git a/elements/dib-run-parts/bin/dib-run-parts b/elements/dib-run-parts/bin/dib-run-parts index 6d965c16c..af5fde934 100755 --- a/elements/dib-run-parts/bin/dib-run-parts +++ b/elements/dib-run-parts/bin/dib-run-parts @@ -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 diff --git a/elements/dkms/post-install.d/99-dkms b/elements/dkms/post-install.d/99-dkms index 1e3773a6b..53911aac1 100755 --- a/elements/dkms/post-install.d/99-dkms +++ b/elements/dkms/post-install.d/99-dkms @@ -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 diff --git a/elements/dpkg/bin/install-packages b/elements/dpkg/bin/install-packages index d5ce2306f..fb62429b4 100755 --- a/elements/dpkg/bin/install-packages +++ b/elements/dpkg/bin/install-packages @@ -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 "$@" } diff --git a/elements/dpkg/root.d/50-build-with-http-cache b/elements/dpkg/root.d/50-build-with-http-cache index dc66629f1..28cedd2d5 100755 --- a/elements/dpkg/root.d/50-build-with-http-cache +++ b/elements/dpkg/root.d/50-build-with-http-cache @@ -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_ diff --git a/elements/local-config/pre-install.d/02-proxy-settings b/elements/local-config/pre-install.d/02-proxy-settings index f4050fdfe..15afdb3f7 100755 --- a/elements/local-config/pre-install.d/02-proxy-settings +++ b/elements/local-config/pre-install.d/02-proxy-settings @@ -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 diff --git a/elements/ramdisk/extra-data.d/scripts/init b/elements/ramdisk/extra-data.d/scripts/init index b58168d02..a947e27d4 100755 --- a/elements/ramdisk/extra-data.d/scripts/init +++ b/elements/ramdisk/extra-data.d/scripts/init @@ -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" diff --git a/elements/ramdisk/init.d/02-start-network b/elements/ramdisk/init.d/02-start-network index c5878bf78..995e6c61e 100644 --- a/elements/ramdisk/init.d/02-start-network +++ b/elements/ramdisk/init.d/02-start-network @@ -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" diff --git a/elements/ramdisk/post-install.d/01-ensure-binaries b/elements/ramdisk/post-install.d/01-ensure-binaries index 128013d37..c6297b673 100755 --- a/elements/ramdisk/post-install.d/01-ensure-binaries +++ b/elements/ramdisk/post-install.d/01-ensure-binaries @@ -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 diff --git a/elements/ramdisk/post-install.d/99-build-ramdisk b/elements/ramdisk/post-install.d/99-build-ramdisk index 3aabf7cbc..49110f064 100755 --- a/elements/ramdisk/post-install.d/99-build-ramdisk +++ b/elements/ramdisk/post-install.d/99-build-ramdisk @@ -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 diff --git a/elements/vm/block-device.d/10-partition b/elements/vm/block-device.d/10-partition index 036063285..10c8dc17f 100755 --- a/elements/vm/block-device.d/10-partition +++ b/elements/vm/block-device.d/10-partition @@ -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} diff --git a/elements/yum/bin/install-packages b/elements/yum/bin/install-packages index 7a351abec..956cc2353 100755 --- a/elements/yum/bin/install-packages +++ b/elements/yum/bin/install-packages @@ -17,6 +17,8 @@ set -e +EXTRA_ARGS= + if [ "$1" = "-u" ] ; then yum -y update exit 0 diff --git a/lib/common-functions b/lib/common-functions index 16e1994e8..0e1b67bdd 100644 --- a/lib/common-functions +++ b/lib/common-functions @@ -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