]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dissect-image: prefer PARTN= uevent property over "partition" sysfs attr
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Apr 2021 09:31:14 +0000 (11:31 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 22 Apr 2021 12:31:27 +0000 (14:31 +0200)
The kernel will send us a PARTN= uevent proprty with partition add
events, let's use it instead of going for the "partition" sysfs attr.
It's less racy that way and there are reports the sysfs attr shows up
after the device, which makes it evern worse.

src/shared/dissect-image.c

index 53b613702e1b2c9b3c4c55a032c9984288a28995..63a81d42ad8fe8a3627e7bac239e6f9075fd79ba 100644 (file)
@@ -139,7 +139,11 @@ static int enumerator_for_parent(sd_device *d, sd_device_enumerator **ret) {
         return 0;
 }
 
-static int device_is_partition(sd_device *d, sd_device *expected_parent, blkid_partition pp) {
+static int device_is_partition(
+                sd_device *d,
+                sd_device *expected_parent,
+                blkid_partition pp) {
+
         const char *v, *parent_syspath, *expected_parent_syspath;
         blkid_loff_t bsize, bstart;
         uint64_t size, start;
@@ -174,9 +178,16 @@ static int device_is_partition(sd_device *d, sd_device *expected_parent, blkid_p
         if (!path_equal(parent_syspath, expected_parent_syspath))
                 return false; /* Has a different parent than what we need, not interesting to us */
 
-        r = sd_device_get_sysattr_value(d, "partition", &v);
+        /* On kernel uevents the partition number we may find in the PARTN= field. Let's use that preferably,
+         * since it's cheaper and more importantly: the sysfs attribute "partition" appear to become
+         * available late, hence let's use the property instead, which is available at the moment we see the
+         * uevent. */
+        r = sd_device_get_property_value(d, "PARTN", &v);
+        if (r == -ENOENT)
+                r = sd_device_get_sysattr_value(d, "partition", &v);
         if (r < 0)
                 return r;
+
         r = safe_atoi(v, &partno);
         if (r < 0)
                 return r;