ZFS/other CoW FS save_env support
cover ZFS/other CoW FS save_env support
Hey all, I previously discussed my concept for this patch in my email
https://lists.gnu.org/archive/html/grub-devel/2020-01/msg00004.html . I'm pleased to announce that I've gotten it into a working state and it is ready to review. There are a number of changes here, which I will break down below.
First, the concept of "special files" is introduced into grub. These are files that are manipulated using different functions from the remainder of the filesystem. A filesystem advertises its support for these files by filling in new entries in the grub_fs struct.
Second, the loadenv command was modified to use the special file functions of the root filesystem if they are detected. If that happens, these functions are used to open, read, and write to the loadenv file instead of the normal grub file functions. A variable was also added that allows the user to force a file to be used instead of the special files, or vice versa.
Third, the grub-editenv command was modified to teach it to probe the root filesystem and then check in an array of structures that contain functions that will read and modify the filesystem's special file in userland. These functions are very similar to normal read and write functions, and so don't result in a very different code flow.
Finally, the GRUB ZFS logic was modified to have new special_file functions. These functions manipulate the pad2 area of the ZFS label, which was previously unused. It now stores a version number, the raw contents of the grubenv file, and an embedded checksum for integrity purposes. GRUB was taught to read and verify these areas, and also to modify them, computing the embeddded checksum appropriately. In addition, if GRUB is build with libzfs support, an entry is added to the grub-editenv table that points GRUB to the appropriate libzfs functions, and init and fini functions are built into grub-editenv itself.
Future work:
* In the ZFS code could store a packed nvlist instead of a raw file, but this would involve further changes to the grub environment file code and was deferred for when it would be more useful and important.
* For the special files code, there is only one special file allowed per filesystem, but a specification interface (using either an enum or a set of names) could be added in the future.
* Other filesystem support was not built into this change, but extensibility was a primary goal, so adding support for btrfs or other filesystems should be relatively straightforward.
* I did not implement the proposed UEFI variable support, but I did develop this with that future in mind, so adding it should be a relatively simple extension of these changes.
This patchset has been tested on systems with and without ZFS as the boot filesystem, and built with and without ZFS libraries installed. It worked in each case I tried, including with manual corruption applied to the ZFS labels to force GRUB to read from the other label. This was tested in conjunction with
https://github.com/zfsonlinux/zfs/pull/10009 , the ZoL patch that enables ZFS to read from and write to the padding areas we reserved for the data.
Paul Dagnelie (7):
Factor out file filter function for reuse
Add support for special envblk functions in GRUB
Factor out envblk buffer creation for reuse
Use envblk file and fs functions to implement reading the grubenv file
from special FS regions
Add ZFS envblock functions
Refactor default envblk size out for reuse
Update editenv to support editing zfs envblock, fix whitespace, and
autodetect bootenv support in libzfs
configure.ac | 8 + grub-core/commands/loadenv.c | 122 +++++++++++++-- grub-core/fs/zfs/zfs.c | 134 +++++++++++++++-- grub-core/kern/file.c | 76 +++++++--- include/grub/file.h | 10 ++ include/grub/fs.h | 12 ++ include/grub/lib/envblk.h | 3 + include/grub/util/install.h | 3 + include/grub/util/libzfs.h | 7 + include/grub/zfs/vdev_impl.h | 33 ++--- util/editenv.c | 31 ++-- util/grub-editenv.c | 278 +++++++++++++++++++++++++++++++++-- 12 files changed, 632 insertions(+), 85 deletions(-)
Signature · 1 line
2.19.0
PATCH 1/7 Factor out file filter function for reuse
This patch refactors out the logic that applies filters to files for reuse in other code.
--- grub-core/kern/file.c | 46 +++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-)
grub-core/kern/file.c +27 -19
@@ -57,14 +57,37 @@ grub_file_get_device_name (const char *name)return 0 ; } +static grub_file_t +grub_apply_file_filters (grub_file_t file,enum grub_file_type type,const char *name) +{ +grub_file_filter_id_t filter; +grub_file_t last_file =NULL ; + +for (filter =0 ; file && filter <ARRAY_SIZE (grub_file_filters); + filter++) +if (grub_file_filters[filter]) + { + last_file = file; + file = grub_file_filters[filter] (file, type); +if (file && file != last_file) + { + file->name =grub_strdup (name); + grub_errno =GRUB_ERR_NONE ; + } + } +if (!file) +grub_file_close (last_file); + +return file; +} +grub_file_t grub_file_open (const char *name,enum grub_file_type type) { -grub_device_t device =0 ; -grub_file_t file =0 , last_file =0 ; +grub_device_t device =NULL ; +grub_file_t file =NULL ;char *device_name;const char *file_name; -grub_file_filter_id_t filter; device_name =grub_file_get_device_name (name);if (grub_errno)
@@ -113,22 +136,7 @@ grub_file_open (const char *name, enum grub_file_type type) file->name =grub_strdup (name); grub_errno =GRUB_ERR_NONE ; -for (filter =0 ; file && filter <ARRAY_SIZE (grub_file_filters); - filter++) -if (grub_file_filters[filter]) - { - last_file = file; - file = grub_file_filters[filter] (file, type); -if (file && file != last_file) - { - file->name =grub_strdup (name); - grub_errno =GRUB_ERR_NONE ; - } - } -if (!file) -grub_file_close (last_file); - -return file; +return grub_apply_file_filters (file, type, name); fail:if (device)
Signature · 1 line
2.19.0
PATCH 2/7 Add support for special envblk functions in GRUB
In reply to Paul DagnelieThis patch introducces the idea of envblk functions to GRUB. These functions are used to interact with a grubenv file stored not in the usual grub tree, but instead in a special filesystem-managed region that can more easily be accessed at boot time. These functions interface between calling code and the per-fs logic, an interface for which is also added as part of this patch.
--- grub-core/kern/file.c | 30 ++++++++++++++++++++++++++++-- include/grub/file.h | 10 ++++++++++ include/grub/fs.h | 12 ++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-)
grub-core/kern/file.c +28 -2
@@ -149,6 +149,27 @@ grub_file_open (const char *name, enum grub_file_type type)return 0 ; } +grub_file_t +grub_file_envblk_open (grub_fs_t fs,grub_device_t device,enum grub_file_type type) +{ +grub_file_t file =NULL ; + file = (grub_file_t )grub_zalloc (sizeof (*file)); +if (! file) +return NULL ; + file->device = device; + file->special =1 ; + +if ((fs->fs_envblk_open) (file) !=GRUB_ERR_NONE ) { +grub_free (file); +return NULL ; + } + + file->fs = fs; + grub_errno =GRUB_ERR_NONE ; + +return grub_apply_file_filters (file, type,NULL ); +} +grub_disk_read_hook_t grub_file_progress_hook; grub_ssize_t
@@ -185,7 +206,10 @@ grub_file_read (grub_file_t file, void *buf, grub_size_t len) file->read_hook_data = file; file->progress_offset = file->offset; } - res = (file->fs->fs_read) (file, buf, len); +if (grub_file_envblk (file)) + res = (file->fs->fs_envblk_read) (file, buf, len); +else + res = (file->fs->fs_read) (file, buf, len); file->read_hook = read_hook; file->read_hook_data = read_hook_data;if (res >0 )
@@ -197,7 +221,9 @@ grub_file_read (grub_file_t file, void *buf, grub_size_t len)grub_err_t grub_file_close (grub_file_t file) { -if (file->fs->fs_close) +if (grub_file_envblk (file) && file->fs->fs_envblk_close) + (file->fs->fs_envblk_close) (file); +else if (file->fs->fs_close) (file->fs->fs_close) (file);if (file->device)
include/grub/file.h +10 -0
@@ -170,6 +170,9 @@ struct grub_file/* Caller-specific data passed to the read hook. */ void *read_hook_data; + +/* If the file is an FS's special file, which uses separate functions for interaction. */ +int special; };typedef struct grub_file *grub_file_t ;
@@ -211,6 +214,7 @@ grub_ssize_t EXPORT_FUNC(grub_file_read) (grub_file_t file, void *buf, grub_size_t len);grub_off_t EXPORT_FUNC (grub_file_seek ) (grub_file_t file,grub_off_t offset);grub_err_t EXPORT_FUNC (grub_file_close ) (grub_file_t file); +grub_file_t EXPORT_FUNC (grub_file_envblk_open ) (grub_fs_t fs,grub_device_t device,enum grub_file_type type);/* Return value of grub_file_size() in case file size is unknown. */ #define GRUB_FILE_SIZE_UNKNOWN 0xffffffffffffffffULL
@@ -233,6 +237,12 @@ grub_file_seekable (const grub_file_t file)return !file->not_easily_seekable; } +static inline int +grub_file_envblk (const grub_file_t file) +{ +return file->special; +} +grub_file_t grub_file_offset_open (grub_file_t parent,enum grub_file_type type,grub_off_t start,grub_off_t size);
include/grub/fs.h +12 -0
@@ -96,6 +96,18 @@ struct grub_fs/* Whether blocklist installs have a chance to work. */ int blocklist_install;#endif + +/* +* The envblk functions are defined on filesystems that need to handle +* grub-writable files in a special way. This is most commonly the case for +* CoW filesystems like btrfs and ZFS. The normal read and close functions +* should detect that they are being called on a special file and act +* appropriately. +*/ +grub_err_t (*fs_envblk_open) (struct grub_file *file); +grub_ssize_t (*fs_envblk_read) (struct grub_file *file,char *buf,grub_size_t len); +grub_err_t (*fs_envblk_write) (struct grub_file *file,char *buf,grub_size_t len); +grub_err_t (*fs_envblk_close) (struct grub_file *file); };typedef struct grub_fs *grub_fs_t ;
Signature · 1 line
2.19.0
PATCH 3/7 Factor out envblk buffer creation for reuse
In reply to Paul DagnelieThis patch factors out the filling of the grubenv buffer into a separate function for reuse.
--- include/grub/util/install.h | 3 +++ util/editenv.c | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-)
include/grub/util/install.h +3 -0
@@ -246,6 +246,9 @@ grub_install_get_blocklist (grub_device_t root_dev, void *data), void *hook_data); +void +grub_util_create_envblk_buffer (char *,size_t ); +void grub_util_create_envblk_file (const char *name);
util/editenv.c +17 -9
@@ -32,13 +32,29 @@#define DEFAULT_ENVBLK_SIZE 1024#define GRUB_ENVBLK_MESSAGE "# WARNING: Do not edit this file by tools other than "PACKAGE"-editenv!!!\n" +void +grub_util_create_envblk_buffer (char *buf,size_t size) +{ +if (size <DEFAULT_ENVBLK_SIZE ) +grub_util_error (_ ("Envblock buffer too small" )); +char *pbuf; + pbuf = buf; +memcpy (pbuf,GRUB_ENVBLK_SIGNATURE ,sizeof (GRUB_ENVBLK_SIGNATURE ) -1 ); + pbuf +=sizeof (GRUB_ENVBLK_SIGNATURE ) -1 ; +memcpy (pbuf,GRUB_ENVBLK_MESSAGE ,sizeof (GRUB_ENVBLK_MESSAGE ) -1 ); + pbuf +=sizeof (GRUB_ENVBLK_MESSAGE ) -1 ; +memset (pbuf ,'#' , + size -sizeof (GRUB_ENVBLK_SIGNATURE ) -sizeof (GRUB_ENVBLK_MESSAGE ) +2 ); +} +void grub_util_create_envblk_file (const char *name) {FILE *fp; -char *buf, *pbuf, *namenew; +char *buf, *namenew; buf =xmalloc (DEFAULT_ENVBLK_SIZE ); +grub_util_create_envblk_buffer (buf,DEFAULT_ENVBLK_SIZE ); namenew =xasprintf ("%s.new" , name); fp =grub_util_fopen (namenew,"wb" );
@@ -46,14 +62,6 @@ grub_util_create_envblk_file (const char *name)grub_util_error (_ ("cannot open `%s': %s" ), namenew,strerror (errno)); - pbuf = buf; -memcpy (pbuf,GRUB_ENVBLK_SIGNATURE ,sizeof (GRUB_ENVBLK_SIGNATURE ) -1 ); - pbuf +=sizeof (GRUB_ENVBLK_SIGNATURE ) -1 ; -memcpy (pbuf,GRUB_ENVBLK_MESSAGE ,sizeof (GRUB_ENVBLK_MESSAGE ) -1 ); - pbuf +=sizeof (GRUB_ENVBLK_MESSAGE ) -1 ; -memset (pbuf ,'#' , -DEFAULT_ENVBLK_SIZE -sizeof (GRUB_ENVBLK_SIGNATURE ) -sizeof (GRUB_ENVBLK_MESSAGE ) +2 ); -if (fwrite (buf,1 ,DEFAULT_ENVBLK_SIZE , fp) !=DEFAULT_ENVBLK_SIZE )grub_util_error (_ ("cannot write to `%s': %s" ), namenew,strerror (errno));
Signature · 1 line
2.19.0
PATCH 4/7 Use envblk file and fs functions to implement reading the grubenv file from special FS regions
In reply to Paul DagnelieThis patch modifies the loadenv command to take advantage of the new envblock functions provided by a previous patch. These functions will be used instead of the normal file-based functions if the boot filesystem is detected to support them. There is also a config variable, grubenv_src, that can be used to force the file or envblock logic to be used.
--- grub-core/commands/loadenv.c | 122 ++++++++++++++++++++++++++++++----- include/grub/lib/envblk.h | 2 + 2 files changed, 109 insertions(+), 15 deletions(-)
grub-core/commands/loadenv.c +107 -15
@@ -79,6 +79,94 @@ open_envblk_file (char *filename,return file; } +static grub_file_t +open_envblk_block (grub_fs_t fs,grub_device_t dev,enum grub_file_type type) +{ +if (fs->fs_envblk_open) +return grub_file_envblk_open (fs, dev, type); +return NULL ; +} + +static grub_file_t +open_envblk (grub_extcmd_context_t ctxt,enum grub_file_type type) +{ +struct grub_arg_list *state = ctxt->state; +grub_file_t file =NULL ; +grub_device_t device =NULL ; +const char *source; +grub_fs_t fs =NULL ; +char *filename = state[0 ].set ? state[0 ].arg :NULL ; + + source =grub_env_get ("grubenv_src" ); +if (! source || !grub_strcmp (source,GRUB_ENVBLK_SRC_BLK )) + { +char *device_name; +const char *prefix; +grub_dprintf ("loadenv" ,"checking for envblk\n" ); + + prefix =grub_env_get ("prefix" ); +if (! prefix) + { +grub_error (GRUB_ERR_FILE_NOT_FOUND ,N_ ("variable `%s' isn't set" ),"prefix" ); +return NULL ; + } + + device_name =grub_file_get_device_name (prefix); +if (grub_errno) +return NULL ; + + device =grub_device_open (device_name); +if (! device) +return NULL ; + + fs =grub_fs_probe (device); +if (! fs) { +grub_device_close (device); +return NULL ; + } + +/* We have to reopen the device here because it was closed in grub_fs_probe. */ + device =grub_device_open (device_name); +grub_free (device_name); +if (! device) +return NULL ; + + } +if (! source) + { +if (fs->fs_envblk_open) + { + source =GRUB_ENVBLK_SRC_BLK ; +grub_dprintf ("loadenv" ,"envblk support detected\n" ); + } +else + { + source =GRUB_ENVBLK_SRC_FILE ; +grub_dprintf ("loadenv" ,"envblk support not detected\n" ); + } + } + +if (!grub_strcmp (source,GRUB_ENVBLK_SRC_FILE )) + { +if (device) +grub_device_close (device); + file =open_envblk_file (filename, type); + +if (! file) +return NULL ; + } +else if (!grub_strcmp (source,GRUB_ENVBLK_SRC_BLK )) + { + file =open_envblk_block (fs, device, type); +if (! file) + { +grub_device_close (device); +return NULL ; + } + } +return file; +} +static grub_envblk_t read_envblk_file (grub_file_t file) {
@@ -165,11 +253,10 @@ grub_cmd_load_env (grub_extcmd_context_t ctxt, int argc, char **args) whitelist.len = argc; whitelist.list = args; -/* state[0] is the -f flag; state[1] is the --skip-sig flag */ - file =open_envblk_file ((state[0 ].set) ? state[0 ].arg :0 , -GRUB_FILE_TYPE_LOADENV - | (state[1 ].set - ?GRUB_FILE_TYPE_SKIP_SIGNATURE :GRUB_FILE_TYPE_NONE )); + file =open_envblk (ctxt,GRUB_FILE_TYPE_LOADENV + | (state[1 ].set + ?GRUB_FILE_TYPE_SKIP_SIGNATURE : +GRUB_FILE_TYPE_NONE ));if (! file)return grub_errno;
@@ -204,10 +291,9 @@ grub_cmd_list_env (grub_extcmd_context_t ctxt,grub_file_t file;grub_envblk_t envblk; - file =open_envblk_file ((state[0 ].set) ? state[0 ].arg :0 , -GRUB_FILE_TYPE_LOADENV - | (state[1 ].set - ?GRUB_FILE_TYPE_SKIP_SIGNATURE :GRUB_FILE_TYPE_NONE )); + file =open_envblk (ctxt,GRUB_FILE_TYPE_LOADENV + | (state[1 ].set + ?GRUB_FILE_TYPE_SKIP_SIGNATURE :GRUB_FILE_TYPE_NONE ));if (! file)return grub_errno;
@@ -379,7 +465,6 @@ save_env_read_hook (grub_disk_addr_t sector, unsigned offset, unsigned length,static grub_err_t grub_cmd_save_env (grub_extcmd_context_t ctxt,int argc,char **args) { -struct grub_arg_list *state = ctxt->state;grub_file_t file;grub_envblk_t envblk;struct grub_cmd_save_env_ctx ctx = {
@@ -390,9 +475,8 @@ grub_cmd_save_env (grub_extcmd_context_t ctxt, int argc, char **args)if (! argc)return grub_error (GRUB_ERR_BAD_ARGUMENT ,"no variable is specified" ); - file =open_envblk_file ((state[0 ].set) ? state[0 ].arg :0 , -GRUB_FILE_TYPE_SAVEENV - |GRUB_FILE_TYPE_SKIP_SIGNATURE ); + file =open_envblk (ctxt,GRUB_FILE_TYPE_SAVEENV + |GRUB_FILE_TYPE_SKIP_SIGNATURE );if (! file)return grub_errno;
@@ -409,7 +493,7 @@ grub_cmd_save_env (grub_extcmd_context_t ctxt, int argc, char **args)if (! envblk) goto fail; -if (check_blocklists (envblk, ctx.head, file)) +if (!grub_file_envblk (file) &&check_blocklists (envblk, ctx.head, file)) goto fail;while (argc)
@@ -432,7 +516,15 @@ grub_cmd_save_env (grub_extcmd_context_t ctxt, int argc, char **args) args++; } -write_blocklists (envblk, ctx.head, file); +if (grub_file_envblk (file)) + { +grub_file_seek (file,0 ); + file->fs->fs_envblk_write (file,grub_envblk_buffer (envblk),grub_envblk_size (envblk)); + } +else + { +write_blocklists (envblk, ctx.head, file); + } fail:if (envblk)
include/grub/lib/envblk.h +2 -0
@@ -21,6 +21,8 @@#define GRUB_ENVBLK_SIGNATURE "# GRUB Environment Block\n"#define GRUB_ENVBLK_DEFCFG "grubenv" +#define GRUB_ENVBLK_SRC_BLK "block" +#define GRUB_ENVBLK_SRC_FILE "file"#ifndef ASM_FILE
Signature · 1 line
2.19.0
PATCH 5/7 Add ZFS envblock functions
In reply to Paul DagnelieThis patch adds a ZFS implementation of the fs_envblk_* functions. These functions will be used to load the grubenv block from a padding area in the label of ZFS. This padding area is protected by an embedded checksum, and multiple copies are stored on each disk. This should provide sufficient reliability for most use cases. Since this area is not part of the block tree, it can be easily modified at boot time with a simple recalculation of the embedded checksum, enabling save_env to work for ZFS boot filesystems.
Note that the open() function is doing the read; this is because the ZFS envblk logic doesn't store the file size, so the only way to retrieve it is to determine the length of the file using strlen. If this is considered too poor of an approach to be acceptable, format changes can still be made to the structure of the padding area.
--- grub-core/fs/zfs/zfs.c | 134 ++++++++++++++++++++++++++++++++--- include/grub/zfs/vdev_impl.h | 33 +++++---- 2 files changed, 139 insertions(+), 28 deletions(-)
grub-core/fs/zfs/zfs.c +123 -11
@@ -30,6 +30,7 @@* */ +#include <stddef.h> #include <grub/err.h> #include <grub/file.h> #include <grub/mm.h>
@@ -1146,7 +1147,6 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data, {int label =0 ;uberblock_phys_t *ub_array, *ubbest =NULL ; -vdev_boot_header_t *bh;grub_err_t err;int vdevnum;struct grub_zfs_device_desc desc;
@@ -1155,13 +1155,6 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,if (!ub_array)return grub_errno; - bh =grub_malloc (VDEV_BOOT_HEADER_SIZE ); -if (!bh) - { -grub_free (ub_array); -return grub_errno; - } - vdevnum =VDEV_LABELS ; desc.dev = dev;
@@ -1175,7 +1168,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data, { desc.vdev_phys_sector = label * (sizeof (vdev_label_t) >>SPA_MINBLOCKSHIFT ) - + ((VDEV_SKIP_SIZE +VDEV_BOOT_HEADER_SIZE ) >>SPA_MINBLOCKSHIFT ) + + ((VDEV_PAD_SIZE *2 ) >>SPA_MINBLOCKSHIFT ) + (label <VDEV_LABELS /2 ?0 :ALIGN_DOWN (grub_disk_get_size (dev->disk),sizeof (vdev_label_t)) -VDEV_LABELS * (sizeof (vdev_label_t) >>SPA_MINBLOCKSHIFT ));
@@ -1184,6 +1177,7 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data, err =grub_disk_read (dev->disk, desc.vdev_phys_sector + (VDEV_PHYS_SIZE >>SPA_MINBLOCKSHIFT ),0 ,VDEV_UBERBLOCK_RING , (char *) ub_array); +if (err) { grub_errno =GRUB_ERR_NONE ;
@@ -1219,12 +1213,10 @@ scan_disk (grub_device_t dev, struct grub_zfs_data *data,continue ;#endif grub_free (ub_array); -grub_free (bh);return GRUB_ERR_NONE ; }grub_free (ub_array); -grub_free (bh);return grub_error (GRUB_ERR_BAD_FS ,"couldn't find a valid label" ); }
@@ -3765,6 +3757,60 @@ zfs_mtime (grub_device_t device, grub_int32_t *mt)return GRUB_ERR_NONE ; } +static grub_err_t +zfs_devs_read_zbt (struct grub_zfs_data *data,grub_uint64_t offset,char *buf,grub_size_t len) +{ +grub_err_t err =GRUB_ERR_NONE ; +zio_cksum_t zc; +unsigned int i; +ZIO_SET_CHECKSUM (&zc, offset,0 ,0 ,0 ); + +for (i =0 ; i < data->n_devices_attached; i++) + { + err =grub_disk_read (data->devices_attached[i].dev->disk, + offset >>SPA_MINBLOCKSHIFT , + offset & ((1 <<SPA_MINBLOCKSHIFT ) -1 ), + len, buf); +if (err) +continue ; +ZIO_SET_CHECKSUM (&zc, offset,0 ,0 ,0 ); + err =zio_checksum_verify (zc,ZIO_CHECKSUM_LABEL ,GRUB_ZFS_LITTLE_ENDIAN , + buf, len); +if (!err) +return GRUB_ERR_NONE ; + } +return err; +} + +static grub_err_t +grub_zfs_envblk_open (struct grub_file *file) +{ +grub_err_t err; +struct grub_zfs_data *data; +vdev_boot_envblock_t *vbe; +int l; + + file->offset =0 ; + data =zfs_mount (file->device); + file->data = data; + data->file_buf =grub_malloc (sizeof (vdev_boot_envblock_t)); +for (l =0 ; l <VDEV_LABELS /2 ; l++) + { +grub_uint64_t offset = l *sizeof (vdev_label_t) + offsetof (vdev_label_t , vl_be); + + err =zfs_devs_read_zbt (data, offset, data->file_buf, +sizeof (vdev_boot_envblock_t)); +if (err ==GRUB_ERR_NONE ) + { + vbe = (vdev_boot_envblock_t *)data->file_buf; + file->size =grub_strlen (vbe->vbe_bootenv); +return err; + } + } +zfs_unmount (data); +return err; +} +/* * zfs_open() locates a file in the rootpool by following the * MOS and places the dnode of the file in the memory address DNODE.
@@ -3850,6 +3896,19 @@ grub_zfs_open (struct grub_file *file, const char *fsfilename)return GRUB_ERR_NONE ; } +static grub_ssize_t +grub_zfs_envblk_read (grub_file_t file,char *buf,grub_size_t len) +{ +struct grub_zfs_data *data = (struct grub_zfs_data *) file->data; +grub_ssize_t olen = len; +grub_uint64_t offset = file->offset + offsetof (vdev_boot_envblock_t , vbe_bootenv); + +if (len + file->offset > file->size) + olen = file->size - file->offset; +grub_memmove (buf, data->file_buf + offset, olen); +return olen; +} +static grub_ssize_t grub_zfs_read (grub_file_t file,char *buf,grub_size_t len) {
@@ -3859,6 +3918,9 @@ grub_zfs_read (grub_file_t file, char *buf, grub_size_t len)grub_size_t read;grub_err_t err; +if (grub_file_envblk (file)) +return grub_zfs_envblk_read (file, buf, len); +/* * If offset is in memory, move it into the buffer provided and return. */
@@ -3924,6 +3986,52 @@ grub_zfs_read (grub_file_t file, char *buf, grub_size_t len)return len; } +static grub_err_t +grub_zfs_envblk_write (struct grub_file *file,char *buf,grub_size_t len) +{ +grub_uint64_t offset = file->offset + offsetof (vdev_boot_envblock_t , vbe_bootenv); +grub_err_t err =GRUB_ERR_NONE ; +struct grub_zfs_data *data = file->data; +unsigned int i, l; +zio_cksum_t cksum; +vdev_boot_envblock_t *vbe = (vdev_boot_envblock_t *)data->file_buf; + +if (len + file->offset > file->size) +return GRUB_ERR_OUT_OF_RANGE ; + +grub_memmove (data->file_buf + offset, buf, len); + +for (l =0 ; l <VDEV_LABELS /2 ; l++) + { + offset = l *sizeof (vdev_label_t) + offsetof (vdev_label_t , vl_be); + vbe->vbe_zbt.zec_magic =ZEC_MAGIC ; +ZIO_SET_CHECKSUM (&vbe->vbe_zbt.zec_cksum, offset,0 ,0 ,0 ); + vbe->vbe_zbt.zec_magic =ZEC_MAGIC ; +zio_checksum_SHA256 (vbe,VDEV_PAD_SIZE ,GRUB_ZFS_LITTLE_ENDIAN , &cksum); + vbe->vbe_zbt.zec_cksum = cksum; + +for (i =0 ; i < data->n_devices_attached; i++) + { +struct grub_zfs_device_desc *desc = &data->devices_attached[i]; +int num_sectors =VDEV_PAD_SIZE >> desc->ashift; +int label_seek = l *sizeof (vdev_label_t) >> desc->ashift; +int j; + +for (j =0 ; j < num_sectors; j++) + { +grub_err_t err2; + + err2 =grub_disk_write (desc->dev->disk, label_seek + num_sectors + j,0 , +1 << desc->ashift, + data->file_buf + (j << desc->ashift)); +if (err2 !=GRUB_ERR_NONE ) + err = err2; + } + } + } +return err; +} +static grub_err_t grub_zfs_close (grub_file_t file) {
@@ -4360,6 +4468,10 @@ static struct grub_fs grub_zfs_fs = { .reserved_first_sector = 1, .blocklist_install = 0, #endif + .fs_envblk_open = grub_zfs_envblk_open, + .fs_envblk_read = grub_zfs_read, + .fs_envblk_write = grub_zfs_envblk_write, + .fs_envblk_close = grub_zfs_close, .next =0 };
include/grub/zfs/vdev_impl.h +16 -17
@@ -23,34 +23,33 @@#ifndef _SYS_VDEV_IMPL_H#define _SYS_VDEV_IMPL_H -#define VDEV_SKIP_SIZE (8 << 10) -#define VDEV_BOOT_HEADER_SIZE (8 << 10) +#define VDEV_PAD_SIZE (8 << 10)#define VDEV_PHYS_SIZE (112 << 10)#define VDEV_UBERBLOCK_RING (128 << 10) -/* ZFS boot block */ -#define VDEV_BOOT_MAGIC 0x2f5b007b10cULL -#define VDEV_BOOT_VERSION 1/* version number */ - -typedef struct vdev_boot_header { -grub_uint64_t vb_magic;/* VDEV_BOOT_MAGIC */ -grub_uint64_t vb_version;/* VDEV_BOOT_VERSION */ -grub_uint64_t vb_offset;/* start offset (bytes) */ -grub_uint64_t vb_size;/* size (bytes) */ -char vb_pad[VDEV_BOOT_HEADER_SIZE -4 *sizeof (grub_uint64_t)]; -}vdev_boot_header_t ; -typedef struct vdev_phys {char vp_nvlist[VDEV_PHYS_SIZE -sizeof (zio_eck_t)];zio_eck_t vp_zbt; }vdev_phys_t ; +typedef enum vbe_vers { +VB_RAW , +VB_NVLIST +}vb_evers_t ; + +typedef struct vdev_boot_envblock { +grub_uint64_t vbe_version; +char vbe_bootenv[VDEV_PAD_SIZE -sizeof (grub_uint64_t) - +sizeof (zio_eck_t)]; +zio_eck_t vbe_zbt; +}vdev_boot_envblock_t ; +typedef struct vdev_label { -char vl_pad[VDEV_SKIP_SIZE ];/* 8K */ -vdev_boot_header_t vl_boot_header;/* 8K */ +char vl_pad1[VDEV_PAD_SIZE ];/* 8K */ +vdev_boot_envblock_t vl_be;/* 8K */ vdev_phys_t vl_vdev_phys;/* 112K */ char vl_uberblock[VDEV_UBERBLOCK_RING ];/* 128K */ -}vdev_label_t ;/* 256K total */ +}vdev_label_t ;/* 256K total */ /* * Size and offset of embedded boot loader region on each label.
Signature · 1 line
2.19.0
PATCH 6/7 Refactor default envblk size out for reuse
In reply to Paul DagnelieThis patch refactors the DEFAULT_ENVBLK_SIZE definition into a header file for reuse in other code, and renames it to match other definitions in that header.
--- include/grub/lib/envblk.h | 1 + util/editenv.c | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-)
include/grub/lib/envblk.h +1 -0
@@ -23,6 +23,7 @@#define GRUB_ENVBLK_DEFCFG "grubenv"#define GRUB_ENVBLK_SRC_BLK "block"#define GRUB_ENVBLK_SRC_FILE "file" +#define GRUB_ENVBLK_DEFAULT_SIZE 1024#ifndef ASM_FILE
util/editenv.c +4 -5
@@ -29,13 +29,12 @@#include <errno.h> #include <string.h> -#define DEFAULT_ENVBLK_SIZE 1024#define GRUB_ENVBLK_MESSAGE "# WARNING: Do not edit this file by tools other than "PACKAGE"-editenv!!!\n"void grub_util_create_envblk_buffer (char *buf,size_t size) { -if (size <DEFAULT_ENVBLK_SIZE ) +if (size <GRUB_ENVBLK_DEFAULT_SIZE )grub_util_error (_ ("Envblock buffer too small" ));char *pbuf; pbuf = buf;
@@ -53,8 +52,8 @@ grub_util_create_envblk_file (const char *name)FILE *fp;char *buf, *namenew; - buf =xmalloc (DEFAULT_ENVBLK_SIZE ); -grub_util_create_envblk_buffer (buf,DEFAULT_ENVBLK_SIZE ); + buf =xmalloc (GRUB_ENVBLK_DEFAULT_SIZE ); +grub_util_create_envblk_buffer (buf,GRUB_ENVBLK_DEFAULT_SIZE ); namenew =xasprintf ("%s.new" , name); fp =grub_util_fopen (namenew,"wb" );
@@ -62,7 +61,7 @@ grub_util_create_envblk_file (const char *name)grub_util_error (_ ("cannot open `%s': %s" ), namenew,strerror (errno)); -if (fwrite (buf,1 ,DEFAULT_ENVBLK_SIZE , fp) !=DEFAULT_ENVBLK_SIZE ) +if (fwrite (buf,1 ,GRUB_ENVBLK_DEFAULT_SIZE , fp) !=GRUB_ENVBLK_DEFAULT_SIZE )grub_util_error (_ ("cannot write to `%s': %s" ), namenew,strerror (errno));
Signature · 1 line
2.19.0
PATCH 7/7 Update editenv to support editing zfs envblock, fix whitespace, and autodetect bootenv support in libzfs
In reply to Paul DagnelieThis patch updates the grub-editenv command to support editing the zfs envblock by using specific libzfs functions. In order to ensure that GRUB will continue to build against both old and new versions of libzfs, logic was also added to the configure script to detect if the bootenv functions are present in the version of libzfs present at build time. There is also a small whitespace fix in grub-editenv unrelated to the content of these changes.
--- configure.ac | 8 ++ include/grub/util/libzfs.h | 7 + util/grub-editenv.c | 278 +++++++++++++++++++++++++++++++++++-- 3 files changed, 284 insertions(+), 9 deletions(-)
configure.ac +8 -0
@@ -1853,6 +1853,14 @@ if test x"$libzfs_excuse" = x ; then LIBNVPAIR="-lnvpair" AC_DEFINE([HAVE_LIBNVPAIR], [1], [Define to 1 if you have the NVPAIR library.]) + + AC_CHECK_LIB([zfs], [zpool_get_bootenv], + [libzfs_have_bootenv="yes"], + []) + if test x"$libzfs_have_bootenv" = xyes ; then + AC_DEFINE([HAVE_LIBZFS_BOOTENV], [1], + [Define to 1 if you have a version of the ZFS library with bootenv support.]) + fi fi AC_SUBST([LIBZFS])
include/grub/util/libzfs.h +7 -0
@@ -40,6 +40,13 @@ extern int zpool_get_physpath (zpool_handle_t *, const char *);extern nvlist_t *zpool_get_config (zpool_handle_t *,nvlist_t **); +extern libzfs_handle_t *zpool_get_handle (zpool_handle_t *); + +extern int zpool_set_bootenv (zpool_handle_t *,const char *); +extern int zpool_get_bootenv (zpool_handle_t *,char *,size_t ,off_t ); + +extern int libzfs_errno (libzfs_handle_t *); +#endif /* ! HAVE_LIBZFS_H */ libzfs_handle_t *grub_get_libzfs_handle (void );
util/grub-editenv.c +269 -9
349 lines not on this page · raw message
Signature · 1 line
2.19.0
Hi Paul,
Quoted 49 lines · Paul Dagnelie
Hey all, I previously discussed my concept for this patch in my email
https://lists.gnu.org/archive/html/grub-devel/2020-01/msg00004.html . I'm pleased to announce that I've gotten it into a working state and it is ready to review. There are a number of changes here, which I will break down below.First, the concept of "special files" is introduced into grub. These are files that are manipulated using different functions from the remainder of the filesystem. A filesystem advertises its support for these files by filling in new entries in the
grub_fsstruct.Second, the loadenv command was modified to use the special file functions of the root filesystem if they are detected. If that happens, these functions are used to open, read, and write to the loadenv file instead of the normal grub file functions. A variable was also added that allows the user to force a file to be used instead of the special files, or vice versa.
Third, the grub-editenv command was modified to teach it to probe the root filesystem and then check in an array of structures that contain functions that will read and modify the filesystem's special file in userland. These functions are very similar to normal read and write functions, and so don't result in a very different code flow.
Finally, the GRUB ZFS logic was modified to have new
special_filefunctions. These functions manipulate the pad2 area of the ZFS label, which was previously unused. It now stores a version number, the raw contents of the grubenv file, and an embedded checksum for integrity purposes. GRUB was taught to read and verify these areas, and also to modify them, computing the embeddded checksum appropriately. In addition, if GRUB is build with libzfs support, an entry is added to the grub-editenv table that points GRUB to the appropriate libzfs functions, and init and fini functions are built into grub-editenv itself.Future work:
* In the ZFS code could store a packed nvlist instead of a raw file, but this would involve further changes to the grub environment file code and was deferred for when it would be more useful and important.
* For the special files code, there is only one special file allowed per filesystem, but a specification interface (using either an enum or a set of names) could be added in the future.
* Other filesystem support was not built into this change, but extensibility was a primary goal, so adding support for btrfs or other filesystems should be relatively straightforward.
* I did not implement the proposed UEFI variable support, but I did develop this with that future in mind, so adding it should be a relatively simple extension of these changes.
Quoted 8 lines
This patchset has been tested on systems with and without ZFS as the boot filesystem, and built with and without ZFS libraries installed. It worked in each case I tried, including with manual corruption applied to the ZFS labels to force GRUB to read from the other label. This was tested in conjunction with
https://github.com/zfsonlinux/zfs/pull/10009 , the ZoL patch that enables ZFS to read from and write to the padding areas we reserved for the data.
First of all I would like to thank you for doing this work. This is very interesting feature. I went quickly through the code and it looks quite nice. However, I realized that it touches a lot of critical places in the GRUB code. ...and we are in freeze right now. So, at this point I think it is dangerous to merge that code. Simply we can make a lot of fallout which we may not be able to catch and clear before the release. Hence, I would like to suggest to defer this work until the release. Sorry about that but I do not want to risk GRUB code breakage at this point. I hope this is not a problem for you.
Daniel
Not a problem, I suspected as much. We don't have any strong need to get this work integrated ASAP, since we are going to have to build a custom version of GRUB for internal use until this makes its way into a release that lands in an Ubuntu LTS. We just want to make sure that we get some good reviews on the changes from people with experience in the codebase, and that one day we will be able to stop maintaining our fork.
Quoted 73 lines · Daniel Kiper
Hi Paul,
Quoted 49 lines · Paul Dagnelie
Hey all, I previously discussed my concept for this patch in my email
https://lists.gnu.org/archive/html/grub-devel/2020-01/msg00004.html . I'm pleased to announce that I've gotten it into a working state and it is ready to review. There are a number of changes here, which I will break down below.First, the concept of "special files" is introduced into grub. These are files that are manipulated using different functions from the remainder of the filesystem. A filesystem advertises its support for these files by filling in new entries in the
grub_fsstruct.Second, the loadenv command was modified to use the special file functions of the root filesystem if they are detected. If that happens, these functions are used to open, read, and write to the loadenv file instead of the normal grub file functions. A variable was also added that allows the user to force a file to be used instead of the special files, or vice versa.
Third, the grub-editenv command was modified to teach it to probe the root filesystem and then check in an array of structures that contain functions that will read and modify the filesystem's special file in userland. These functions are very similar to normal read and write functions, and so don't result in a very different code flow.
Finally, the GRUB ZFS logic was modified to have new
special_filefunctions. These functions manipulate the pad2 area of the ZFS label, which was previously unused. It now stores a version number, the raw contents of the grubenv file, and an embedded checksum for integrity purposes. GRUB was taught to read and verify these areas, and also to modify them, computing the embeddded checksum appropriately. In addition, if GRUB is build with libzfs support, an entry is added to the grub-editenv table that points GRUB to the appropriate libzfs functions, and init and fini functions are built into grub-editenv itself.Future work:
* In the ZFS code could store a packed nvlist instead of a raw file, but this would involve further changes to the grub environment file code and was deferred for when it would be more useful and important.
* For the special files code, there is only one special file allowed per filesystem, but a specification interface (using either an enum or a set of names) could be added in the future.
* Other filesystem support was not built into this change, but extensibility was a primary goal, so adding support for btrfs or other filesystems should be relatively straightforward.
* I did not implement the proposed UEFI variable support, but I did develop this with that future in mind, so adding it should be a relatively simple extension of these changes.Quoted 8 lines
This patchset has been tested on systems with and without ZFS as the boot filesystem, and built with and without ZFS libraries installed. It worked in each case I tried, including with manual corruption applied to the ZFS labels to force GRUB to read from the other label. This was tested in conjunction with
https://github.com/zfsonlinux/zfs/pull/10009 , the ZoL patch that enables ZFS to read from and write to the padding areas we reserved for the data.First of all I would like to thank you for doing this work. This is very interesting feature. I went quickly through the code and it looks quite nice. However, I realized that it touches a lot of critical places in the GRUB code. ...and we are in freeze right now. So, at this point I think it is dangerous to merge that code. Simply we can make a lot of fallout which we may not be able to catch and clear before the release. Hence, I would like to suggest to defer this work until the release. Sorry about that but I do not want to risk GRUB code breakage at this point. I hope this is not a problem for you.
Daniel
Signature · 1 line
Paul Dagnelie