validatefs: split out validating gpt label and type

No functional change, just refactoring.
This addresses https://github.com/systemd/systemd/pull/37434#discussion_r2088950725.
This commit is contained in:
Yu Watanabe
2025-05-15 02:34:18 +09:00
parent e4db2c9b83
commit f78e89b018

View File

@@ -246,6 +246,56 @@ static int validate_mount_point(const char *path, const ValidateFields *f) {
strna(joined), path);
}
static int validate_gpt_label(blkid_probe b, const ValidateFields *f) {
assert(b);
assert(f);
if (strv_isempty(f->gpt_label))
return 0;
const char *v = NULL;
(void) blkid_probe_lookup_value(b, "PART_ENTRY_NAME", &v, /* ret_len= */ NULL);
if (strv_contains(f->gpt_label, strempty(v)))
return 0;
_cleanup_free_ char *joined = strv_join(f->gpt_label, "', '");
return log_error_errno(
SYNTHETIC_ERRNO(EPERM),
"File system is supposed to be placed in a partition with labels '%s' only, but is placed in one labelled '%s', refusing.",
strna(joined), strempty(v));
}
static int validate_gpt_type(blkid_probe b, const ValidateFields *f) {
assert(b);
assert(f);
if (f->n_gpt_type_uuid == 0)
return 0;
const char *v = NULL;
(void) blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, /* ret_len= */ NULL);
sd_id128_t id;
if (!v || sd_id128_from_string(v, &id) < 0) {
_cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
return log_error_errno(
SYNTHETIC_ERRNO(EPERM),
"File system is supposed to be placed in a partition of type UUIDs %s only, but has no type, refusing.",
strna(joined));
}
FOREACH_ARRAY(u, f->gpt_type_uuid, f->n_gpt_type_uuid)
if (sd_id128_equal(*u, id))
return 0;
_cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
return log_error_errno(
SYNTHETIC_ERRNO(EPERM),
"File system is supposed to be placed in a partition of type UUIDs %s only, but has type '%s', refusing.",
strna(joined), SD_ID128_TO_UUID_STRING(id));
}
static int validate_gpt_metadata_one(sd_device *d, const char *path, const ValidateFields *f) {
int r;
@@ -286,50 +336,13 @@ static int validate_gpt_metadata_one(sd_device *d, const char *path, const Valid
if (!streq_ptr(v, "gpt"))
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "File system is supposed to be on a GPT partition table, but is not, refusing.");
if (!strv_isempty(f->gpt_label)) {
v = NULL;
(void) blkid_probe_lookup_value(b, "PART_ENTRY_NAME", &v, /* ret_len= */ NULL);
r = validate_gpt_label(b, f);
if (r < 0)
return r;
if (!strv_contains(f->gpt_label, strempty(v))) {
_cleanup_free_ char *joined = strv_join(f->gpt_label, "', '");
return log_error_errno(
SYNTHETIC_ERRNO(EPERM),
"File system is supposed to be placed in a partition with labels '%s' only, but is placed in one labelled '%s', refusing.",
strna(joined), strempty(v));
}
}
if (f->n_gpt_type_uuid > 0) {
v = NULL;
(void) blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, /* ret_len= */ NULL);
sd_id128_t id;
if (!v || sd_id128_from_string(v, &id) < 0) {
_cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
return log_error_errno(
SYNTHETIC_ERRNO(EPERM),
"File system is supposed to be placed in a partition of type UUIDs %s only, but has no type, refusing.",
strna(joined));
}
bool found = false;
FOREACH_ARRAY(u, f->gpt_type_uuid, f->n_gpt_type_uuid)
if (sd_id128_equal(*u, id)) {
found = true;
break;
}
if (!found) {
_cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
return log_error_errno(
SYNTHETIC_ERRNO(EPERM),
"File system is supposed to be placed in a partition of type UUIDs %s only, but has type '%s', refusing.",
strna(joined), SD_ID128_TO_UUID_STRING(id));
}
}
r = validate_gpt_type(b, f);
if (r < 0)
return r;
return 0;
}