mkcw: mkcw_check_image use bats run_with_log

Add `run_with_log` to mkcw tests.

Add `sleep 1` during cleanup between attempting `luksClose`
and unmounting the filesystem mounted on the device /dev/mapper/"$uuid".
Without this somehow we end up in a state where mount is still being
used by the kernel because when we do `lsof /dev/mapper/"$uuid"` it
shows nothing but `dmsetup info -c $uuid` shows the device is still
under use. Adding `sleep 1` in between somehow fixes this.

Also this problem with `cryptsetup` is pretty common for reference
one thread which I found https://lore.kernel.org/all/508950BA.1030309@dennis.birkholz.biz/T/

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
This commit is contained in:
flouthoc 2025-02-17 14:23:26 -08:00
parent d7d7878622
commit c87fd8e191
No known key found for this signature in database
GPG Key ID: F1993E9E45D69299
2 changed files with 54 additions and 5 deletions

View File

@ -296,6 +296,49 @@ function rm() {
run_unshared rm "$@"
}
#################
# run_with_log # Logs command before running it
#################
#
function run_with_log() {
local expected_rc=0
local retry=1
local cmd="$*"
case "$1" in
[0-9]) expected_rc=$1; shift;;
[1-9][0-9]) expected_rc=$1; shift;;
[12][0-9][0-9]) expected_rc=$1; shift;;
'?') expected_rc= ; shift;; # ignore exit code
--retry) retry=3; shift;; # retry with sleep of 1 sec
esac
while [ $retry -gt 0 ]; do
retry=$(( retry - 1 ))
echo "$_LOG_PROMPT $cmd"
run "$@"
echo "$output"
if [ "$status" -ne 0 ]; then
echo -n "[ rc=$status ";
if [ -n "$expected_rc" ]; then
if [ "$status" -eq "$expected_rc" ]; then
echo -n "(expected) ";
else
echo -n "(** EXPECTED $expected_rc **) ";
fi
fi
echo "]"
fi
if [ -n "$expected_rc" ]; then
if [ "$status" -eq "$expected_rc" ]; then
return
elif [ $retry -gt 0 ]; then
echo "[ RETRYING ]" >&2
sleep 1
else
die "exit code is $status; expected $expected_rc"
fi
fi
done
}
#################
# run_buildah # Invoke buildah, with timeout, using BATS 'run'

View File

@ -22,9 +22,9 @@ function mkcw_check_image() {
# Decrypt, mount, and take a look around.
uuid=$(cryptsetup luksUUID "$mountpoint"/disk.img)
cryptsetup luksOpen --key-file "$TEST_SCRATCH_DIR"/key "$mountpoint"/disk.img "$uuid"
run_with_log cryptsetup luksOpen --key-file "$TEST_SCRATCH_DIR"/key "$mountpoint"/disk.img "$uuid"
mkdir -p "$TEST_SCRATCH_DIR"/mount
mount /dev/mapper/"$uuid" "$TEST_SCRATCH_DIR"/mount
run_with_log mount /dev/mapper/"$uuid" "$TEST_SCRATCH_DIR"/mount
# Should have a not-empty config file with parts of an image's config.
test -s "$TEST_SCRATCH_DIR"/mount/.krun_config.json
# Should have a /tmp directory, at least.
@ -42,9 +42,15 @@ function mkcw_check_image() {
fi
# Clean up.
umount "$TEST_SCRATCH_DIR"/mount
cryptsetup luksClose "$uuid"
buildah umount "$ctrID"
run_with_log umount -f "$TEST_SCRATCH_DIR"/mount
# `Retry` if `luksClose` fails with defaults of `run_with_log` because
# when unmounting the filesystem mounted on the device /dev/mapper/"$uuid"
# without `retry` somehow we end up in a state where mount is still being
# used by the kernel because when we do `lsof /dev/mapper/"$uuid"` it
# shows nothing but `dmsetup info -c $uuid` shows the device is still
# under use. Adding `--retry` in between somehow fixes this.
run_with_log --retry cryptsetup luksClose "$uuid"
run_buildah umount "$ctrID"
}
@test "mkcw-convert" {