From: Frantisek Sumsal Date: Fri, 16 Apr 2021 16:33:44 +0000 (+0200) Subject: test: shell code cleanup X-Git-Tag: v249-rc1~396^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1b8fcd9c63f4ad6ce3853457ad31a7aeeb40d578;p=thirdparty%2Fsystemd.git test: shell code cleanup Just some basic stuff, like double quotes around strings, using predictable values for possibly unset variables, formatting, etc. --- diff --git a/test/test-functions b/test/test-functions index 063a6096fdc..159db9a569c 100644 --- a/test/test-functions +++ b/test/test-functions @@ -5,11 +5,12 @@ PATH=/sbin:/bin:/usr/sbin:/usr/bin export PATH os_release=$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release) -LOOKS_LIKE_DEBIAN=$(source $os_release && [[ "$ID" = "debian" || " $ID_LIKE " = *" debian "* ]] && echo yes || :) -LOOKS_LIKE_ARCH=$(source $os_release && [[ "$ID" = "arch" || " $ID_LIKE " = *" arch "* ]] && echo yes || :) -LOOKS_LIKE_SUSE=$(source $os_release && [[ " $ID_LIKE " = *" suse "* ]] && echo yes || :) -KERNEL_VER=${KERNEL_VER-$(uname -r)} -KERNEL_MODS="/lib/modules/$KERNEL_VER/" +# shellcheck source=/dev/null +source "$os_release" +[[ "$ID" = "debian" || " $ID_LIKE " = *" debian "* ]] && LOOKS_LIKE_DEBIAN=yes || LOOKS_LIKE_DEBIAN="" +[[ "$ID" = "arch" || " $ID_LIKE " = *" arch "* ]] && LOOKS_LIKE_ARCH=yes || LOOKS_LIKE_ARCH="" +[[ " $ID_LIKE " = *" suse "* ]] && LOOKS_LIKE_SUSE=yes || LOOKS_LIKE_SUSE="" +KERNEL_VER="${KERNEL_VER-$(uname -r)}" QEMU_TIMEOUT="${QEMU_TIMEOUT:-infinity}" NSPAWN_TIMEOUT="${NSPAWN_TIMEOUT:-infinity}" TIMED_OUT= # will be 1 after run_* if *_TIMEOUT is set and test timed out @@ -32,7 +33,7 @@ LOOPDEV= # check if it's not explicitly disabled (TEST_NO_KVM) and we're not already # running under KVM. If these conditions are met, enable KVM (and possibly # nested KVM), otherwise disable it. -if [[ -n "$TEST_NESTED_KVM" || ( -z "$TEST_NO_KVM" && $(systemd-detect-virt -v) != kvm ) ]]; then +if [[ -n "${TEST_NESTED_KVM:=}" || ( -z "${TEST_NO_KVM:=}" && $(systemd-detect-virt -v) != kvm ) ]]; then QEMU_KVM=yes else QEMU_KVM=no @@ -44,15 +45,17 @@ if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then fi # The calling test.sh scripts have TEST_BASE_DIR set via their Makefile, but we don't need them to provide it -TEST_BASE_DIR=${TEST_BASE_DIR:-$(realpath $(dirname "$BASH_SOURCE"))} +TEST_BASE_DIR=${TEST_BASE_DIR:-$(realpath "$(dirname "${BASH_SOURCE[0]}")")} TEST_UNITS_DIR="$TEST_BASE_DIR/units" SOURCE_DIR=$(realpath "$TEST_BASE_DIR/..") TOOLS_DIR="$SOURCE_DIR/tools" +# These variables are used by test scripts +export TEST_BASE_DIR TEST_UNITS_DIR SOURCE_DIR TOOLS_DIR # note that find-build-dir.sh will return $BUILD_DIR if provided, else it will try to find it -if ! BUILD_DIR=$($TOOLS_DIR/find-build-dir.sh); then +if ! BUILD_DIR="$("$TOOLS_DIR"/find-build-dir.sh)"; then if [ "$NO_BUILD" ]; then - BUILD_DIR=$SOURCE_DIR + BUILD_DIR="$SOURCE_DIR" else echo "ERROR: no build found, please set BUILD_DIR or use NO_BUILD" >&2 exit 1 @@ -66,12 +69,12 @@ PATH_TO_INIT=$ROOTLIBDIR/systemd [ "$SYSTEMD_NSPAWN" ] || SYSTEMD_NSPAWN=$(which -a $BUILD_DIR/systemd-nspawn systemd-nspawn 2>/dev/null | grep '^/' -m1) [ "$JOURNALCTL" ] || JOURNALCTL=$(which -a $BUILD_DIR/journalctl journalctl 2>/dev/null | grep '^/' -m1) -TESTFILE=${BASH_SOURCE[1]} +TESTFILE="${BASH_SOURCE[1]}" if [ -z "$TESTFILE" ]; then echo "ERROR: test-functions must be sourced from one of the TEST-*/test.sh scripts" >&2 exit 1 fi -TESTNAME=$(basename $(dirname $(realpath $TESTFILE))) +TESTNAME="$(basename "$(dirname "$(realpath "$TESTFILE")")")" STATEDIR="$BUILD_DIR/test/$TESTNAME" STATEFILE="$STATEDIR/.testdir" IMAGESTATEDIR="$STATEDIR/.." @@ -183,8 +186,9 @@ is_built_with_asan() { fi # Borrowed from https://github.com/google/oss-fuzz/blob/cd9acd02f9d3f6e80011cc1e9549be526ce5f270/infra/base-images/base-runner/bad_build_check#L182 - local _asan_calls=$(objdump -dC $SYSTEMD_JOURNALD | egrep "callq?\s+[0-9a-f]+\s+<__asan" -c) - if (( $_asan_calls < 1000 )); then + local _asan_calls + _asan_calls="$(objdump -dC "$SYSTEMD_JOURNALD" | grep -E "callq?\s+[0-9a-f]+\s+<__asan" -c)" + if ((_asan_calls < 1000)); then return 1 else return 0 @@ -227,31 +231,32 @@ if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then echo "Detected ASan RT '$ASAN_RT_NAME' located at '$ASAN_RT_PATH'" fi -function find_qemu_bin() { +find_qemu_bin() { + QEMU_BIN="${QEMU_BIN:-""}" # SUSE and Red Hat call the binary qemu-kvm. Debian and Gentoo call it kvm. if [[ $QEMU_KVM == "yes" ]]; then - [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1) + [[ -n "$QEMU_BIN" ]] || QEMU_BIN="$(command -v kvm qemu-kvm 2>/dev/null | grep '^/' -m1)" fi - [ "$ARCH" ] || ARCH=$(uname -m) + [[ -n "$ARCH" ]] || ARCH="$(uname -m)" case $ARCH in x86_64) # QEMU's own build system calls it qemu-system-x86_64 - [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1) + [[ -n "$QEMU_BIN" ]] || QEMU_BIN="$(command -v qemu-system-x86_64 2>/dev/null | grep '^/' -m1)" ;; i*86) # new i386 version of QEMU - [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1) + [[ -n "$QEMU_BIN" ]] || QEMU_BIN="$(command -v qemu-system-i386 2>/dev/null | grep '^/' -m1)" # i386 version of QEMU - [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1) + [[ -n "$QEMU_BIN" ]] || QEMU_BIN="$(command -v qemu 2>/dev/null | grep '^/' -m1)" ;; ppc64*) - [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-ppc64 2>/dev/null | grep '^/' -m1) + [[ -n "$QEMU_BIN" ]] || QEMU_BIN="$(command -v qemu-system-ppc64 2>/dev/null | grep '^/' -m1)" ;; esac - if [ ! -e "$QEMU_BIN" ]; then + if [[ ! -e "$QEMU_BIN" ]]; then echo "Could not find a suitable QEMU binary" >&2 return 1 fi @@ -261,11 +266,12 @@ function find_qemu_bin() { # returns 0 if newer or equal # returns 1 if older # returns 2 if failing -function qemu_min_version() { +qemu_min_version() { find_qemu_bin || return 2 # get version from binary - qemu_ver=$($QEMU_BIN --version | awk '/^QEMU emulator version ([0-9]*\.[0-9]*\.[0-9]*)/ {print $4}') + local qemu_ver + qemu_ver="$("$QEMU_BIN" --version | awk '/^QEMU emulator version ([0-9]*\.[0-9]*\.[0-9]*)/ {print $4}')" # Check version string format echo "$qemu_ver" | grep -q '^[0-9]*\.[0-9]*\.[0-9]*$' || return 2 @@ -279,14 +285,14 @@ function qemu_min_version() { # success), or 1 if QEMU is not available. run_qemu() { if [ -f /etc/machine-id ]; then - read MACHINE_ID < /etc/machine-id + read -r MACHINE_ID /dev/null 2>&1; then @@ -478,6 +485,9 @@ install_verity_minimal() { dfatal "veritysetup not found" exit 1 fi + # Local modifications of some global variables is intentional in this + # subshell (SC2030) + # shellcheck disable=SC2030 ( BASICTOOLS=( bash @@ -486,12 +496,15 @@ install_verity_minimal() { mount sleep ) - oldinitdir=$initdir - rm -rfv $TESTDIR/minimal - export initdir=$TESTDIR/minimal - mkdir -p $initdir/usr/lib/systemd/system $initdir/usr/lib/extension-release.d $initdir/etc $initdir/var/tmp $initdir/opt + oldinitdir="$initdir" + rm -rfv "$TESTDIR/minimal" + export initdir="$TESTDIR/minimal" + mkdir -p "$initdir/usr/lib/systemd/system" "$initdir/usr/lib/extension-release.d" "$initdir/etc" "$initdir/var/tmp" "$initdir/opt" setup_basic_dirs install_basic_tools + # Shellcheck treats [[ -v VAR ]] as an assignment to avoid a different + # issue, thus falsely triggering SC2030 in this case + # See: koalaman/shellcheck#1409 if [[ -v ASAN_RT_PATH ]]; then # If we're compiled with ASan, install the ASan RT (and its dependencies) # into the verity images to get rid of the annoying errors about @@ -499,71 +512,71 @@ install_verity_minimal() { inst_libs "$ASAN_RT_PATH" inst_library "$ASAN_RT_PATH" fi - cp $os_release $initdir/usr/lib/os-release - ln -s ../usr/lib/os-release $initdir/etc/os-release - touch $initdir/etc/machine-id $initdir/etc/resolv.conf - touch $initdir/opt/some_file - echo MARKER=1 >>$initdir/usr/lib/os-release - echo -e "[Service]\nExecStartPre=cat /usr/lib/os-release\nExecStart=sleep 120" >$initdir/usr/lib/systemd/system/app0.service - cp $initdir/usr/lib/systemd/system/app0.service $initdir/usr/lib/systemd/system/app0-foo.service - - mksquashfs $initdir $oldinitdir/usr/share/minimal_0.raw - veritysetup format $oldinitdir/usr/share/minimal_0.raw $oldinitdir/usr/share/minimal_0.verity | \ - grep '^Root hash:' | cut -f2 | tr -d '\n' >$oldinitdir/usr/share/minimal_0.roothash - - sed -i "s/MARKER=1/MARKER=2/g" $initdir/usr/lib/os-release - rm $initdir/usr/lib/systemd/system/app0-foo.service - cp $initdir/usr/lib/systemd/system/app0.service $initdir/usr/lib/systemd/system/app0-bar.service - - mksquashfs $initdir $oldinitdir/usr/share/minimal_1.raw - veritysetup format $oldinitdir/usr/share/minimal_1.raw $oldinitdir/usr/share/minimal_1.verity | \ - grep '^Root hash:' | cut -f2 | tr -d '\n' >$oldinitdir/usr/share/minimal_1.roothash + cp "$os_release" "$initdir/usr/lib/os-release" + ln -s ../usr/lib/os-release "$initdir/etc/os-release" + touch "$initdir/etc/machine-id" "$initdir/etc/resolv.conf" + touch "$initdir/opt/some_file" + echo MARKER=1 >>"$initdir/usr/lib/os-release" + echo -e "[Service]\nExecStartPre=cat /usr/lib/os-release\nExecStart=sleep 120" >"$initdir/usr/lib/systemd/system/app0.service" + cp "$initdir/usr/lib/systemd/system/app0.service" "$initdir/usr/lib/systemd/system/app0-foo.service" + + mksquashfs "$initdir" "$oldinitdir/usr/share/minimal_0.raw" + veritysetup format "$oldinitdir/usr/share/minimal_0.raw" "$oldinitdir/usr/share/minimal_0.verity" | \ + grep '^Root hash:' | cut -f2 | tr -d '\n' >"$oldinitdir/usr/share/minimal_0.roothash" + + sed -i "s/MARKER=1/MARKER=2/g" "$initdir/usr/lib/os-release" + rm "$initdir/usr/lib/systemd/system/app0-foo.service" + cp "$initdir/usr/lib/systemd/system/app0.service" "$initdir/usr/lib/systemd/system/app0-bar.service" + + mksquashfs "$initdir" "$oldinitdir/usr/share/minimal_1.raw" + veritysetup format "$oldinitdir/usr/share/minimal_1.raw" "$oldinitdir/usr/share/minimal_1.verity" | \ + grep '^Root hash:' | cut -f2 | tr -d '\n' >"$oldinitdir/usr/share/minimal_1.roothash" # Rolling distros like Arch do not set VERSION_ID local version_id="" - if grep -q "^VERSION_ID=" $os_release; then - version_id="$(grep "^VERSION_ID=" $os_release)" + if grep -q "^VERSION_ID=" "$os_release"; then + version_id="$(grep "^VERSION_ID=" "$os_release")" fi - export initdir=$TESTDIR/app0 - mkdir -p $initdir/usr/lib/extension-release.d $initdir/usr/lib/systemd/system $initdir/opt - grep "^ID=" $os_release >$initdir/usr/lib/extension-release.d/extension-release.app0 - echo "${version_id}" >>$initdir/usr/lib/extension-release.d/extension-release.app0 - cat <$initdir/usr/lib/systemd/system/app0.service + export initdir="$TESTDIR/app0" + mkdir -p "$initdir/usr/lib/extension-release.d" "$initdir/usr/lib/systemd/system" "$initdir/opt" + grep "^ID=" "$os_release" >"$initdir/usr/lib/extension-release.d/extension-release.app0" + echo "${version_id}" >>"$initdir/usr/lib/extension-release.d/extension-release.app0" + cat >"$initdir/usr/lib/systemd/system/app0.service" <$initdir/opt/script0.sh + cat >"$initdir/opt/script0.sh" <$initdir/usr/lib/systemd/system/some_file - mksquashfs $initdir $oldinitdir/usr/share/app0.raw - - export initdir=$TESTDIR/app1 - mkdir -p $initdir/usr/lib/extension-release.d $initdir/usr/lib/systemd/system $initdir/opt - grep "^ID=" $os_release >$initdir/usr/lib/extension-release.d/extension-release.app1 - echo "${version_id}" >>$initdir/usr/lib/extension-release.d/extension-release.app1 - cat <$initdir/usr/lib/systemd/system/app1.service + chmod +x "$initdir/opt/script0.sh" + echo MARKER=1 >"$initdir/usr/lib/systemd/system/some_file" + mksquashfs "$initdir" "$oldinitdir/usr/share/app0.raw" + + export initdir="$TESTDIR/app1" + mkdir -p "$initdir/usr/lib/extension-release.d" "$initdir/usr/lib/systemd/system" "$initdir/opt" + grep "^ID=" "$os_release" >"$initdir/usr/lib/extension-release.d/extension-release.app1" + echo "${version_id}" >>"$initdir/usr/lib/extension-release.d/extension-release.app1" + cat >"$initdir/usr/lib/systemd/system/app1.service" <$initdir/opt/script1.sh + cat >"$initdir/opt/script1.sh" <$initdir/usr/lib/systemd/system/other_file - mksquashfs $initdir $oldinitdir/usr/share/app1.raw + chmod +x "$initdir/opt/script1.sh" + echo MARKER=1 >"$initdir/usr/lib/systemd/system/other_file" + mksquashfs "$initdir" "$oldinitdir/usr/share/app1.raw" ) } @@ -603,26 +616,29 @@ setup_basic_environment() { } setup_selinux() { + dinfo "Setup SELinux" # don't forget KERNEL_APPEND='... selinux=1 ...' if [[ "$SETUP_SELINUX" != "yes" ]]; then - ddebug "Don't setup SELinux" + dinfo "SETUP_SELINUX != yes, skipping SELinux configuration" return 0 fi - ddebug "Setup SELinux" - local _conf_dir=/etc/selinux - local _fixfiles_tools="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles" - rm -rf $initdir/$_conf_dir - if ! cp -ar $_conf_dir $initdir/$_conf_dir; then - dfatal "Failed to copy $_conf_dir" + local conf_dir=/etc/selinux + local fixfiles_tools=(bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles) + + # Make sure the following statement can't expand to "/" to prevent + # a potential where-are-my-backups situation + rm -rf "${initdir:?}/$conf_dir" + if ! cp -ar "$conf_dir" "$initdir/$conf_dir"; then + dfatal "Failed to copy $conf_dir" exit 1 fi - touch $initdir/.autorelabel - mkdir -p $initdir/usr/lib/systemd/tests/testdata/units/basic.target.wants - ln -sf ../autorelabel.service $initdir/usr/lib/systemd/tests/testdata/units/basic.target.wants/ + touch "$initdir/.autorelabel" + mkdir -p "$initdir/usr/lib/systemd/tests/testdata/units/basic.target.wants" + ln -sf ../autorelabel.service "$initdir/usr/lib/systemd/tests/testdata/units/basic.target.wants/" - dracut_install $_fixfiles_tools + dracut_install "${fixfiles_tools[@]}" dracut_install fixfiles dracut_install sestatus } @@ -633,35 +649,36 @@ install_valgrind() { exit 1 fi - local _valgrind_bins=$(strace -e execve valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if /^execve\("([^"]+)"/') - dracut_install $_valgrind_bins + local valgrind_bins valgrind_libs valgrind_dbg_and_supp + + valgrind_bins="$(strace -e execve valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if /^execve\("([^"]+)"/')" + dracut_install "$valgrind_bins" - local _valgrind_libs=$(LD_DEBUG=files valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if m{calling init: (/.*vgpreload_.*)}') - dracut_install $_valgrind_libs + valgrind_libs="$(LD_DEBUG=files valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')" + dracut_install "$valgrind_libs" - local _valgrind_dbg_and_supp=$( + valgrind_dbg_and_supp="$( strace -e open valgrind /bin/true 2>&1 >/dev/null | perl -lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }' - ) - dracut_install $_valgrind_dbg_and_supp + )" + dracut_install "$valgrind_dbg_and_supp" } create_valgrind_wrapper() { - local _valgrind_wrapper=$initdir/$ROOTLIBDIR/systemd-under-valgrind - ddebug "Create $_valgrind_wrapper" - cat >$_valgrind_wrapper <"$valgrind_wrapper" <$_asan_wrapper <"$asan_wrapper" <$_strace_wrapper <"$strace_wrapper" <>$initdir/etc/systemd/system.conf + echo LogLevel=debug >>"$initdir/etc/systemd/system.conf" # store coredumps in journal - echo Storage=journal >>$initdir/etc/systemd/coredump.conf + echo Storage=journal >>"$initdir/etc/systemd/coredump.conf" } get_ldpath() { - local _bin="$1" - local rpath=$(objdump -p "$_bin" 2>/dev/null | awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" | paste -sd :) + local rpath + rpath="$(objdump -p "${1:?}" 2>/dev/null | awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" | paste -sd :)" if [ -z "$rpath" ] ; then - echo $BUILD_DIR + echo "$BUILD_DIR" else - echo $rpath + echo "$rpath" fi } install_missing_libraries() { + dinfo "Install missing libraries" # install possible missing libraries - for i in $initdir{,/usr}/{sbin,bin}/* $initdir{,/usr}/lib/systemd/{,tests/{,manual/,unsafe/}}*; do - LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$(get_ldpath $i)" inst_libs $i + for i in "${initdir:?}"{,/usr}/{sbin,bin}/* "$initdir"{,/usr}/lib/systemd/{,tests/{,manual/,unsafe/}}*; do + LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$(get_ldpath "$i")" inst_libs "$i" done + local lib path # A number of dependencies is now optional via dlopen, so the install # script will not pick them up, since it looks at linkage. for lib in libcryptsetup libidn libidn2 pwquality libqrencode tss2-esys tss2-rc tss2-mu libfido2; do ddebug "Searching for $lib via pkg-config" - if pkg-config --exists ${lib}; then - path=$(pkg-config --variable=libdir ${lib}) + if pkg-config --exists "$lib"; then + path="$(pkg-config --variable=libdir "$lib")" if [ -z "${path}" ]; then ddebug "$lib.pc does not contain a libdir variable, skipping" continue @@ -904,7 +926,7 @@ install_missing_libraries() { } cleanup_loopdev() { - if [ -n "${LOOPDEV}" ]; then + if [ -n "${LOOPDEV:=}" ]; then ddebug "losetup -d $LOOPDEV" losetup -d "${LOOPDEV}" unset LOOPDEV @@ -914,66 +936,65 @@ cleanup_loopdev() { trap cleanup_loopdev EXIT INT QUIT PIPE create_empty_image() { - if [ -z "$IMAGE_NAME" ]; then + if [ -z "${IMAGE_NAME:=}" ]; then echo "create_empty_image: \$IMAGE_NAME not set" exit 1 fi - local _size=500 + local size=500 if [[ "$STRIP_BINARIES" = "no" ]]; then - _size=$((4*_size)) + size=$((4 * size)) fi - echo "Setting up $IMAGE_PUBLIC (${_size} MB)" - rm -f "$IMAGE_PRIVATE" "$IMAGE_PUBLIC" + echo "Setting up ${IMAGE_PUBLIC:?} (${size} MB)" + rm -f "${IMAGE_PRIVATE:?}" "$IMAGE_PUBLIC" # Create the blank file to use as a root filesystem - truncate -s "${_size}M" "$IMAGE_PUBLIC" + truncate -s "${size}M" "$IMAGE_PUBLIC" LOOPDEV=$(losetup --show -P -f "$IMAGE_PUBLIC") [ -b "$LOOPDEV" ] || return 1 sfdisk "$LOOPDEV" <$initdir/etc/rc.d/rc.local <"$initdir/etc/rc.d/rc.local" <$initdir/etc/sysusers.d/testuser.conf <"$initdir/etc/sysusers.d/testuser.conf" <$initdir/etc/environment - >$initdir/etc/machine-id - >$initdir/etc/resolv.conf + : >"${initdir:?}/etc/environment" + : >"$initdir/etc/machine-id" + : >"$initdir/etc/resolv.conf" # set the hostname - echo systemd-testsuite >$initdir/etc/hostname + echo systemd-testsuite >"$initdir/etc/hostname" # let's set up just one image with the traditional verbose output - if [ ${IMAGE_NAME} != "basic" ]; then - mkdir -p $initdir/etc/systemd/system.conf.d - echo -e '[Manager]\nStatusUnitFormat=name' >$initdir/etc/systemd/system.conf.d/status.conf + if [ "${IMAGE_NAME:?}" != "basic" ]; then + mkdir -p "$initdir/etc/systemd/system.conf.d" + echo -e '[Manager]\nStatusUnitFormat=name' >"$initdir/etc/systemd/system.conf.d/status.conf" fi } install_basic_tools() { + dinfo "Install basic tools" dracut_install "${BASICTOOLS[@]}" dracut_install -o sushell # in Debian ldconfig is just a shell script wrapper around ldconfig.real @@ -1196,13 +1225,14 @@ install_basic_tools() { } install_debug_tools() { + dinfo "Install debug tools" dracut_install "${DEBUGTOOLS[@]}" if [[ $INTERACTIVE_DEBUG ]]; then # Set default TERM from vt220 to linux, so at least basic key shortcuts work - local _getty_override="$initdir/etc/systemd/system/serial-getty@.service.d" - mkdir -p "$_getty_override" - echo -e "[Service]\nEnvironment=TERM=linux" >"$_getty_override/default-TERM.conf" + local getty_override="${initdir:?}/etc/systemd/system/serial-getty@.service.d" + mkdir -p "$getty_override" + echo -e "[Service]\nEnvironment=TERM=linux" >"$getty_override/default-TERM.conf" cat >"$initdir/etc/motd" <&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}') dracut_install $NSS_LIBS } install_dbus() { - inst $ROOTLIBDIR/system/dbus.socket + dinfo "Install dbus" + inst "${ROOTLIBDIR:?}/system/dbus.socket" # Newer Fedora versions use dbus-broker by default. Let's install it if it's available. - if [ -f $ROOTLIBDIR/system/dbus-broker.service ]; then - inst $ROOTLIBDIR/system/dbus-broker.service + if [ -f "$ROOTLIBDIR/system/dbus-broker.service" ]; then + inst "$ROOTLIBDIR/system/dbus-broker.service" inst_symlink /etc/systemd/system/dbus.service inst /usr/bin/dbus-broker inst /usr/bin/dbus-broker-launch - elif [ -f $ROOTLIBDIR/system/dbus-daemon.service ]; then + elif [ -f "$ROOTLIBDIR/system/dbus-daemon.service" ]; then # Fedora rawhide replaced dbus.service with dbus-daemon.service - inst $ROOTLIBDIR/system/dbus-daemon.service + inst "$ROOTLIBDIR/system/dbus-daemon.service" # Alias symlink inst_symlink /etc/systemd/system/dbus.service else - inst $ROOTLIBDIR/system/dbus.service + inst "$ROOTLIBDIR/system/dbus.service" fi find \ @@ -1245,8 +1277,8 @@ install_dbus() { done # setup policy for Type=dbus test - mkdir -p $initdir/etc/dbus-1/system.d - cat >$initdir/etc/dbus-1/system.d/systemd.test.ExecStopPost.conf <"$initdir/etc/dbus-1/system.d/systemd.test.ExecStopPost.conf" < @@ -1259,33 +1291,34 @@ EOF } install_user_dbus() { + dinfo "Install user dbus" local userunitdir - if ! userunitdir=$(pkg-config --variable=systemduserunitdir systemd); then - echo "WARNING! Cannot determine userunitdir from pkg-config, assuming /usr/lib/systemd/user" >&2 - local userunitdir=/usr/lib/systemd/user + if ! userunitdir="$(pkg-config --variable=systemduserunitdir systemd)"; then + dwarn "WARNING! Cannot determine userunitdir from pkg-config, assuming /usr/lib/systemd/user" + userunitdir=/usr/lib/systemd/user fi - inst $userunitdir/dbus.socket - inst_symlink $userunitdir/sockets.target.wants/dbus.socket || inst_symlink /etc/systemd/user/sockets.target.wants/dbus.socket + inst "$userunitdir/dbus.socket" + inst_symlink "$userunitdir/sockets.target.wants/dbus.socket" || inst_symlink /etc/systemd/user/sockets.target.wants/dbus.socket # Append the After= dependency on dbus in case it isn't already set up - mkdir -p "$initdir/etc/systemd/system/user@.service.d/" - cat <"$initdir/etc/systemd/system/user@.service.d/dbus.conf" + mkdir -p "${initdir:?}/etc/systemd/system/user@.service.d/" + cat >"$initdir/etc/systemd/system/user@.service.d/dbus.conf" <$STATEFILE<"$STATEFILE" < __abssize || __level > __cursize )) @@ -1655,24 +1702,27 @@ convert_abs_rel() { # will create ${initdir}/lib64, ${initdir}/lib64/file, # and a symlink ${initdir}/lib -> lib64. inst_dir() { - [[ -e ${initdir}/"$1" ]] && return 0 # already there + local dir="${1:?}" + local part="${dir%/*}" + local file - local _dir="$1" _part="${1%/*}" _file - while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do - _dir="$_part $_dir" - _part=${_part%/*} + [[ -e "${initdir:?}/${dir}" ]] && return 0 # already there + + while [[ "$part" != "${part%/*}" ]] && ! [[ -e "${initdir}/${part}" ]]; do + dir="$part $dir" + part="${part%/*}" done # iterate over parent directories - for _file in $_dir; do - [[ -e "${initdir}/$_file" ]] && continue - if [[ -L $_file ]]; then - inst_symlink "$_file" + for file in $dir; do + [[ -e "${initdir}/$file" ]] && continue + if [[ -L $file ]]; then + inst_symlink "$file" else # create directory - mkdir -m 0755 -p "${initdir}/$_file" || return 1 - [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file" - chmod u+w "${initdir}/$_file" + mkdir -m 0755 "${initdir}/$file" || return 1 + [[ -e "$file" ]] && chmod --reference="$file" "${initdir}/$file" + chmod u+w "${initdir}/$file" fi done } @@ -1682,21 +1732,22 @@ inst_dir() { # Location of the image dir is assumed to be $initdir # We never overwrite the target if it exists. inst_simple() { - [[ -f "$1" ]] || return 1 + [[ -f "${1:?}" ]] || return 1 strstr "$1" "/" || return 1 - local _src=$1 target="${2:-$1}" - if ! [[ -d ${initdir}/$target ]]; then + local src="$1" + local target="${2:-$1}" + if ! [[ -d ${initdir:?}/$target ]]; then [[ -e ${initdir}/$target ]] && return 0 [[ -L ${initdir}/$target ]] && return 0 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}" fi # install checksum files also - if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then - inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac" + if [[ -e "${src%/*}/.${src##*/}.hmac" ]]; then + inst "${src%/*}/.${src##*/}.hmac" "${target%/*}/.${target##*/}.hmac" fi - ddebug "Installing $_src" - cp --sparse=always -pfL "$_src" "${initdir}/$target" + ddebug "Installing $src" + cp --sparse=always -pfL "$src" "${initdir}/$target" } # find symlinks linked to given library file @@ -1710,15 +1761,16 @@ inst_simple() { # output: libfoo.so.8 libfoo.so # (Only if libfoo.so.8 and libfoo.so exists on host system.) rev_lib_symlinks() { - [[ ! $1 ]] && return 0 - - local fn="$1" orig="$(readlink -f "$1")" links='' + local fn="${1:?}" + local links="" + local orig + orig="$(readlink -f "$1")" - [[ ${fn} =~ .*\.so\..* ]] || return 1 + [[ "${fn}" =~ .*\.so\..* ]] || return 1 - until [[ ${fn##*.} == so ]]; do + until [[ "${fn##*.}" == so ]]; do fn="${fn%.*}" - [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}" + [[ -L "${fn}" && "$(readlink -f "${fn}")" == "${orig}" ]] && links+=" ${fn}" done echo "${links}" @@ -1728,185 +1780,204 @@ rev_lib_symlinks() { # It handles making symlinks according to how the original library # is referenced. inst_library() { - local _src="$1" _dest=${2:-$1} _lib _reallib _symlink + local src="${1:?}" + local dest="${2:-$1}" + local reallib symlink + strstr "$1" "/" || return 1 - [[ -e $initdir/$_dest ]] && return 0 - if [[ -L $_src ]]; then + [[ -e ${initdir:?}/$dest ]] && return 0 + if [[ -L $src ]]; then # install checksum files also - if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then - inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac" + if [[ -e "${src%/*}/.${src##*/}.hmac" ]]; then + inst "${src%/*}/.${src##*/}.hmac" "${dest%/*}/.${dest##*/}.hmac" fi - _reallib=$(readlink -f "$_src") - inst_simple "$_reallib" "$_reallib" - inst_dir "${_dest%/*}" - [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/} - ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}" + reallib="$(readlink -f "$src")" + inst_simple "$reallib" "$reallib" + inst_dir "${dest%/*}" + [[ -d "${dest%/*}" ]] && dest="$(readlink -f "${dest%/*}")/${dest##*/}" + ln -sfn -- "$(convert_abs_rel "${dest}" "${reallib}")" "${initdir}/${dest}" else - inst_simple "$_src" "$_dest" + inst_simple "$src" "$dest" fi # Create additional symlinks. See rev_symlinks description. - for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do - [[ -e $initdir/$_symlink ]] || { - ddebug "Creating extra symlink: $_symlink" - inst_symlink $_symlink - } + for symlink in $(rev_lib_symlinks "$src") ${reallib:+$(rev_lib_symlinks "$reallib")}; do + if [[ ! -e "$initdir/$symlink" ]]; then + ddebug "Creating extra symlink: $symlink" + inst_symlink "$symlink" + fi done } # find a binary. If we were not passed the full path directly, # search in the usual places to find the binary. find_binary() { - if [[ -z ${1##/*} ]]; then - if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then - echo $1 + local bin="${1:?}" + if [[ -z ${bin##/*} ]]; then + if [[ -x "$bin" ]] || { strstr "$bin" ".so" && ldd "$bin" &>/dev/null; }; then + echo "$bin" return 0 fi fi - type -P $1 + type -P "$bin" } # Same as above, but specialized to install binary executables. # Install binary executable, and all shared library dependencies, if any. inst_binary() { - local _bin _target + local bin="${1:?}" + local path target # In certain cases we might attempt to install a binary which is already # present in the test image, yet it's missing from the host system. # In such cases, let's check if the binary indeed exists in the image # before doing any other chcecks. If it does, immediately return with # success. - [[ $# -eq 1 && -e $initdir/$1 || -e $initdir/bin/$1 || -e $initdir/sbin/$1 || -e $initdir/usr/bin/$1 || -e $initdir/usr/sbin/$1 ]] && return 0 + if [[ $# -eq 1 ]]; then + for path in "" bin sbin usr/bin usr/sbin; do + [[ -e "${initdir:?}${path:+/$path}/${bin}" ]] && return 0 + done + fi - _bin=$(find_binary "$1") || return 1 - _target=${2:-$_bin} - [[ -e $initdir/$_target ]] && return 0 - [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0 - local _file _line - local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)' + bin="$(find_binary "$bin")" || return 1 + target="${2:-$bin}" + [[ -e "${initdir:?}/$target" ]] && return 0 + [[ -L "$bin" ]] && inst_symlink "$bin" "$target" && return 0 + + local file line + local so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)' # I love bash! - LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do - [[ $_line = 'not a dynamic executable' ]] && break + while read -r line; do + [[ "$line" = 'not a dynamic executable' ]] && break - if [[ $_line =~ $_so_regex ]]; then - _file=${BASH_REMATCH[1]} - [[ -e ${initdir}/$_file ]] && continue - inst_library "$_file" + if [[ "$line" =~ $so_regex ]]; then + file="${BASH_REMATCH[1]}" + [[ -e "${initdir}/$file" ]] && continue + inst_library "$file" continue fi - if [[ $_line =~ not\ found ]]; then - dfatal "Missing a shared library required by $_bin." - dfatal "Run \"ldd $_bin\" to find out what it is." - dfatal "$_line" + if [[ "$line" =~ not\ found ]]; then + dfatal "Missing a shared library required by $bin." + dfatal "Run \"ldd $bin\" to find out what it is." + dfatal "$line" dfatal "dracut cannot create an initrd." exit 1 fi - done - inst_simple "$_bin" "$_target" + done < <(LC_ALL=C ldd "$bin" 2>/dev/null) + inst_simple "$bin" "$target" } # same as above, except for shell scripts. # If your shell script does not start with shebang, it is not a shell script. inst_script() { - local _bin - _bin=$(find_binary "$1") || return 1 + local bin line shebang_regex + bin="$(find_binary "${1:?}")" || return 1 shift - local _line _shebang_regex - read -r -n 80 _line <"$_bin" + + read -r -n 80 line <"$bin" # If debug is set, clean unprintable chars to prevent messing up the term - [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]') - _shebang_regex='(#! *)(/[^ ]+).*' - [[ $_line =~ $_shebang_regex ]] || return 1 - inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@" + [[ "${debug:=}" ]] && line="$(echo -n "$line" | tr -c -d '[:print:][:space:]')" + shebang_regex='(#! *)(/[^ ]+).*' + [[ "$line" =~ $shebang_regex ]] || return 1 + inst "${BASH_REMATCH[2]}" && inst_simple "$bin" "$@" } # same as above, but specialized for symlinks inst_symlink() { - local _src=$1 _target=${2:-$1} _realsrc - strstr "$1" "/" || return 1 - [[ -L $1 ]] || return 1 - [[ -L $initdir/$_target ]] && return 0 - _realsrc=$(readlink -f "$_src") - if ! [[ -e $initdir/$_realsrc ]]; then - if [[ -d $_realsrc ]]; then - inst_dir "$_realsrc" + local src="${1:?}" + local target="${2:-$src}" + local realsrc + + strstr "$src" "/" || return 1 + [[ -L "$src" ]] || return 1 + [[ -L "${initdir:?}/$target" ]] && return 0 + realsrc="$(readlink -f "$src")" + if ! [[ -e "$initdir/$realsrc" ]]; then + if [[ -d "$realsrc" ]]; then + inst_dir "$realsrc" else - inst "$_realsrc" + inst "$realsrc" fi fi - [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}" - [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/} - ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target" + [[ ! -e "$initdir/${target%/*}" ]] && inst_dir "${target%/*}" + [[ -d "${target%/*}" ]] && target="$(readlink -f "${target%/*}")/${target##*/}" + ln -sfn -- "$(convert_abs_rel "${target}" "${realsrc}")" "$initdir/$target" } # attempt to install any programs specified in a udev rule inst_rule_programs() { - local _prog _bin + local rule="${1:?}" + local prog bin - if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then - for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do - if [ -x /lib/udev/$_prog ]; then - _bin=/lib/udev/$_prog - else - _bin=$(find_binary "$_prog") || { - dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found" - continue; - } + awk 'match($0, /PROGRAM==?"([^ "]+)/, m) { print m[1]; }' "$rule" | while read -r prog; do + if [ -x "/lib/udev/$prog" ]; then + bin="/lib/udev/$prog" + else + if ! bin="$(find_binary "$prog")"; then + dinfo "Skipping program $prog used in udev rule $(basename "$rule") as it cannot be found" + continue fi + fi - #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" - dracut_install "$_bin" - done - fi + #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)" + dracut_install "$bin" + done } # udev rules always get installed in the same place, so # create a function to install them to make life simpler. inst_rules() { - local _target=/etc/udev/rules.d _rule _found + local target=/etc/udev/rules.d + local found rule inst_dir "/lib/udev/rules.d" - inst_dir "$_target" - for _rule in "$@"; do + inst_dir "$target" + for rule in "$@"; do if [ "${rule#/}" = "$rule" ]; then for r in /lib/udev/rules.d /etc/udev/rules.d; do - if [[ -f $r/$_rule ]]; then - _found="$r/$_rule" - inst_simple "$_found" - inst_rule_programs "$_found" + if [[ -f "$r/$rule" ]]; then + found="$r/$rule" + inst_simple "$found" + inst_rule_programs "$found" fi done fi - for r in '' ./ $dracutbasedir/rules.d/; do - if [[ -f ${r}$_rule ]]; then - _found="${r}$_rule" - inst_simple "$_found" "$_target/${_found##*/}" - inst_rule_programs "$_found" + for r in '' ./; do + if [[ -f "${r}${rule}" ]]; then + found="${r}${rule}" + inst_simple "$found" "$target/${found##*/}" + inst_rule_programs "$found" fi done - [[ $_found ]] || dinfo "Skipping udev rule: $_rule" - _found= + [[ $found ]] || dinfo "Skipping udev rule: $rule" + found= done } # general purpose installation function # Same args as above. inst() { - local _x - case $# in 1) ;; - 2) [[ ! $initdir && -d $2 ]] && export initdir=$2 - [[ $initdir = $2 ]] && set $1;; - 3) [[ -z $initdir ]] && export initdir=$2 - set $1 $3;; - *) dfatal "inst only takes 1 or 2 or 3 arguments" - exit 1;; + 2) + [[ ! "$initdir" && -d "$2" ]] && export initdir="$2" + [[ "$initdir" = "$2" ]] && set "$1" + ;; + 3) + [[ -z "$initdir" ]] && export initdir="$2" + set "$1" "$3" + ;; + *) + dfatal "inst only takes 1 or 2 or 3 arguments" + exit 1 + ;; esac - for _x in inst_symlink inst_script inst_binary inst_simple; do - $_x "$@" && return 0 + + local fun + for fun in inst_symlink inst_script inst_binary inst_simple; do + "$fun" "$@" && return 0 done return 1 } @@ -1925,14 +1996,14 @@ inst() { # Lets assume that /bin/baz exists, so it will be installed as /bin/foo in # initramfs. inst_any() { - local to f + local dest file - [[ $1 = '-d' ]] && to="$2" && shift 2 + [[ "${1:?}" = '-d' ]] && dest="${2:?}" && shift 2 - for f in "$@"; do - if [[ -e $f ]]; then - [[ $to ]] && inst "$f" "$to" && return 0 - inst "$f" && return 0 + for file in "$@"; do + if [[ -e "$file" ]]; then + [[ -n "$dest" ]] && inst "$file" "$dest" && return 0 + inst "$file" && return 0 fi done @@ -1943,22 +2014,24 @@ inst_any() { # Install to the initramfs image # -o optionally install the and don't fail, if it is not there dracut_install() { - local _optional=no - if [[ $1 = '-o' ]]; then - _optional=yes + local optional=no + local prog="${1:?}" + + if [[ "$prog" = '-o' ]]; then + optional=yes shift fi - while (($# > 0)); do - if ! inst "$1" ; then - if [[ $_optional = yes ]]; then - dinfo "Skipping program $1 as it cannot be found and is" \ + + for prog in "$@"; do + if ! inst "$prog" ; then + if [[ "$optional" = yes ]]; then + dinfo "Skipping program $prog as it cannot be found and is" \ "flagged to be optional" else - dfatal "Failed to install $1" + dfatal "Failed to install $prog" exit 1 fi fi - shift done } @@ -2186,16 +2259,17 @@ instmods() { } setup_suse() { - ln -fs ../usr/bin/systemctl $initdir/bin/ - ln -fs ../usr/lib/systemd $initdir/lib/ + ln -fs ../usr/bin/systemctl "${initdir:?}/bin/" + ln -fs ../usr/lib/systemd "$initdir/lib/" inst_simple "/usr/lib/systemd/system/haveged.service" instmods ext4 } _umount_dir() { - if mountpoint -q $1; then - ddebug "umount $1" - umount $1 + local mountpoint="${1:?}" + if mountpoint -q "$mountpoint"; then + ddebug "umount $mountpoint" + umount "$mountpoint" fi } @@ -2208,14 +2282,14 @@ _test_cleanup() { # (post-test) cleanup should always ignore failure and cleanup as much as possible ( set +e - _umount_dir $initdir - rm -vf "$IMAGE_PUBLIC" + [[ -n "$initdir" ]] && _umount_dir "$initdir" + [[ -n "$IMAGE_PUBLIC" ]] && rm -vf "$IMAGE_PUBLIC" # If multiple setups/cleans are ran in parallel, this can cause a race - if [ ${TEST_PARALLELIZE} -ne 1 ]; then + if [[ -n "$IMAGESTATEDIR" && $TEST_PARALLELIZE -ne 1 ]]; then rm -vf "${IMAGESTATEDIR}/default.img" fi - rm -vfr "$TESTDIR" - rm -vf "$STATEFILE" + [[ -n "$TESTDIR" ]] && rm -vfr "$TESTDIR" + [[ -n "$STATEFILE" ]] && rm -vf "$STATEFILE" ) || : } @@ -2227,7 +2301,7 @@ test_cleanup() { test_cleanup_again() { [ -n "$TESTDIR" ] || return rm -rf "$TESTDIR/unprivileged-nspawn-root" - _umount_dir $initdir + [[ -n "$initdir" ]] && _umount_dir "$initdir" } test_create_image() { @@ -2242,25 +2316,25 @@ test_create_image() { } test_setup() { - if [ ${TEST_REQUIRE_INSTALL_TESTS} -ne 0 ] && \ - type -P meson >/dev/null && \ - [[ "$(meson configure $BUILD_DIR | grep install-tests | awk '{ print $2 }')" != "true" ]]; then + if [ "${TEST_REQUIRE_INSTALL_TESTS:?}" -ne 0 ] && \ + command -v meson >/dev/null && \ + [[ "$(meson configure "${BUILD_DIR:?}" | grep install-tests | awk '{ print $2 }')" != "true" ]]; then dfatal "$BUILD_DIR needs to be built with -Dinstall-tests=true" exit 1 fi - if [ -e "$IMAGE_PRIVATE" ]; then - echo "Reusing existing image $IMAGE_PRIVATE → $(realpath $IMAGE_PRIVATE)" + if [ -e "${IMAGE_PRIVATE:?}" ]; then + echo "Reusing existing image $IMAGE_PRIVATE → $(realpath "$IMAGE_PRIVATE")" mount_initdir else - if [ ! -e "$IMAGE_PUBLIC" ]; then + if [ ! -e "${IMAGE_PUBLIC:?}" ]; then # default.img is the base that every test uses and optionally appends to - if [ ! -e "${IMAGESTATEDIR}/default.img" ] || [ -n "${TEST_FORCE_NEWIMAGE}" ]; then + if [ ! -e "${IMAGESTATEDIR:?}/default.img" ] || [ -n "${TEST_FORCE_NEWIMAGE:=}" ]; then # Create the backing public image, but then completely unmount # it and drop the loopback device responsible for it, since we're # going to symlink/copy the image and mount it again from # elsewhere. - local image_old=${IMAGE_PUBLIC} + local image_old="${IMAGE_PUBLIC}" if [ -z "${TEST_FORCE_NEWIMAGE}" ]; then IMAGE_PUBLIC="${IMAGESTATEDIR}/default.img" fi @@ -2270,7 +2344,7 @@ test_setup() { cleanup_loopdev IMAGE_PUBLIC="${image_old}" fi - if [ "${IMAGE_NAME}" != "default" ] && [ -z "${TEST_FORCE_NEWIMAGE}" ]; then + if [ "${IMAGE_NAME:?}" != "default" ] && [ -z "${TEST_FORCE_NEWIMAGE}" ]; then cp -v "$(realpath "${IMAGESTATEDIR}/default.img")" "$IMAGE_PUBLIC" fi fi @@ -2280,16 +2354,16 @@ test_setup() { hook_defined=$? fi - echo "Reusing existing cached image $IMAGE_PUBLIC → $(realpath $IMAGE_PUBLIC)" - if [ ${TEST_PARALLELIZE} -ne 0 ] || [ ${hook_defined} -eq 0 ]; then - cp -v "$(realpath $IMAGE_PUBLIC)" "$IMAGE_PRIVATE" + echo "Reusing existing cached image $IMAGE_PUBLIC → $(realpath "$IMAGE_PUBLIC")" + if [ "$TEST_PARALLELIZE" -ne 0 ] || [ "$hook_defined" -eq 0 ]; then + cp -v -- "$(realpath "$IMAGE_PUBLIC")" "$IMAGE_PRIVATE" else - ln -sv "$(realpath $IMAGE_PUBLIC)" "$IMAGE_PRIVATE" + ln -sv -- "$(realpath "$IMAGE_PUBLIC")" "$IMAGE_PRIVATE" fi mount_initdir - if [ ${hook_defined} -eq 0 ]; then - test_append_files "$initdir" + if [ "$hook_defined" -eq 0 ]; then + test_append_files "${initdir:?}" fi fi @@ -2297,26 +2371,27 @@ test_setup() { } test_run() { + local test_id="${1:?}" mount_initdir - if [ -z "$TEST_NO_QEMU" ]; then - if run_qemu "$1"; then + if [ -z "${TEST_NO_QEMU:=}" ]; then + if run_qemu "$test_id"; then check_result_qemu || { echo "QEMU test failed"; return 1; } else dwarn "can't run QEMU, skipping" fi fi - if [ -z "$TEST_NO_NSPAWN" ]; then + if [ -z "${TEST_NO_NSPAWN:=}" ]; then mount_initdir - if run_nspawn "$initdir" "$1"; then + if run_nspawn "${initdir:?}" "$test_id"; then check_result_nspawn "$initdir" || { echo "nspawn-root test failed"; return 1; } else dwarn "can't run systemd-nspawn, skipping" fi - if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then + if [[ "${RUN_IN_UNPRIVILEGED_CONTAINER:=}" = "yes" ]]; then dir="$TESTDIR/unprivileged-nspawn-root" - if NSPAWN_ARGUMENTS="-U --private-network $NSPAWN_ARGUMENTS" run_nspawn "$dir" "$1"; then + if NSPAWN_ARGUMENTS="-U --private-network ${NSPAWN_ARGUMENTS:-}" run_nspawn "$dir" "$test_id"; then check_result_nspawn "$dir" || { echo "unprivileged-nspawn-root test failed"; return 1; } else dwarn "can't run systemd-nspawn, skipping" @@ -2332,26 +2407,26 @@ do_test() { exit 0 fi - if [ -n "$TEST_NO_QEMU" ] && [ -n "$TEST_NO_NSPAWN" ]; then + if [ -n "${TEST_NO_QEMU:=}" ] && [ -n "${TEST_NO_NSPAWN:=}" ]; then echo "TEST: $TEST_DESCRIPTION [SKIPPED]: both QEMU and nspawn disabled" >&2 exit 0 fi - if [ -n "$TEST_QEMU_ONLY" ] && [ -z "$TEST_NO_NSPAWN" ]; then + if [ -n "${TEST_QEMU_ONLY:=}" ] && [ -z "$TEST_NO_NSPAWN" ]; then echo "TEST: $TEST_DESCRIPTION [SKIPPED]: QEMU-only tests requested" >&2 exit 0 fi - if [ -n "$TEST_PREFER_NSPAWN" ] && [ -z "$TEST_NO_NSPAWN" ]; then + if [ -n "${TEST_PREFER_NSPAWN:=}" ] && [ -z "$TEST_NO_NSPAWN" ]; then TEST_NO_QEMU=1 fi # Detect lib paths - [[ $libdir ]] || for libdir in /lib64 /lib; do + [[ "$libdir" ]] || for libdir in /lib64 /lib; do [[ -d $libdir ]] && libdirs+=" $libdir" && break done - [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do + [[ "$usrlibdir" ]] || for usrlibdir in /usr/lib64 /usr/lib; do [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break done @@ -2360,7 +2435,7 @@ do_test() { import_testdir import_initdir - testname="$(basename $PWD)" + testname="$(basename "$PWD")" while (($# > 0)); do case $1 in @@ -2368,7 +2443,7 @@ do_test() { echo "${testname} RUN: $TEST_DESCRIPTION" test_run "$2" ret=$? - if (( $ret == 0 )); then + if [ $ret -eq 0 ]; then echo "${testname} RUN: $TEST_DESCRIPTION [OK]" else echo "${testname} RUN: $TEST_DESCRIPTION [FAILED]"