Skip to contentSkip to conversation

Add support for external environment block on Btrfs

PATCH In review Michael Chang via Grub-devel 3 participants 24 messages Oct 17, 2025 – Oct 30, 2025

lore
Share & tools
lore
#0 lore raw

v6 cover Add support for external environment block on Btrfs

This patch series adds support for storing the GRUB environment block in a reserved area of the Btrfs header. On copy on write filesystems such as Btrfs, the normal file based envblk cannot be updated safely at runtime because block addresses are not stable. The reserved area provides a fixed location that GRUB can write directly, allowing commands such as grub-reboot and savedefault to work on Btrfs volumes.

The series proceeds in small chunks to keep each change buildable and easier to review. The first patches add new data structures and helpers for creating, opening, and writing an environment block in the reserved area. Later patches update set_variables, unset_variables, and list_variables so they can use the external block when it is present. An entry is added to the Btrfs header to reserve space at 256 KiB for the environment block. Finally, grub.cfg is modified so that load_env and save_env use the external block automatically when env_block is defined.

The first two patches are new in this series as feedback from the review to use a portable string specifier for size_t. Unfortunately the z length modifier is missing in GRUB printf routine's format specifier. The new patches add support and test case for that. In theory they can be reviewed separately.

And also yet another new patch "btrfs: update doc link for bootloader support" added at the end to address v3 review feedback.

v2:
- Define ENV_BTRFS_OFFSET as 256*1024
- Do not conflate type and variable definitions
- Align typedef with struct declaration to follow coding style
- Use bool as the return type of is_abstraction()
- Add "if (dev->disk != NULL)" check in is_abstraction()
- Refine the loop logic in is_abstraction() tests
- Remove extra indentation and redundant lines in read_envblk_fs
- Use off_t and size_t for offset and size variables, fix similar cases throughout
- Use explicit check "(fp == NULL)" instead of "(! fp)", fix similar cases throughout
- Use bool for the "found" field in var_lookup_ctx
- Add documentation describing the Btrfs environment block and special environment block variables
v3:
- Replace !fp with explicit (fp == NULL) for clarity
- Fix missing blank line before return statement
- Fix missing space after cast operator
v4:
- Rename ENV_BTRFS_OFFSET to GRUB_ENV_BTRFS_OFFSET
- Move GRUB_ENV_BTRFS_OFFSET into "btrfs: add environment block to reserved header area"
- Adjust patch order to resolve relocated GRUB_ENV_BTRFS_OFFSET
- Add patch "btrfs: update doc link for bootloader support"
- Do better typedef
- Add heads-up comments to both fs_envblk_spec and btrfs_head that they have to stay in sync
- Fix a few places to use explicit checks for NULL, true/false etc
- Use goto labels to cleanup consistently
- Mark function names with trailing "()" in commits and comments
- Rename envblk_fs to envblk_on_block for better clarity
- Avoid redundant curly braces in if .. else if clause
- Highlight overflow guard around envioronment block in embed_region
- Rename a few functions for clarity
- Use portable printf formats for off_t and size_t
- Support z length modifier in GRUB printf format string
- Add z modifier to printf unit test
v5:
- Move fs_envblk_spec table into "btrfs: add environment block to reserved header area"
- Move "util/grub-editenv: add basic structures and probe call for external envblk" the 9th patch behind all wiring patches and rename it to "util/grub-editenv: add probe call for external envblk" as it now only does probing
- Add fs_envblk_init() to wire open helper in the base structures into patch "util/grub-editenv: add fs_envblk open helper"
- Add "Man 5 btrfs" an alternate to the doc link
v6:
- Move fs_envblk_spec structure typedef and variable from "btrfs: add environment block to reserved header area" into "util/grub-editenv:
add fs_envblk open helper". The fs_envblk_spec remains empty since no Btrfs area is defined yet.
- Reorder "btrfs: add environment block to reserved header area" to follow all groundwork patches, and provide the defined reserved area to fs_envblk_spec so that it becomes supported in grub-editenv.

Michael Chang (12):
misc: add z length modifier support
tests: add z modifier printf tests util/grub-editenv: add fs_envblk open helper util/grub-editenv: add fs_envblk write helper util/grub-editenv: wire set_variables to optional fs_envblk util/grub-editenv: wire unset_variables to optional fs_envblk util/grub-editenv: wire list_variables to optional fs_envblk util/grub-editenv: add probe call for external envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
docs: add Btrfs env block and special env vars
btrfs: update doc link for bootloader support

 docs/grub.texi           |  60 ++++++
 grub-core/fs/btrfs.c     |  13 +-
 grub-core/kern/misc.c    |  20 ++
 include/grub/fs.h        |   2 +
 tests/printf_unit_test.c |  10 +
 util/grub-editenv.c      | 407 ++++++++++++++++++++++++++++++++++++++-
 util/grub.d/00_header.in |  26 ++-
 7 files changed, 529 insertions(+), 9 deletions(-)
Signature · 7 lines
2.51.0


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

PATCH v6 1/12 misc: add z length modifier support

Add support for the 'z' length modifier in the printf code. This allows printing of size_t and ssize_t values using %zu, %zd and related formats. The parser maps 'z' to the correct integer width based on sizeof (size_t).

---
 grub-core/kern/misc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
grub-core/kern/misc.c +20 -0
index 2b7922393..ff614add5 100644
@@ -26,6 +26,7 @@
 #include <grub/i18n.h>
 #include <grub/types.h>
 #include <grub/charset.h>
+#include <stddef.h>
 
 union printf_arg
 {
@@ -739,6 +740,9 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
   COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
   COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
 		       || sizeof (int) == sizeof (void *));
+  COMPILE_TIME_ASSERT (sizeof (size_t) == sizeof (unsigned)
+		       || sizeof (size_t) == sizeof (unsigned long)
+		       || sizeof (size_t) == sizeof (unsigned long long));
 
   fmt = fmt0;
   while ((c = *fmt++) != 0)
@@ -773,11 +777,17 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	fmt++;
 
       c = *fmt++;
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  goto do_count;
+	}
       if (c == 'l')
 	c = *fmt++;
       if (c == 'l')
 	c = *fmt++;
 
+ do_count:
       switch (c)
 	{
 	case 'p':
@@ -874,6 +884,14 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	  continue;
 	}
 
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  if (sizeof (size_t) == sizeof (unsigned long))
+	    longfmt = 1;
+	  else if (sizeof (size_t) == sizeof (unsigned long long))
+	    longfmt = 2;
+	}
       if (c == 'l')
 	{
 	  c = *fmt++;
@@ -1050,6 +1068,8 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
 	}
 
       c = *fmt++;
+      if (c == 'z')
+	c = *fmt++;
       if (c == 'l')
 	c = *fmt++;
       if (c == 'l')
Signature · 7 lines
2.51.0


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

Re: [PATCH v6 01/12] misc: add z length modifier support

Quoted 53 lines · Michael Chang via Grub-devel

Add support for the 'z' length modifier in the printf code. This allows printing of size_t and ssize_t values using %zu, %zd and related formats. The parser maps 'z' to the correct integer width based on sizeof (size_t).

---
 grub-core/kern/misc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
grub-core/kern/misc.c +10 -0
index 2b7922393..ff614add5 100644
@@ -26,6 +26,7 @@
 #include <grub/i18n.h>
 #include <grub/types.h>
 #include <grub/charset.h>
+#include <stddef.h>
 
 union printf_arg
 {
@@ -739,6 +740,9 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
   COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
   COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
 		       || sizeof (int) == sizeof (void *));
+  COMPILE_TIME_ASSERT (sizeof (size_t) == sizeof (unsigned)
+		       || sizeof (size_t) == sizeof (unsigned long)
+		       || sizeof (size_t) == sizeof (unsigned long long));
 
   fmt = fmt0;
   while ((c = *fmt++) != 0)
@@ -773,11 +777,17 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	fmt++;
 
       c = *fmt++;
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  goto do_count;
+	}
       if (c == 'l')
 	c = *fmt++;
       if (c == 'l')
 	c = *fmt++;
 
+ do_count:
       switch (c)
 	{
 	case 'p':

grub-core/kern/misc.c parse_printf_arg_fmt (const char *fmt0, struct printf_args *args, · line 893

Quoted 10 lines
grub-core/kern/misc.c +7 -0
@@ -874,6 +884,14 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	  continue;
 	}
 
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  if (sizeof (size_t) == sizeof (unsigned long))
+	    longfmt = 1;
+	  else if (sizeof (size_t) == sizeof (unsigned long long))
+	    longfmt = 2;

I have just realized we should probably have:

#ifdef GRUB_UTIL
if (sizeof (size_t) == sizeof (unsigned long))

    longfmt = 1;
  else if (sizeof (size_t) == sizeof (unsigned long long))
    longfmt = 2;
#else
  if (sizeof (grub_size_t) == sizeof (unsigned long))
    longfmt = 1;
  else if (sizeof (grub_size_t) == sizeof (unsigned long long))
    longfmt = 2;

#endif

Same for COMPILE_TIME_ASSERT() above.

If it makes sense for you please post updated patch set. I want to merge it tomorrow...

Daniel

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

#3 lore raw

Re: [PATCH v6 01/12] misc: add z length modifier support

Quoted 77 lines · Daniel Kiper
Quoted 53 lines · Michael Chang via Grub-devel

Add support for the 'z' length modifier in the printf code. This allows printing of size_t and ssize_t values using %zu, %zd and related formats. The parser maps 'z' to the correct integer width based on sizeof (size_t).

---
 grub-core/kern/misc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
grub-core/kern/misc.c +10 -0
index 2b7922393..ff614add5 100644
@@ -26,6 +26,7 @@
 #include <grub/i18n.h>
 #include <grub/types.h>
 #include <grub/charset.h>
+#include <stddef.h>
 
 union printf_arg
 {
@@ -739,6 +740,9 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
   COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
   COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
 		       || sizeof (int) == sizeof (void *));
+  COMPILE_TIME_ASSERT (sizeof (size_t) == sizeof (unsigned)
+		       || sizeof (size_t) == sizeof (unsigned long)
+		       || sizeof (size_t) == sizeof (unsigned long long));
 
   fmt = fmt0;
   while ((c = *fmt++) != 0)
@@ -773,11 +777,17 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	fmt++;
 
       c = *fmt++;
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  goto do_count;
+	}
       if (c == 'l')
 	c = *fmt++;
       if (c == 'l')
 	c = *fmt++;
 
