Date: Sat, 1 Jul 2017 12:02:53 GMT From: kneitinger@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r324147 - soc2017/kneitinger/libbe-head/lib/libbe Message-ID: <201707011202.v61C2rFn099947@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: kneitinger Date: Sat Jul 1 12:02:53 2017 New Revision: 324147 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=324147 Log: libbe(3): add zfs property list, refactor libbe_handle_t struct to be opaque Added: soc2017/kneitinger/libbe-head/lib/libbe/be_impl.h soc2017/kneitinger/libbe-head/lib/libbe/be_info.c Modified: soc2017/kneitinger/libbe-head/lib/libbe/Makefile soc2017/kneitinger/libbe-head/lib/libbe/be.c soc2017/kneitinger/libbe-head/lib/libbe/be.h soc2017/kneitinger/libbe-head/lib/libbe/be_error.c Modified: soc2017/kneitinger/libbe-head/lib/libbe/Makefile ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/Makefile Sat Jul 1 11:18:48 2017 (r324146) +++ soc2017/kneitinger/libbe-head/lib/libbe/Makefile Sat Jul 1 12:02:53 2017 (r324147) @@ -28,7 +28,7 @@ LIB= be SHLIB_MAJOR= 1 SHLIB_MINOR= 0 -SRCS= be.c be_error.c +SRCS= be.c be_error.c be_info.c INCS= be.h MAN= libbe.3 Modified: soc2017/kneitinger/libbe-head/lib/libbe/be.c ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be.c Sat Jul 1 11:18:48 2017 (r324146) +++ soc2017/kneitinger/libbe-head/lib/libbe/be.c Sat Jul 1 12:02:53 2017 (r324147) @@ -33,18 +33,7 @@ #include <sys/types.h> #include "be.h" - -/* - * Test iterator function to verify datasets were mounted correctly. Will be - * deleted shortly - */ -static int -zfs_printer(zfs_handle_t *zfs_hdl, void *data) -{ - fprintf(stdout, "Name: %s\n", zfs_get_name(zfs_hdl)); - return (0); -} - +#include "be_impl.h" /* * Initializes the libbe context to operate in the root boot environment @@ -81,6 +70,7 @@ char *path = strrchr(buf, ':'); path = path ? path + 1 : buf; + /* Obtain zfs_handle_t to the active bootenv */ if ((lbh->be_active = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET)) == NULL) { libzfs_fini(lbh->lzh); @@ -97,6 +87,8 @@ return (NULL); } + + /* Obtain zfs_handle_t to the bootenv root */ if ((lbh->be_root = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) { zfs_close(lbh->be_active); @@ -108,10 +100,8 @@ /* TODO: verify that /boot is mounted on the active be */ - /* Quick test to print child datasets and therefore verify that be_root - * was opened correctly. Will soon be deleted. - */ - zfs_iter_filesystems(lbh->be_root, zfs_printer, NULL); + prop_list_builder(lbh); + return (lbh); } @@ -126,37 +116,6 @@ zfs_close(lbh->be_active); zfs_close(lbh->be_root); libzfs_fini(lbh->lzh); + /* TODO: clean up property list */ free(lbh); } - - -/* - * Returns the name of the active boot environment - */ -const char * -be_get_active_name(libbe_handle_t *lbh) -{ - const char *full_path = zfs_get_name(lbh->be_active); - - return (strrchr(full_path, '/') + 1); -} - - -/* - * Returns full path of the active boot environment - */ -const char * -be_get_active_path(libbe_handle_t *lbh) -{ - return (zfs_get_name(lbh->be_active)); -} - - -/* - * Returns the path of the boot environment root dataset - */ -const char * -be_get_root_path(libbe_handle_t *lbh) -{ - return (zfs_get_name(lbh->be_root)); -} Modified: soc2017/kneitinger/libbe-head/lib/libbe/be.h ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be.h Sat Jul 1 11:18:48 2017 (r324146) +++ soc2017/kneitinger/libbe-head/lib/libbe/be.h Sat Jul 1 12:02:53 2017 (r324147) @@ -33,38 +33,36 @@ #define MAX_PATHLEN 512 -typedef enum be_error { - BE_ERR_SUCCESS = 0, /* No error */ - BE_ERR_INVALIDNAME, /* invalid boot env name */ - BE_ERR_EXISTS, /* boot env name already taken */ - BE_ERR_PERMS, /* insufficient permissions */ - BE_ERR_UNKNOWN, /* unknown error */ -} be_error_t; - -/* - * TODO: this should be moved to a different file so as to hide the internals - * from the interface - */ -typedef struct libbe_handle { - libzfs_handle_t *lzh; - zfs_handle_t *be_root; - zfs_handle_t *be_active; - be_error_t error; -} libbe_handle_t; +typedef struct libbe_handle libbe_handle_t; -/* Library handling functions */ +/* Library handling functions: be.c */ libbe_handle_t *libbe_init(void); void libbe_close(libbe_handle_t *); -/* Bootenv information functions */ +/* Bootenv information functions: be_info.c */ const char *be_get_active_name(libbe_handle_t *); const char *be_get_active_path(libbe_handle_t *); const char *be_get_root_path(libbe_handle_t *); +nvlist_t *be_get_bootenv_props(libbe_handle_t *); + +/* Bootenv creation functions */ +int be_create(libbe_handle_t *, char *); +int be_create_from_existing(libbe_handle_t *, char *, char *); /* Bootenv manipulation functions */ +int be_rename(libbe_handle_t *, char *, char *); + +/* Bootenv removal functions */ +int be_destroy(libbe_handle_t *, char *, int); + +/* Bootenv mounting functions */ +int be_mount(libbe_handle_t *, char *, char *); +int be_unmount(libbe_handle_t *, char *, int); +/* Bootenv jail functions */ +int be_jail(libbe_handle_t *, char *); -/* Error related functions */ +/* Error related functions: be_error.c */ int libbe_errno(libbe_handle_t *); const char *libbe_error_description(libbe_handle_t *); Modified: soc2017/kneitinger/libbe-head/lib/libbe/be_error.c ============================================================================== --- soc2017/kneitinger/libbe-head/lib/libbe/be_error.c Sat Jul 1 11:18:48 2017 (r324146) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_error.c Sat Jul 1 12:02:53 2017 (r324147) @@ -27,7 +27,7 @@ */ #include "be.h" - +#include "be_impl.h" /* * Usage Added: soc2017/kneitinger/libbe-head/lib/libbe/be_impl.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_impl.h Sat Jul 1 12:02:53 2017 (r324147) @@ -0,0 +1,53 @@ +/* + * be_impl.h + * + * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _LIBBE_IMPL_H +#define _LIBBE_IMPL_H + +#include <libzfs.h> + +typedef enum be_error { + BE_ERR_SUCCESS = 0, /* No error */ + BE_ERR_INVALIDNAME, /* invalid boot env name */ + BE_ERR_EXISTS, /* boot env name already taken */ + BE_ERR_PERMS, /* insufficient permissions */ + BE_ERR_UNKNOWN, /* unknown error */ +} be_error_t; + +struct libbe_handle { + libzfs_handle_t *lzh; + zfs_handle_t *be_root; + zfs_handle_t *be_active; + be_error_t error; + nvlist_t *list; +}; + +int prop_list_builder(libbe_handle_t *); +void prop_list_free(libbe_handle_t *); + +#endif /* _LIBBE_IMPL_H */ Added: soc2017/kneitinger/libbe-head/lib/libbe/be_info.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2017/kneitinger/libbe-head/lib/libbe/be_info.c Sat Jul 1 12:02:53 2017 (r324147) @@ -0,0 +1,187 @@ +/* + * be_info.c + * + * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "be.h" +#include "be_impl.h" + +static int prop_list_builder_cb(zfs_handle_t *, void *); + +/* + * Returns the name of the active boot environment + */ +const char * +be_get_active_name(libbe_handle_t *lbh) +{ + const char *full_path = zfs_get_name(lbh->be_active); + + return (strrchr(full_path, '/') + 1); +} + + +/* + * Returns full path of the active boot environment + */ +const char * +be_get_active_path(libbe_handle_t *lbh) +{ + return (zfs_get_name(lbh->be_active)); +} + + +/* + * Returns the path of the boot environment root dataset + */ +const char * +be_get_root_path(libbe_handle_t *lbh) +{ + return (zfs_get_name(lbh->be_root)); +} + + +/* + * Returns an nvlist of the bootenv's properties + * TODO: the nvlist should be passed as a param and ints should return status + */ +nvlist_t * +be_get_bootenv_props(libbe_handle_t *lbh) +{ + return (lbh->list); +} + + +/* + * Internal callback function used by zfs_iter_filesystems. For each dataset in + * the bootenv root, populate an nvlist_t of its relevant properties. + * TODO: should any other properties be included? + */ +static int +prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data) +{ + boolean_t mounted, active, nextboot; + /* TODO: what type for size? Number bytes as int or long? */ + + char buf[512]; + + nvlist_t *props; + + libbe_handle_t *lbh = (libbe_handle_t *)data; + + + nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); + + const char *dataset = zfs_get_name(zfs_hdl); + nvlist_add_string(props, "dataset", dataset); + + const char *name = strrchr(dataset, '/') + 1; + nvlist_add_string(props, "name", name); + + + mounted = zfs_prop_get_int(zfs_hdl, ZFS_PROP_MOUNTED); + nvlist_add_boolean_value(props, "mounted", mounted); + + if (mounted) { + zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, 512, + NULL, NULL, 0, 1); + nvlist_add_string(props, "mountpoint", buf); + } + + if ((zfs_prop_get(zfs_hdl, ZFS_PROP_ORIGIN, buf, 512, + NULL, NULL, 0, 1)) == 0) { + nvlist_add_string(props, "origin", buf); + } + + if ((zfs_prop_get(zfs_hdl, ZFS_PROP_CREATION, buf, 512, + NULL, NULL, 0, 1)) == 0) { + nvlist_add_string(props, "creation", buf); + } + + nvlist_add_boolean_value(props, "active", + (strcmp(be_get_active_path(lbh), dataset) == 0)); + + if ((zfs_prop_get(zfs_hdl, ZFS_PROP_USED, buf, 512, + NULL, NULL, 0, 1)) == 0) { + nvlist_add_string(props, "used", buf); + } + + if ((zfs_prop_get(zfs_hdl, ZFS_PROP_USEDDS, buf, 512, + NULL, NULL, 0, 1)) == 0) { + nvlist_add_string(props, "usedds", buf); + } + + if ((zfs_prop_get(zfs_hdl, ZFS_PROP_USEDSNAP, buf, 512, + NULL, NULL, 0, 1)) == 0) { + nvlist_add_string(props, "usedsnap", buf); + } + + if ((zfs_prop_get(zfs_hdl, ZFS_PROP_USEDREFRESERV, buf, 512, + NULL, NULL, 0, 1)) == 0) { + nvlist_add_string(props, "usedrefreserv", buf); + } + + if ((zfs_prop_get(zfs_hdl, ZFS_PROP_REFERENCED, buf, 512, + NULL, NULL, 0, 1)) == 0) { + nvlist_add_string(props, "referenced", buf); + } + + /* TODO figure out how to read nextboot (set in libzfs_pool.c) */ + + nvlist_add_nvlist(lbh->list, name, props); + + return (0); +} + + +/* + * Updates the properties of each bootenv in the libbe handle + * TODO: rename to be_proplist_update + */ +int +prop_list_builder(libbe_handle_t *lbh) +{ + if (lbh->list != NULL) { + /* TODO: should actually call prop_list_free */ + nvlist_free(lbh->list); + return (1); + } + + if (nvlist_alloc(&lbh->list, NV_UNIQUE_NAME, KM_SLEEP) != 0) { + /* TODO: actually handle error */ + return (1); + } + + zfs_iter_filesystems(lbh->be_root, prop_list_builder_cb, lbh); + + return (0); +} + + +void +prop_list_free(libbe_handle_t *lbh) +{ + /* TODO */ +}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201707011202.v61C2rFn099947>
