From owner-svn-src-projects@freebsd.org Sun Aug 5 19:38:57 2018 Return-Path: Delivered-To: svn-src-projects@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A7FAF105BC2A for ; Sun, 5 Aug 2018 19:38:57 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5C25374AAB; Sun, 5 Aug 2018 19:38:57 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3A58922922; Sun, 5 Aug 2018 19:38:57 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w75Jcv43023784; Sun, 5 Aug 2018 19:38:57 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w75JcuTj023779; Sun, 5 Aug 2018 19:38:56 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201808051938.w75JcuTj023779@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Sun, 5 Aug 2018 19:38:56 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r337358 - in projects/bectl: lib/libbe sbin/bectl X-SVN-Group: projects X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: in projects/bectl: lib/libbe sbin/bectl X-SVN-Commit-Revision: 337358 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 05 Aug 2018 19:38:57 -0000 Author: kevans Date: Sun Aug 5 19:38:56 2018 New Revision: 337358 URL: https://svnweb.freebsd.org/changeset/base/337358 Log: bectl(8): Implement `bectl list -s` be_get_dataset_snapshots has been added to libbe(3), effectively returning the same information as be_get_bootenv_props but for snapshots of the given dataset. The assumption is that one will have the BE dataset name before wanting to grab snapshots. Modified: projects/bectl/lib/libbe/be.h projects/bectl/lib/libbe/be_info.c projects/bectl/lib/libbe/libbe.3 projects/bectl/sbin/bectl/bectl.c Modified: projects/bectl/lib/libbe/be.h ============================================================================== --- projects/bectl/lib/libbe/be.h Sun Aug 5 19:21:50 2018 (r337357) +++ projects/bectl/lib/libbe/be.h Sun Aug 5 19:38:56 2018 (r337358) @@ -68,6 +68,7 @@ const char *be_root_path(libbe_handle_t *); int be_get_bootenv_props(libbe_handle_t *, nvlist_t *); int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *); +int be_get_dataset_snapshots(libbe_handle_t *, const char *, nvlist_t *); int be_prop_list_alloc(nvlist_t **be_list); void be_prop_list_free(nvlist_t *be_list); Modified: projects/bectl/lib/libbe/be_info.c ============================================================================== --- projects/bectl/lib/libbe/be_info.c Sun Aug 5 19:21:50 2018 (r337357) +++ projects/bectl/lib/libbe/be_info.c Sun Aug 5 19:38:56 2018 (r337358) @@ -29,6 +29,8 @@ #include "be.h" #include "be_impl.h" +static int snapshot_prop_list_builder(zfs_handle_t *hdl, prop_data_t *data); + /* * Returns the name of the active boot environment */ @@ -117,6 +119,25 @@ be_get_dataset_props(libbe_handle_t *lbh, const char * return (ret); } +int +be_get_dataset_snapshots(libbe_handle_t *lbh, const char *name, nvlist_t *props) +{ + zfs_handle_t *ds_hdl; + prop_data_t data; + int ret; + + data.lbh = lbh; + data.list = props; + data.single_object = false; + if ((ds_hdl = zfs_open(lbh->lzh, name, + ZFS_TYPE_FILESYSTEM)) == NULL) + return (BE_ERR_ZFSOPEN); + + ret = snapshot_prop_list_builder(ds_hdl, &data); + zfs_close(ds_hdl); + return (ret); +} + /* * Internal callback function used by zfs_iter_filesystems. For each dataset in * the bootenv root, populate an nvlist_t of its relevant properties. @@ -219,6 +240,13 @@ prop_list_builder(prop_data_t *data) zfs_close(root_hdl); return (0); +} + +static int +snapshot_prop_list_builder(zfs_handle_t *hdl, prop_data_t *data) +{ + + return (zfs_iter_snapshots_sorted(hdl, prop_list_builder_cb, data)); } Modified: projects/bectl/lib/libbe/libbe.3 ============================================================================== --- projects/bectl/lib/libbe/libbe.3 Sun Aug 5 19:21:50 2018 (r337357) +++ projects/bectl/lib/libbe/libbe.3 Sun Aug 5 19:38:56 2018 (r337358) @@ -144,6 +144,9 @@ of state to be retained, such as errors from previous .Ft int .Fn be_get_dataset_props "libbe_handle_t *, const char *, nvlist_t *" ; .Pp +.Ft int +.Fn be_get_dataset_snapshots "libbe_handle_t *, const char *, nvlist_t *" ; +.Pp .Ft void .Fn be_prop_list_free "nvlist_t *" ; .\" .Ft void Modified: projects/bectl/sbin/bectl/bectl.c ============================================================================== --- projects/bectl/sbin/bectl/bectl.c Sun Aug 5 19:21:50 2018 (r337357) +++ projects/bectl/sbin/bectl/bectl.c Sun Aug 5 19:38:56 2018 (r337358) @@ -74,6 +74,7 @@ static int bectl_cmd_add(int argc, char *argv[]); static int bectl_cmd_jail(int argc, char *argv[]); static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops); static void print_padding(const char *fval, int colsz, struct printc *pc); +static int print_snapshots(const char *dsname, struct printc *pc); static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc); static void print_headers(nvlist_t *props, struct printc *pc); static int bectl_cmd_list(int argc, char *argv[]); @@ -505,6 +506,28 @@ dataset_space(const char *oname) return (space); } +static int +print_snapshots(const char *dsname, struct printc *pc) +{ + nvpair_t *cur; + nvlist_t *props, *sprops; + + if (be_prop_list_alloc(&props) != 0) { + fprintf(stderr, "bectl list: failed to allocate snapshot nvlist\n"); + return (1); + } + if (be_get_dataset_snapshots(be, dsname, props) != 0) { + fprintf(stderr, "bectl list: failed to fetch boot ds snapshots\n"); + return (1); + } + for (cur = nvlist_next_nvpair(props, NULL); cur != NULL; + cur = nvlist_next_nvpair(props, cur)) { + nvpair_value_nvlist(cur, &sprops); + print_info(nvpair_name(cur), sprops, pc); + } + return (0); +} + static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc) { @@ -513,27 +536,34 @@ print_info(const char *name, nvlist_t *dsprops, struct unsigned long long ctimenum, space; nvlist_t *originprops; const char *oname; - char *propstr; + char *dsname, *propstr; int active_colsz; boolean_t active_now, active_reboot; + dsname = NULL; originprops = NULL; printf("%*s%s", pc->current_indent, "", name); + nvlist_lookup_string(dsprops, "dataset", &dsname); /* Recurse at the base level if we're breaking info down */ if (pc->current_indent == 0 && (pc->show_all_datasets || pc->show_snaps)) { printf("\n"); - if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0) + if (dsname == NULL) /* XXX TODO: Error? */ return; pc->current_indent += INDENT_INCREMENT; - print_info(propstr, dsprops, pc); + print_info(dsname, dsprops, pc); pc->current_indent += INDENT_INCREMENT; if ((oname = get_origin_props(dsprops, &originprops)) != NULL) { print_info(oname, originprops, pc); nvlist_free(originprops); } + + /* Back up a level; snapshots at the same level as dataset */ + pc->current_indent -= INDENT_INCREMENT; + if (pc->show_snaps) + print_snapshots(dsname, pc); pc->current_indent = 0; return; } else @@ -593,11 +623,8 @@ print_info(const char *name, nvlist_t *dsprops, struct } printf("\n"); - if (originprops != NULL) { - /*if (pc->show_all_datasets) { - }*/ + if (originprops != NULL) be_prop_list_free(originprops); - } #undef BUFSZ }