+ do_count:
       switch (c)
 	{
 	case 'p':

grub-core/kern/misc.c parse_printf_arg_fmt (const char *fmt0, struct printf_args *args, · line 893

Quoted 10 lines
grub-core/kern/misc.c +7 -0
@@ -874,6 +884,14 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	  continue;
 	}
 
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  if (sizeof (size_t) == sizeof (unsigned long))
+	    longfmt = 1;
+	  else if (sizeof (size_t) == sizeof (unsigned long long))
+	    longfmt = 2;

I have just realized we should probably have:

#ifdef GRUB_UTIL
if (sizeof (size_t) == sizeof (unsigned long))

    longfmt = 1;
  else if (sizeof (size_t) == sizeof (unsigned long long))
    longfmt = 2;
#else
  if (sizeof (grub_size_t) == sizeof (unsigned long))
    longfmt = 1;
  else if (sizeof (grub_size_t) == sizeof (unsigned long long))
    longfmt = 2;
Quoted 6 lines

#endif

Same for COMPILE_TIME_ASSERT() above.

If it makes sense for you please post updated patch set. I want to merge it tomorrow...

The type size_t is not only used in Linux utilities but also by core components that import sources from Linux libraries, such as libgcrypt and zstd, which run in GRUB's arch-platform pre-boot environment.

Likewise, grub_size_t is not limited to the pre-boot context but is also used in Linux utilities.

As a result, their scope is not restricted by conditional checks. Each type should follow its own established convention for portable format
specifiers: 'z' for size_t and PRI[ux]GRUB_SIZE for grub_size_t. This can avoid ambiguity.

Thanks,
Michael

Quoted 2 lines

Daniel

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

#4 lore raw

Re: [PATCH v6 01/12] misc: add z length modifier support

Quoted 89 lines · Michael Chang via Grub-devel
Quoted 77 lines · Daniel Kiper
Quoted 53 lines · Michael Chang via Grub-devel

Add support for the 'z' length modifier in the printf code. This allows printing of size_t and ssize_t values using %zu, %zd and related formats. The parser maps 'z' to the correct integer width based on sizeof (size_t).

---
 grub-core/kern/misc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
grub-core/kern/misc.c +10 -0
index 2b7922393..ff614add5 100644
@@ -26,6 +26,7 @@
 #include <grub/i18n.h>
 #include <grub/types.h>
 #include <grub/charset.h>
+#include <stddef.h>
 
 union printf_arg
 {
@@ -739,6 +740,9 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
   COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
   COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
 		       || sizeof (int) == sizeof (void *));
+  COMPILE_TIME_ASSERT (sizeof (size_t) == sizeof (unsigned)
+		       || sizeof (size_t) == sizeof (unsigned long)
+		       || sizeof (size_t) == sizeof (unsigned long long));
 
   fmt = fmt0;
   while ((c = *fmt++) != 0)
@@ -773,11 +777,17 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	fmt++;
 
       c = *fmt++;
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  goto do_count;
+	}
       if (c == 'l')
 	c = *fmt++;
       if (c == 'l')
 	c = *fmt++;
 
