Skip to contentSkip to conversation

kpartx: compare image filenames with backing files

PATCH In review Yuval Turgeman 3 participants 3 messages Jul 16, 2018 – Jul 27, 2018 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kpartx: compare image filenames with backing files

When running kpartx -d, find_loop_by_file tries to compare the image's realpath with the output from loopinfo.lo_name, which may hold only the basename of the image, making kpartx exit without finding the loopdev for the image.

In case the original comparison fails, this patch will check the image's realpath with the loop device's backing_file so that find_loop_by_file will return the correct value.

---
 kpartx/lopart.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
kpartx/lopart.c +30 -0
index 69c1eda1..9b652554 100644
@@ -69,6 +69,8 @@ char *find_loop_by_file(const char *filename)
 	struct loop_info loopinfo;
 	const char VIRT_BLOCK[] = "/sys/devices/virtual/block";
 	char path[PATH_MAX];
+	char bf_path[PATH_MAX];
+	char backing_file[PATH_MAX];
 
 	dir = opendir(VIRT_BLOCK);
 	if (!dir)
@@ -122,6 +124,34 @@ char *find_loop_by_file(const char *filename)
 			found = realpath(path, NULL);
 			break;
 		}
+
+		/*
+		 * filename is a realpath, while loopinfo.lo_name may hold just the
+		 * basename.  If that's the case, try to match filename against the
+		 * backing_file entry for this loop entry
+		 */
+		if (snprintf(bf_path, PATH_MAX, "%s/%s/loop/backing_file", VIRT_BLOCK,
+					 dent->d_name) >= PATH_MAX)
+			continue;
+
+		fd = open(bf_path, O_RDONLY);
+		if (fd < 0)
+			continue;
+
+		bytes_read = read(fd, backing_file, sizeof(backing_file) - 1);
+		if (bytes_read <= 0) {
+			close(fd);
+			continue;
+		}
+
+		close(fd);
+
+		backing_file[bytes_read-1] = '\0';
+
+		if (0 == strcmp(filename, backing_file)) {
+			found = realpath(path, NULL);
+			break;
+		}
 	}
 	closedir(dir);
 	return found;
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Yuval Turgeman

When running kpartx -d, find_loop_by_file tries to compare the image's realpath with the output from loopinfo.lo_name, which may hold only the basename of the image, making kpartx exit without finding the loopdev for the image.

In case the original comparison fails, this patch will check the image's realpath with the loop device's backing_file so that find_loop_by_file will return the correct value.

Looks good.

Quoted 61 lines
---
 kpartx/lopart.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
kpartx/lopart.c +30 -0
index 69c1eda1..9b652554 100644
@@ -69,6 +69,8 @@ char *find_loop_by_file(const char *filename)
 	struct loop_info loopinfo;
 	const char VIRT_BLOCK[] = "/sys/devices/virtual/block";
 	char path[PATH_MAX];
+	char bf_path[PATH_MAX];
+	char backing_file[PATH_MAX];
 
 	dir = opendir(VIRT_BLOCK);
 	if (!dir)
@@ -122,6 +124,34 @@ char *find_loop_by_file(const char *filename)
 			found = realpath(path, NULL);
 			break;
 		}
+
+		/*
+		 * filename is a realpath, while loopinfo.lo_name may hold just the
+		 * basename.  If that's the case, try to match filename against the
+		 * backing_file entry for this loop entry
+		 */
+		if (snprintf(bf_path, PATH_MAX, "%s/%s/loop/backing_file", VIRT_BLOCK,
+					 dent->d_name) >= PATH_MAX)
+			continue;
+
+		fd = open(bf_path, O_RDONLY);
+		if (fd < 0)
+			continue;
+
+		bytes_read = read(fd, backing_file, sizeof(backing_file) - 1);
+		if (bytes_read <= 0) {
+			close(fd);
+			continue;
+		}
+
+		close(fd);
+
+		backing_file[bytes_read-1] = '\0';
+
+		if (0 == strcmp(filename, backing_file)) {
+			found = realpath(path, NULL);
+			break;
+		}
 	}
 	closedir(dir);
 	return found;
Signature · 6 lines
2.17.1

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#2 lore raw
In reply to Yuval Turgeman
Quoted 8 lines · Yuval Turgeman

When running kpartx -d, find_loop_by_file tries to compare the image's realpath with the output from loopinfo.lo_name, which may hold only the basename of the image, making kpartx exit without finding the loopdev for the image.

In case the original comparison fails, this patch will check the image's realpath with the loop device's backing_file so that find_loop_by_file will return the correct value.

That seems like it does the same as my patch[1] from February, but in a more sane way. "Good" I did not follow up more on that :) /me will check if it solves the problem shortly.

[1] https://www.redhat.com/archives/dm-devel/2018-February/msg00019.html

Signature · 2 lines
debian developer - deb.li/jak | jak-linux.org - free software dev
ubuntu core developer                              i speak de, en