osmith has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/40314?usp=email )
Change subject: testenv: fix not aborting on build_initrd error ......................................................................
testenv: fix not aborting on build_initrd error
Without this patch the build_initrd function keeps going on errors:
+ cp -a --parents /lib/modules/6.1.0-34-amd64/kernel/drivers/net/net_failover.ko /tmp/testenv-ggsn_tests-osmo_ggsn_v4_only-20250509-1040-a21668df-euc9e2fs/ggsn/_initrd/usr cp: 'lib/modules/6.1.0-34-amd64/kernel/drivers/net': No such file or directory + cp -a --parents /lib/modules/6.1.0-34-amd64/kernel/drivers/net/virtio_net.ko /tmp/testenv-ggsn_tests-osmo_ggsn_v4_only-20250509-1040-a21668df-euc9e2fs/ggsn/_initrd/usr …
The reason for that is that even though the script runs with set -e, the -e flag gets ignored between if…then where the build_initrd shell function gets called:
if ! build_initrd >build_initrd.log 2>&1; then qemu_initrd_exit_error "build_initrd.log" fi
Fix it by using a trap instead.
Related: https://unix.stackexchange.com/a/65564 Change-Id: I2870b7e7ba28b2afc72e86b7a3bc389103564c80 --- M _testenv/data/scripts/qemu/qemu_functions.sh M ggsn_tests/osmo-ggsn/run.sh 2 files changed, 18 insertions(+), 8 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-ttcn3-hacks refs/changes/14/40314/1
diff --git a/_testenv/data/scripts/qemu/qemu_functions.sh b/_testenv/data/scripts/qemu/qemu_functions.sh index 9c92a37..46e7273 100755 --- a/_testenv/data/scripts/qemu/qemu_functions.sh +++ b/_testenv/data/scripts/qemu/qemu_functions.sh @@ -136,10 +136,12 @@ | gzip -1 > "$INITRD_DIR".gz ) }
+# Trap for qemu_build_initrd_with_log_redirect +# Building the initrd is quite verbose, therefore put it in a log file +# and only output its contents on error (see e.g. osmo-ggsn/run.sh) qemu_initrd_exit_error() { - # Building the initrd is quite verbose, therefore put it in a log file - # and only output its contents on error (see e.g. osmo-ggsn/run.sh) - cat "$1" + trap - EXIT INT TERM 0 + cat "build_initrd.log" set +x echo echo "ERROR: failed to build the initrd!" @@ -147,6 +149,18 @@ exit 1 }
+# Users of qemu_functions should define a build_initrd function that calls +# qemu_initrd_init, qemu_initrd_add_* and qemu_initrd_finish. See ggsn's run.sh +# as example. The function here redirects the very verbose output of building +# the initrd to a separate log file, and aborts and display the log on error. +qemu_build_initrd_with_log_redirect() { + # Use a trap instead of "if !build_initrd; then ..." logic as "set -e" + # gets otherwise: https://unix.stackexchange.com/a/65564 + trap qemu_initrd_exit_error EXIT INT TERM 0 + build_initrd >build_initrd.log 2>&1 + trap - EXIT INT TERM 0 +} + qemu_random_mac() { printf "52:54:" date "+%c %N" | sha1sum | sed 's/(.{2})/\1:/g' | cut -d: -f 1-4 diff --git a/ggsn_tests/osmo-ggsn/run.sh b/ggsn_tests/osmo-ggsn/run.sh index ae77484..27e633e 100755 --- a/ggsn_tests/osmo-ggsn/run.sh +++ b/ggsn_tests/osmo-ggsn/run.sh @@ -32,9 +32,5 @@ }
. qemu_functions.sh - -if ! build_initrd >build_initrd.log 2>&1; then - qemu_initrd_exit_error "build_initrd.log" -fi - +qemu_build_initrd_with_log_redirect qemu_run