+ do_count:
       switch (c)
 	{
 	case 'p':

grub-core/kern/misc.c parse_printf_arg_fmt (const char *fmt0, struct printf_args *args, · line 893

Quoted 10 lines
grub-core/kern/misc.c +7 -0
@@ -874,6 +884,14 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
 	  continue;
 	}
 
+      if (c == 'z')
+	{
+	  c = *fmt++;
+	  if (sizeof (size_t) == sizeof (unsigned long))
+	    longfmt = 1;
+	  else if (sizeof (size_t) == sizeof (unsigned long long))
+	    longfmt = 2;

I have just realized we should probably have:

#ifdef GRUB_UTIL
if (sizeof (size_t) == sizeof (unsigned long))

    longfmt = 1;
  else if (sizeof (size_t) == sizeof (unsigned long long))
    longfmt = 2;
#else
  if (sizeof (grub_size_t) == sizeof (unsigned long))
    longfmt = 1;
  else if (sizeof (grub_size_t) == sizeof (unsigned long long))
    longfmt = 2;
Quoted 6 lines

#endif

Same for COMPILE_TIME_ASSERT() above.

If it makes sense for you please post updated patch set. I want to merge it tomorrow...

The type size_t is not only used in Linux utilities but also by core components that import sources from Linux libraries, such as libgcrypt and zstd, which run in GRUB's arch-platform pre-boot environment.

Quoted 7 lines

Likewise, grub_size_t is not limited to the pre-boot context but is also used in Linux utilities.

As a result, their scope is not restricted by conditional checks. Each type should follow its own established convention for portable format
specifiers: 'z' for size_t and PRI[ux]GRUB_SIZE for grub_size_t. This can avoid ambiguity.

Ugh... Somehow I totally forgot about PRI[ux]GRUB_SIZE... :-( Ignore my comment then...

Daniel

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

#5 lore raw

PATCH v6 2/12 tests: add z modifier printf tests

In reply to Michael Chang via Grub-devel

Add unit tests for %zd, %zu and %zx to verify size_t and ssize_t formatting matches system snprintf.

---
 tests/printf_unit_test.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tests/printf_unit_test.c +10 -0
index 098c29fd9..f2de187b0 100644
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <grub/test.h>
 #include <grub/misc.h>
+#include <stdint.h>
 
 #define MSG "printf test failed: %s, %s", real, expected
 
@@ -73,6 +74,15 @@ printf_test (void)
   grub_snprintf (real, sizeof (real), "%%0%dd ", 1);
   snprintf (expected, sizeof (expected), "%%0%dd ", 1);
   grub_test_assert (strcmp (real, expected) == 0, MSG);
+  grub_snprintf (real, sizeof (real), "%zd %zd %zd", (ssize_t) -1, (ssize_t) (SIZE_MAX >> 1), (ssize_t) 42);
+  snprintf (expected, sizeof (expected), "%zd %zd %zd", (ssize_t) -1, (ssize_t) (SIZE_MAX >> 1), (ssize_t) 42);
+  grub_test_assert (strcmp (real, expected) == 0, MSG);
+  grub_snprintf (real, sizeof (real), "%zu %zu %zu", (size_t) 0, (size_t) SIZE_MAX, (size_t) 42);
+  snprintf (expected, sizeof (expected), "%zu %zu %zu", (size_t) 0, (size_t) SIZE_MAX, (size_t) 42);
+  grub_test_assert (strcmp (real, expected) == 0, MSG);
+  grub_snprintf (real, sizeof (real), "%zx %zx %zx", (ssize_t) (SIZE_MAX >> 1), (size_t) SIZE_MAX, (size_t) 0xdeadbeefU);
+  snprintf (expected, sizeof (expected), "%zx %zx %zx", (ssize_t) (SIZE_MAX >> 1), (size_t) SIZE_MAX, (size_t) 0xdeadbeefU);
+  grub_test_assert (strcmp (real, expected) == 0, MSG);
 }
 
 GRUB_UNIT_TEST ("printf_unit_test", printf_test);
Signature · 7 lines
2.51.0


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

PATCH v6 3/12 util/grub-editenv: add fs_envblk open helper

In reply to Michael Chang via Grub-devel

This patch adds the logic to locate and open an environment block that is stored in a reserved area on the device. It introduces the function fs_envblk_open() together with helper routines to read the block pointed to by the env_block variable, and to create the block on disk when it does not exist yet. When a block is created, the code records its location inside the file based envblk by setting env_block in block list syntax of offset plus size in sectors.

The env_block variable acts as a link from the file envblk to the raw disk region so that later runs of grub-editenv can follow it and access the external block. The helper is exposed through a small ops table attached to fs_envblk so that later patches can call fs_envblk->ops->open() without touching core code again. At this stage variables are still stored in the file envblk and no redirection has been applied.

In relation to this, the fs_envblk_spec table defines the file-system specific layout of the reserved raw blocks used for environment storage. It is prepared to facilitate integration in grub-editenv, with Btrfs to be added in the future once its reserved area is defined.

An fs_envblk_init() helper is added to prepare it for using the ops with its associated data context if the feature is available. It is not used yet, but will be used later when a filesystem and its device are probed to initialize the fs_envblk handle and enable access to the feature.

---
 util/grub-editenv.c | 170 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 170 insertions(+)
util/grub-editenv.c +170 -0
index db6f187cc..7c0105188 100644
@@ -120,6 +120,176 @@ block, use `rm %s'."),
   NULL, help_filter, NULL
 };
 
+struct fs_envblk_spec {
+  const char *fs_name;
+  off_t offset;
+  size_t size;
+};
+typedef struct fs_envblk_spec fs_envblk_spec_t;
+
+static grub_envblk_t fs_envblk_open (grub_envblk_t envblk);
+
+struct fs_envblk_ops {
+  grub_envblk_t (*open) (grub_envblk_t);
+};
+typedef struct fs_envblk_ops fs_envblk_ops_t;
+
+struct fs_envblk {
+  fs_envblk_spec_t *spec;
+  fs_envblk_ops_t *ops;
+  const char *dev;
+};
+typedef struct fs_envblk *fs_envblk_t;
+
+static fs_envblk_ops_t fs_envblk_ops = {
+  .open = fs_envblk_open
+};
+
+/*
+ * fs_envblk_spec describes the file-system specific layout of reserved raw
+ * blocks used as environment blocks.
+ */
+static fs_envblk_spec_t fs_envblk_spec[] = {
+  { NULL, 0, 0 }
+};
+
+static fs_envblk_t fs_envblk = NULL;
+
+static void __attribute__ ((unused))
+fs_envblk_init (const char *fs_name, const char *dev)
+{
+  fs_envblk_spec_t *p;
+
+  if (fs_name == NULL || dev == NULL)
+    return;
+
+  for (p = fs_envblk_spec; p->fs_name != NULL; p++)
+    {
+      if (strcmp (fs_name, p->fs_name) == 0)
+	{
+	  if (fs_envblk == NULL)
+	    fs_envblk = xmalloc (sizeof (*fs_envblk));
+	  fs_envblk->spec = p;
+	  fs_envblk->dev = xstrdup (dev);
+	  fs_envblk->ops = &fs_envblk_ops;
+	  break;
+	}
+    }
+}
+
+static int
+read_env_block_var (const char *varname, const char *value, void *hook_data)
+{
+  grub_envblk_t *p_envblk = (grub_envblk_t *) hook_data;
+  off_t off;
+  size_t sz;
+  char *p, *buf;
+  FILE *fp;
+
+  if (p_envblk == NULL || fs_envblk == NULL)
+    return 1;
+
+  if (strcmp (varname, "env_block") != 0)
+    return 0;
+
+  off = strtol (value, &p, 10);
+  if (*p == '+')
+    sz = strtol (p + 1, &p, 10);
+  else
+    return 0;
+
+  if (*p != '\0' || sz == 0)
+    return 0;
+
+  off <<= GRUB_DISK_SECTOR_BITS;
+  sz <<= GRUB_DISK_SECTOR_BITS;
+
+  fp = grub_util_fopen (fs_envblk->dev, "rb");
+  if (fp == NULL)
+    grub_util_error (_("cannot open `%s': %s"), fs_envblk->dev, strerror (errno));
+
+  if (fseek (fp, off, SEEK_SET) < 0)
+    grub_util_error (_("cannot seek `%s': %s"), fs_envblk->dev, strerror (errno));
+
+  buf = xmalloc (sz);
+  if ((fread (buf, 1, sz, fp)) != sz)
+    grub_util_error (_("cannot read `%s': %s"), fs_envblk->dev, strerror (errno));
+
+  fclose (fp);
+
+  *p_envblk = grub_envblk_open (buf, sz);
+
+  return 1;
+}
+
+static void
+create_env_on_block (void)
+{
+  FILE *fp;
+  char *buf;
+  const char *device;
+  off_t offset;
+  size_t size;
+
+  if (fs_envblk == NULL)
+    return;
+
+  device = fs_envblk->dev;
+  offset = fs_envblk->spec->offset;
+  size = fs_envblk->spec->size;
+
+  fp = grub_util_fopen (device, "r+b");
+  if (fp == NULL)
+    grub_util_error (_("cannot open `%s': %s"), device, strerror (errno));
+
+  buf = xmalloc (size);
+  memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
+  memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#', size - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
+
+  if (fseek (fp, offset, SEEK_SET) < 0)
+    grub_util_error (_("cannot seek `%s': %s"), device, strerror (errno));
+
+  if (fwrite (buf, 1, size, fp) != size)
+    grub_util_error (_("cannot write to `%s': %s"), device, strerror (errno));
+
+  grub_util_file_sync (fp);
+  free (buf);
+  fclose (fp);
+}
+
+static grub_envblk_t
+fs_envblk_open (grub_envblk_t envblk)
+{
+  grub_envblk_t envblk_on_block = NULL;
+  char *val;
+  off_t offset;
+  size_t size;
+
+  if (envblk == NULL)
+    return NULL;
+
+  offset = fs_envblk->spec->offset;
+  size = fs_envblk->spec->size;
+
+  grub_envblk_iterate (envblk, &envblk_on_block, read_env_block_var);
+
+  if (envblk_on_block != NULL && grub_envblk_size (envblk_on_block) == size)
+    return envblk_on_block;
+
+  create_env_on_block ();
+
+  offset = offset >> GRUB_DISK_SECTOR_BITS;
+  size =  (size + GRUB_DISK_SECTOR_SIZE - 1) >> GRUB_DISK_SECTOR_BITS;
+
+  val = xasprintf ("%lld+%zu", (long long) offset, size);
+  if (grub_envblk_set (envblk, "env_block", val) == 0)
+    grub_util_error ("%s", _("environment block too small"));
+  grub_envblk_iterate (envblk, &envblk_on_block, read_env_block_var);
+  free (val);
+
+  return envblk_on_block;
+}
+
 static grub_envblk_t
 open_envblk_file (const char *name)
 {
Signature · 7 lines
2.51.0


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

PATCH v6 4/12 util/grub-editenv: add fs_envblk write helper

In reply to Michael Chang via Grub-devel

This patch adds the function fs_envblk_write to update the reserved environment block on disk. The helper takes an in memory envblk buffer and writes it back to the device at the location defined by the fs_envblk specification. It performs size checks and uses file sync to ensure that the updated data is flushed.

The helper is also added into the fs_envblk ops table, together with the open helper from the previous patch. With this change the basic input and output path for an external environment block is complete. The choice of which variables should be written externally will be handled by later patches.

---
 util/grub-editenv.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
util/grub-editenv.c +37 -1
index 7c0105188..c1a8a5117 100644
@@ -128,9 +128,11 @@ struct fs_envblk_spec {
 typedef struct fs_envblk_spec fs_envblk_spec_t;
 
 static grub_envblk_t fs_envblk_open (grub_envblk_t envblk);
+static void fs_envblk_write (grub_envblk_t envblk);
 
 struct fs_envblk_ops {
   grub_envblk_t (*open) (grub_envblk_t);
+  void (*write) (grub_envblk_t);
 };
 typedef struct fs_envblk_ops fs_envblk_ops_t;
 
@@ -142,7 +144,8 @@ struct fs_envblk {
 typedef struct fs_envblk *fs_envblk_t;
 
 static fs_envblk_ops_t fs_envblk_ops = {
-  .open = fs_envblk_open
+  .open = fs_envblk_open,
+  .write = fs_envblk_write
 };
 
 /*
@@ -378,6 +381,39 @@ write_envblk (const char *name, grub_envblk_t envblk)
   fclose (fp);
 }
 
+static void
+fs_envblk_write (grub_envblk_t envblk)
+{
+  FILE *fp;
+  const char *device;
+  off_t offset;
+  size_t size;
+
+  if (envblk == NULL)
+    return;
+
+  device = fs_envblk->dev;
+  offset = fs_envblk->spec->offset;
+  size = fs_envblk->spec->size;
+
+  if (grub_envblk_size (envblk) > size)
+    grub_util_error ("%s", _("environment block too small"));
+
+  fp = grub_util_fopen (device, "r+b");
+
+  if (fp == NULL)
+    grub_util_error (_("cannot open `%s': %s"), device, strerror (errno));
+
+  if (fseek (fp, offset, SEEK_SET) < 0)
+    grub_util_error (_("cannot seek `%s': %s"), device, strerror (errno));
+
+  if (fwrite (grub_envblk_buffer (envblk), 1, grub_envblk_size (envblk), fp) != grub_envblk_size (envblk))
+    grub_util_error (_("cannot write to `%s': %s"), device, strerror (errno));
+
+  grub_util_file_sync (fp);
+  fclose (fp);
+}
+
 static void
 set_variables (const char *name, int argc, char *argv[])
 {
Signature · 7 lines
2.51.0


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

PATCH v6 5/12 util/grub-editenv: wire set_variables to optional fs_envblk

In reply to Michael Chang via Grub-devel

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 | 58 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 2 deletions(-)
util/grub-editenv.c +56 -2
index c1a8a5117..7d4981d67 100644
@@ -414,12 +414,35 @@ fs_envblk_write (grub_envblk_t envblk)
   fclose (fp);
 }
 
+struct var_lookup_ctx {
+  const char *varname;
+  bool found;
+};
+typedef struct var_lookup_ctx var_lookup_ctx_t;
+
+static int
+var_lookup_iter (const char *varname, const char *value __attribute__ ((unused)), void *hook_data)
+{
+  var_lookup_ctx_t *ctx = (var_lookup_ctx_t *) hook_data;
+
+  if (grub_strcmp (ctx->varname, varname) == 0)
+    {
+      ctx->found = true;
+      return 1;
+    }
+  return 0;
+}
+
 static void
 set_variables (const char *name, int argc, char *argv[])
 {
   grub_envblk_t envblk;
+  grub_envblk_t envblk_on_block = NULL;
 
   envblk = open_envblk_file (name);
+  if (fs_envblk != NULL)
+    envblk_on_block = fs_envblk->ops->open (envblk);
+
   while (argc)
     {
       char *p;
@@ -430,15 +453,46 @@ set_variables (const char *name, int argc, char *argv[])
 
       *(p++) = 0;
 
-      if (! grub_envblk_set (envblk, argv[0], p))
-        grub_util_error ("%s", _("environment block too small"));
+      if ((strcmp (argv[0], "next_entry") == 0) && envblk_on_block != NULL)
+	{
+	  if (grub_envblk_set (envblk_on_block, argv[0], p) == 0)
+	    grub_util_error ("%s", _("environment block too small"));
+	  goto next;
+	}
+
+      if (strcmp (argv[0], "env_block") == 0)
+	{
+	  grub_util_warn (_("can't set env_block as it's read-only"));
+	  goto next;
+	}
+
+      if (grub_envblk_set (envblk, argv[0], p) == 0)
+	grub_util_error ("%s", _("environment block too small"));
 
+      if (envblk_on_block != NULL)
+	{
+	  var_lookup_ctx_t ctx = {
+	    .varname = argv[0],
+	    .found = false
+	  };
+
+	  grub_envblk_iterate (envblk_on_block, &ctx, var_lookup_iter);
+	  if (ctx.found == true)
+	    grub_envblk_delete (envblk_on_block, argv[0]);
+	}
+ next:
       argc--;
       argv++;
     }
 
   write_envblk (name, envblk);
   grub_envblk_close (envblk);
+
+  if (envblk_on_block != NULL)
+    {
+      fs_envblk->ops->write (envblk_on_block);
+      grub_envblk_close (envblk_on_block);
+    }
 }
 
 static void
Signature · 7 lines
2.51.0


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

PATCH v6 6/12 util/grub-editenv: wire unset_variables to optional fs_envblk

In reply to Michael Chang via Grub-devel

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.

---
 util/grub-editenv.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
util/grub-editenv.c +14 -0
index 7d4981d67..7f39ff120 100644
@@ -499,18 +499,32 @@ static void
 unset_variables (const char *name, int argc, char *argv[])
 {
   grub_envblk_t envblk;
+  grub_envblk_t envblk_on_block = NULL;
 
   envblk = open_envblk_file (name);
+
+  if (fs_envblk != NULL)
+    envblk_on_block = fs_envblk->ops->open (envblk);
+
   while (argc)
     {
       grub_envblk_delete (envblk, argv[0]);
 
+      if (envblk_on_block != NULL)
+	grub_envblk_delete (envblk_on_block, argv[0]);
+
       argc--;
       argv++;
     }
 
   write_envblk (name, envblk);
   grub_envblk_close (envblk);
+
+  if (envblk_on_block != NULL)
+    {
+      fs_envblk->ops->write (envblk_on_block);
+      grub_envblk_close (envblk_on_block);
+    }
 }
 
 int
Signature · 7 lines
2.51.0


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

PATCH v6 7/12 util/grub-editenv: wire list_variables to optional fs_envblk

In reply to Michael Chang via Grub-devel

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.

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.

---
 util/grub-editenv.c | 7 +++++++
 1 file changed, 7 insertions(+)
util/grub-editenv.c +7 -0
index 7f39ff120..f8fa294d8 100644
@@ -355,10 +355,17 @@ static void
 list_variables (const char *name)
 {
   grub_envblk_t envblk;
+  grub_envblk_t envblk_on_block = NULL;
 
   envblk = open_envblk_file (name);
+  grub_envblk_iterate (envblk, &envblk_on_block, read_env_block_var);
   grub_envblk_iterate (envblk, NULL, print_var);
   grub_envblk_close (envblk);
+  if (envblk_on_block != NULL)
+    {
+      grub_envblk_iterate (envblk_on_block, NULL, print_var);
+      grub_envblk_close (envblk_on_block);
+    }
 }
 
 static void
Signature · 7 lines
2.51.0


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

PATCH v6 8/12 util/grub-editenv: add probe call for external envblk

In reply to Michael Chang via Grub-devel

This patch adds the probe_fs_envblk() function to identify the root filesystem and invoke fs_envblk_init() with the probed filesystem type and device. This checks if the feature is available and initializes the handle, fs_envblk, to access the external environment block. It avoids configurations with diskfilter or cryptodisk where filesystem blocks may be remapped or encrypted.

The probe is only invoked when grub-editenv is working on the default environment file path. This restriction ensures that probing and possible raw device access are not triggered for arbitrary user supplied paths, but only for the standard grubenv file. In that case the code checks if the filename equals DEFAULT_ENVBLK_PATH and then calls probe_fs_envblk with fs_envblk_spec. The result is stored in the global fs_envblk handle. At this stage the external environment block is only detected and recorded, and the behavior of grub-editenv is unchanged.

---
 util/grub-editenv.c | 115 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 113 insertions(+), 2 deletions(-)
util/grub-editenv.c +113 -2
index f8fa294d8..1b923bf97 100644
@@ -23,8 +23,11 @@
 #include <grub/util/misc.h>
 #include <grub/lib/envblk.h>
 #include <grub/i18n.h>
-#include <grub/emu/hostfile.h>
+#include <grub/emu/hostdisk.h>
 #include <grub/util/install.h>
+#include <grub/emu/getroot.h>
+#include <grub/fs.h>
+#include <grub/crypto.h>
 
 #include <stdio.h>
 #include <unistd.h>
@@ -158,7 +161,7 @@ static fs_envblk_spec_t fs_envblk_spec[] = {
 
 static fs_envblk_t fs_envblk = NULL;
 
-static void __attribute__ ((unused))
+static void
 fs_envblk_init (const char *fs_name, const char *dev)
 {
   fs_envblk_spec_t *p;
@@ -534,6 +537,111 @@ unset_variables (const char *name, int argc, char *argv[])
     }
 }
 
+static bool
+is_abstraction (grub_device_t dev)
+{
+  if (dev == NULL || dev->disk == NULL)
+    return false;
+
+  if (dev->disk->dev->id == GRUB_DISK_DEVICE_DISKFILTER_ID ||
+      dev->disk->dev->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
+    return true;
+
+  return false;
+}
+
+static void
+probe_fs_envblk (fs_envblk_spec_t *spec)
+{
+  char **grub_devices = NULL;
+  char **curdev, **curdrive;
+  size_t ndev = 0;
+  char **grub_drives = NULL;
+  grub_device_t grub_dev = NULL;
+  grub_fs_t grub_fs = NULL;
+  bool have_abstraction = false;
+
+  grub_util_biosdisk_init (DEFAULT_DEVICE_MAP);
+  grub_init_all ();
+  grub_gcry_init_all ();
+
+  grub_lvm_fini ();
+  grub_mdraid09_fini ();
+  grub_mdraid1x_fini ();
+  grub_diskfilter_fini ();
+  grub_diskfilter_init ();
+  grub_mdraid09_init ();
+  grub_mdraid1x_init ();
+  grub_lvm_init ();
+
+  grub_devices = grub_guess_root_devices (DEFAULT_DIRECTORY);
+
+  if (grub_devices == NULL || grub_devices[0] == NULL)
+    {
+      grub_util_warn (_("cannot find a device for %s (is /dev mounted?)"), DEFAULT_DIRECTORY);
+      goto cleanup;
+    }
+
+  for (curdev = grub_devices; *curdev != NULL; curdev++, ndev++)
+    grub_util_pull_device (*curdev);
+
+  grub_drives = xcalloc ((ndev + 1), sizeof (grub_drives[0]));
+
+  for (curdev = grub_devices, curdrive = grub_drives; *curdev != NULL; curdev++,
+       curdrive++)
+    {
+      *curdrive = grub_util_get_grub_dev (*curdev);
+      if (*curdrive == NULL)
+	{
+	  grub_util_warn (_("cannot find a GRUB drive for %s.  Check your device.map"),
+			  *curdev);
+	  goto cleanup;
+	}
+    }
+  *curdrive = NULL;
+
+  grub_dev = grub_device_open (grub_drives[0]);
+  if (grub_dev == NULL)
+    {
+      grub_util_warn (_("cannot open device %s: %s"), grub_drives[0], grub_errmsg);
+      grub_errno = GRUB_ERR_NONE;
+      goto cleanup;
+    }
+
+  grub_fs = grub_fs_probe (grub_dev);
+  if (grub_fs == NULL)
+    {
+      grub_util_warn (_("cannot probe fs for %s: %s"), grub_drives[0], grub_errmsg);
+      grub_errno = GRUB_ERR_NONE;
+      goto cleanup;
+    }
+
+  have_abstraction = is_abstraction (grub_dev);
+  for (curdrive = grub_drives + 1; *curdrive != NULL && have_abstraction == false; curdrive++)
+    {
+      grub_device_t dev = grub_device_open (*curdrive);
+
+      if (dev == NULL)
+	continue;
+      have_abstraction = is_abstraction (dev);
+      grub_device_close (dev);
+    }
+
+  if (have_abstraction == false)
+    fs_envblk_init (grub_fs->name, grub_devices[0]);
+
+ cleanup:
+  if (grub_devices != NULL)
+    for (curdev = grub_devices; *curdev != NULL; curdev++)
+      free (*curdev);
+  free (grub_devices);
+  free (grub_drives);
+  grub_device_close (grub_dev);
+  grub_gcry_fini_all ();
+  grub_fini_all ();
+  grub_util_biosdisk_fini ();
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -565,6 +673,9 @@ main (int argc, char *argv[])
       command  = argv[curindex++];
     }
 
+  if (strcmp (filename, DEFAULT_ENVBLK_PATH) == 0)
+    probe_fs_envblk (fs_envblk_spec);
+
   if (strcmp (command, "create") == 0)
     grub_util_create_envblk_file (filename);
   else if (strcmp (command, "list") == 0)
Signature · 7 lines
2.51.0


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

PATCH v6 9/12 btrfs: add environment block to reserved header area

In reply to Michael Chang via Grub-devel

This patch reserves space for the GRUB environment block inside the Btrfs header. The block is placed at an offset of GRUB_ENV_BTRFS_OFFSET , 256 KiB from the start of the device, and occupies one sector. To protect the space, overflow guard sectors are placed before and after the reserved block.

The Btrfs header already defines regions for bootloader use. By adding this entry, GRUB gains a fixed and safe location to store the environment block without conflicting with other structures in the header.

Add Btrfs and its reserved area information to the fs_envblk_spec table. With the groundworks done in previous patches, the function is now complete and working in grub-editenv.

---
 grub-core/fs/btrfs.c | 10 +++++++++-
 include/grub/fs.h    |  2 ++
 util/grub-editenv.c  |  9 ++++++++-
 3 files changed, 19 insertions(+), 2 deletions(-)
grub-core/fs/btrfs.c +9 -1
index 7bf8d922f..fa5d860eb 100644
@@ -2337,11 +2337,16 @@ struct embed_region {
  * https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs(5)#BOOTLOADER_SUPPORT
  * The first 1 MiB on each device is unused with the exception of primary
  * superblock that is on the offset 64 KiB and spans 4 KiB.
+ *
+ * Note: If this table is modified, also update
+ * util/grub-editenv.c::fs_envblk_spec, which describes the file-system
+ * specific layout of reserved raw blocks used as environment blocks so that
+ * both stay consistent.
  */
 
 static const struct {
   struct embed_region available;
-  struct embed_region used[6];
+  struct embed_region used[9];
 } btrfs_head = {
   .available = {0, GRUB_DISK_KiB_TO_SECTORS (1024)}, /* The first 1 MiB. */
   .used = {
@@ -2349,6 +2354,9 @@ static const struct {
     {GRUB_DISK_KiB_TO_SECTORS (64) - 1, 1},                        /* Overflow guard. */
     {GRUB_DISK_KiB_TO_SECTORS (64), GRUB_DISK_KiB_TO_SECTORS (4)}, /* 4 KiB superblock. */
     {GRUB_DISK_KiB_TO_SECTORS (68), 1},                            /* Overflow guard. */
+    {(GRUB_ENV_BTRFS_OFFSET >> GRUB_DISK_SECTOR_BITS) - 1, 1},     /* Overflow guard. */
+    {(GRUB_ENV_BTRFS_OFFSET >> GRUB_DISK_SECTOR_BITS), 1},         /* Environment Block. */
+    {(GRUB_ENV_BTRFS_OFFSET >> GRUB_DISK_SECTOR_BITS) + 1, 1},     /* Overflow guard. */
     {GRUB_DISK_KiB_TO_SECTORS (1024) - 1, 1},                      /* Overflow guard. */
     {0, 0}                                                         /* Array terminator. */
   }
include/grub/fs.h +2 -0
index df4c93b16..89e4d2b9b 100644
@@ -132,4 +132,6 @@ grub_fs_unregister (grub_fs_t fs)
 
 grub_fs_t EXPORT_FUNC(grub_fs_probe) (grub_device_t device);
 
+#define GRUB_ENV_BTRFS_OFFSET (256 * 1024)
+
 #endif /* ! GRUB_FS_HEADER */
util/grub-editenv.c +8 -1
index 1b923bf97..41bb86739 100644
@@ -153,9 +153,16 @@ static fs_envblk_ops_t fs_envblk_ops = {
 
 /*
  * fs_envblk_spec describes the file-system specific layout of reserved raw
- * blocks used as environment blocks.
+ * blocks used as environment blocks. At present only Btrfs is supported. Other
+ * file-systems may be added if they provide a similar facility and avoid the
+ * limitation of writing to COW.
+ *
+ * Note: If this table is modified, also update
+ * grub-core/fs/btrfs.c::btrfs_head, which defines the layout in the Btrfs
+ * header and exports GRUB_ENV_BTRFS_OFFSET, so that both stay consistent.
  */
 static fs_envblk_spec_t fs_envblk_spec[] = {
+  { "btrfs", GRUB_ENV_BTRFS_OFFSET, GRUB_DISK_SECTOR_SIZE },
   { NULL, 0, 0 }
 };
 
Signature · 7 lines
2.51.0


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

PATCH v6 10/12 00_header.in: wire grub.cfg to use env_block when present

In reply to Michael Chang via Grub-devel

This patch extends the generated grub.cfg so that it can use the external environment block when the variable env_block is defined. During boot, if env_block is set, grub.cfg builds a device path for it, exports the variable, and then loads its contents in addition to the normal grubenv file.

When GRUB writes variables such as next_entry or saved_entry, the save commands are changed to write into env_block if it is set, and to fall back to the grubenv file otherwise. In this way the external environment block is used automatically, and existing commands like savedefault or save_env do not need to change.

---
 util/grub.d/00_header.in | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
util/grub.d/00_header.in +23 -3
index f86b69bad..9d36feda3 100644
@@ -46,6 +46,13 @@ cat << EOF
 if [ -s \$prefix/grubenv ]; then
   load_env
 fi
+
+if [ "\${env_block}" ] ; then
+  set env_block="(\${root})\${env_block}"
+  export env_block
+  load_env -f "\${env_block}"
+fi
+
 EOF
 if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then
     cat <<EOF
@@ -54,7 +61,11 @@ if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then
 elif [ "\${next_entry}" ] ; then
    set default="\${next_entry}"
    set next_entry=
-   save_env next_entry
+   if [ "\${env_block}" ] ; then
+     save_env -f "\${env_block}" next_entry
+   else
+     save_env next_entry
+   fi
    set boot_once=true
 else
    set default="${GRUB_DEFAULT}"
@@ -65,7 +76,11 @@ else
 if [ "\${next_entry}" ] ; then
    set default="\${next_entry}"
    set next_entry=
-   save_env next_entry
+   if [ "\${env_block}" ] ; then
+     save_env -f "\${env_block}" next_entry
+   else
+     save_env next_entry
+   fi
    set boot_once=true
 else
    set default="${GRUB_DEFAULT}"
@@ -93,7 +108,12 @@ fi
 function savedefault {
   if [ -z "\${boot_once}" ]; then
     saved_entry="\${chosen}"
-    save_env saved_entry
+    if [ "\${env_block}" ] ; then
+      save_env -f "\${env_block}" saved_entry
+    else
+      save_env saved_entry
+    fi
+
   fi
 }
 
Signature · 7 lines
2.51.0


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

PATCH v6 11/12 docs: add Btrfs env block and special env vars

In reply to Michael Chang via Grub-devel

Update grub.texi to describe the external environment block in the reserved area of Btrfs header used for grub-reboot and savedefault, and add a section documenting the saved_entry, next_entry, and env_block variables.

---
 docs/grub.texi | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
docs/grub.texi +60 -0
index 34b3484dc..6ffb3a370 100644
@@ -3271,6 +3271,7 @@ GRUB.  Others may be used freely in GRUB configuration files.
 @menu
 * Special environment variables::
 * Environment block::
+* Special environment block variables::
 @end menu
 
 
@@ -3868,9 +3869,68 @@ For safety reasons, this storage is only available when installed on a plain
 disk (no LVM or RAID), using a non-checksumming filesystem (no ZFS), and
 using BIOS or EFI functions (no ATA, USB or IEEE1275).
 
+On Btrfs filesystems, a reserved area in the filesystem header may be used to
+store the environment block. This static block avoids the problems of updating
+a normal file on a copy-on-write filesystem, where writing raw block is not
+stable and requires metadata update. The reserved area provides a fixed
+location that GRUB can update directly, allowing commands such as
+@command{grub-reboot} and @samp{GRUB_SAVEDEFAULT} to function correctly on
+Btrfs volumes.
+
 @command{grub-mkconfig} uses this facility to implement
 @samp{GRUB_SAVEDEFAULT} (@pxref{Simple configuration}).
 
+
+@node Special environment block variables
+@section Special environment block variables
+
+These special variables are usually written to the environment block
+(@pxref{Environment block}) to customize the behavior of @file{grub.cfg}
+generated by @command{grub-mkconfig}.
+
+@menu
+* saved_entry::
+* next_entry::
+* env_block::
+@end menu
+
+
+@node saved_entry
+@subsection saved_entry
+
+The @var{saved_entry} variable sets the default boot entry in @file{grub.cfg}
+created by @command{grub-mkconfig}. It can be set with
+@command{grub-set-default} to choose a default entry, or at runtime with the
+@code{savedefault} function in grub.cfg to save the current entry as the new
+default. This may require write access by GRUB.
+
+
+@node next_entry
+@subsection next_entry
+
+The @var{next_entry} variable sets the boot entry for the next boot only. After
+it is used, GRUB clears the value so it is not reused. This requires write
+access to the environment block (@pxref{Environment block}) at runtime. The
+@command{grub-reboot} command is usually used instead of changing this variable
+directly.
+
+
+@node env_block
+@subsection env_block
+
+If the filesystem is Btrfs and the disk is not an abstracted device such as
+LVM, RAID, or encryption, the reserved space in the Btrfs header can be used as
+the environment block (@pxref{Environment block}). This provides a fixed raw
+block that GRUB can reliably write to. The @var{env_block} records this
+location in GRUB blocklist syntax (@pxref{Block list syntax}) so that
+@command{grub-editenv} and @file{grub.cfg} know how to access and use the
+external raw block.
+
+This variable is initialized when @file{grubenv} is first created by
+@command{grub-editenv} and is treated as read-only to avoid being overwritten
+with an unpredictable value.
+
+
 @node Modules
 @chapter Modules
 
Signature · 7 lines
2.51.0


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

Re: [PATCH v6 11/12] docs: add Btrfs env block and special env vars

a minor merge conflict:

This patch in particular needs to be rebased to latest master

otherwise I am seeing this on my b4 command

$ b4 shazam
https://lore.kernel.org/grub-devel/CAEg-Je8-yE=upFm3RG_4LuG1fRhHGDtco9tEuORE3N3CvqA2Kg@mail.gmail.com/T/#t
Grabbing thread from
lore.kernel.org/all/CAEg-Je8-yE%[email protected]/t.mbox.gz
Checking for newer revisions
Grabbing search results from lore.kernel.org
Analyzing 19 messages in the thread
Analyzing 99 code-review messages
Checking attestation on all messages, may take a moment...
---
  ✗ [PATCH v6 1/12] misc: add z length modifier support
  ✗ [PATCH v6 2/12] tests: add z modifier printf tests
  ✗ [PATCH v6 3/12] util/grub-editenv: add fs_envblk open helper
  ✗ [PATCH v6 4/12] util/grub-editenv: add fs_envblk write helper
  ✗ [PATCH v6 5/12] util/grub-editenv: wire set_variables to optional
fs_envblk
  ✗ [PATCH v6 6/12] util/grub-editenv: wire unset_variables to optional
fs_envblk
  ✗ [PATCH v6 7/12] util/grub-editenv: wire list_variables to optional
fs_envblk
  ✗ [PATCH v6 8/12] util/grub-editenv: add probe call for external envblk
  ✗ [PATCH v6 9/12] btrfs: add environment block to reserved header area
  ✗ [PATCH v6 10/12] 00_header.in: wire grub.cfg to use env_block when
present
  ✗ [PATCH v6 11/12] docs: add Btrfs env block and special env vars
  ✗ [PATCH v6 12/12] btrfs: update doc link for bootloader support
  ---
  ✗ BADSIG: DKIM/suse.com
---
Total patches: 12
---
Applying: misc: add z length modifier support
Applying: tests: add z modifier printf tests
Applying: util/grub-editenv: add fs_envblk open helper
Applying: util/grub-editenv: add fs_envblk write helper
Applying: util/grub-editenv: wire set_variables to optional fs_envblk
Applying: util/grub-editenv: wire unset_variables to optional fs_envblk
Applying: util/grub-editenv: wire list_variables to optional fs_envblk
Applying: util/grub-editenv: add probe call for external envblk
Applying: btrfs: add environment block to reserved header area
Applying: 00_header.in: wire grub.cfg to use env_block when present
Applying: docs: add Btrfs env block and special env vars
Patch failed at 0011 docs: add Btrfs env block and special env vars
error: patch failed: docs/grub.texi:3271
error: docs/grub.texi: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am
--abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Quoted 112 lines · Michael Chang via Grub-devel

Update grub.texi to describe the external environment block in the reserved area of Btrfs header used for grub-reboot and savedefault, and add a section documenting the saved_entry, next_entry, and env_block variables.

---
 docs/grub.texi | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
docs/grub.texi +0 -0
index 34b3484dc..6ffb3a370 100644
@@ -3271,6 +3271,7 @@ GRUB.  Others may be used freely in GRUB

configuration files.
@menu
* Special environment variables::
* Environment block::

docs/grub.texi +1 -0
+* Special environment block variables::
 @end menu
@@ -3868,9 +3869,68 @@ For safety reasons, this storage is only available

when installed on a plain
disk (no LVM or RAID), using a non-checksumming filesystem (no ZFS), and using BIOS or EFI functions (no ATA, USB or IEEE1275).

+On Btrfs filesystems, a reserved area in the filesystem header may be used to
+store the environment block. This static block avoids the problems of updating
+a normal file on a copy-on-write filesystem, where writing raw block is not

docs/grub.texi +24 -0
+stable and requires metadata update. The reserved area provides a fixed
+location that GRUB can update directly, allowing commands such as
+@command{grub-reboot} and @samp{GRUB_SAVEDEFAULT} to function correctly on
+Btrfs volumes.
+
 @command{grub-mkconfig} uses this facility to implement
 @samp{GRUB_SAVEDEFAULT} (@pxref{Simple configuration}).
 
+
+@node Special environment block variables
+@section Special environment block variables
+
+These special variables are usually written to the environment block
+(@pxref{Environment block}) to customize the behavior of @file{grub.cfg}
+generated by @command{grub-mkconfig}.
+
+@menu
+* saved_entry::
+* next_entry::
+* env_block::
+@end menu
+
+
+@node saved_entry
+@subsection saved_entry
+
+The @var{saved_entry} variable sets the default boot entry in

@file{grub.cfg}
+created by @command{grub-mkconfig}. It can be set with +@command{grub-set-default} to choose a default entry, or at runtime with the
+@code{savedefault} function in grub.cfg to save the current entry as the new

docs/grub.texi +7 -0
+default. This may require write access by GRUB.
+
+
+@node next_entry
+@subsection next_entry
+
+The @var{next_entry} variable sets the boot entry for the next boot only.

After

docs/grub.texi +2 -0
+it is used, GRUB clears the value so it is not reused. This requires write
+access to the environment block (@pxref{Environment block}) at runtime.

The
+@command{grub-reboot} command is usually used instead of changing this variable

docs/grub.texi +7 -0
+directly.
+
+
+@node env_block
+@subsection env_block
+
+If the filesystem is Btrfs and the disk is not an abstracted device such

as
+LVM, RAID, or encryption, the reserved space in the Btrfs header can be used as
+the environment block (@pxref{Environment block}). This provides a fixed raw

docs/grub.texi +7 -0
+block that GRUB can reliably write to. The @var{env_block} records this
+location in GRUB blocklist syntax (@pxref{Block list syntax}) so that
+@command{grub-editenv} and @file{grub.cfg} know how to access and use the
+external raw block.
+
+This variable is initialized when @file{grubenv} is first created by
+@command{grub-editenv} and is treated as read-only to avoid being

overwritten

docs/grub.texi +3 -1
+with an unpredictable value.
+
+
 @node Modules
 @chapter Modules
 
--

2.51.0

Quoted 5 lines

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

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

#16 lore raw

Re: [PATCH v6 11/12] docs: add Btrfs env block and special env vars

Hi Leo,

Thanks for reporting this. I see it has already been merged into git, so you can simply apply from there now.

Thanks,
Michael

Quoted 175 lines · Leo Sandoval

a minor merge conflict:

This patch in particular needs to be rebased to latest master

otherwise I am seeing this on my b4 command

$ b4 shazam
https://lore.kernel.org/grub-devel/CAEg-Je8-yE=upFm3RG_4LuG1fRhHGDtco9tEuORE3N3CvqA2Kg@mail.gmail.com/T/#t
Grabbing thread from
lore.kernel.org/all/CAEg-Je8-yE%[email protected]/t.mbox.gz
Checking for newer revisions
Grabbing search results from lore.kernel.org
Analyzing 19 messages in the thread
Analyzing 99 code-review messages
Checking attestation on all messages, may take a moment...
---
  ✗ [PATCH v6 1/12] misc: add z length modifier support
  ✗ [PATCH v6 2/12] tests: add z modifier printf tests
  ✗ [PATCH v6 3/12] util/grub-editenv: add fs_envblk open helper
  ✗ [PATCH v6 4/12] util/grub-editenv: add fs_envblk write helper
  ✗ [PATCH v6 5/12] util/grub-editenv: wire set_variables to optional
fs_envblk
  ✗ [PATCH v6 6/12] util/grub-editenv: wire unset_variables to optional
fs_envblk
  ✗ [PATCH v6 7/12] util/grub-editenv: wire list_variables to optional
fs_envblk
  ✗ [PATCH v6 8/12] util/grub-editenv: add probe call for external envblk
  ✗ [PATCH v6 9/12] btrfs: add environment block to reserved header area
  ✗ [PATCH v6 10/12] 00_header.in: wire grub.cfg to use env_block when
present
  ✗ [PATCH v6 11/12] docs: add Btrfs env block and special env vars
  ✗ [PATCH v6 12/12] btrfs: update doc link for bootloader support
  ---
  ✗ BADSIG: DKIM/suse.com
---
Total patches: 12
---
Applying: misc: add z length modifier support
Applying: tests: add z modifier printf tests
Applying: util/grub-editenv: add fs_envblk open helper
Applying: util/grub-editenv: add fs_envblk write helper
Applying: util/grub-editenv: wire set_variables to optional fs_envblk
Applying: util/grub-editenv: wire unset_variables to optional fs_envblk
Applying: util/grub-editenv: wire list_variables to optional fs_envblk
Applying: util/grub-editenv: add probe call for external envblk
Applying: btrfs: add environment block to reserved header area
Applying: 00_header.in: wire grub.cfg to use env_block when present
Applying: docs: add Btrfs env block and special env vars
Patch failed at 0011 docs: add Btrfs env block and special env vars
error: patch failed: docs/grub.texi:3271
error: docs/grub.texi: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am
--abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Quoted 112 lines · Michael Chang via Grub-devel

Update grub.texi to describe the external environment block in the reserved area of Btrfs header used for grub-reboot and savedefault, and add a section documenting the saved_entry, next_entry, and env_block variables.

---
 docs/grub.texi | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
docs/grub.texi +0 -0
index 34b3484dc..6ffb3a370 100644
@@ -3271,6 +3271,7 @@ GRUB.  Others may be used freely in GRUB

configuration files.
@menu
* Special environment variables::
* Environment block::

docs/grub.texi +1 -0
+* Special environment block variables::
 @end menu
@@ -3868,9 +3869,68 @@ For safety reasons, this storage is only available

when installed on a plain
disk (no LVM or RAID), using a non-checksumming filesystem (no ZFS), and using BIOS or EFI functions (no ATA, USB or IEEE1275).

+On Btrfs filesystems, a reserved area in the filesystem header may be used to
+store the environment block. This static block avoids the problems of updating
+a normal file on a copy-on-write filesystem, where writing raw block is not

docs/grub.texi +24 -0
+stable and requires metadata update. The reserved area provides a fixed
+location that GRUB can update directly, allowing commands such as
+@command{grub-reboot} and @samp{GRUB_SAVEDEFAULT} to function correctly on
+Btrfs volumes.
+
 @command{grub-mkconfig} uses this facility to implement
 @samp{GRUB_SAVEDEFAULT} (@pxref{Simple configuration}).
 
+
+@node Special environment block variables
+@section Special environment block variables
+
+These special variables are usually written to the environment block
+(@pxref{Environment block}) to customize the behavior of @file{grub.cfg}
+generated by @command{grub-mkconfig}.
+
+@menu
+* saved_entry::
+* next_entry::
+* env_block::
+@end menu
+
+
+@node saved_entry
+@subsection saved_entry
+
+The @var{saved_entry} variable sets the default boot entry in

@file{grub.cfg}
+created by @command{grub-mkconfig}. It can be set with +@command{grub-set-default} to choose a default entry, or at runtime with the
+@code{savedefault} function in grub.cfg to save the current entry as the new

docs/grub.texi +7 -0
+default. This may require write access by GRUB.
+
+
+@node next_entry
+@subsection next_entry
+
+The @var{next_entry} variable sets the boot entry for the next boot only.

After

docs/grub.texi +2 -0
+it is used, GRUB clears the value so it is not reused. This requires write
+access to the environment block (@pxref{Environment block}) at runtime.

The
+@command{grub-reboot} command is usually used instead of changing this variable

docs/grub.texi +7 -0
+directly.
+
+
+@node env_block
+@subsection env_block
+
+If the filesystem is Btrfs and the disk is not an abstracted device such

as
+LVM, RAID, or encryption, the reserved space in the Btrfs header can be used as
+the environment block (@pxref{Environment block}). This provides a fixed raw

docs/grub.texi +7 -0
+block that GRUB can reliably write to. The @var{env_block} records this
+location in GRUB blocklist syntax (@pxref{Block list syntax}) so that
+@command{grub-editenv} and @file{grub.cfg} know how to access and use the
+external raw block.
+
+This variable is initialized when @file{grubenv} is first created by
+@command{grub-editenv} and is treated as read-only to avoid being

overwritten

docs/grub.texi +3 -1
+with an unpredictable value.
+
+
 @node Modules
 @chapter Modules
 
--

2.51.0

Quoted 5 lines
Quoted 5 lines

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

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

#17 lore raw

PATCH v6 12/12 btrfs: update doc link for bootloader support

In reply to Michael Chang via Grub-devel

The old wiki link is obsolete and no longer updated, change it to the current documentation.

---
 grub-core/fs/btrfs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
grub-core/fs/btrfs.c +2 -1
index fa5d860eb..8929320a3 100644
@@ -2334,7 +2334,8 @@ struct embed_region {
 };
 
 /*
- * https://btrfs.wiki.kernel.org/index.php/Manpage/btrfs(5)#BOOTLOADER_SUPPORT
+ * https://btrfs.readthedocs.io/en/latest/btrfs-man5.html#man-btrfs5-bootloader-support
+ * or invoke "man 5 btrfs" and visit the "bootloader support" subsection.
  * The first 1 MiB on each device is unused with the exception of primary
  * superblock that is on the offset 64 KiB and spans 4 KiB.
  *
Signature · 7 lines
2.51.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#18 lore raw
In reply to Michael Chang via Grub-devel
Quoted 106 lines · Michael Chang

This patch series adds support for storing the GRUB environment block in a reserved area of the Btrfs header. On copy on write filesystems such as Btrfs, the normal file based envblk cannot be updated safely at runtime because block addresses are not stable. The reserved area provides a fixed location that GRUB can write directly, allowing commands such as grub-reboot and savedefault to work on Btrfs volumes.

The series proceeds in small chunks to keep each change buildable and easier to review. The first patches add new data structures and helpers for creating, opening, and writing an environment block in the reserved area. Later patches update set_variables, unset_variables, and list_variables so they can use the external block when it is present. An entry is added to the Btrfs header to reserve space at 256 KiB for the environment block. Finally, grub.cfg is modified so that load_env and save_env use the external block automatically when env_block is defined.

The first two patches are new in this series as feedback from the review to use a portable string specifier for size_t. Unfortunately the z length modifier is missing in GRUB printf routine's format specifier. The new patches add support and test case for that. In theory they can be reviewed separately.

And also yet another new patch "btrfs: update doc link for bootloader support" added at the end to address v3 review feedback.

v2:
- Define ENV_BTRFS_OFFSET as 256*1024
- Do not conflate type and variable definitions
- Align typedef with struct declaration to follow coding style
- Use bool as the return type of is_abstraction()
- Add "if (dev->disk != NULL)" check in is_abstraction()
- Refine the loop logic in is_abstraction() tests
- Remove extra indentation and redundant lines in read_envblk_fs
- Use off_t and size_t for offset and size variables, fix similar cases throughout
- Use explicit check "(fp == NULL)" instead of "(! fp)", fix similar cases throughout
- Use bool for the "found" field in var_lookup_ctx
- Add documentation describing the Btrfs environment block and special environment block variables
v3:
- Replace !fp with explicit (fp == NULL) for clarity
- Fix missing blank line before return statement
- Fix missing space after cast operator
v4:
- Rename ENV_BTRFS_OFFSET to GRUB_ENV_BTRFS_OFFSET
- Move GRUB_ENV_BTRFS_OFFSET into "btrfs: add environment block to reserved header area"
- Adjust patch order to resolve relocated GRUB_ENV_BTRFS_OFFSET
- Add patch "btrfs: update doc link for bootloader support"
- Do better typedef
- Add heads-up comments to both fs_envblk_spec and btrfs_head that they have to stay in sync
- Fix a few places to use explicit checks for NULL, true/false etc
- Use goto labels to cleanup consistently
- Mark function names with trailing "()" in commits and comments
- Rename envblk_fs to envblk_on_block for better clarity
- Avoid redundant curly braces in if .. else if clause
- Highlight overflow guard around envioronment block in embed_region
- Rename a few functions for clarity
- Use portable printf formats for off_t and size_t
- Support z length modifier in GRUB printf format string
- Add z modifier to printf unit test
v5:
- Move fs_envblk_spec table into "btrfs: add environment block to reserved header area"
- Move "util/grub-editenv: add basic structures and probe call for external envblk" the 9th patch behind all wiring patches and rename it to "util/grub-editenv: add probe call for external envblk" as it now only does probing
- Add fs_envblk_init() to wire open helper in the base structures into patch "util/grub-editenv: add fs_envblk open helper"
- Add "Man 5 btrfs" an alternate to the doc link
v6:
- Move fs_envblk_spec structure typedef and variable from "btrfs: add environment block to reserved header area" into "util/grub-editenv:
add fs_envblk open helper". The fs_envblk_spec remains empty since no Btrfs area is defined yet.
- Reorder "btrfs: add environment block to reserved header area" to follow all groundwork patches, and provide the defined reserved area to fs_envblk_spec so that it becomes supported in grub-editenv.

Michael Chang (12):
misc: add z length modifier support
tests: add z modifier printf tests util/grub-editenv: add fs_envblk open helper util/grub-editenv: add fs_envblk write helper util/grub-editenv: wire set_variables to optional fs_envblk util/grub-editenv: wire unset_variables to optional fs_envblk util/grub-editenv: wire list_variables to optional fs_envblk util/grub-editenv: add probe call for external envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
docs: add Btrfs env block and special env vars
btrfs: update doc link for bootloader support

 docs/grub.texi           |  60 ++++++
 grub-core/fs/btrfs.c     |  13 +-
 grub-core/kern/misc.c    |  20 ++
 include/grub/fs.h        |   2 +
 tests/printf_unit_test.c |  10 +
 util/grub-editenv.c      | 407 ++++++++++++++++++++++++++++++++++++++-
 util/grub.d/00_header.in |  26 ++-
 7 files changed, 529 insertions(+), 9 deletions(-)
Quoted 3 lines

--
2.51.0

Series LGTM. Thanks for this. I hope we're now at the point where this can land. :)

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#19 lore raw
In reply to Michael Chang via Grub-devel
Quoted 82 lines · Michael Chang via Grub-devel

This patch series adds support for storing the GRUB environment block in a reserved area of the Btrfs header. On copy on write filesystems such as Btrfs, the normal file based envblk cannot be updated safely at runtime because block addresses are not stable. The reserved area provides a fixed location that GRUB can write directly, allowing commands such as grub-reboot and savedefault to work on Btrfs volumes.

The series proceeds in small chunks to keep each change buildable and easier to review. The first patches add new data structures and helpers for creating, opening, and writing an environment block in the reserved area. Later patches update set_variables, unset_variables, and list_variables so they can use the external block when it is present. An entry is added to the Btrfs header to reserve space at 256 KiB for the environment block. Finally, grub.cfg is modified so that load_env and save_env use the external block automatically when env_block is defined.

The first two patches are new in this series as feedback from the review to use a portable string specifier for size_t. Unfortunately the z length modifier is missing in GRUB printf routine's format specifier. The new patches add support and test case for that. In theory they can be reviewed separately.

And also yet another new patch "btrfs: update doc link for bootloader support" added at the end to address v3 review feedback.

v2:
- Define ENV_BTRFS_OFFSET as 256*1024
- Do not conflate type and variable definitions
- Align typedef with struct declaration to follow coding style
- Use bool as the return type of is_abstraction()
- Add "if (dev->disk != NULL)" check in is_abstraction()
- Refine the loop logic in is_abstraction() tests
- Remove extra indentation and redundant lines in read_envblk_fs
- Use off_t and size_t for offset and size variables, fix similar cases throughout
- Use explicit check "(fp == NULL)" instead of "(! fp)", fix similar cases throughout
- Use bool for the "found" field in var_lookup_ctx
- Add documentation describing the Btrfs environment block and special environment block variables
v3:
- Replace !fp with explicit (fp == NULL) for clarity
- Fix missing blank line before return statement
- Fix missing space after cast operator
v4:
- Rename ENV_BTRFS_OFFSET to GRUB_ENV_BTRFS_OFFSET
- Move GRUB_ENV_BTRFS_OFFSET into "btrfs: add environment block to reserved header area"
- Adjust patch order to resolve relocated GRUB_ENV_BTRFS_OFFSET
- Add patch "btrfs: update doc link for bootloader support"
- Do better typedef
- Add heads-up comments to both fs_envblk_spec and btrfs_head that they have to stay in sync
- Fix a few places to use explicit checks for NULL, true/false etc
- Use goto labels to cleanup consistently
- Mark function names with trailing "()" in commits and comments
- Rename envblk_fs to envblk_on_block for better clarity
- Avoid redundant curly braces in if .. else if clause
- Highlight overflow guard around envioronment block in embed_region
- Rename a few functions for clarity
- Use portable printf formats for off_t and size_t
- Support z length modifier in GRUB printf format string
- Add z modifier to printf unit test
v5:
- Move fs_envblk_spec table into "btrfs: add environment block to reserved header area"
- Move "util/grub-editenv: add basic structures and probe call for external envblk" the 9th patch behind all wiring patches and rename it to "util/grub-editenv: add probe call for external envblk" as it now only does probing
- Add fs_envblk_init() to wire open helper in the base structures into patch "util/grub-editenv: add fs_envblk open helper"
- Add "Man 5 btrfs" an alternate to the doc link
v6:
- Move fs_envblk_spec structure typedef and variable from "btrfs: add environment block to reserved header area" into "util/grub-editenv:
add fs_envblk open helper". The fs_envblk_spec remains empty since no Btrfs area is defined yet.
- Reorder "btrfs: add environment block to reserved header area" to follow all groundwork patches, and provide the defined reserved area to fs_envblk_spec so that it becomes supported in grub-editenv.

Quoted 13 lines

Michael Chang (12):
misc: add z length modifier support
tests: add z modifier printf tests util/grub-editenv: add fs_envblk open helper util/grub-editenv: add fs_envblk write helper util/grub-editenv: wire set_variables to optional fs_envblk util/grub-editenv: wire unset_variables to optional fs_envblk util/grub-editenv: wire list_variables to optional fs_envblk util/grub-editenv: add probe call for external envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
docs: add Btrfs env block and special env vars
btrfs: update doc link for bootloader support

For all patches Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

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

#20 lore raw
Quoted 98 lines · Daniel Kiper
Quoted 82 lines · Michael Chang via Grub-devel

This patch series adds support for storing the GRUB environment block in a reserved area of the Btrfs header. On copy on write filesystems such as Btrfs, the normal file based envblk cannot be updated safely at runtime because block addresses are not stable. The reserved area provides a fixed location that GRUB can write directly, allowing commands such as grub-reboot and savedefault to work on Btrfs volumes.

The series proceeds in small chunks to keep each change buildable and easier to review. The first patches add new data structures and helpers for creating, opening, and writing an environment block in the reserved area. Later patches update set_variables, unset_variables, and list_variables so they can use the external block when it is present. An entry is added to the Btrfs header to reserve space at 256 KiB for the environment block. Finally, grub.cfg is modified so that load_env and save_env use the external block automatically when env_block is defined.

The first two patches are new in this series as feedback from the review to use a portable string specifier for size_t. Unfortunately the z length modifier is missing in GRUB printf routine's format specifier. The new patches add support and test case for that. In theory they can be reviewed separately.

And also yet another new patch "btrfs: update doc link for bootloader support" added at the end to address v3 review feedback.

v2:
- Define ENV_BTRFS_OFFSET as 256*1024
- Do not conflate type and variable definitions
- Align typedef with struct declaration to follow coding style
- Use bool as the return type of is_abstraction()
- Add "if (dev->disk != NULL)" check in is_abstraction()
- Refine the loop logic in is_abstraction() tests
- Remove extra indentation and redundant lines in read_envblk_fs
- Use off_t and size_t for offset and size variables, fix similar cases throughout
- Use explicit check "(fp == NULL)" instead of "(! fp)", fix similar cases throughout
- Use bool for the "found" field in var_lookup_ctx
- Add documentation describing the Btrfs environment block and special environment block variables
v3:
- Replace !fp with explicit (fp == NULL) for clarity
- Fix missing blank line before return statement
- Fix missing space after cast operator
v4:
- Rename ENV_BTRFS_OFFSET to GRUB_ENV_BTRFS_OFFSET
- Move GRUB_ENV_BTRFS_OFFSET into "btrfs: add environment block to reserved header area"
- Adjust patch order to resolve relocated GRUB_ENV_BTRFS_OFFSET
- Add patch "btrfs: update doc link for bootloader support"
- Do better typedef
- Add heads-up comments to both fs_envblk_spec and btrfs_head that they have to stay in sync
- Fix a few places to use explicit checks for NULL, true/false etc
- Use goto labels to cleanup consistently
- Mark function names with trailing "()" in commits and comments
- Rename envblk_fs to envblk_on_block for better clarity
- Avoid redundant curly braces in if .. else if clause
- Highlight overflow guard around envioronment block in embed_region
- Rename a few functions for clarity
- Use portable printf formats for off_t and size_t
- Support z length modifier in GRUB printf format string
- Add z modifier to printf unit test
v5:
- Move fs_envblk_spec table into "btrfs: add environment block to reserved header area"
- Move "util/grub-editenv: add basic structures and probe call for external envblk" the 9th patch behind all wiring patches and rename it to "util/grub-editenv: add probe call for external envblk" as it now only does probing
- Add fs_envblk_init() to wire open helper in the base structures into patch "util/grub-editenv: add fs_envblk open helper"
- Add "Man 5 btrfs" an alternate to the doc link
v6:
- Move fs_envblk_spec structure typedef and variable from "btrfs: add environment block to reserved header area" into "util/grub-editenv:
add fs_envblk open helper". The fs_envblk_spec remains empty since no Btrfs area is defined yet.
- Reorder "btrfs: add environment block to reserved header area" to follow all groundwork patches, and provide the defined reserved area to fs_envblk_spec so that it becomes supported in grub-editenv.

Quoted 13 lines

Michael Chang (12):
misc: add z length modifier support
tests: add z modifier printf tests util/grub-editenv: add fs_envblk open helper util/grub-editenv: add fs_envblk write helper util/grub-editenv: wire set_variables to optional fs_envblk util/grub-editenv: wire unset_variables to optional fs_envblk util/grub-editenv: wire list_variables to optional fs_envblk util/grub-editenv: add probe call for external envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
docs: add Btrfs env block and special env vars
btrfs: update doc link for bootloader support

Quoted 2 lines

For all patches Reviewed-by: Daniel Kiper <[email protected]>...

So can this land in time for GRUB 2.14?

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#21 lore raw
In reply to Daniel Kiper
Quoted 98 lines · Daniel Kiper
Quoted 82 lines · Michael Chang via Grub-devel

This patch series adds support for storing the GRUB environment block in a reserved area of the Btrfs header. On copy on write filesystems such as Btrfs, the normal file based envblk cannot be updated safely at runtime because block addresses are not stable. The reserved area provides a fixed location that GRUB can write directly, allowing commands such as grub-reboot and savedefault to work on Btrfs volumes.

The series proceeds in small chunks to keep each change buildable and easier to review. The first patches add new data structures and helpers for creating, opening, and writing an environment block in the reserved area. Later patches update set_variables, unset_variables, and list_variables so they can use the external block when it is present. An entry is added to the Btrfs header to reserve space at 256 KiB for the environment block. Finally, grub.cfg is modified so that load_env and save_env use the external block automatically when env_block is defined.

The first two patches are new in this series as feedback from the review to use a portable string specifier for size_t. Unfortunately the z length modifier is missing in GRUB printf routine's format specifier. The new patches add support and test case for that. In theory they can be reviewed separately.

And also yet another new patch "btrfs: update doc link for bootloader support" added at the end to address v3 review feedback.

v2:
- Define ENV_BTRFS_OFFSET as 256*1024
- Do not conflate type and variable definitions
- Align typedef with struct declaration to follow coding style
- Use bool as the return type of is_abstraction()
- Add "if (dev->disk != NULL)" check in is_abstraction()
- Refine the loop logic in is_abstraction() tests
- Remove extra indentation and redundant lines in read_envblk_fs
- Use off_t and size_t for offset and size variables, fix similar cases throughout
- Use explicit check "(fp == NULL)" instead of "(! fp)", fix similar cases throughout
- Use bool for the "found" field in var_lookup_ctx
- Add documentation describing the Btrfs environment block and special environment block variables
v3:
- Replace !fp with explicit (fp == NULL) for clarity
- Fix missing blank line before return statement
- Fix missing space after cast operator
v4:
- Rename ENV_BTRFS_OFFSET to GRUB_ENV_BTRFS_OFFSET
- Move GRUB_ENV_BTRFS_OFFSET into "btrfs: add environment block to reserved header area"
- Adjust patch order to resolve relocated GRUB_ENV_BTRFS_OFFSET
- Add patch "btrfs: update doc link for bootloader support"
- Do better typedef
- Add heads-up comments to both fs_envblk_spec and btrfs_head that they have to stay in sync
- Fix a few places to use explicit checks for NULL, true/false etc
- Use goto labels to cleanup consistently
- Mark function names with trailing "()" in commits and comments
- Rename envblk_fs to envblk_on_block for better clarity
- Avoid redundant curly braces in if .. else if clause
- Highlight overflow guard around envioronment block in embed_region
- Rename a few functions for clarity
- Use portable printf formats for off_t and size_t
- Support z length modifier in GRUB printf format string
- Add z modifier to printf unit test
v5:
- Move fs_envblk_spec table into "btrfs: add environment block to reserved header area"
- Move "util/grub-editenv: add basic structures and probe call for external envblk" the 9th patch behind all wiring patches and rename it to "util/grub-editenv: add probe call for external envblk" as it now only does probing
- Add fs_envblk_init() to wire open helper in the base structures into patch "util/grub-editenv: add fs_envblk open helper"
- Add "Man 5 btrfs" an alternate to the doc link
v6:
- Move fs_envblk_spec structure typedef and variable from "btrfs: add environment block to reserved header area" into "util/grub-editenv:
add fs_envblk open helper". The fs_envblk_spec remains empty since no Btrfs area is defined yet.
- Reorder "btrfs: add environment block to reserved header area" to follow all groundwork patches, and provide the defined reserved area to fs_envblk_spec so that it becomes supported in grub-editenv.

Quoted 13 lines

Michael Chang (12):
misc: add z length modifier support
tests: add z modifier printf tests util/grub-editenv: add fs_envblk open helper util/grub-editenv: add fs_envblk write helper util/grub-editenv: wire set_variables to optional fs_envblk util/grub-editenv: wire unset_variables to optional fs_envblk util/grub-editenv: wire list_variables to optional fs_envblk util/grub-editenv: add probe call for external envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
docs: add Btrfs env block and special env vars
btrfs: update doc link for bootloader support

Quoted 2 lines

For all patches Reviewed-by: Daniel Kiper <[email protected]>...

Sorry for resending this but it occurred to me that I was using the wrong email, so I'm asking again if this can land in time for GRUB 2.14?

--
真実はいつも一つ!/ Always, there's only one truth!

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

#22 lore raw
Quoted 98 lines · Neal Gompa
Quoted 97 lines · Daniel Kiper
Quoted 95 lines · Michael Chang via Grub-devel

This patch series adds support for storing the GRUB environment block in a reserved area of the Btrfs header. On copy on write filesystems such as Btrfs, the normal file based envblk cannot be updated safely at runtime because block addresses are not stable. The reserved area provides a fixed location that GRUB can write directly, allowing commands such as grub-reboot and savedefault to work on Btrfs volumes.

The series proceeds in small chunks to keep each change buildable and easier to review. The first patches add new data structures and helpers for creating, opening, and writing an environment block in the reserved area. Later patches update set_variables, unset_variables, and list_variables so they can use the external block when it is present. An entry is added to the Btrfs header to reserve space at 256 KiB for the environment block. Finally, grub.cfg is modified so that load_env and save_env use the external block automatically when env_block is defined.

The first two patches are new in this series as feedback from the review to use a portable string specifier for size_t. Unfortunately the z length modifier is missing in GRUB printf routine's format specifier. The new patches add support and test case for that. In theory they can be reviewed separately.

And also yet another new patch "btrfs: update doc link for bootloader support" added at the end to address v3 review feedback.

v2:
- Define ENV_BTRFS_OFFSET as 256*1024
- Do not conflate type and variable definitions
- Align typedef with struct declaration to follow coding style
- Use bool as the return type of is_abstraction()
- Add "if (dev->disk != NULL)" check in is_abstraction()
- Refine the loop logic in is_abstraction() tests
- Remove extra indentation and redundant lines in read_envblk_fs
- Use off_t and size_t for offset and size variables, fix similar cases throughout
- Use explicit check "(fp == NULL)" instead of "(! fp)", fix similar cases throughout
- Use bool for the "found" field in var_lookup_ctx
- Add documentation describing the Btrfs environment block and special environment block variables
v3:
- Replace !fp with explicit (fp == NULL) for clarity
- Fix missing blank line before return statement
- Fix missing space after cast operator
v4:
- Rename ENV_BTRFS_OFFSET to GRUB_ENV_BTRFS_OFFSET
- Move GRUB_ENV_BTRFS_OFFSET into "btrfs: add environment block to reserved header area"
- Adjust patch order to resolve relocated GRUB_ENV_BTRFS_OFFSET
- Add patch "btrfs: update doc link for bootloader support"
- Do better typedef
- Add heads-up comments to both fs_envblk_spec and btrfs_head that they have to stay in sync
- Fix a few places to use explicit checks for NULL, true/false etc
- Use goto labels to cleanup consistently
- Mark function names with trailing "()" in commits and comments
- Rename envblk_fs to envblk_on_block for better clarity
- Avoid redundant curly braces in if .. else if clause
- Highlight overflow guard around envioronment block in embed_region
- Rename a few functions for clarity
- Use portable printf formats for off_t and size_t
- Support z length modifier in GRUB printf format string
- Add z modifier to printf unit test
v5:
- Move fs_envblk_spec table into "btrfs: add environment block to reserved header area"
- Move "util/grub-editenv: add basic structures and probe call for external envblk" the 9th patch behind all wiring patches and rename it to "util/grub-editenv: add probe call for external envblk" as it now only does probing
- Add fs_envblk_init() to wire open helper in the base structures into patch "util/grub-editenv: add fs_envblk open helper"
- Add "Man 5 btrfs" an alternate to the doc link
v6:
- Move fs_envblk_spec structure typedef and variable from "btrfs: add environment block to reserved header area" into "util/grub-editenv:
add fs_envblk open helper". The fs_envblk_spec remains empty since no Btrfs area is defined yet.
- Reorder "btrfs: add environment block to reserved header area" to follow all groundwork patches, and provide the defined reserved area to fs_envblk_spec so that it becomes supported in grub-editenv.

Michael Chang (12):
misc: add z length modifier support
tests: add z modifier printf tests util/grub-editenv: add fs_envblk open helper util/grub-editenv: add fs_envblk write helper util/grub-editenv: wire set_variables to optional fs_envblk util/grub-editenv: wire unset_variables to optional fs_envblk util/grub-editenv: wire list_variables to optional fs_envblk util/grub-editenv: add probe call for external envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
docs: add Btrfs env block and special env vars
btrfs: update doc link for bootloader support

Quoted 4 lines
Quoted 1 line

For all patches Reviewed-by: Daniel Kiper <[email protected]>...

Sorry for resending this but it occurred to me that I was using the wrong email, so I'm asking again if this can land in time for GRUB 2.14?

If you take a look at the GRUB git repository you will see that relevant patches are in for a few days...

Daniel

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

#23 lore raw
Quoted 100 lines · Daniel Kiper
Quoted 98 lines · Neal Gompa
Quoted 97 lines · Daniel Kiper
Quoted 95 lines · Michael Chang via Grub-devel

This patch series adds support for storing the GRUB environment block in a reserved area of the Btrfs header. On copy on write filesystems such as Btrfs, the normal file based envblk cannot be updated safely at runtime because block addresses are not stable. The reserved area provides a fixed location that GRUB can write directly, allowing commands such as grub-reboot and savedefault to work on Btrfs volumes.

The series proceeds in small chunks to keep each change buildable and easier to review. The first patches add new data structures and helpers for creating, opening, and writing an environment block in the reserved area. Later patches update set_variables, unset_variables, and list_variables so they can use the external block when it is present. An entry is added to the Btrfs header to reserve space at 256 KiB for the environment block. Finally, grub.cfg is modified so that load_env and save_env use the external block automatically when env_block is defined.

The first two patches are new in this series as feedback from the review to use a portable string specifier for size_t. Unfortunately the z length modifier is missing in GRUB printf routine's format specifier. The new patches add support and test case for that. In theory they can be reviewed separately.

And also yet another new patch "btrfs: update doc link for bootloader support" added at the end to address v3 review feedback.

v2:
- Define ENV_BTRFS_OFFSET as 256*1024
- Do not conflate type and variable definitions
- Align typedef with struct declaration to follow coding style
- Use bool as the return type of is_abstraction()
- Add "if (dev->disk != NULL)" check in is_abstraction()
- Refine the loop logic in is_abstraction() tests
- Remove extra indentation and redundant lines in read_envblk_fs
- Use off_t and size_t for offset and size variables, fix similar cases throughout
- Use explicit check "(fp == NULL)" instead of "(! fp)", fix similar cases throughout
- Use bool for the "found" field in var_lookup_ctx
- Add documentation describing the Btrfs environment block and special environment block variables
v3:
- Replace !fp with explicit (fp == NULL) for clarity
- Fix missing blank line before return statement
- Fix missing space after cast operator
v4:
- Rename ENV_BTRFS_OFFSET to GRUB_ENV_BTRFS_OFFSET
- Move GRUB_ENV_BTRFS_OFFSET into "btrfs: add environment block to reserved header area"
- Adjust patch order to resolve relocated GRUB_ENV_BTRFS_OFFSET
- Add patch "btrfs: update doc link for bootloader support"
- Do better typedef
- Add heads-up comments to both fs_envblk_spec and btrfs_head that they have to stay in sync
- Fix a few places to use explicit checks for NULL, true/false etc
- Use goto labels to cleanup consistently
- Mark function names with trailing "()" in commits and comments
- Rename envblk_fs to envblk_on_block for better clarity
- Avoid redundant curly braces in if .. else if clause
- Highlight overflow guard around envioronment block in embed_region
- Rename a few functions for clarity
- Use portable printf formats for off_t and size_t
- Support z length modifier in GRUB printf format string
- Add z modifier to printf unit test
v5:
- Move fs_envblk_spec table into "btrfs: add environment block to reserved header area"
- Move "util/grub-editenv: add basic structures and probe call for external envblk" the 9th patch behind all wiring patches and rename it to "util/grub-editenv: add probe call for external envblk" as it now only does probing
- Add fs_envblk_init() to wire open helper in the base structures into patch "util/grub-editenv: add fs_envblk open helper"
- Add "Man 5 btrfs" an alternate to the doc link
v6:
- Move fs_envblk_spec structure typedef and variable from "btrfs: add environment block to reserved header area" into "util/grub-editenv:
add fs_envblk open helper". The fs_envblk_spec remains empty since no Btrfs area is defined yet.
- Reorder "btrfs: add environment block to reserved header area" to follow all groundwork patches, and provide the defined reserved area to fs_envblk_spec so that it becomes supported in grub-editenv.

Michael Chang (12):
misc: add z length modifier support
tests: add z modifier printf tests util/grub-editenv: add fs_envblk open helper util/grub-editenv: add fs_envblk write helper util/grub-editenv: wire set_variables to optional fs_envblk util/grub-editenv: wire unset_variables to optional fs_envblk util/grub-editenv: wire list_variables to optional fs_envblk util/grub-editenv: add probe call for external envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
docs: add Btrfs env block and special env vars
btrfs: update doc link for bootloader support

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

For all patches Reviewed-by: Daniel Kiper <[email protected]>...

Sorry for resending this but it occurred to me that I was using the wrong email, so I'm asking again if this can land in time for GRUB 2.14?

If you take a look at the GRUB git repository you will see that relevant patches are in for a few days...

So they are... Sorry about the noise. When I looked last week, they weren't there yet... ^_^;

Signature · 6 lines
真実はいつも一つ!/ Always, there's only one truth!

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