]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loop-util: do not try to enumerate partitions twice 24611/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 8 Sep 2022 20:12:07 +0000 (05:12 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 8 Sep 2022 20:16:41 +0000 (05:16 +0900)
src/shared/blockdev-util.c
src/shared/loop-util.c

index 66807c673d1f3c13bf54483895d354e72a6b0360..0087d764b6f47e1c143ab6cc73e848fb1b960df0 100644 (file)
@@ -559,6 +559,7 @@ int block_device_remove_all_partitions(sd_device *dev, int fd) {
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
         _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
         _cleanup_close_ int fd_close = -1;
+        bool has_partitions = false;
         sd_device *part;
         int r, k = 0;
 
@@ -593,6 +594,8 @@ int block_device_remove_all_partitions(sd_device *dev, int fd) {
                 const char *v, *devname;
                 int nr;
 
+                has_partitions = true;
+
                 r = sd_device_get_devname(part, &devname);
                 if (r < 0)
                         return r;
@@ -612,14 +615,14 @@ int block_device_remove_all_partitions(sd_device *dev, int fd) {
                 }
                 if (r < 0) {
                         log_debug_errno(r, "Failed to remove partition %s: %m", devname);
-                        k = k ?: r;
+                        k = k < 0 ? k : r;
                         continue;
                 }
 
                 log_debug("Removed partition %s", devname);
         }
 
-        return k;
+        return k < 0 ? k : has_partitions;
 }
 
 int block_device_has_partitions(sd_device *dev) {
index 1cc3f2642eba2e4f79266d34b9063ec48a7c28a2..49f4b2bf6efaf704d18d1251d02ec4f9776aaec9 100644 (file)
@@ -277,19 +277,13 @@ static int loop_configure(
          * superficially is detached but still has partition block devices associated for it. Let's then
          * manually remove the partitions via BLKPG, and tell the caller we did that via EUCLEAN, so they try
          * again. */
-        r = block_device_has_partitions(dev);
+        r = block_device_remove_all_partitions(dev, fd);
         if (r < 0)
                 return r;
-        if (r > 0) {
-                /* Remove all partitions, and report this to the caller, to try again, and count this as
+        if (r > 0)
+                /* Removed all partitions. Let's report this to the caller, to try again, and count this as
                  * an attempt. */
-
-                r = block_device_remove_all_partitions(dev, fd);
-                if (r < 0)
-                        return r;
-
                 return -EUCLEAN;
-        }
 
         if (!loop_configure_broken) {
                 /* Acquire uevent seqnum immediately before attaching the loopback device. This allows