Skip to contentSkip to conversation

common/metadump: fix _cleanup_verify_metadump system destructor fleet

PATCH In review Darrick J. Wong 2 participants 2 messages Feb 5, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common/metadump: fix _cleanup_verify_metadump system destructor fleet

From: Darrick J. Wong <[email protected]>

Zorro reported that the cleanup function in common/xfs_metadump_tests zapped one of his test machines because of a well known shell variable expansion + globbing footgun. This can trigger when running fstests on older configurations where a test adds _cleanup_verify_metadump to the local _cleanup function but exits before calling _setup_verify_metadump to set XFS_METADUMP_IMG to a non-empty value.

Redesign the cleanup function to check for non-empty values of XFS_METADUMP_{FILE,IMG} before proceeding with the rm. Change the globbed parameter of "rm -f $XFS_METADUMP_IMG*" to a for loop so that if the glob does not match any files, the loop variable will be set to a path that does not resolve anywhere.

Longer term maybe we ought to set -u or something. Or figure out how to make the root filesystem readonly when we're running a test.

---
 common/xfs_metadump_tests |   19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
common/xfs_metadump_tests +15 -4
index 5a5579b6b9..c8652f16ea 100644
@@ -15,12 +15,23 @@ _setup_verify_metadump()
 
 _cleanup_verify_metadump()
 {
+	local img
+
 	_scratch_unmount &>> $seqres.full
 
-	losetup -n -a -O BACK-FILE,NAME | grep "^$XFS_METADUMP_IMG" | while read backing ldev; do
-		losetup -d "$ldev"
-	done
-	rm -f "$XFS_METADUMP_FILE" "$XFS_METADUMP_IMG"*
+	test -n "$XFS_METADUMP_FILE" && rm -f "$XFS_METADUMP_FILE"
+
+	if [ -n "$XFS_METADUMP_IMG" ]; then
+		losetup -n -a -O BACK-FILE,NAME | grep "^$XFS_METADUMP_IMG" | while read backing ldev; do
+			losetup -d "$ldev"
+		done
+
+		# Don't call rm directly with a globbed argument here to avoid
+		# issues issues with variable expansions.
+		for img in "$XFS_METADUMP_IMG"*; do
+			test -e "$img" && rm -f "$img"
+		done
+	fi
 }
 
 # Can xfs_metadump snapshot the fs metadata to a v1 metadump file?
#1 lore raw
Quoted 15 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Zorro reported that the cleanup function in common/xfs_metadump_tests zapped one of his test machines because of a well known shell variable expansion + globbing footgun. This can trigger when running fstests on older configurations where a test adds _cleanup_verify_metadump to the local _cleanup function but exits before calling _setup_verify_metadump to set XFS_METADUMP_IMG to a non-empty value.

Redesign the cleanup function to check for non-empty values of XFS_METADUMP_{FILE,IMG} before proceeding with the rm. Change the globbed parameter of "rm -f $XFS_METADUMP_IMG*" to a for loop so that if the glob does not match any files, the loop variable will be set to a path that does not resolve anywhere.

Quoted 8 lines

Longer term maybe we ought to set -u or something. Or figure out how to make the root filesystem readonly when we're running a test.

---

Hi Darrick,

This change looks good to me, but I've reset the for-next branch, remove the v2024.02.04 totally, to avoid it kills others' systems. Now there's not v2024.02.04 release anymore, so you can resend a V2 of:
[PATCHSET] fstests: random fixes for v2024.01.14
or resend the:
[PATCH 05/10] common: refactor metadump v1 and v2 tests
if you don't have more changes on others.

Quoted 1 line
 common/xfs_metadump_tests |   19 +++++++++++++++----

I saw you name this patch as common/metadump, if already have a patchset which changes the common/xfs_metadump_tests to common/metadump, feel free to send to me directly as V2 :)

Thanks,
Zorro

Quoted 35 lines
 1 file changed, 15 insertions(+), 4 deletions(-)
common/xfs_metadump_tests +15 -4
index 5a5579b6b9..c8652f16ea 100644
@@ -15,12 +15,23 @@ _setup_verify_metadump()
 
 _cleanup_verify_metadump()
 {
+	local img
+
 	_scratch_unmount &>> $seqres.full
 
-	losetup -n -a -O BACK-FILE,NAME | grep "^$XFS_METADUMP_IMG" | while read backing ldev; do
-		losetup -d "$ldev"
-	done
-	rm -f "$XFS_METADUMP_FILE" "$XFS_METADUMP_IMG"*
+	test -n "$XFS_METADUMP_FILE" && rm -f "$XFS_METADUMP_FILE"
+
+	if [ -n "$XFS_METADUMP_IMG" ]; then
+		losetup -n -a -O BACK-FILE,NAME | grep "^$XFS_METADUMP_IMG" | while read backing ldev; do
+			losetup -d "$ldev"
+		done
+
+		# Don't call rm directly with a globbed argument here to avoid
+		# issues issues with variable expansions.
+		for img in "$XFS_METADUMP_IMG"*; do
+			test -e "$img" && rm -f "$img"
+		done
+	fi
 }
 
 # Can xfs_metadump snapshot the fs metadata to a v1 metadump file?