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. In relation to this, 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 with grub-editenv, providing a consistent way to locate and manage the environment block. At present only Btrfs is supported, but other file systems may be added if they provide a similar facility and avoid the limitation of writing to COW. Signed-off-by: Michael Chang <[email protected]> Reviewed-by: Neal Gompa <[email protected]> --- grub-core/fs/btrfs.c | 10 +++++++++- include/grub/fs.h | 2 ++ util/grub-editenv.c | 23 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c index 7bf8d922f..fa5d860eb 100644 --- a/grub-core/fs/btrfs.c +++ b/grub-core/fs/btrfs.c @@ -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. */ } diff --git a/include/grub/fs.h b/include/grub/fs.h index df4c93b16..89e4d2b9b 100644 --- a/include/grub/fs.h +++ b/include/grub/fs.h @@ -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 */ diff --git a/util/grub-editenv.c b/util/grub-editenv.c index db6f187cc..8f355d8bb 100644 --- a/util/grub-editenv.c +++ b/util/grub-editenv.c @@ -25,6 +25,7 @@ #include <grub/i18n.h> #include <grub/emu/hostfile.h> #include <grub/util/install.h> +#include <grub/fs.h> #include <stdio.h> #include <unistd.h> @@ -120,6 +121,28 @@ 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; + +/* + * fs_envblk_spec describes the file-system specific layout of reserved raw + * 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[] __attribute__ ((unused)) = { + { "btrfs", GRUB_ENV_BTRFS_OFFSET, GRUB_DISK_SECTOR_SIZE }, + { NULL, 0, 0 } +}; + static grub_envblk_t open_envblk_file (const char *name) { -- 2.51.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
