Blame SOURCES/0060-kpartx-free-loop-device-after-listing-partitions.patch

d5de56
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d5de56
From: Benjamin Marzinski <bmarzins@redhat.com>
d5de56
Date: Tue, 9 Feb 2021 17:16:04 -0600
d5de56
Subject: [PATCH] kpartx: free loop device after listing partitions
d5de56
d5de56
If "kpartx -l" is run on a file that doesn't already have a loop device
d5de56
associated with it, it will create a loop device to run the command.
d5de56
Starting with da59d15c6 ("Fix loopback file with kpartx -av"), it will
d5de56
not free the loop device when exitting. This is because it checks if the
d5de56
the file it stat()ed is a regular file, before freeing the loop device.
d5de56
However, after da59d15c6, stat() is rerun on the loop device itself, so
d5de56
the check fails.  There is no need to check this, if loopcreated is
d5de56
true, then the file will be a kpartx created loop device, and should be
d5de56
freed.
d5de56
d5de56
Also, keep kpartx from printing that the loop device has been removed
d5de56
at normal verbosity.
d5de56
d5de56
Fixes: da59d15c6 ("Fix loopback file with kpartx -av")
d5de56
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
d5de56
---
d5de56
 kpartx/kpartx.c | 9 +++++----
d5de56
 1 file changed, 5 insertions(+), 4 deletions(-)
d5de56
d5de56
diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c
d5de56
index 653ce0c8..a337a07b 100644
d5de56
--- a/kpartx/kpartx.c
d5de56
+++ b/kpartx/kpartx.c
d5de56
@@ -407,7 +407,7 @@ main(int argc, char **argv){
d5de56
 					fprintf(stderr, "can't del loop : %s\n",
d5de56
 					       loopdev);
d5de56
 				r = 1;
d5de56
-			} else
d5de56
+			} else if (verbose)
d5de56
 				fprintf(stderr, "loop deleted : %s\n", loopdev);
d5de56
 		}
d5de56
 		goto end;
d5de56
@@ -649,16 +649,17 @@ main(int argc, char **argv){
d5de56
 		if (n > 0)
d5de56
 			break;
d5de56
 	}
d5de56
-	if (what == LIST && loopcreated && S_ISREG (buf.st_mode)) {
d5de56
+	if (what == LIST && loopcreated) {
d5de56
 		if (fd != -1)
d5de56
 			close(fd);
d5de56
 		if (del_loop(device)) {
d5de56
 			if (verbose)
d5de56
-				printf("can't del loop : %s\n",
d5de56
+				fprintf(stderr, "can't del loop : %s\n",
d5de56
 					device);
d5de56
 			exit(1);
d5de56
 		}
d5de56
-		printf("loop deleted : %s\n", device);
d5de56
+		if (verbose)
d5de56
+			fprintf(stderr, "loop deleted : %s\n", device);
d5de56
 	}
d5de56
 
d5de56
 end:
d5de56
-- 
d5de56
2.17.2
d5de56