Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: detect loop devices with deleted files as an invariant #14568

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(deleted) is unlikely to shwo up in device name or file name, so we can be less picky about the format I think

Suggested change
losetup -l | grep -F ' (deleted)' > "$TESTSTMP/tests.invariant.$n"
losetup -l | grep -F '(deleted)' > "$TESTSTMP/tests.invariant.$n"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part is relevant. The space is not quoted otherwise. I'd keep this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did an experiment and I ran into another fragile part of the userspace API where escaping is just not done. The files are literally called like that, neither is deleted:

zyga@falka:~/snapd$ losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                         DIO LOG-SEC
/dev/loop1         0      0         0  0 /home/zyga/snapd/potato (deleted)   0     512
/dev/loop0         0      0         0  0 /home/zyga/snapd/potato             0     512
zyga@falka:~/snapd$ cat /sys/class/block/loop[01]/loop/backing_file
/home/zyga/snapd/potato
/home/zyga/snapd/potato (deleted)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zyga@falka:~/snapd$ losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                   DIO LOG-SEC
/dev/loop1         0      0         0  0 /home/zyga/snapd/potato (deleted) (deleted)   0     512
/dev/loop0         0      0         0  0 /home/zyga/snapd/potato (deleted)             0     512

The joke is that text formats are easy for humans and machines to parse.

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
Loading