aboutsummaryrefslogtreecommitdiffstats
path: root/lib/loopdev.c
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2021-04-20 13:20:12 +0200
committerKarel Zak <kzak@redhat.com>2021-04-20 13:20:12 +0200
commitd4423cce9b9001c9de7ebc6f64f6cc2bb854944c (patch)
tree0fec029fc4704b0421df9a44a215cc31c93fe995 /lib/loopdev.c
parent01480c61e139bc37ee5df348309c2c6e9b0f63d1 (diff)
downloadutil-linux-d4423cce9b9001c9de7ebc6f64f6cc2bb854944c.tar.gz
lib/loopdev: fix is_loopdev() to be usable with partitions
The current implementation of the function does not care if the device is whole-disk device or partition, all is loopdev. This is regression as the original is_loopdev() version was based on whole-disk devices major numbers only. Fixes: https://github.com/karelzak/util-linux/issues/1202 Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/loopdev.c')
-rw-r--r--lib/loopdev.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/loopdev.c b/lib/loopdev.c
index b946acf319..1eef15d896 100644
--- a/lib/loopdev.c
+++ b/lib/loopdev.c
@@ -641,7 +641,7 @@ int is_loopdev(const char *device)
rc = 0;
else if (major(st.st_rdev) == LOOPDEV_MAJOR)
rc = 1;
- else {
+ else if (sysfs_devno_is_wholedisk(st.st_rdev)) {
/* It's possible that kernel creates a device with a different
* major number ... check by /sys it's really loop device.
*/
@@ -1881,3 +1881,22 @@ int loopdev_count_by_backing_file(const char *filename, char **loopdev)
return count;
}
+#ifdef TEST_PROGRAM_LOOPDEV
+int main(int argc, char *argv[])
+{
+ if (argc < 2)
+ goto usage;
+
+ if (strcmp(argv[1], "--is-loopdev") == 0 && argc == 3)
+ printf("%s: %s\n", argv[2], is_loopdev(argv[2]) ? "OK" : "FAIL");
+ else
+ goto usage;
+
+ return EXIT_SUCCESS;
+usage:
+ fprintf(stderr, "usage: %1$s --is-loopdev <dev>\n",
+ program_invocation_short_name);
+ return EXIT_FAILURE;
+}
+#endif
+