]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/dissect-image.c
dissect-image: wait for the main device and all partitions to be known by udev
[thirdparty/systemd.git] / src / shared / dissect-image.c
index 0147c45d8e067882d9299c929714db8ecc6d03bc..4c2e41c8b8ee16b57d7cab7807b981d6f4273e56 100644 (file)
@@ -40,6 +40,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "tmpfile-util.h"
+#include "udev-util.h"
 #include "user-util.h"
 #include "xattr-util.h"
 
@@ -165,6 +166,10 @@ static int wait_for_partitions_to_appear(
                 if (device_is_mmc_special_partition(q))
                         continue;
 
+                r = device_wait_for_initialization(q, "block", NULL);
+                if (r < 0)
+                        return r;
+
                 n++;
         }
 
@@ -219,10 +224,17 @@ static int loop_wait_for_partitions_to_appear(
                 sd_device *d,
                 unsigned num_partitions,
                 sd_device_enumerator **ret_enumerator) {
+        _cleanup_(sd_device_unrefp) sd_device *device = NULL;
         int r;
 
+        log_debug("Waiting for device (parent + %d partitions) to appear...", num_partitions);
+
+        r = device_wait_for_initialization(d, "block", &device);
+        if (r < 0)
+                return r;
+
         for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) {
-                r = wait_for_partitions_to_appear(fd, d, num_partitions, ret_enumerator);
+                r = wait_for_partitions_to_appear(fd, device, num_partitions, ret_enumerator);
                 if (r != -EAGAIN)
                         return r;
         }
@@ -320,6 +332,10 @@ int dissect_image(
         if (!m)
                 return -ENOMEM;
 
+        r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
+        if (r < 0)
+                return r;
+
         if (!(flags & DISSECT_IMAGE_GPT_ONLY) &&
             (flags & DISSECT_IMAGE_REQUIRE_ROOT)) {
                 const char *usage = NULL;
@@ -353,6 +369,10 @@ int dissect_image(
 
                         m->encrypted = streq_ptr(fstype, "crypto_LUKS");
 
+                        r = loop_wait_for_partitions_to_appear(fd, d, 0, &e);
+                        if (r < 0)
+                                return r;
+
                         *ret = TAKE_PTR(m);
 
                         return 0;
@@ -374,10 +394,6 @@ int dissect_image(
         if (!pl)
                 return -errno ?: -ENOMEM;
 
-        r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
-        if (r < 0)
-                return r;
-
         r = loop_wait_for_partitions_to_appear(fd, d, blkid_partlist_numof_partitions(pl), &e);
         if (r < 0)
                 return r;