From 3902eaf9ed180235c7b0cb6e513d50c3bd9bd2a1 Mon Sep 17 00:00:00 2001 From: Toomas Soome Date: Thu, 17 Sep 2020 20:51:09 +0300 Subject: [PATCH] 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 Signed-off-by: Toomas Soome Closes #10937 --- lib/libzfsbootenv/lzbe_pair.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/libzfsbootenv/lzbe_pair.c b/lib/libzfsbootenv/lzbe_pair.c index e702a8585..831355ba4 100644 --- a/lib/libzfsbootenv/lzbe_pair.c +++ b/lib/libzfsbootenv/lzbe_pair.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include /* * 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; zpool_handle_t *zphdl; nvlist_t *nv; + uint64_t version; int rv = -1; if (pool == NULL || *pool == '\0') @@ -92,6 +95,21 @@ lzbe_nvlist_set(const char *pool, const char *key, void *ptr) if (key != NULL) { rv = zpool_get_bootenv(zphdl, &nv); 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); if (rv == 0) rv = zpool_set_bootenv(zphdl, nv);