Add support for external environment block on Btrfs
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.
Michael Chang (8):
util/grub-editenv: add basic structures and probe call for external
envblk
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
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to use env_block when present
grub-core/fs/btrfs.c | 3 +- include/grub/fs.h | 2 + util/grub-editenv.c | 394 ++++++++++++++++++++++++++++++++++++++- util/grub.d/00_header.in | 26 ++- 4 files changed, 418 insertions(+), 7 deletions(-)
Signature · 7 lines
2.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 1/8 util/grub-editenv: add basic structures and probe call for external envblk
This patch prepares for using an environment block stored in a reserved area of the filesystem. It adds a constant ENV_BTRFS_OFFSET at 256 KiB in the Btrfs header. It also introduces the fs_envblk_spec and fs_envblk structures together with the probe_fs_envblk function to identify the root filesystem and to avoid configurations that involve diskfilter or cryptodisk.
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.
--- include/grub/fs.h | 2 + util/grub-editenv.c | 156 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 1 deletion(-)
include/grub/fs.h +2 -0
@@ -132,4 +132,6 @@ grub_fs_unregister (grub_fs_t fs)grub_fs_t EXPORT_FUNC (grub_fs_probe ) (grub_device_t device); +#define ENV_BTRFS_OFFSET (256) +#endif /* ! GRUB_FS_HEADER */
util/grub-editenv.c +155 -1
181 lines not on this page · raw message
Signature · 7 lines
2.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/8] util/grub-editenv: add basic structures and probe call for external envblk
Quoted 2 lines · Michael Chang via Grub-devel
This patch prepares for using an environment block stored in a reserved area of the filesystem. It adds a constant
ENV_BTRFS_OFFSETat 256 KiB
Where this value come from? Please explain how did you come up with it? What are pros and cons of it? Etc...
Quoted 6 lines
in the Btrfs header. It also introduces the
fs_envblk_specandfs_envblkstructures together with theprobe_fs_envblkfunction to identify the root filesystem and to avoid configurations that involve diskfilter or cryptodisk.The probe is only invoked when grub editenv is working on the default
The project name is GRUB not grub. Please use it correctly. Though probably I would use here grub-editenv instead...
Quoted 3 lines
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
I can understand this restriction to some extent but I think it should be possible to lift it with --force argument. And of course Btrfs detection should be reliable...
Quoted 7 lines
checks if the filename equals
DEFAULT_ENVBLK_PATHand then callsprobe_fs_envblkwithfs_envblk_spec. The result is stored in the globalfs_envblkhandle. At this stage the external environment block is only detected and recorded, and the behavior of grub editenv is unchanged.Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>
I consider patch splitting as significant change. So, IMO Neal's RB should be dropped from all patches...
Quoted 10 lines
--- include/grub/fs.h | 2 + util/grub-editenv.c | 156 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 1 deletion(-)include/grub/fs.h +0
-0
include/grub/fs.h grub_fs_unregister (grub_fs_t fs) · line 135
Quoted 4 lines
include/grub/fs.h +1
-0@@ -132,4 +132,6 @@ grub_fs_unregister (grub_fs_t fs)grub_fs_t EXPORT_FUNC (grub_fs_probe ) (grub_device_t device); +#define ENV_BTRFS_OFFSET (256)
256 * 1024...
Quoted 19 lines
include/grub/fs.h +0
-0#endif /* ! GRUB_FS_HEADER */ util/grub-editenv.c +4
-1@@ -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>
util/grub-editenv.c block, use `rm %s'."), · line 130
Quoted 8 lines
util/grub-editenv.c +5
-0@@ -120,6 +123,25 @@ block, use `rm %s'."),NULL , help_filter,NULL }; +struct fs_envblk_spec { +const char *fs_name; +int offset; +int size; +} fs_envblk_spec[] = {
May I ask you to not conflate types and variables definitions?
util/grub-editenv.c block, use `rm %s'."), · line 131
Quoted 1 line
util/grub-editenv.c +1
-0+ { "btrfs", ENV_BTRFS_OFFSET * 1024, GRUB_DISK_SECTOR_SIZE },
... and drop multiplication from here...
util/grub-editenv.c block, use `rm %s'."), · line 141
Quoted 10 lines
util/grub-editenv.c +10
-0+ { NULL, 0, 0 } +}; + +struct fs_envblk { +struct fs_envblk_spec *spec; +const char *dev; +}; + +typedef struct fs_envblk_spec *fs_envblk_spec_t ; +typedef struct fs_envblk *fs_envblk_t ;
Please stick to GRUB coding style:
struct fs_envblk {
fs_envblk_spec_t spec;
const char *dev;
};
typedef struct fs_envblk *fs_envblk_t;
Same for fs_envblk_spec_t please...
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 278
Quoted 10 lines
util/grub-editenv.c +3
-0+fs_envblk_t fs_envblk =NULL ; +static grub_envblk_t open_envblk_file (const char *name) {@@ -253,6 +275,135 @@ unset_variables (const char *name, int argc, char *argv[])grub_envblk_close (envblk); } +static int
Please use bool here.
Quoted 10 lines
util/grub-editenv.c +10
-0+is_abstraction (grub_disk_t disk) +{ +if (disk->dev->id ==GRUB_DISK_DEVICE_DISKFILTER_ID || + disk->dev->id ==GRUB_DISK_DEVICE_CRYPTODISK_ID ) +return 1 ; +return 0 ; +} + +static fs_envblk_t +probe_fs_envblk (fs_envblk_spec_t spec)
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 298
Quoted 10 lines
util/grub-editenv.c +10
-0+{ +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 ; +char *devname =NULL ; +fs_envblk_spec_t p; +int have_abstraction =0 ;
bool?
Quoted 17 lines
util/grub-editenv.c +17
-0+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 ) + {
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 326
Quoted 10 lines
util/grub-editenv.c +10
-0+grub_util_warn (_ ("cannot find a device for %s (is /dev mounted?)" ),DEFAULT_DIRECTORY ); + goto cleanup; + } + + devname =xstrdup (grub_devices[0 ]); + +for (curdev = grub_devices; *curdev; curdev++) + { +grub_util_pull_device (*curdev); + ndev++;
If you put ndev++ behind curdev++ then you can drop three lines of code...
Quoted 24 lines
util/grub-editenv.c +24
-0+ } + + grub_drives =xcalloc ((ndev +1 ),sizeof (grub_drives[0 ])); + +for (curdev = grub_devices, curdrive = grub_drives; *curdev; 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 =0 ; + + 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; + }
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 360
Quoted 10 lines
util/grub-editenv.c +10
-0+ + 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; + } + +if (grub_dev->disk)
grub_dev->disk != NULL
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 361
Quoted 2 lines
util/grub-editenv.c +2
-0+ { + have_abstraction |=is_abstraction (grub_dev->disk);
Hmmm... Why not "=" instead of "|="? If yes then probably you can drop have_abstraction initialization at the beginning of the function.
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 362
Quoted 1 line
util/grub-editenv.c +1
-0+ }
You can drop curly braces here...
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 364
Quoted 1 line
util/grub-editenv.c +1
-0+for (curdrive = grub_drives +1 ; *curdrive; curdrive++)
s/*curdrive/*curdrive != NULL/
I can see similar minor issues in many places. Please fix it...
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 365
Quoted 2 lines
util/grub-editenv.c +2
-0+ { +grub_device_t dev =grub_device_open (*curdrive);
Please add an empty line here...
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 370
Quoted 4 lines
util/grub-editenv.c +4
-0+if (dev ==NULL ) +continue ; +if (dev->disk) + have_abstraction |=is_abstraction (dev->disk);
Again, s/|=/=/?
Ahhh... Probably this should be
if (is_abstraction (dev->disk) == true)
have_abstraction = true;And why could not you move "if (dev->disk != NULL)" check into the is_abstraction() function?
Quoted 40 lines
util/grub-editenv.c +36
-0+grub_device_close (dev); + } + + cleanup: +if (grub_devices !=NULL ) +for (curdev = grub_devices; *curdev; 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 (); + +if (grub_fs ==NULL ) + { +free (devname); +return NULL ; + } + +for (p = spec; p->fs_name; p++) + { +if (strcmp (grub_fs->name, p->fs_name) ==0 && !have_abstraction) + { + fs_envblk =xmalloc (sizeof (fs_envblk_t)); + fs_envblk->spec = p; + fs_envblk->dev = devname; +return fs_envblk; + } + } + +free (devname); +return NULL ; +} + +int main (int argc,char *argv[]) {
util/grub-editenv.c main (int argc, char *argv[]) · line 439
Quoted 5 lines
util/grub-editenv.c +2
-0@@ -284,6 +435,9 @@ main (int argc, char *argv[]) command = argv[curindex++]; } +if (strcmp (filename,DEFAULT_ENVBLK_PATH ) ==0 ) + fs_envblk =probe_fs_envblk (fs_envblk_spec);
I am not convinced... I think it should be possible to skip this check when user knows what he/she is doing...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/8] util/grub-editenv: add basic structures and probe call for external envblk
Quoted 6 lines · Daniel Kiper
Quoted 2 lines · Michael Chang via Grub-devel
This patch prepares for using an environment block stored in a reserved area of the filesystem. It adds a constant
ENV_BTRFS_OFFSETat 256 KiBWhere this value come from? Please explain how did you come up with it? What are pros and cons of it? Etc...
It was decided long time ago that the exact reasoning is hard to trace now. IIRC the value was chosen so that it is not placed too close to the Btrfs superblock and also leaves some distance from the beginning of the data blocks.
Quoted 10 lines
Quoted 7 lines
in the Btrfs header. It also introduces the
fs_envblk_specandfs_envblkstructures together with theprobe_fs_envblkfunction to identify the root filesystem and to avoid configurations that involve diskfilter or cryptodisk.The probe is only invoked when grub editenv is working on the default
The project name is GRUB not grub. Please use it correctly. Though probably I would use here grub-editenv instead...
I'll correct it to grub-editenv next version.
Quoted 8 lines
Quoted 4 lines
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
I can understand this restriction to some extent but I think it should be possible to lift it with --force argument. And of course Btrfs detection should be reliable...
The scope of this series is to make that grub-reboot and savedefault work when /boot is on Btrfs and the boot scripts are generated by grub-mkconfig, in other words the default configuration. Other scenarios have not been tested, and it is unclear whether this change would be appropriate or desirable in those cases.
Note that there is only one external environment block stored in the Btrfs header, while a Btrfs filesystem may have multiple subvolumes and snapshots, each with its own private grubenv file for settings. It is not always desirable to enable the external block by default as that may overlay settings, so the decision has been relatively conservative.
Quoted 11 lines
Quoted 8 lines
checks if the filename equals
DEFAULT_ENVBLK_PATHand then callsprobe_fs_envblkwithfs_envblk_spec. The result is stored in the globalfs_envblkhandle. At this stage the external environment block is only detected and recorded, and the behavior of grub editenv is unchanged.Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>I consider patch splitting as significant change. So, IMO Neal's RB should be dropped from all patches...
OK. I will drop the Reviewed-by next version.
Quoted 12 lines
Quoted 12 lines
--- include/grub/fs.h | 2 + util/grub-editenv.c | 156 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 1 deletion(-)include/grub/fs.h +0
-0@@ -132,4 +132,6 @@ grub_fs_unregister (grub_fs_t fs)
Quoted 5 lines
include/grub/fs.h grub_fs_unregister (grub_fs_t fs) · line 135
Quoted 3 lines
include/grub/fs.h +1
-0grub_fs_t EXPORT_FUNC (grub_fs_probe ) (grub_device_t device); +#define ENV_BTRFS_OFFSET (256)256 * 1024...
OK.
Quoted 23 lines
Quoted 23 lines
include/grub/fs.h +0
-0#endif /* ! GRUB_FS_HEADER */ util/grub-editenv.c +4
-1@@ -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> @@ -120,6 +123,25 @@ block, use `rm %s'."), NULL, help_filter, NULL };
Quoted 7 lines
util/grub-editenv.c block, use `rm %s'."), · line 130
Quoted 5 lines
util/grub-editenv.c +5
-0+struct fs_envblk_spec { +const char *fs_name; +int offset; +int size; +} fs_envblk_spec[] = {May I ask you to not conflate types and variables definitions?
OK.
Quoted 4 lines
util/grub-editenv.c block, use `rm %s'."), · line 131
Quoted 2 lines
util/grub-editenv.c +1
-0+ { "btrfs", ENV_BTRFS_OFFSET * 1024, GRUB_DISK_SECTOR_SIZE },... and drop multiplication from here...
OK.
Quoted 14 lines
util/grub-editenv.c block, use `rm %s'."), · line 141
Quoted 11 lines
util/grub-editenv.c +10
-0+ { NULL, 0, 0 } +}; + +struct fs_envblk { +struct fs_envblk_spec *spec; +const char *dev; +}; + +typedef struct fs_envblk_spec *fs_envblk_spec_t ; +typedef struct fs_envblk *fs_envblk_t ;Please stick to GRUB coding style:
Quoted 7 lines
struct
fs_envblk{
fs_envblk_spec_tspec;
const char *dev;
};
typedef structfs_envblk*fs_envblk_t;Same for
fs_envblk_spec_tplease...
OK.
Quoted 10 lines
Quoted 10 lines
util/grub-editenv.c +2
-0+fs_envblk_t fs_envblk =NULL ; +static grub_envblk_t open_envblk_file (const char *name) {@@ -253,6 +275,135 @@ unset_variables (const char *name, int argc, char *argv[])grub_envblk_close (envblk); }
Quoted 3 lines
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 278
Quoted 1 line
util/grub-editenv.c +1
-0+static intPlease use bool here.
OK.
Quoted 15 lines
Quoted 15 lines
util/grub-editenv.c +14
-0+is_abstraction (grub_disk_t disk) +{ +if (disk->dev->id ==GRUB_DISK_DEVICE_DISKFILTER_ID || + disk->dev->id ==GRUB_DISK_DEVICE_CRYPTODISK_ID ) +return 1 ; +return 0 ; +} + +static fs_envblk_t +probe_fs_envblk (fs_envblk_spec_t spec) +{ +char **grub_devices =NULL ; +char **curdev, **curdrive; +size_t ndev =0 ;
Quoted 8 lines
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 298
Quoted 6 lines
util/grub-editenv.c +6
-0+char **grub_drives =NULL ; +grub_device_t grub_dev =NULL ; +grub_fs_t grub_fs =NULL ; +char *devname =NULL ; +fs_envblk_spec_t p; +int have_abstraction =0 ;bool?
OK.
Quoted 23 lines
Quoted 23 lines
util/grub-editenv.c +22
-0+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; + } + + devname =xstrdup (grub_devices[0 ]);
Quoted 8 lines
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 326
Quoted 5 lines
util/grub-editenv.c +5
-0+ +for (curdev = grub_devices; *curdev; curdev++) + { +grub_util_pull_device (*curdev); + ndev++;If you put ndev++ behind curdev++ then you can drop three lines of code...
Yes.
Quoted 29 lines
Quoted 29 lines
util/grub-editenv.c +28
-0+ } + + grub_drives =xcalloc ((ndev +1 ),sizeof (grub_drives[0 ])); + +for (curdev = grub_devices, curdrive = grub_drives; *curdev; 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 =0 ; + + 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 ) + {
Quoted 8 lines
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 360
Quoted 6 lines
util/grub-editenv.c +6
-0+grub_util_warn (_ ("cannot probe fs for %s: %s" ), grub_drives[0 ], grub_errmsg); + grub_errno =GRUB_ERR_NONE ; + goto cleanup; + } + +if (grub_dev->disk)
grub_dev->disk!= NULL
OK.
Quoted 9 lines
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 361
Quoted 3 lines
util/grub-editenv.c +2
-0+ { + have_abstraction |=is_abstraction (grub_dev->disk);Hmmm... Why not "=" instead of "|="? If yes then probably you can drop
have_abstractioninitialization at the beginning of the function.util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 362
Quoted 1 line
util/grub-editenv.c +1
-0+ }
Quoted 7 lines
You can drop curly braces here...
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 364
Quoted 1 line
util/grub-editenv.c +1
-0+for (curdrive = grub_drives +1 ; *curdrive; curdrive++)s/*curdrive/*curdrive != NULL/
I can see similar minor issues in many places. Please fix it...
OK.
Quoted 5 lines
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 365
Quoted 3 lines
util/grub-editenv.c +2
-0+ { +grub_device_t dev =grub_device_open (*curdrive);Please add an empty line here...
OK.
Quoted 8 lines
util/grub-editenv.c unset_variables (const char *name, int argc, char *argv[]) · line 370
Quoted 5 lines
util/grub-editenv.c +4
-0+if (dev ==NULL ) +continue ; +if (dev->disk) + have_abstraction |=is_abstraction (dev->disk);Again, s/|=/=/?
Quoted 4 lines
Ahhh... Probably this should be
if (is_abstraction (dev->disk) == true) have_abstraction = true;
OK. I'll avoid "|=" to make the expression more easy to understand.
Quoted 3 lines
And why could not you move "if (
dev->disk!= NULL)" check into theis_abstraction()function?
Yes, that makes sense to me. I'll move the check to the function as you said.
Quoted 44 lines
Quoted 44 lines
util/grub-editenv.c +36
-0+grub_device_close (dev); + } + + cleanup: +if (grub_devices !=NULL ) +for (curdev = grub_devices; *curdev; 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 (); + +if (grub_fs ==NULL ) + { +free (devname); +return NULL ; + } + +for (p = spec; p->fs_name; p++) + { +if (strcmp (grub_fs->name, p->fs_name) ==0 && !have_abstraction) + { + fs_envblk =xmalloc (sizeof (fs_envblk_t)); + fs_envblk->spec = p; + fs_envblk->dev = devname; +return fs_envblk; + } + } + +free (devname); +return NULL ; +} + +int main (int argc,char *argv[]) {@@ -284,6 +435,9 @@ main (int argc, char *argv[]) command = argv[curindex++]; }
Quoted 5 lines
util/grub-editenv.c main (int argc, char *argv[]) · line 439
Quoted 2 lines
util/grub-editenv.c +2
-0+if (strcmp (filename,DEFAULT_ENVBLK_PATH ) ==0 ) + fs_envblk =probe_fs_envblk (fs_envblk_spec);I am not convinced... I think it should be possible to skip this check when user knows what he/she is doing...
As above, I would like to keep this in the initial support and enable it per the request, as there is a clear usecase.
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 2/8 util/grub-editenv: add fs_envblk open helper
In reply to Michael Chang via Grub-develThis 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 yet exist. 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.
--- util/grub-editenv.c | 127 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
util/grub-editenv.c +127 -0
@@ -132,8 +132,17 @@ struct fs_envblk_spec { { NULL, 0, 0 } }; +static grub_envblk_t fs_envblk_open (grub_envblk_t envblk); + +struct fs_envblk_ops { +grub_envblk_t (*open) (grub_envblk_t ); +} fs_envblk_ops = { + .open = fs_envblk_open +}; +struct fs_envblk {struct fs_envblk_spec *spec; +struct fs_envblk_ops *ops;const char *dev; };
@@ -142,6 +151,123 @@ typedef struct fs_envblk *fs_envblk_t;fs_envblk_t fs_envblk =NULL ; +static int +read_envblk_fs (const char *varname,const char *value,void *hook_data) +{ +grub_envblk_t *p_envblk = (grub_envblk_t *)hook_data; + +if (p_envblk ==NULL || fs_envblk ==NULL ) +return 1 ; + +if (strcmp (varname,"env_block" ) ==0 ) + { +int off, sz; +char *p; + + off =strtol (value, &p,10 ); +if (*p =='+' ) + sz =strtol (p+1 , &p,10 ); +else +return 0 ; + +if (*p =='\0' ) + { +FILE *fp; +char *buf; + + off <<=GRUB_DISK_SECTOR_BITS ; + sz <<=GRUB_DISK_SECTOR_BITS ; + + fp =grub_util_fopen (fs_envblk->dev,"rb" ); +if (! fp) +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 0 ; +} + +static void +create_envblk_fs (void ) +{ +FILE *fp; +char *buf; +const char *device; +int offset, 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) +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_fs =NULL ; +char *val; +int offset, size; + +if (envblk ==NULL ) +return NULL ; + + offset = fs_envblk->spec->offset; + size = fs_envblk->spec->size; + +grub_envblk_iterate (envblk, &envblk_fs, read_envblk_fs); + +if (envblk_fs &&grub_envblk_size (envblk_fs) == size) +return envblk_fs; + +create_envblk_fs (); + + offset = offset >>GRUB_DISK_SECTOR_BITS ; + size = (size +GRUB_DISK_SECTOR_SIZE -1 ) >>GRUB_DISK_SECTOR_BITS ; + + val =xasprintf ("%d+%d" , offset, size); +if (!grub_envblk_set (envblk,"env_block" , val)) +grub_util_error ("%s" ,_ ("environment block too small" )); +grub_envblk_iterate (envblk, &envblk_fs, read_envblk_fs); +free (val); + +return envblk_fs; +} +static grub_envblk_t open_envblk_file (const char *name) {
@@ -395,6 +521,7 @@ probe_fs_envblk (fs_envblk_spec_t spec) fs_envblk =xmalloc (sizeof (fs_envblk_t)); fs_envblk->spec = p; fs_envblk->dev = devname; + fs_envblk->ops = &fs_envblk_ops;return fs_envblk; } }
Signature · 7 lines
2.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 2/8] util/grub-editenv: add fs_envblk open helper
Quoted 45 lines · 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_opentogether with helper routines to read the block pointed to by theenv_blockvariable, and to create the block on disk when it does not yet exist. When a block is created, the code records its location inside the file based envblk by settingenv_blockin block list syntax of offset plus size in sectors.The
env_blockvariable 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 tofs_envblkso that later patches can callfs_envblk->ops->openwithout touching core code again. At this stage variables are still stored in the file envblk and no redirection has been applied.Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>--- util/grub-editenv.c | 127 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)util/grub-editenv.c +9
-0@@ -132,8 +132,17 @@ struct fs_envblk_spec { { NULL, 0, 0 } }; +static grub_envblk_t fs_envblk_open (grub_envblk_t envblk); + +struct fs_envblk_ops { +grub_envblk_t (*open) (grub_envblk_t ); +} fs_envblk_ops = { + .open = fs_envblk_open +}; +struct fs_envblk {struct fs_envblk_spec *spec; +struct fs_envblk_ops *ops;const char *dev; };
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 154
Quoted 4 lines
util/grub-editenv.c +1
-0@@ -142,6 +151,123 @@ typedef struct fs_envblk *fs_envblk_t;fs_envblk_t fs_envblk =NULL ; +static int
Why not bool?
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 162
Quoted 8 lines
util/grub-editenv.c +8
-0+read_envblk_fs (const char *varname, const char *value, void *hook_data) +{ + grub_envblk_t *p_envblk = (grub_envblk_t *)hook_data; + +if (p_envblk ==NULL || fs_envblk ==NULL ) +return 1 ; + +if (strcmp (varname,"env_block" ) ==0 )
Return if !=0 and you can drop one indention below...
Quoted 14 lines
util/grub-editenv.c +14
-0+ { +int off, sz; +char *p; + + off =strtol (value, &p,10 ); +if (*p =='+' ) + sz =strtol (p+1 , &p,10 ); +else +return 0 ; + +if (*p =='\0' ) + { +FILE *fp; +char *buf;
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 186
Quoted 10 lines
util/grub-editenv.c +10
-0+ + off <<=GRUB_DISK_SECTOR_BITS ; + sz <<=GRUB_DISK_SECTOR_BITS ; + + fp =grub_util_fopen (fs_envblk->dev,"rb" ); +if (! fp) +grub_util_error (_ ("cannot open `%s': %s" ), fs_envblk->dev, +strerror (errno)); + +
Please drop this redundant line...
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 194
Quoted 8 lines
util/grub-editenv.c +8
-0+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));
You blindly trust off and sz values. I think you should check limits here. Or somewhere else...
Quoted 17 lines
util/grub-editenv.c +17
-0+ +fclose (fp); + + *p_envblk =grub_envblk_open (buf, sz); + } + } + +return 0 ; +} + +static void +create_envblk_fs (void ) +{ +FILE *fp; +char *buf; +const char *device; +int offset, size;
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 221
Quoted 10 lines
util/grub-editenv.c +10
-0+ +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)
fp == NULL please...
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 226
Quoted 5 lines
util/grub-editenv.c +5
-0+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 );
GRUB_ENVBLK_SIGNATURE definition is missing in this patch...
Quoted 13 lines
util/grub-editenv.c +13
-0+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)
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 250
Quoted 10 lines
util/grub-editenv.c +10
-0+{ +grub_envblk_t envblk_fs =NULL ; +char *val; +int offset, size; + +if (envblk ==NULL ) +return NULL ; + + offset = fs_envblk->spec->offset; + size = fs_envblk->spec->size;
I think offset should be defined as off_t, size as size_t, fs_envblk->spec->offset, and fs_envblk->spec->size accordingly.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 2/8] util/grub-editenv: add fs_envblk open helper
Quoted 45 lines · Daniel Kiper
Quoted 44 lines · 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_opentogether with helper routines to read the block pointed to by theenv_blockvariable, and to create the block on disk when it does not yet exist. When a block is created, the code records its location inside the file based envblk by settingenv_blockin block list syntax of offset plus size in sectors.The
env_blockvariable 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 tofs_envblkso that later patches can callfs_envblk->ops->openwithout touching core code again. At this stage variables are still stored in the file envblk and no redirection has been applied.Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>--- util/grub-editenv.c | 127 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)util/grub-editenv.c +9
-0@@ -132,8 +132,17 @@ struct fs_envblk_spec { { NULL, 0, 0 } }; +static grub_envblk_t fs_envblk_open (grub_envblk_t envblk); + +struct fs_envblk_ops { +grub_envblk_t (*open) (grub_envblk_t ); +} fs_envblk_ops = { + .open = fs_envblk_open +}; +struct fs_envblk {struct fs_envblk_spec *spec; +struct fs_envblk_ops *ops;const char *dev; };
Quoted 7 lines
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 154
Quoted 5 lines
util/grub-editenv.c +1
-0@@ -142,6 +151,123 @@ typedef struct fs_envblk *fs_envblk_t;fs_envblk_t fs_envblk =NULL ; +static intWhy not bool?
OK. Will change it.
Quoted 11 lines
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 162
Quoted 9 lines
util/grub-editenv.c +8
-0+read_envblk_fs (const char *varname, const char *value, void *hook_data) +{ + grub_envblk_t *p_envblk = (grub_envblk_t *)hook_data; + +if (p_envblk ==NULL || fs_envblk ==NULL ) +return 1 ; + +if (strcmp (varname,"env_block" ) ==0 )Return if !=0 and you can drop one indention below...
Yes. Will change it.
Quoted 19 lines
Quoted 19 lines
util/grub-editenv.c +18
-0+ { +int off, sz; +char *p; + + off =strtol (value, &p,10 ); +if (*p =='+' ) + sz =strtol (p+1 , &p,10 ); +else +return 0 ; + +if (*p =='\0' ) + { +FILE *fp; +char *buf; + + off <<=GRUB_DISK_SECTOR_BITS ; + sz <<=GRUB_DISK_SECTOR_BITS ; +
Quoted 8 lines
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 186
Quoted 6 lines
util/grub-editenv.c +6
-0+ fp =grub_util_fopen (fs_envblk->dev,"rb" ); +if (! fp) +grub_util_error (_ ("cannot open `%s': %s" ), fs_envblk->dev, +strerror (errno)); + +Please drop this redundant line...
OK.
Quoted 12 lines
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 194
Quoted 9 lines
util/grub-editenv.c +8
-0+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));You blindly trust off and sz values. I think you should check limits here. Or somewhere else...
I consider it trusted because the off and sz values are taken from the env_block variable, which is read-only. This variable is only set at the time the /boot/grub/grubenv file is created, using predefined values from the fs_envblk_spec table. The related implementation can be found in a later patch:
"util/grub-editenv: wire set_variables to optional fs_envblk"
In addition, grub_envblk_open() validates the data read from (off, sz) and discards any invalid results.
Quoted 22 lines
Quoted 22 lines
util/grub-editenv.c +21
-0+ +fclose (fp); + + *p_envblk =grub_envblk_open (buf, sz); + } + } + +return 0 ; +} + +static void +create_envblk_fs (void ) +{ +FILE *fp; +char *buf; +const char *device; +int offset, size; + +if (fs_envblk ==NULL ) +return ; +
Quoted 8 lines
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 221
Quoted 6 lines
util/grub-editenv.c +6
-0+ device = fs_envblk->dev; + offset = fs_envblk->spec->offset; + size = fs_envblk->spec->size; + + fp =grub_util_fopen (device,"r+b" ); +if (! fp)fp == NULL please...
OK.
Quoted 8 lines
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 226
Quoted 6 lines
util/grub-editenv.c +5
-0+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 );
GRUB_ENVBLK_SIGNATUREdefinition is missing in this patch...
I think the definition has been present and used in the source before this patch series.
Quoted 19 lines
Quoted 19 lines
util/grub-editenv.c +18
-0+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_fs =NULL ; +char *val; +int offset, size; +
Quoted 8 lines
util/grub-editenv.c typedef struct fs_envblk *fs_envblk_t; · line 250
Quoted 5 lines
util/grub-editenv.c +5
-0+if (envblk ==NULL ) +return NULL ; + + offset = fs_envblk->spec->offset; + size = fs_envblk->spec->size;I think offset should be defined as
off_t, size assize_t,fs_envblk->spec->offset, andfs_envblk->spec->sizeaccordingly.
OK. I will do it.
Thanks,
Michael
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 3/8 util/grub-editenv: add fs_envblk write helper
In reply to Michael Chang via Grub-develThis 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 | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
util/grub-editenv.c +36 -1
@@ -133,11 +133,14 @@ struct fs_envblk_spec { };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 ); } fs_envblk_ops = { - .open = fs_envblk_open + .open = fs_envblk_open, + .write = fs_envblk_write };struct fs_envblk {
@@ -356,6 +359,38 @@ 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; +int offset, 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) +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.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 3/8] util/grub-editenv: add fs_envblk write helper
Quoted 39 lines · Michael Chang via Grub-devel
This patch adds the function
fs_envblk_writeto 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 thefs_envblkspecification. It performs size checks and uses file sync to ensure that the updated data is flushed.The helper is also added into the
fs_envblkops 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.Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>--- util/grub-editenv.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)util/grub-editenv.c +4
-1@@ -133,11 +133,14 @@ struct fs_envblk_spec { };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 ); } fs_envblk_ops = { - .open = fs_envblk_open + .open = fs_envblk_open, + .write = fs_envblk_write };struct fs_envblk {
util/grub-editenv.c write_envblk (const char *name, grub_envblk_t envblk) · line 367
Quoted 9 lines
util/grub-editenv.c +6
-0@@ -356,6 +359,38 @@ 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; +int offset, size;
Again, please use proper types for offset and size.
util/grub-editenv.c write_envblk (const char *name, grub_envblk_t envblk) · line 379
Quoted 11 lines
util/grub-editenv.c +11
-0+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" );
Why do you use "b" here? Environment block is not a binary (file)...
And s/r+/w+/?
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 3/8] util/grub-editenv: add fs_envblk write helper
Quoted 43 lines · Daniel Kiper
Quoted 42 lines · Michael Chang via Grub-devel
This patch adds the function
fs_envblk_writeto 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 thefs_envblkspecification. It performs size checks and uses file sync to ensure that the updated data is flushed.The helper is also added into the
fs_envblkops 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.Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>--- util/grub-editenv.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)util/grub-editenv.c +4
-1@@ -133,11 +133,14 @@ struct fs_envblk_spec { };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 ); } fs_envblk_ops = { - .open = fs_envblk_open + .open = fs_envblk_open, + .write = fs_envblk_write };struct fs_envblk {@@ -356,6 +359,38 @@ write_envblk (const char *name, grub_envblk_t envblk)fclose (fp); }
Quoted 8 lines
util/grub-editenv.c write_envblk (const char *name, grub_envblk_t envblk) · line 367
Quoted 6 lines
util/grub-editenv.c +6
-0+static void +fs_envblk_write (grub_envblk_t envblk) +{ +FILE *fp; +const char *device; +int offset, size;Again, please use proper types for offset and size.
OK. Will do it.
Quoted 14 lines
util/grub-editenv.c write_envblk (const char *name, grub_envblk_t envblk) · line 379
Quoted 12 lines
util/grub-editenv.c +11
-0+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" );Why do you use "b" here? Environment block is not a binary (file)...
Just in case there are side effects with newline characters on non-POSIX systems like MS Windows. Also existing grubenv uses "b" when opening files in write_envblk().
Quoted 2 lines
And s/r+/w+/?
IMHO "r+" is more suitable here as it will simply fail if the device file does not exist and unlike "w+" it will not attempt to create or truncate the file.
Thanks,
Michael
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 4/8 util/grub-editenv: wire set_variables to optional fs_envblk
In reply to Michael Chang via Grub-develThis patch changes set_variables so that it can use an external environment block when one is present. The variable next_entry is written into the external block, env_block is treated as read only, and all other variables are written into the normal file based envblk.
A cleanup step is added to handle cases where GRUB at runtime writes variables into the external block because file based updates are not safe on a copy on write filesystem such as Btrfs. For example, the savedefault command can update saved_entry, and on Btrfs GRUB will place that update in the external block instead of the file envblk. If an older copy remains in the external block, it would override the newer value from the file envblk when GRUB first loads the file and then applies the external block on top of it. To avoid this, whenever a variable is updated in the file envblk, any same named key in the external block is deleted.
--- util/grub-editenv.c | 55 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)
util/grub-editenv.c +53 -2
@@ -391,12 +391,33 @@ fs_envblk_write (grub_envblk_t envblk)fclose (fp); } +struct var_lookup_ctx { +const char *varname; +int found; +}; + +static int +var_lookup_iter (const char *varname,const char *value __attribute__ ((unused)),void *hook_data) +{ +struct var_lookup_ctx *ctx = (struct var_lookup_ctx *)hook_data; +if (grub_strcmp (ctx->varname, varname) ==0 ) + { + ctx->found =1 ; +return 1 ; + } +return 0 ; +} +static void set_variables (const char *name,int argc,char *argv[]) {grub_envblk_t envblk; +grub_envblk_t envblk_fs =NULL ; envblk =open_envblk_file (name); +if (fs_envblk !=NULL ) + envblk_fs = fs_envblk->ops->open (envblk); +while (argc) {char *p;
@@ -407,8 +428,32 @@ 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_fs) + { +if (!grub_envblk_set (envblk_fs, argv[0 ], p)) +grub_util_error ("%s" ,_ ("environment block too small" )); + } +else if (strcmp (argv[0 ],"env_block" ) ==0 ) + { +grub_util_warn (_ ("can't set env_block as it's read-only" )); + } +else + { +if (!grub_envblk_set (envblk, argv[0 ], p)) +grub_util_error ("%s" ,_ ("environment block too small" )); + +if (envblk_fs) + { +struct var_lookup_ctx ctx = { + .varname = argv[0 ], + .found =0 + }; + +grub_envblk_iterate (envblk_fs, &ctx, var_lookup_iter); +if (ctx.found) +grub_envblk_delete (envblk_fs, argv[0 ]); + } + } argc--; argv++;
@@ -416,6 +461,12 @@ set_variables (const char *name, int argc, char *argv[])write_envblk (name, envblk);grub_envblk_close (envblk); + +if (envblk_fs) + { + fs_envblk->ops->write (envblk_fs); +grub_envblk_close (envblk_fs); + } } static void
Signature · 7 lines
2.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 4/8] util/grub-editenv: wire set_variables to optional fs_envblk
Quoted 5 lines · Michael Chang via Grub-devel
This patch changes
set_variablesso that it can use an external environment block when one is present. The variablenext_entryis written into the external block,env_blockis treated as read only, and all other variables are written into the normal file based envblk.
Quoted 10 lines
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.
Sorry, I do not understand that. Why do you use two environment blocks instead of one? I think only one should be used. When env block is moved to Brtfs bootloader block then environment file should be immediately deleted from the filesystem.
Quoted 11 lines
Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>--- util/grub-editenv.c | 55 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)util/grub-editenv.c +0
-0
util/grub-editenv.c fs_envblk_write (grub_envblk_t envblk) · line 396
Quoted 6 lines
util/grub-editenv.c +3
-0@@ -391,12 +391,33 @@ fs_envblk_write (grub_envblk_t envblk)fclose (fp); } +struct var_lookup_ctx { +const char *varname; +int found;
bool please...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 4/8] util/grub-editenv: wire set_variables to optional fs_envblk
Quoted 17 lines · Daniel Kiper
Quoted 5 lines · Michael Chang via Grub-devel
This patch changes
set_variablesso that it can use an external environment block when one is present. The variablenext_entryis written into the external block,env_blockis treated as read only, and all other variables are written into the normal file based envblk.Quoted 10 lines
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.
Quoted 4 lines
Sorry, I do not understand that. Why do you use two environment blocks instead of one? I think only one should be used. When env block is moved to Brtfs bootloader block then environment file should be immediately deleted from the filesystem.
The grubenv file is still used as it is the destination for grub-set-default, grub-reboot, and other commands that set and list environment variables via grub-editenv. The idea is that by introducing the env_block variable in the grubenv file, it can point to a writable device block via blocklist syntax that a patched grub.cfg may use to read and write settings in addition to the read-only grubenv file at grub runtime.
It is not intended to replace the grubenv file, but rather to extend it to provide a static RW block for filesystems like btrfs, in order to workaround cases such as grub-reboot and savedefault that requires a writable envblk to work.
Quoted 15 lines
Quoted 15 lines
Signed-off-by Michael Chang <[email protected]>Reviewed-by Neal Gompa <[email protected]>--- util/grub-editenv.c | 55 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-)util/grub-editenv.c +0
-0@@ -391,12 +391,33 @@ fs_envblk_write (grub_envblk_t envblk)fclose (fp); }
Quoted 5 lines
util/grub-editenv.c fs_envblk_write (grub_envblk_t envblk) · line 396
Quoted 3 lines
util/grub-editenv.c +3
-0+struct var_lookup_ctx { +const char *varname; +int found;bool please...
OK.
Thanks,
Michael
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 5/8 util/grub-editenv: wire unset_variables to optional fs_envblk
In reply to Michael Chang via Grub-develThis 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
@@ -473,18 +473,32 @@ static void unset_variables (const char *name, int argc, char *argv[]) { grub_envblk_t envblk; +grub_envblk_t envblk_fs =NULL ; envblk =open_envblk_file (name); + +if (fs_envblk !=NULL ) + envblk_fs = fs_envblk->ops->open (envblk); +while (argc) {grub_envblk_delete (envblk, argv[0 ]); +if (envblk_fs) +grub_envblk_delete (envblk_fs, argv[0 ]); + argc--; argv++; }write_envblk (name, envblk);grub_envblk_close (envblk); + +if (envblk_fs) + { + fs_envblk->ops->write (envblk_fs); +grub_envblk_close (envblk_fs); + } } static int
Signature · 7 lines
2.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 6/8 util/grub-editenv: wire list_variables to optional fs_envblk
In reply to Michael Chang via Grub-develThis 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
@@ -333,10 +333,17 @@ static void list_variables (const char *name) {grub_envblk_t envblk; +grub_envblk_t envblk_fs =NULL ; envblk =open_envblk_file (name); +grub_envblk_iterate (envblk, &envblk_fs, read_envblk_fs);grub_envblk_iterate (envblk,NULL , print_var);grub_envblk_close (envblk); +if (envblk_fs) + { +grub_envblk_iterate (envblk_fs,NULL , print_var); +grub_envblk_close (envblk_fs); + } } static void
Signature · 7 lines
2.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 7/8 btrfs: add environment block to reserved header area
In reply to Michael Chang via Grub-develThis patch reserves space for the GRUB environment block inside the Btrfs header. The block is placed at an offset of 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.
--- grub-core/fs/btrfs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
grub-core/fs/btrfs.c +2 -1
@@ -2341,7 +2341,7 @@ struct embed_region {static const struct {struct embed_region available; -struct embed_region used[6 ]; +struct embed_region used[7 ]; } btrfs_head = { .available = {0 ,GRUB_DISK_KiB_TO_SECTORS (1024 )},/* The first 1 MiB. */ .used = {
@@ -2349,6 +2349,7 @@ 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_DISK_KiB_TO_SECTORS (ENV_BTRFS_OFFSET) - 1, 3}, /* Environment Block. */ {GRUB_DISK_KiB_TO_SECTORS (1024) - 1, 1}, /* Overflow guard. */ {0, 0} /* Array terminator. */ }
Signature · 7 lines
2.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 8/8 00_header.in: wire grub.cfg to use env_block when present
In reply to Michael Chang via Grub-develThis 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
@@ -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.50.1 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 16 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, andlist_variablesso 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 thatload_envandsave_envuse the external block automatically whenenv_blockis defined.
Quoted 10 lines
Michael Chang (8):
util/grub-editenv: add basic structures and probe call for external
envblk
util/grub-editenv: addfs_envblkopen helper util/grub-editenv: addfs_envblkwrite helper util/grub-editenv: wireset_variablesto optionalfs_envblkutil/grub-editenv: wireunset_variablesto optionalfs_envblkutil/grub-editenv: wirelist_variablesto optionalfs_envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to useenv_blockwhen present
I have stopped reviewing the series at the patch #4. Some code requires clarification. Additionally, please add a description of the new feature to the GRUB documentation.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 28 lines · Daniel Kiper
Quoted 16 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, andlist_variablesso 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 thatload_envandsave_envuse the external block automatically whenenv_blockis defined.Quoted 10 lines
Michael Chang (8):
util/grub-editenv: add basic structures and probe call for external
envblk
util/grub-editenv: addfs_envblkopen helper util/grub-editenv: addfs_envblkwrite helper util/grub-editenv: wireset_variablesto optionalfs_envblkutil/grub-editenv: wireunset_variablesto optionalfs_envblkutil/grub-editenv: wirelist_variablesto optionalfs_envblk
btrfs: add environment block to reserved header area 00_header.in: wire grub.cfg to useenv_blockwhen present
Quoted 3 lines
I have stopped reviewing the series at the patch #4. Some code requires clarification. Additionally, please add a description of the new feature to the GRUB documentation.
Thanks a lot for your time and effort in reviewing the patches. I will add a section in the documentation to better describe the feature in the next version.
Thanks,
Michael
Quoted 2 lines
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel