Skip to content

Commit

Permalink
tests: detect loop devices with deleted files as an invariant
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zyga committed Oct 2, 2024
1 parent 0d7c868 commit 346c55e
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tests/lib/tools/tests.invariant
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 346c55e

Please sign in to comment.