Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nHvJN-0007xD-R5 for mharc-grub-devel@gnu.org; Wed, 09 Feb 2022 17:19:41 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42596) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nHvJH-0007sz-HA for grub-devel@gnu.org; Wed, 09 Feb 2022 17:19:35 -0500 Received: from [2607:f8b0:4864:20::732] (port=34376 helo=mail-qk1-x732.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nHvJF-00087U-2K for grub-devel@gnu.org; Wed, 09 Feb 2022 17:19:35 -0500 Received: by mail-qk1-x732.google.com with SMTP id bs32so3068146qkb.1 for ; Wed, 09 Feb 2022 14:19:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=WKwW5DUw/Rsex4y2PjD8fKj8kH+X8b1M3in+Fodb1/w=; b=eYMYdett6rcGcUojOMnjrVjBoMObmbTJu9/6AEOv98aFDpbg5uWOBdtxwfJ++iedzj npCEThdZ96l3OzAisM/g3okOhroqhokHb8hCwQRgJwnLHqIVbuOywlsAeFHICufDNO9t N7X3fak/qtI9qeKGkriDLVeKtrqPhj8VZ2IT21Xh9KRXwxSUpalMVVHlB++ZZKQvKY1M LLKSUe7TDkL/+pkhk6Y8jRMo5j7UBLpeDjwK/pCk8D2fy/Qj8O7qsy8msRYzO1PpQcGK 9TWSbyVfOQsfkAQRG5NSjWFkayQkNJ3FX55UFIyVmWLDBskhbEQ365Q+cT1kPL+u2qf9 3smQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=WKwW5DUw/Rsex4y2PjD8fKj8kH+X8b1M3in+Fodb1/w=; b=XzxuhAJ7L2uNuN18EAS5Jwcc/sLYBQS5A2FfiPc+ux4i5rMLbCixMAvayxOP+oZzyf gPFFt1aengXTAnA5EUYQ4CxQDOr8dD6OMjYtYABkOdRAcz9SwOjZUFTYsrHaj0wO/KT8 Uo4PFjmpeKyGZvOFZrXD7nfuZsA7urs8VP0Mcb1lX8lVAGnskyV8NlOfuFjTAinUeu8o NLhDM5Qywz3Ydr4Jaj71z6DLX3M8J6lfNThOLlP3YNLb9sb5uPnJZRDEPI37Rrnz9dic aQPF8werlCkuKMPhUCm13BXqA83OwxP9y7dCqePFsXuIQmqUMM7RjKSMuM5vx8x/TuKt NVgw== X-Gm-Message-State: AOAM531PsSFJYQUPkuJN+Ufh3xsILeZLmKItbHr7YTm1f4BsWQMDFchM jFlmOlQ/VxfQwW/nj1P3sRF03TA6Nf8= X-Google-Smtp-Source: ABdhPJywnj7Eb/QYMcjr+YJeuVrlwmehiUYJqtto7Pa5idjqp9yL2ihqgcPDduZ9mCk27W3+wnG4jA== X-Received: by 2002:a37:ac08:: with SMTP id e8mr2349471qkm.712.1644445171068; Wed, 09 Feb 2022 14:19:31 -0800 (PST) Received: from ?IPV6:2602:ae:1883:e000:ac95:6ea0:4340:9c31? ([2602:ae:1883:e000:ac95:6ea0:4340:9c31]) by smtp.gmail.com with ESMTPSA id u17sm8687091qkj.44.2022.02.09.14.19.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Feb 2022 14:19:30 -0800 (PST) Message-ID: <23dbc142-8c11-88b9-7245-8d43523bb481@gmail.com> Date: Wed, 9 Feb 2022 17:19:30 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v3] commands/search: Add support to search by PARTUUID Content-Language: en-US To: grub-devel@gnu.org References: <20210304190105.1345-1-vkuzmichev@dev.rtsoft.ru> <20210415135907.18557-1-vkuzmichev@dev.rtsoft.ru> <20220201205115.3abe6862@crass-HP-ZBook-15-G2> From: Nicholas Vinson In-Reply-To: <20220201205115.3abe6862@crass-HP-ZBook-15-G2> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::732 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::732; envelope-from=nvinson234@gmail.com; helo=mail-qk1-x732.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, NICE_REPLY_A=-0.001, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Feb 2022 22:19:35 -0000 On 2/1/22 21:54, Glenn Washburn wrote: > Hi Vitaly, > > Now that GRUB is out of a feature freeze, there's a chance this can > make it in. > > On Thu, 15 Apr 2021 16:59:07 +0300 > Vitaly Kuzmichev via Grub-devel wrote: > >> Improve 'search' grub-shell command with functionality to search for >> a partition by PARTUUID string. This is useful on systems where FSUUID >> is not guaranteed to be constant, e.g. it is not preserved between >> system updates, and modifying grub.cfg is undesired. >> >> V2: >> This patch is based on Michael Grzeschik version from 27 May 2019 [1] >> with the following changes: >> >> 1. Addressed review feedback from Daniel Kiper [2] and Nick Vinson [3]. >> >> 2. Added support for GPT PARTUUID. >> >> 3. Moved MBR disk signature reading from partmap/msdos.c to >> commands/search.c to read it only when it is needed. >> >> [1] https://lists.gnu.org/archive/html/grub-devel/2019-05/msg00124.html >> [2] https://lists.gnu.org/archive/html/grub-devel/2019-05/msg00131.html >> [3] https://lists.gnu.org/archive/html/grub-devel/2019-05/msg00134.html >> >> V3: >> Minor coding style and spelling fixes. >> >> Signed-off-by: Vitaly Kuzmichev >> --- >> grub-core/Makefile.core.def | 5 ++ >> grub-core/commands/search.c | 80 +++++++++++++++++++++++++++- >> grub-core/commands/search_partuuid.c | 5 ++ >> grub-core/commands/search_wrap.c | 10 +++- >> include/grub/search.h | 2 + >> 5 files changed, 98 insertions(+), 4 deletions(-) >> create mode 100644 grub-core/commands/search_partuuid.c > > There should be a documentation change as well to document the new > parameter. > >> >> diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def >> index 8022e1c0a..4568943e3 100644 >> --- a/grub-core/Makefile.core.def >> +++ b/grub-core/Makefile.core.def >> @@ -1063,6 +1063,11 @@ module = { >> common = commands/search_file.c; >> }; >> >> +module = { >> + name = search_partuuid; >> + common = commands/search_partuuid.c; >> +}; >> + >> module = { >> name = search_fs_uuid; >> common = commands/search_uuid.c; >> diff --git a/grub-core/commands/search.c b/grub-core/commands/search.c >> index ed090b3af..5014982fd 100644 >> --- a/grub-core/commands/search.c >> +++ b/grub-core/commands/search.c >> @@ -54,6 +54,28 @@ struct search_ctx >> int is_cache; >> }; >> >> +#ifdef DO_SEARCH_PARTUUID >> +#include >> +#include > > These are better just put at the top with the rest of the includes. I > don't think they need to be ifdef'd, I wouldn't complain if they were > either. > >> + >> +/* Helper for iterate_device. */ >> +static char * >> +gpt_guid_to_str (grub_gpt_part_guid_t *gpt_guid) >> +{ >> + /* Convert mixed-endian UUID from bytes to string */ >> + return >> + grub_xasprintf ( >> + "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", >> + grub_le_to_cpu32(gpt_guid->data1), >> + grub_le_to_cpu16(gpt_guid->data2), >> + grub_le_to_cpu16(gpt_guid->data3), >> + gpt_guid->data4[0], gpt_guid->data4[1], >> + gpt_guid->data4[2], gpt_guid->data4[3], >> + gpt_guid->data4[4], gpt_guid->data4[5], >> + gpt_guid->data4[6], gpt_guid->data4[7]); > > I think its better to do the compare with dashes stripped out of both. > Or even better to use grub_uuidcascmp (which comes from my patch > series[1] that I hope to get accepted at some point). > If you want to compare without dashes, I think it'd make more sense to convert the UUID into an array of 2 64-bit numbers. The resultant comparisons would be much more efficient than a string compare. Additionally, the conversion might be slightly less ugly than converting to a string. grub_uint64_t uuid[2] = { grub_le_to_cpu32(gpt_guid->data1) << 32 | grub_le_to_cpu16(gpt_guid->data2) << 16 | grub_le_to_cpu16(gpt_guid->data2), gpt_guid->data4[0] << 56 | gpt_guid->data4[1] << 48 | gpt_guid->data4[2] << 40 | gpt_guid->data4[3] << 32 | gpt_guid->data4[4] << 24 | gpt_guid->data4[5] << 16 | gpt_guid->data4[6] << 8 | gpt_guid->data4[7] }; >> +} >> +#endif >> + >> /* Helper for FUNC_NAME. */ >> static int >> iterate_device (const char *name, void *data) >> @@ -66,13 +88,63 @@ iterate_device (const char *name, void *data) >> name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9') >> return 1; >> >> -#ifdef DO_SEARCH_FS_UUID >> +#if defined (DO_SEARCH_FS_UUID) || defined (DO_SEARCH_PARTUUID) >> #define compare_fn grub_strcasecmp >> #else >> #define compare_fn grub_strcmp >> #endif >> >> -#ifdef DO_SEARCH_FILE >> +#ifdef DO_SEARCH_PARTUUID >> + { >> + struct grub_gpt_partentry gptdata; >> + grub_uint32_t disk_sig; >> + grub_disk_t ptdisk; >> + grub_disk_t disk; >> + char *part_uuid; >> + >> + ptdisk = grub_disk_open (name); >> + if (ptdisk && ptdisk->partition && ptdisk->partition->partmap) >> + { >> + if (grub_strcmp (ptdisk->partition->partmap->name, "gpt") == 0) >> + { >> + disk = grub_disk_open (ptdisk->name); >> + if (disk && grub_disk_read (disk, ptdisk->partition->offset, >> + ptdisk->partition->index, >> + sizeof (gptdata), &gptdata) == 0) >> + { >> + part_uuid = gpt_guid_to_str (&gptdata.guid); >> + >> + if (part_uuid && compare_fn (part_uuid, ctx->key) == 0) >> + found = 1; >> + if (part_uuid) >> + grub_free (part_uuid); >> + } >> + if (disk) >> + grub_disk_close (disk); >> + } >> + else if (grub_strcmp (ptdisk->partition->partmap->name, "msdos") == 0) >> + { >> + disk = grub_disk_open (ptdisk->name); >> + if (disk && grub_disk_read (disk, 0, >> + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC, >> + sizeof(disk_sig), &disk_sig) == 0) >> + { >> + part_uuid = grub_xasprintf ("%08x-%02x", >> + grub_le_to_cpu32(disk_sig), >> + ptdisk->partition->index + 1); > > Maybe strip off the dashes for the compare here too. > Same suggestion here, but the top 88 bits would always be 0. Regards, Nicholas Vinson >> + if (part_uuid && compare_fn (part_uuid, ctx->key) == 0) >> + found = 1; >> + if (part_uuid) >> + grub_free (part_uuid); >> + } >> + if (disk) >> + grub_disk_close (disk); >> + } >> + } >> + if (ptdisk) >> + grub_disk_close (ptdisk); >> + } >> +#elif defined (DO_SEARCH_FILE) >> { >> char *buf; >> grub_file_t file; >> @@ -313,6 +385,8 @@ static grub_command_t cmd; >> >> #ifdef DO_SEARCH_FILE >> GRUB_MOD_INIT(search_fs_file) >> +#elif defined (DO_SEARCH_PARTUUID) >> +GRUB_MOD_INIT(search_partuuid) >> #elif defined (DO_SEARCH_FS_UUID) >> GRUB_MOD_INIT(search_fs_uuid) >> #else >> @@ -327,6 +401,8 @@ GRUB_MOD_INIT(search_label) >> >> #ifdef DO_SEARCH_FILE >> GRUB_MOD_FINI(search_fs_file) >> +#elif defined (DO_SEARCH_PARTUUID) >> +GRUB_MOD_FINI(search_partuuid) >> #elif defined (DO_SEARCH_FS_UUID) >> GRUB_MOD_FINI(search_fs_uuid) >> #else >> diff --git a/grub-core/commands/search_partuuid.c b/grub-core/commands/search_partuuid.c >> new file mode 100644 >> index 000000000..e4aa20b5f >> --- /dev/null >> +++ b/grub-core/commands/search_partuuid.c >> @@ -0,0 +1,5 @@ >> +#define DO_SEARCH_PARTUUID 1 >> +#define FUNC_NAME grub_search_partuuid >> +#define COMMAND_NAME "search.partuuid" >> +#define HELP_MESSAGE N_("Search devices by PARTUUID. If VARIABLE is specified, the first device found is set to a variable.") >> +#include "search.c" >> diff --git a/grub-core/commands/search_wrap.c b/grub-core/commands/search_wrap.c >> index 47fc8eb99..f516b584e 100644 >> --- a/grub-core/commands/search_wrap.c >> +++ b/grub-core/commands/search_wrap.c >> @@ -36,6 +36,8 @@ static const struct grub_arg_option options[] = >> 0, 0}, >> {"fs-uuid", 'u', 0, N_("Search devices by a filesystem UUID."), >> 0, 0}, >> + {"partuuid", 'p', 0, N_("Search devices by a PARTUUID."), > > s/PARTUUID/partition UUID/ > >> + 0, 0}, >> {"set", 's', GRUB_ARG_OPTION_OPTIONAL, >> N_("Set a variable to the first device found."), N_("VARNAME"), >> ARG_TYPE_STRING}, >> @@ -71,6 +73,7 @@ enum options >> SEARCH_FILE, >> SEARCH_LABEL, >> SEARCH_FS_UUID, >> + SEARCH_PARTUUID, >> SEARCH_SET, >> SEARCH_NO_FLOPPY, >> SEARCH_HINT, >> @@ -186,6 +189,9 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args) >> else if (state[SEARCH_FS_UUID].set) >> grub_search_fs_uuid (id, var, state[SEARCH_NO_FLOPPY].set, >> hints, nhints); >> + else if (state[SEARCH_PARTUUID].set) >> + grub_search_partuuid (id, var, state[SEARCH_NO_FLOPPY].set, >> + hints, nhints); >> else if (state[SEARCH_FILE].set) >> grub_search_fs_file (id, var, state[SEARCH_NO_FLOPPY].set, >> hints, nhints); >> @@ -206,8 +212,8 @@ GRUB_MOD_INIT(search) >> GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH, >> N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]" >> " NAME"), >> - N_("Search devices by file, filesystem label" >> - " or filesystem UUID." >> + N_("Search devices by file, filesystem label," >> + " PARTUUID or filesystem UUID." > > s/PARTUUID/partition UUID/ > >> " If --set is specified, the first device found is" >> " set to a variable. If no variable name is" >> " specified, `root' is used."), >> diff --git a/include/grub/search.h b/include/grub/search.h >> index d80347df3..7755645f6 100644 >> --- a/include/grub/search.h >> +++ b/include/grub/search.h >> @@ -25,5 +25,7 @@ void grub_search_fs_uuid (const char *key, const char *var, int no_floppy, >> char **hints, unsigned nhints); >> void grub_search_label (const char *key, const char *var, int no_floppy, >> char **hints, unsigned nhints); >> +void grub_search_partuuid (const char *key, const char *var, int no_floppy, >> + char **hints, unsigned nhints); >> >> #endif > > Glenn > > [1] https://lists.gnu.org/archive/html/grub-devel/2021-03/msg00290.html > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel