]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blkid-util: add blkid_probe_lookup_value_id128() helper 39084/head
authorLennart Poettering <lennart@poettering.net>
Mon, 8 Sep 2025 20:27:34 +0000 (22:27 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 24 Sep 2025 16:13:10 +0000 (18:13 +0200)
And similar, add a blkid_probe_lookup_value_u64() helper

src/home/homework-luks.c
src/shared/blkid-util.c
src/shared/blkid-util.h
src/shared/dissect-image.c
src/shared/find-esp.c

index 5a5d235d9d4cb82487e04600e085882206c71dd5..06c1195eb2b0e4963a39f4f74f3163bde4af59f9 100644 (file)
@@ -135,7 +135,7 @@ static int probe_file_system_by_fd(
                 sd_id128_t *ret_uuid) {
 
         _cleanup_(blkid_free_probep) blkid_probe b = NULL;
-        const char *fstype = NULL, *uuid = NULL;
+        const char *fstype = NULL;
         sd_id128_t id;
         int r;
 
@@ -172,11 +172,9 @@ static int probe_file_system_by_fd(
         if (!fstype)
                 return -ENOPKG;
 
-        (void) sym_blkid_probe_lookup_value(b, "UUID", &uuid, NULL);
-        if (!uuid)
+        r = blkid_probe_lookup_value_id128(b, "UUID", &id);
+        if (r == -ENXIO)
                 return -ENOPKG;
-
-        r = sd_id128_from_string(uuid, &id);
         if (r < 0)
                 return r;
 
index dd6af9401ac34a7577a4e4400a61e8429166122b..60f1332c26e82b1012ca40ecea9f459a55bfdd68 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "blkid-util.h"
 #include "log.h"
+#include "parse-util.h"
 #include "string-util.h"
 
 #if HAVE_BLKID
@@ -118,4 +119,27 @@ int blkid_partition_get_type_id128(blkid_partition p, sd_id128_t *ret) {
         return sd_id128_from_string(s, ret);
 }
 
+int blkid_probe_lookup_value_id128(blkid_probe b, const char *field, sd_id128_t *ret) {
+        assert(b);
+        assert(field);
+
+        const char *u = NULL;
+        (void) sym_blkid_probe_lookup_value(b, field, &u, /* ret_size= */ NULL);
+        if (!u)
+                return -ENXIO;
+
+        return sd_id128_from_string(u, ret);
+}
+
+int blkid_probe_lookup_value_u64(blkid_probe b, const char *field, uint64_t *ret) {
+        assert(b);
+        assert(field);
+
+        const char *u = NULL;
+        (void) sym_blkid_probe_lookup_value(b, field, &u, /* ret_size= */ NULL);
+        if (!u)
+                return -ENXIO;
+
+        return safe_atou64(u, ret);
+}
 #endif
index b6f072c7b6ffe46d8c89c055197438a7f318622f..e1202590dcfc5ace4d29e132d6b6002ea22fbade 100644 (file)
@@ -63,6 +63,8 @@ enum {
         _BLKID_SAFEPROBE_ERROR     = -1,
 };
 
+int blkid_probe_lookup_value_id128(blkid_probe b, const char *field, sd_id128_t *ret);
+int blkid_probe_lookup_value_u64(blkid_probe b, const char *field, uint64_t *ret);
 #else
 static inline int dlopen_libblkid(void) {
         return -EOPNOTSUPP;
index 20d6eed923b217d00dd0cb0f8e573fed131ca5aa..672c6a65cd56899230a5bbc852f254928e91b13e 100644 (file)
@@ -734,7 +734,7 @@ static int dissect_image(
         _cleanup_(blkid_free_probep) blkid_probe b = NULL;
         _cleanup_free_ char *generic_node = NULL;
         sd_id128_t generic_uuid = SD_ID128_NULL;
-        const char *pttype = NULL, *sptuuid = NULL;
+        const char *pttype = NULL;
         blkid_partlist pl;
         int r, generic_nr = -1, n_partitions;
 
@@ -842,7 +842,7 @@ static int dissect_image(
                 (void) sym_blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
                 if (STRPTR_IN_SET(usage, "filesystem", "crypto")) {
                         _cleanup_free_ char *t = NULL, *n = NULL, *o = NULL;
-                        const char *fstype = NULL, *options = NULL, *suuid = NULL;
+                        const char *fstype = NULL, *options = NULL;
                         _cleanup_close_ int mount_node_fd = -EBADF;
                         sd_id128_t uuid = SD_ID128_NULL;
                         PartitionPolicyFlags found_flags;
@@ -860,7 +860,11 @@ static int dissect_image(
                                 return -ENOPKG;
 
                         (void) sym_blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
-                        (void) sym_blkid_probe_lookup_value(b, "UUID", &suuid, NULL);
+
+                        /* blkid will return FAT's serial number as UUID, hence it is quite possible that
+                         * parsing this will fail. We'll ignore the ID, since it's just too short to be
+                         * useful as true identifier. */
+                        (void) blkid_probe_lookup_value_id128(b, "UUID", &uuid);
 
                         encrypted = streq_ptr(fstype, "crypto_LUKS");
 
@@ -889,15 +893,6 @@ static int dissect_image(
                                         return -ENOMEM;
                         }
 
-                        if (suuid) {
-                                /* blkid will return FAT's serial number as UUID, hence it is quite possible
-                                 * that parsing this will fail. We'll ignore the ID, since it's just too
-                                 * short to be useful as true identifier. */
-                                r = sd_id128_from_string(suuid, &uuid);
-                                if (r < 0)
-                                        log_debug_errno(r, "Failed to parse file system UUID '%s', ignoring: %m", suuid);
-                        }
-
                         r = make_partition_devname(devname, diskseq, -1, flags, &n);
                         if (r < 0)
                                 return r;
@@ -962,12 +957,7 @@ static int dissect_image(
                         return -EPROTONOSUPPORT;
         }
 
-        (void) sym_blkid_probe_lookup_value(b, "PTUUID", &sptuuid, NULL);
-        if (sptuuid) {
-                r = sd_id128_from_string(sptuuid, &m->image_uuid);
-                if (r < 0)
-                        log_debug_errno(r, "Failed to parse partition table UUID '%s', ignoring: %m", sptuuid);
-        }
+        (void) blkid_probe_lookup_value_id128(b, "PTUUID", &m->image_uuid);
 
         errno = 0;
         pl = sym_blkid_probe_get_partitions(b);
index b1c9ae28a7a283633dcb0ba89b662ed405228dab..77910ed9b4c2c0474a4f6ae700c63bcd31948234 100644 (file)
@@ -132,13 +132,9 @@ static int verify_esp_blkid(
                                        SYNTHETIC_ERRNO(searching ? EADDRNOTAVAIL : ENODEV),
                                        "File system \"%s\" has wrong type for an EFI System Partition (ESP).", node);
 
-        errno = 0;
-        r = sym_blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL);
-        if (r != 0)
-                return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition entry UUID of \"%s\": %m", node);
-        r = sd_id128_from_string(v, &uuid);
+        r = blkid_probe_lookup_value_id128(b, "PART_ENTRY_UUID", &uuid);
         if (r < 0)
-                return log_error_errno(r, "Partition \"%s\" has invalid UUID \"%s\".", node, v);
+                return log_error_errno(r, "Failed to probe partition entry UUID of \"%s\": %m", node);
 
         errno = 0;
         r = sym_blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
@@ -148,21 +144,13 @@ static int verify_esp_blkid(
         if (r < 0)
                 return log_error_errno(r, "Failed to parse PART_ENTRY_NUMBER field.");
 
-        errno = 0;
-        r = sym_blkid_probe_lookup_value(b, "PART_ENTRY_OFFSET", &v, NULL);
-        if (r != 0)
-                return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition offset of \"%s\": %m", node);
-        r = safe_atou64(v, &pstart);
+        r = blkid_probe_lookup_value_u64(b, "PART_ENTRY_OFFSET", &pstart);
         if (r < 0)
-                return log_error_errno(r, "Failed to parse PART_ENTRY_OFFSET field.");
+                return log_error_errno(r, "Failed to probe partition offset of \"%s\": %m", node);
 
-        errno = 0;
-        r = sym_blkid_probe_lookup_value(b, "PART_ENTRY_SIZE", &v, NULL);
-        if (r != 0)
-                return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe partition size of \"%s\": %m", node);
-        r = safe_atou64(v, &psize);
+        r = blkid_probe_lookup_value_u64(b, "PART_ENTRY_SIZE", &psize);
         if (r < 0)
-                return log_error_errno(r, "Failed to parse PART_ENTRY_SIZE field.");
+                return log_error_errno(r, "Failed to probe partition size of \"%s\": %m", node);
 #endif
 
         if (ret_part)
@@ -651,13 +639,9 @@ static int verify_xbootldr_blkid(
                                               searching ? SYNTHETIC_ERRNO(EADDRNOTAVAIL) : SYNTHETIC_ERRNO(ENODEV),
                                               "%s: Partition has wrong PART_ENTRY_TYPE=%s for XBOOTLDR partition.", node, v);
 
-                errno = 0;
-                r = sym_blkid_probe_lookup_value(b, "PART_ENTRY_UUID", &v, NULL);
+                r = blkid_probe_lookup_value_id128(b, "PART_ENTRY_UUID", &uuid);
                 if (r != 0)
                         return log_error_errno(errno_or_else(EIO), "%s: Failed to probe PART_ENTRY_UUID: %m", node);
-                r = sd_id128_from_string(v, &uuid);
-                if (r < 0)
-                        return log_error_errno(r, "%s: Partition has invalid UUID PART_ENTRY_TYPE=%s: %m", node, v);
 
         } else if (streq(type, "dos")) {