From 346c55ef2b862e9e38b5e5e6966b73ca41c156b0 Mon Sep 17 00:00:00 2001 From: Zygmunt Krynicki Date: Fri, 13 Sep 2024 13:33:01 +0200 Subject: [PATCH] tests: detect loop devices with deleted files as an invariant When a loop device is created with a given backing file, the file itself may be deleted. The kernel keeps the reference to the file alive but it also adjusts the name of the file to have the suffix " (deleted)". We should not have loop devices with this property across tests. They might indicate that a mount sticks around and leaks through a reference somehow, or that the low-level loop-back utilities are buggy. Signed-off-by: Zygmunt Krynicki --- tests/lib/tools/tests.invariant | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/lib/tools/tests.invariant b/tests/lib/tools/tests.invariant index 4651cfd19f0..843b0934a1c 100755 --- a/tests/lib/tools/tests.invariant +++ b/tests/lib/tools/tests.invariant @@ -11,6 +11,7 @@ show_help() { echo " leftover-defer-sh: defer.sh must not be left over by tests" echo " broken-snaps: snaps must not be left around that are in a broken state" echo " segmentation-violations: snapd must not have segmentation-violation errors in journal logs" + echo " deleted-loop-devices: no loop devices associated with deleted files must exist" } if [ $# -eq 0 ]; then @@ -207,6 +208,17 @@ check_fakestore_cleaned() { fi } +check_deleted_loop_devices() { + n="$1" # invariant name + + losetup -l | grep -F ' (deleted)' > "$TESTSTMP/tests.invariant.$n" + if [ -s "$TESTSTMP/tests.invariant.$n" ]; then + echo "tests.invariant: detected loopback devices associated with deleted files" >&2 + cat "$TESTSTMP/tests.invariant.$n" >&2 + return 1 + fi +} + check_invariant() { case "$1" in root-files-in-home) @@ -236,6 +248,9 @@ check_invariant() { check-fakestore-cleaned) check_fakestore_cleaned ;; + deleted-loop-devices) + check_deleted_loop_devices "$1" + ;; *) echo "tests.invariant: unknown invariant $1" >&2 exit 1 @@ -244,7 +259,7 @@ check_invariant() { } main() { - ALL_INVARIANTS="root-files-in-home crashed-snap-confine lxcfs-mounted stray-dbus-daemon leftover-defer-sh broken-snaps cgroup-scopes segmentation-violations check-fakestore-cleaned" + ALL_INVARIANTS="root-files-in-home crashed-snap-confine lxcfs-mounted stray-dbus-daemon leftover-defer-sh broken-snaps cgroup-scopes segmentation-violations check-fakestore-cleaned deleted-loop-devices" case "$action" in check)