svn commit: r336747 - projects/bectl/sbin/bectl
Kyle Evans
kevans at FreeBSD.org
Thu Jul 26 18:56:51 UTC 2018
Author: kevans
Date: Thu Jul 26 18:56:50 2018
New Revision: 336747
URL: https://svnweb.freebsd.org/changeset/base/336747
Log:
bectl(8): Hack together a more proper `bectl list`
Note that the space is currently just the 'used' property of the dataset.
Modified:
projects/bectl/sbin/bectl/Makefile
projects/bectl/sbin/bectl/bectl.c
Modified: projects/bectl/sbin/bectl/Makefile
==============================================================================
--- projects/bectl/sbin/bectl/Makefile Thu Jul 26 18:34:38 2018 (r336746)
+++ projects/bectl/sbin/bectl/Makefile Thu Jul 26 18:56:50 2018 (r336747)
@@ -6,6 +6,7 @@ MAN= bectl.8
LIBADD+= be
LIBADD+= jail
LIBADD+= nvpair
+LIBADD+= util
CFLAGS+= -I${SRCTOP}/cddl/contrib/opensolaris/lib/libzfs/common
CFLAGS+= -I${SRCTOP}/sys/cddl/compat/opensolaris
Modified: projects/bectl/sbin/bectl/bectl.c
==============================================================================
--- projects/bectl/sbin/bectl/bectl.c Thu Jul 26 18:34:38 2018 (r336746)
+++ projects/bectl/sbin/bectl/bectl.c Thu Jul 26 18:56:50 2018 (r336747)
@@ -31,16 +31,24 @@
#include <sys/mount.h>
#include <errno.h>
#include <jail.h>
+#include <libutil.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
+#include <time.h>
#include <unistd.h>
#include <be.h>
+#define HEADER_BE "BE"
+#define HEADER_ACTIVE "Active"
+#define HEADER_MOUNT "Mountpoint"
+#define HEADER_SPACE "Space"
+#define HEADER_CREATED "Created"
+
static int bectl_cmd_activate(int argc, char *argv[]);
static int bectl_cmd_create(int argc, char *argv[]);
static int bectl_cmd_destroy(int argc, char *argv[]);
@@ -406,8 +414,14 @@ bectl_cmd_jail(int argc, char *argv[])
static int
bectl_cmd_list(int argc, char *argv[])
{
- nvlist_t *props;
- int opt;
+#define BUFSZ 64
+ nvpair_t *cur;
+ nvlist_t *props, *dsprops;
+ unsigned long long ctimenum, space;
+ size_t be_maxcol;
+ int active_colsz, active_colsz_def, be_colsz, mount_colsz, opt, space_colsz;
+ char buf[BUFSZ], *creation, *mnt, *spacestr;
+ boolean_t active_now, active_reboot;
bool show_all_datasets, show_space, hide_headers, show_snaps;
props = NULL;
@@ -450,10 +464,69 @@ bectl_cmd_list(int argc, char *argv[])
return (1);
}
- dump_nvlist(props, 0);
+ be_maxcol = strlen(HEADER_BE);
+ for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
+ cur = nvlist_next_nvpair(props, cur)) {
+ be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur)));
+ }
+
+ be_colsz = -be_maxcol;
+ /* To be made negative after calculating final col sz */
+ active_colsz_def = strlen(HEADER_ACTIVE);
+ mount_colsz = -(int)strlen(HEADER_MOUNT);
+ space_colsz = -(int)strlen(HEADER_SPACE);
+ printf("%*s %s %s %s %s\n", be_colsz, HEADER_BE, HEADER_ACTIVE,
+ HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
+ buf[5] = '\0';
+ cur = NULL;
+ for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
+ cur = nvlist_next_nvpair(props, cur)) {
+ printf("%*s ", be_colsz, nvpair_name(cur));
+ // NR
+ active_colsz = active_colsz_def;
+ nvpair_value_nvlist(cur, &dsprops);
+ if (nvlist_lookup_boolean_value(dsprops, "active",
+ &active_now) == 0 && active_now) {
+ printf("N");
+ active_colsz--;
+ }
+ if (nvlist_lookup_boolean_value(dsprops, "nextboot",
+ &active_reboot) == 0 && active_reboot) {
+ printf("R");
+ active_colsz--;
+ }
+ if (active_colsz == active_colsz_def) {
+ printf("-");
+ active_colsz--;
+ }
+ printf("%*s ", -active_colsz, " ");
+ if (nvlist_lookup_string(dsprops, "mountpoint", &mnt) == 0)
+ printf("%*s ", mount_colsz, mnt);
+ else
+ printf("%*s ", mount_colsz, "-");
+ // used
+ if (nvlist_lookup_string(dsprops, "used", &spacestr) == 0) {
+ space = strtoull(spacestr, NULL, 10);
+ humanize_number(buf, 6, space, "", HN_AUTOSCALE,
+ HN_DECIMAL | HN_NOSPACE | HN_B);
+ printf("%*s ", space_colsz, buf);
+ } else
+ printf("%*s ", space_colsz, "-");
+
+ if (nvlist_lookup_string(dsprops, "creation", &creation) == 0) {
+ ctimenum = strtoull(creation, NULL, 10);
+ strftime(buf, BUFSZ, "%Y-%m-%d %H:%M",
+ localtime((time_t *)&ctimenum));
+ printf("%s", buf);
+ }
+
+ // creation
+ printf("\n");
+ }
be_prop_list_free(props);
return (0);
+#undef BUFSZ
}
More information about the svn-src-projects
mailing list