blkid-util: add blkid_probe_lookup_value_id128() helper

And similar, add a blkid_probe_lookup_value_u64() helper
This commit is contained in:
Lennart Poettering
2025-09-24 18:13:10 +02:00
parent c349edfe49
commit 4b2b2f3ab7
5 changed files with 44 additions and 46 deletions
+3 -5
View 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;
+24
View 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
+2
View 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;
+8 -18
View 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);
+7 -23
View 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")) {