4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 typedef struct prop_data {
37 static int prop_list_builder_cb(zfs_handle_t *, void *);
38 static int prop_list_builder(prop_data_t *);
41 * Returns the name of the active boot environment
44 be_active_name(libbe_handle_t *lbh)
47 return (strrchr(lbh->rootfs, '/') + sizeof(char));
52 * Returns full path of the active boot environment
55 be_active_path(libbe_handle_t *lbh)
62 * Returns the name of the next active boot environment
65 be_nextboot_name(libbe_handle_t *lbh)
68 return (strrchr(lbh->bootfs, '/') + sizeof(char));
73 * Returns full path of the active boot environment
76 be_nextboot_path(libbe_handle_t *lbh)
84 * Returns the path of the boot environment root dataset
87 be_root_path(libbe_handle_t *lbh)
95 * Populates dsnvl with one nvlist per bootenv dataset describing the properties
96 * of that dataset that we've declared ourselves to care about.
99 be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl)
105 return (prop_list_builder(&data));
110 * Internal callback function used by zfs_iter_filesystems. For each dataset in
111 * the bootenv root, populate an nvlist_t of its relevant properties.
112 * TODO: should any other properties be included?
115 prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
117 char buf[512], *mountpoint;
121 const char *dataset, *name;
126 * some system for defining constants for the nvlist keys
129 data = (prop_data_t *)data_p;
132 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
134 dataset = zfs_get_name(zfs_hdl);
135 nvlist_add_string(props, "dataset", dataset);
137 name = strrchr(dataset, '/') + 1;
138 nvlist_add_string(props, "name", name);
140 mounted = zfs_is_mounted(zfs_hdl, &mountpoint);
141 nvlist_add_boolean_value(props, "mounted", mounted);
144 nvlist_add_string(props, "mountpoint", mountpoint);
146 if (zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512,
148 nvlist_add_string(props, "origin", buf);
150 if (zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512,
152 nvlist_add_string(props, "creation", buf);
154 nvlist_add_boolean_value(props, "active",
155 (strcmp(be_active_path(lbh), dataset) == 0));
157 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512,
159 nvlist_add_string(props, "used", buf);
161 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512,
163 nvlist_add_string(props, "usedds", buf);
165 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512,
167 nvlist_add_string(props, "usedsnap", buf);
169 if (zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512,
171 nvlist_add_string(props, "usedrefreserv", buf);
173 if (zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512,
175 nvlist_add_string(props, "referenced", buf);
177 nvlist_add_boolean_value(props, "nextboot",
178 (strcmp(be_nextboot_path(lbh), dataset) == 0));
180 nvlist_add_nvlist(data->list, name, props);
187 * Updates the properties of each bootenv in the libbe handle
188 * XXX TODO: rename to be_proplist_update
189 * XXX TODO: ensure that this is always consistent (run after adds, deletes,
193 prop_list_builder(prop_data_t *data)
195 zfs_handle_t *root_hdl;
197 if ((root_hdl = zfs_open(data->lbh->lzh, data->lbh->root,
198 ZFS_TYPE_FILESYSTEM)) == NULL)
199 return (BE_ERR_ZFSOPEN);
201 /* XXX TODO: some error checking here */
202 zfs_iter_filesystems(root_hdl, prop_list_builder_cb, data);
211 be_prop_list_alloc(nvlist_t **be_list)
214 return (nvlist_alloc(be_list, NV_UNIQUE_NAME, KM_SLEEP));
218 * frees property list and its children
221 be_prop_list_free(nvlist_t *be_list)
226 be_pair = nvlist_next_nvpair(be_list, NULL);
227 if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
228 nvlist_free(prop_list);
230 while ((be_pair = nvlist_next_nvpair(be_list, be_pair)) != NULL) {
231 if (nvpair_value_nvlist(be_pair, &prop_list) == 0)
232 nvlist_free(prop_list);
241 be_exists(libbe_handle_t *lbh, char *be)
243 char buf[BE_MAXPATHLEN];
245 be_root_concat(lbh, be, buf);
248 * XXX TODO: check mountpoint prop and see if its /, AND that result
249 * with below expression.
251 return (zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_DATASET));