libzfsbootenv: lzbe_nvlist_set needs to store bootenv version VB_NVLIST

A small bug did slip into initial libzfsbootenv; while storing nvlist
in nvlist, we should make sure the bootenv is using VB_NVLIST format.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Toomas Soome <tsoome@me.com>
Closes #10937
This commit is contained in:
Toomas Soome 2020-09-17 20:51:09 +03:00 committed by GitHub
parent 7ead2be3d2
commit 741b20ce0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -16,6 +16,8 @@
#include <string.h> #include <string.h>
#include <libzfs.h> #include <libzfs.h>
#include <libzfsbootenv.h> #include <libzfsbootenv.h>
#include <sys/zfs_bootenv.h>
#include <sys/vdev_impl.h>
/* /*
* Get or create nvlist. If key is not NULL, get nvlist from bootenv, * Get or create nvlist. If key is not NULL, get nvlist from bootenv,
@ -74,6 +76,7 @@ lzbe_nvlist_set(const char *pool, const char *key, void *ptr)
libzfs_handle_t *hdl; libzfs_handle_t *hdl;
zpool_handle_t *zphdl; zpool_handle_t *zphdl;
nvlist_t *nv; nvlist_t *nv;
uint64_t version;
int rv = -1; int rv = -1;
if (pool == NULL || *pool == '\0') if (pool == NULL || *pool == '\0')
@ -92,6 +95,21 @@ lzbe_nvlist_set(const char *pool, const char *key, void *ptr)
if (key != NULL) { if (key != NULL) {
rv = zpool_get_bootenv(zphdl, &nv); rv = zpool_get_bootenv(zphdl, &nv);
if (rv == 0) { if (rv == 0) {
/*
* We got the nvlist, check for version.
* if version is missing or is not VB_NVLIST,
* create new list.
*/
rv = nvlist_lookup_uint64(nv, BOOTENV_VERSION,
&version);
if (rv != 0 || version != VB_NVLIST) {
/* Drop this nvlist */
fnvlist_free(nv);
/* Create and prepare new nvlist */
nv = fnvlist_alloc();
fnvlist_add_uint64(nv, BOOTENV_VERSION,
VB_NVLIST);
}
rv = nvlist_add_nvlist(nv, key, ptr); rv = nvlist_add_nvlist(nv, key, ptr);
if (rv == 0) if (rv == 0)
rv = zpool_set_bootenv(zphdl, nv); rv = zpool_set_bootenv(zphdl, nv);