]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blkid: add helpers that get gpt partition uuid as sd_id128_t
authorLennart Poettering <lennart@poettering.net>
Tue, 25 Oct 2022 15:32:01 +0000 (17:32 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 29 Nov 2022 15:05:17 +0000 (16:05 +0100)
just some refactoring to make things simpler.

src/home/homework-luks.c
src/partition/repart.c
src/shared/blkid-util.h
src/shared/dissect-image.c
src/udev/udev-builtin-blkid.c

index 48e8cd1808dd3ee5afcdca7f30028ce19e105341..3d3369f299878a128455b0c5dc19f92350667301 100644 (file)
@@ -695,9 +695,8 @@ static int luks_validate(
                 return errno > 0 ? -errno : -EIO;
 
         for (int i = 0; i < n; i++) {
-                blkid_partition pp;
                 sd_id128_t id = SD_ID128_NULL;
-                const char *sid;
+                blkid_partition pp;
 
                 errno = 0;
                 pp = blkid_partlist_get_partition(pl, i);
@@ -710,15 +709,12 @@ static int luks_validate(
                 if (!streq_ptr(blkid_partition_get_name(pp), label))
                         continue;
 
-                sid = blkid_partition_get_uuid(pp);
-                if (sid) {
-                        r = sd_id128_from_string(sid, &id);
-                        if (r < 0)
-                                log_debug_errno(r, "Couldn't parse partition UUID %s, weird: %m", sid);
 
-                        if (!sd_id128_is_null(partition_uuid) && !sd_id128_equal(id, partition_uuid))
-                                continue;
-                }
+                r = blkid_partition_get_uuid_id128(pp, &id);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to read partition UUID, ignoring: %m");
+                else if (!sd_id128_is_null(partition_uuid) && !sd_id128_equal(id, partition_uuid))
+                        continue;
 
                 if (found)
                         return -ENOPKG;
index 7857f7b3d199d5c06aea420680d9b21d1cca57f0..879f42617ab9089477ff5bf7a37f3710ea61251d 100644 (file)
@@ -4899,21 +4899,18 @@ static int resolve_copy_blocks_auto_candidate(
                 return false;
         }
 
-        t = blkid_partition_get_uuid(pp);
-        if (isempty(t)) {
-                log_debug("Partition %u:%u has no UUID.",
-                          major(partition_devno), minor(partition_devno));
+        r = blkid_partition_get_uuid_id128(pp, &u);
+        if (r == -ENXIO) {
+                log_debug_errno(r, "Partition " DEVNUM_FORMAT_STR " has no UUID.", DEVNUM_FORMAT_VAL(partition_devno));
                 return false;
         }
-
-        r = sd_id128_from_string(t, &u);
         if (r < 0) {
-                log_debug_errno(r, "Failed to parse partition UUID \"%s\": %m", t);
+                log_debug_errno(r, "Failed to read partition UUID of " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(partition_devno));
                 return false;
         }
 
-        log_debug("Automatically found partition %u:%u of right type " SD_ID128_FORMAT_STR ".",
-                  major(partition_devno), minor(partition_devno),
+        log_debug("Automatically found partition " DEVNUM_FORMAT_STR " of right type " SD_ID128_FORMAT_STR ".",
+                  DEVNUM_FORMAT_VAL(partition_devno),
                   SD_ID128_FORMAT_VAL(pt_parsed));
 
         if (ret_uuid)
index aa444990fd8b4b83ef6ccc8cff94824cd46fd870..d4e78627b8cbc04ea181b5ce956ce8a7005ff712 100644 (file)
@@ -4,7 +4,34 @@
 #if HAVE_BLKID
 #  include <blkid.h>
 
+#  include "sd-id128.h"
+
 #  include "macro.h"
+#  include "string-util.h"
 
 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(blkid_probe, blkid_free_probe, NULL);
+
+static inline int blkid_partition_get_uuid_id128(blkid_partition p, sd_id128_t *ret) {
+        const char *s;
+
+        assert(p);
+
+        s = blkid_partition_get_uuid(p);
+        if (isempty(s))
+                return -ENXIO;
+
+        return sd_id128_from_string(s, ret);
+}
+
+static inline int blkid_partition_get_type_id128(blkid_partition p, sd_id128_t *ret) {
+        const char *s;
+
+        assert(p);
+
+        s = blkid_partition_get_type_string(p);
+        if (isempty(s))
+                return -ENXIO;
+
+        return sd_id128_from_string(s, ret);
+}
 #endif
index 708355872fc324ab36ad8a3b8862628197c94255..d7a047e68b8de76d0fffe7f4bf9ed1b357745756 100644 (file)
@@ -627,22 +627,22 @@ static int dissect_image(
                 }
 
                 if (is_gpt) {
-                        const char *stype, *sid, *fstype = NULL, *label;
+                        const char *fstype = NULL, *label;
                         sd_id128_t type_id, id;
                         GptPartitionType type;
                         bool rw = true, growfs = false;
 
-                        sid = blkid_partition_get_uuid(pp);
-                        if (!sid)
-                                continue;
-                        if (sd_id128_from_string(sid, &id) < 0)
+                        r = blkid_partition_get_uuid_id128(pp, &id);
+                        if (r < 0) {
+                                log_debug_errno(r, "Failed to read partition UUID, ignoring: %m");
                                 continue;
+                        }
 
-                        stype = blkid_partition_get_type_string(pp);
-                        if (!stype)
-                                continue;
-                        if (sd_id128_from_string(stype, &type_id) < 0)
+                        r = blkid_partition_get_type_id128(pp, &type_id);
+                        if (r < 0) {
+                                log_debug_errno(r, "Failed to read partition type UUID, ignoring: %m");
                                 continue;
+                        }
 
                         type = gpt_partition_type_from_uuid(type_id);
 
@@ -966,7 +966,7 @@ static int dissect_image(
                                 _cleanup_close_ int mount_node_fd = -1;
                                 _cleanup_free_ char *o = NULL;
                                 sd_id128_t id = SD_ID128_NULL;
-                                const char *sid, *options = NULL;
+                                const char *options = NULL;
 
                                 /* First one wins */
                                 if (m->partitions[PARTITION_XBOOTLDR].found)
@@ -978,9 +978,7 @@ static int dissect_image(
                                                 return mount_node_fd;
                                 }
 
-                                sid = blkid_partition_get_uuid(pp);
-                                if (sid)
-                                        (void) sd_id128_from_string(sid, &id);
+                                (void) blkid_partition_get_uuid_id128(pp, &id);
 
                                 options = mount_options_from_designator(mount_options, PARTITION_XBOOTLDR);
                                 if (options) {
index 9f5646ffdd997a2a0eece0c3873d5b81c5b09143..9b5dfbe33be4c6dd4eefde258e354457a403e7d9 100644 (file)
@@ -119,8 +119,9 @@ static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) {
 
 #if defined(SD_GPT_ROOT_NATIVE) && ENABLE_EFI
 
-        _cleanup_free_ char *root_id = NULL, *root_label = NULL;
+        _cleanup_free_ char *root_label = NULL;
         bool found_esp_or_xbootldr = false;
+        sd_id128_t root_id = SD_ID128_NULL;
         int r;
 
         assert(pr);
@@ -137,34 +138,32 @@ static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) {
         int nvals = blkid_partlist_numof_partitions(pl);
         for (int i = 0; i < nvals; i++) {
                 blkid_partition pp;
-                const char *stype, *sid, *label;
-                sd_id128_t type;
+                const char *label;
+                sd_id128_t type, id;
 
                 pp = blkid_partlist_get_partition(pl, i);
                 if (!pp)
                         continue;
 
-                sid = blkid_partition_get_uuid(pp);
-                if (!sid)
+                r = blkid_partition_get_uuid_id128(pp, &id);
+                if (r < 0) {
+                        log_debug_errno(r, "Failed to get partition UUID, ignoring: %m");
                         continue;
+                }
 
-                label = blkid_partition_get_name(pp); /* returns NULL if empty */
-
-                stype = blkid_partition_get_type_string(pp);
-                if (!stype)
+                r = blkid_partition_get_type_id128(pp, &type);
+                if (r < 0) {
+                        log_debug_errno(r, "Failed to get partition type UUID, ignoring: %m");
                         continue;
+                }
 
-                if (sd_id128_from_string(stype, &type) < 0)
-                        continue;
+                label = blkid_partition_get_name(pp); /* returns NULL if empty */
 
                 if (sd_id128_in_set(type, SD_GPT_ESP, SD_GPT_XBOOTLDR)) {
-                        sd_id128_t id, esp_or_xbootldr;
+                        sd_id128_t esp_or_xbootldr;
 
                         /* We found an ESP or XBOOTLDR, let's see if it matches the ESP/XBOOTLDR we booted from. */
 
-                        if (sd_id128_from_string(sid, &id) < 0)
-                                continue;
-
                         r = efi_loader_get_device_part_uuid(&esp_or_xbootldr);
                         if (r < 0)
                                 return r;
@@ -182,10 +181,8 @@ static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) {
                         /* We found a suitable root partition, let's remember the first one, or the one with
                          * the newest version, as determined by comparing the partition labels. */
 
-                        if (!root_id || strverscmp_improved(label, root_label) > 0) {
-                                r = free_and_strdup(&root_id, sid);
-                                if (r < 0)
-                                        return r;
+                        if (sd_id128_is_null(root_id) || strverscmp_improved(label, root_label) > 0) {
+                                root_id = id;
 
                                 r = free_and_strdup(&root_label, label);
                                 if (r < 0)
@@ -196,8 +193,8 @@ static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) {
 
         /* We found the ESP/XBOOTLDR on this disk, and also found a root partition, nice! Let's export its
          * UUID */
-        if (found_esp_or_xbootldr && root_id)
-                udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT_UUID", root_id);
+        if (found_esp_or_xbootldr && !sd_id128_is_null(root_id))
+                udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id));
 #endif
 
         return 0;