Skip to contentSkip to conversation

util/grub-editenv: wire set_variables to optional fs_envblk

PATCH Avnish Chouhan 2 participants 4 messages Sep 17, 2025 – Sep 24, 2025 1 message not in the archive

lore
Share & tools
lore

Avnish Chouhan

author Reviewed-by
#1 lore raw
In reply to a message not in the archive
Quoted 39 lines · On 2025-09-15 14:39, [email protected] wrote:

Message: 1
Date: Mon, 15 Sep 2025 17:08:43 +0800
From: Michael Chang <[email protected]>
To: The development of GNU GRUB <[email protected]>
Cc: Neal Gompa <[email protected]>, Marta Lewandowska
<[email protected]>
Subject: [PATCH v2 4/9] util/grub-editenv: wire set_variables to
optional fs_envblk
Message-ID: <[email protected]>

This patch changes set_variables so that it can use an external environment block when one is present. The variable next_entry is written into the external block, env_block is treated as read only, and all other variables are written into the normal file based envblk.

A cleanup step is added to handle cases where GRUB at runtime writes variables into the external block because file based updates are not safe on a copy on write filesystem such as Btrfs. For example, the savedefault command can update saved_entry, and on Btrfs GRUB will place that update in the external block instead of the file envblk. If an older copy remains in the external block, it would override the newer value from the file envblk when GRUB first loads the file and then applies the external block on top of it. To avoid this, whenever a variable is updated in the file envblk, any same named key in the external block is deleted.

---
 util/grub-editenv.c | 55 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
util/grub-editenv.c +0 -0
index 26a81d2d0..d47adeb5e 100644
@@ -394,12 +394,33 @@ fs_envblk_write (grub_envblk_t envblk)
   fclose (fp);
 }
 

util/grub-editenv.c fs_envblk_write (grub_envblk_t envblk) · line 406

Quoted 10 lines
util/grub-editenv.c +9 -0
+struct var_lookup_ctx {
+  const char *varname;
+  bool found;
+};
+
+static int
+var_lookup_iter (const char *varname, const char *value __attribute__
 ((unused)), void *hook_data)
+{
+  struct var_lookup_ctx *ctx = (struct var_lookup_ctx *)hook_data;

Hi Michael,

Missing a space before "hook_data;"

Thank you!

Regards,
Avnish Chouhan

****

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author Reviewed-by
#3 lore raw

Re: [PATCH v2 5/9] util/grub-editenv: wire unset_variables to optional fs_envblk

In reply to a message not in the archive
Quoted 10 lines · On 2025-09-15 14:39, [email protected] wrote:

Message: 3
Date: Mon, 15 Sep 2025 17:08:44 +0800
From: Michael Chang <[email protected]>
To: The development of GNU GRUB <[email protected]>
Cc: Neal Gompa <[email protected]>, Marta Lewandowska
<[email protected]>
Subject: [PATCH v2 5/9] util/grub-editenv: wire unset_variables to
optional fs_envblk
Message-ID: <[email protected]>

Quoted 7 lines

This patch updates unset_variables so that removals are also applied to the external environment block when it is present. The code opens the external block, deletes the same named keys there, and then writes the external block back using fs_envblk_write. The file based envblk is still updated and written as before.

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author Reviewed-by
#4 lore raw

Re: [PATCH v2 6/9] util/grub-editenv: wire list_variables to optional fs_envblk

In reply to a message not in the archive
Quoted 15 lines · On 2025-09-15 14:39, [email protected] wrote:

Message: 2
Date: Mon, 15 Sep 2025 17:08:45 +0800
From: Michael Chang <[email protected]>
To: The development of GNU GRUB <[email protected]>
Cc: Neal Gompa <[email protected]>, Marta Lewandowska
<[email protected]>
Subject: [PATCH v2 6/9] util/grub-editenv: wire list_variables to
optional fs_envblk
Message-ID: <[email protected]>

This patch updates list_variables so that it also prints entries from the external environment block when one is present. The function first lists all variables from the file based envblk, then iterates over the external envblk and prints those as well.

Quoted 5 lines

The output format remains the same as before. The change makes it possible to inspect variables regardless of whether they are stored in the file envblk or in the reserved block.

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
In reply to Avnish Chouhan
Quoted 53 lines · Avnish Chouhan
Quoted 39 lines · On 2025-09-15 14:39, [email protected] wrote:

Message: 1
Date: Mon, 15 Sep 2025 17:08:43 +0800
From: Michael Chang <[email protected]>
To: The development of GNU GRUB <[email protected]>
Cc: Neal Gompa <[email protected]>, Marta Lewandowska
<[email protected]>
Subject: [PATCH v2 4/9] util/grub-editenv: wire set_variables to
optional fs_envblk
Message-ID: <[email protected]>

This patch changes set_variables so that it can use an external environment block when one is present. The variable next_entry is written into the external block, env_block is treated as read only, and all other variables are written into the normal file based envblk.

A cleanup step is added to handle cases where GRUB at runtime writes variables into the external block because file based updates are not safe on a copy on write filesystem such as Btrfs. For example, the savedefault command can update saved_entry, and on Btrfs GRUB will place that update in the external block instead of the file envblk. If an older copy remains in the external block, it would override the newer value from the file envblk when GRUB first loads the file and then applies the external block on top of it. To avoid this, whenever a variable is updated in the file envblk, any same named key in the external block is deleted.

---
 util/grub-editenv.c | 55 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)
util/grub-editenv.c +0 -0
index 26a81d2d0..d47adeb5e 100644
@@ -394,12 +394,33 @@ fs_envblk_write (grub_envblk_t envblk)
   fclose (fp);
 }
 

util/grub-editenv.c fs_envblk_write (grub_envblk_t envblk) · line 406

Quoted 10 lines
util/grub-editenv.c +9 -0
+struct var_lookup_ctx {
+  const char *varname;
+  bool found;
+};
+
+static int
+var_lookup_iter (const char *varname, const char *value __attribute__
 ((unused)), void *hook_data)
+{
+  struct var_lookup_ctx *ctx = (struct var_lookup_ctx *)hook_data;

Hi Michael,

Quoted 1 line

Missing a space before "hook_data;"

OK. I'll put a space the cast operator.

Thanks,
Michael

Quoted 10 lines

Thank you!

Regards,
Avnish Chouhan

****

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel