From: Yu Watanabe Date: Wed, 14 May 2025 17:34:18 +0000 (+0900) Subject: validatefs: split out validating gpt label and type X-Git-Tag: v258-rc1~610^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F37447%2Fhead;p=thirdparty%2Fsystemd.git validatefs: split out validating gpt label and type No functional change, just refactoring. This addresses https://github.com/systemd/systemd/pull/37434#discussion_r2088950725. --- diff --git a/src/validatefs/validatefs.c b/src/validatefs/validatefs.c index ea0b159f500..8dfe5073da1 100644 --- a/src/validatefs/validatefs.c +++ b/src/validatefs/validatefs.c @@ -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); - - 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); + r = validate_gpt_label(b, f); + if (r < 0) + return r; - 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; }