2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
54 bool show_all_datasets;
59 static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops);
60 static void print_padding(const char *fval, int colsz, struct printc *pc);
61 static int print_snapshots(const char *dsname, struct printc *pc);
62 static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc);
63 static void print_headers(nvlist_t *props, struct printc *pc);
64 static unsigned long long dataset_space(const char *oname);
66 #define HEADER_BE "BE"
67 #define HEADER_BEPLUS "BE/Dataset/Snapshot"
68 #define HEADER_ACTIVE "Active"
69 #define HEADER_MOUNT "Mountpoint"
70 #define HEADER_SPACE "Space"
71 #define HEADER_CREATED "Created"
74 #define INDENT_INCREMENT 2
77 * Given a set of dataset properties (for a BE dataset), populate originprops
78 * with the origin's properties.
81 get_origin_props(nvlist_t *dsprops, nvlist_t **originprops)
85 if (nvlist_lookup_string(dsprops, "origin", &propstr) == 0) {
86 if (be_prop_list_alloc(originprops) != 0) {
88 "bectl list: failed to allocate origin prop nvlist\n");
91 if (be_get_dataset_props(be, propstr, *originprops) != 0) {
92 /* XXX TODO: Real errors */
94 "bectl list: failed to fetch origin properties\n");
104 print_padding(const char *fval, int colsz, struct printc *pc)
107 /* -H flag handling; all delimiters/padding are a single tab */
108 if (pc->script_fmt) {
114 colsz -= strlen(fval);
115 printf("%*s ", colsz, "");
118 static unsigned long long
119 dataset_space(const char *oname)
121 unsigned long long space;
122 char *dsname, *propstr, *sep;
126 dsname = strdup(oname);
130 /* Truncate snapshot to dataset name, as needed */
131 if ((sep = strchr(dsname, '@')) != NULL)
134 if (be_prop_list_alloc(&dsprops) != 0) {
139 if (be_get_dataset_props(be, dsname, dsprops) != 0) {
140 nvlist_free(dsprops);
145 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0)
146 space = strtoull(propstr, NULL, 10);
148 nvlist_free(dsprops);
154 print_snapshots(const char *dsname, struct printc *pc)
157 nvlist_t *props, *sprops;
159 if (be_prop_list_alloc(&props) != 0) {
160 fprintf(stderr, "bectl list: failed to allocate snapshot nvlist\n");
163 if (be_get_dataset_snapshots(be, dsname, props) != 0) {
164 fprintf(stderr, "bectl list: failed to fetch boot ds snapshots\n");
167 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
168 cur = nvlist_next_nvpair(props, cur)) {
169 nvpair_value_nvlist(cur, &sprops);
170 print_info(nvpair_name(cur), sprops, pc);
176 print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
180 unsigned long long ctimenum, space;
181 nvlist_t *originprops;
183 char *dsname, *propstr;
185 boolean_t active_now, active_reboot, bootonce;
189 printf("%*s%s", pc->current_indent, "", name);
190 nvlist_lookup_string(dsprops, "dataset", &dsname);
192 /* Recurse at the base level if we're breaking info down */
193 if (pc->current_indent == 0 && (pc->show_all_datasets ||
197 /* XXX TODO: Error? */
200 * Whether we're dealing with -a or -s, we'll always print the
201 * dataset name/information followed by its origin. For -s, we
202 * additionally iterate through all snapshots of this boot
203 * environment and also print their information.
205 pc->current_indent += INDENT_INCREMENT;
206 print_info(dsname, dsprops, pc);
207 pc->current_indent += INDENT_INCREMENT;
208 if ((oname = get_origin_props(dsprops, &originprops)) != NULL) {
209 print_info(oname, originprops, pc);
210 nvlist_free(originprops);
213 /* Back up a level; snapshots at the same level as dataset */
214 pc->current_indent -= INDENT_INCREMENT;
216 print_snapshots(dsname, pc);
217 pc->current_indent = 0;
220 print_padding(name, pc->be_colsz - pc->current_indent, pc);
222 active_colsz = pc->active_colsz_def;
223 if (nvlist_lookup_boolean_value(dsprops, "active",
224 &active_now) == 0 && active_now) {
228 if (nvlist_lookup_boolean_value(dsprops, "nextboot",
229 &active_reboot) == 0 && active_reboot) {
233 if (nvlist_lookup_boolean_value(dsprops, "bootonce",
234 &bootonce) == 0 && bootonce) {
238 if (active_colsz == pc->active_colsz_def) {
242 print_padding(NULL, active_colsz, pc);
243 if (nvlist_lookup_string(dsprops, "mounted", &propstr) == 0) {
244 printf("%s", propstr);
245 print_padding(propstr, pc->mount_colsz, pc);
248 print_padding("-", pc->mount_colsz, pc);
251 oname = get_origin_props(dsprops, &originprops);
252 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) {
254 * The space used column is some composition of:
255 * - The "used" property of the dataset
256 * - The "used" property of the origin snapshot (not -a or -s)
257 * - The "used" property of the origin dataset (-D flag only)
259 * The -D flag is ignored if -a or -s are specified.
261 space = strtoull(propstr, NULL, 10);
263 if (!pc->show_all_datasets && !pc->show_snaps &&
264 originprops != NULL &&
265 nvlist_lookup_string(originprops, "used", &propstr) == 0)
266 space += strtoull(propstr, NULL, 10);
268 if (pc->show_space && oname != NULL)
269 space += dataset_space(oname);
271 /* Alas, there's more to it,. */
272 be_nicenum(space, buf, 6);
274 print_padding(buf, pc->space_colsz, pc);
277 print_padding("-", pc->space_colsz, pc);
280 if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) {
281 ctimenum = strtoull(propstr, NULL, 10);
282 strftime(buf, BUFSZ, "%Y-%m-%d %H:%M",
283 localtime((time_t *)&ctimenum));
288 if (originprops != NULL)
289 be_prop_list_free(originprops);
294 print_headers(nvlist_t *props, struct printc *pc)
296 const char *chosen_be_header;
300 size_t be_maxcol, mount_colsz;
302 if (pc->show_all_datasets || pc->show_snaps)
303 chosen_be_header = HEADER_BEPLUS;
305 chosen_be_header = HEADER_BE;
306 be_maxcol = strlen(chosen_be_header);
307 mount_colsz = strlen(HEADER_MOUNT);
308 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
309 cur = nvlist_next_nvpair(props, cur)) {
310 be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur)));
311 nvpair_value_nvlist(cur, &dsprops);
313 if (nvlist_lookup_string(dsprops, "mounted", &propstr) == 0)
314 mount_colsz = MAX(mount_colsz, strlen(propstr));
315 if (!pc->show_all_datasets && !pc->show_snaps)
317 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
319 be_maxcol = MAX(be_maxcol, strlen(propstr) + INDENT_INCREMENT);
320 if (nvlist_lookup_string(dsprops, "origin", &propstr) != 0)
322 be_maxcol = MAX(be_maxcol,
323 strlen(propstr) + INDENT_INCREMENT * 2);
326 pc->be_colsz = be_maxcol;
327 pc->active_colsz_def = strlen(HEADER_ACTIVE);
328 pc->mount_colsz = mount_colsz;
329 pc->space_colsz = strlen(HEADER_SPACE);
330 printf("%*s %s %*s %s %s\n", -pc->be_colsz, chosen_be_header,
331 HEADER_ACTIVE, -pc->mount_colsz, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
334 * All other invocations in which we aren't using the default header
335 * will produce quite a bit of input. Throw an extra blank line after
336 * the header to make it look nicer.
338 if (strcmp(chosen_be_header, HEADER_BE) != 0)
343 * Sort the given nvlist of boot environments by property.
346 prop_list_sort(nvlist_t *props, char *property, bool reverse)
352 struct sort_column sc_prev, sc_next;
354 /* a temporary list to work with */
355 nvlist_dup(props, &nvl, 0);
357 nvp_count = fnvlist_num_pairs(nvl);
358 for (i = 0; i < nvp_count; i++) {
360 nvp = nvlist_next_nvpair(nvl, NULL);
361 nvpair_value_nvlist(nvp, &sc_prev.nvl);
362 nvlist_lookup_string(sc_prev.nvl, "name", &sc_prev.name);
363 nvlist_lookup_string(sc_prev.nvl, property, &sc_prev.val);
365 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
367 nvpair_value_nvlist(nvp, &sc_next.nvl);
368 nvlist_lookup_string(sc_next.nvl, "name", &sc_next.name);
369 nvlist_lookup_string(sc_next.nvl, property, &sc_next.val);
371 /* properties that use numerical comparison */
372 if (strcmp(property, "creation") == 0 ||
373 strcmp(property, "used") == 0 ||
374 strcmp(property, "usedds") == 0 ||
375 strcmp(property, "usedsnap") == 0 ||
376 strcmp(property, "usedrefreserv") == 0) {
378 lval = strtoull(sc_prev.val, NULL, 10);
379 rval = strtoull(sc_next.val, NULL, 10);
381 if ((lval < rval && reverse) ||
382 (lval > rval && !reverse))
386 /* properties that use string comparison */
387 else if (strcmp(property, "name") == 0 ||
388 strcmp(property, "origin") == 0) {
389 if ((strcmp(sc_prev.val, sc_next.val) < 0 && reverse) ||
390 (strcmp(sc_prev.val, sc_next.val) > 0 && !reverse))
396 * The 'props' nvlist has been created to only have unique names.
397 * When a name is added, any existing nvlist's with the same name
398 * will be removed. Eventually, all existing nvlist's are replaced
401 nvlist_add_nvlist(props, sc_prev.name, sc_prev.nvl);
402 nvlist_remove_all(nvl, sc_prev.name);
405 be_prop_list_free(nvl);
411 bectl_cmd_list(int argc, char *argv[])
415 nvlist_t *dsprops, *props;
423 bzero(&pc, sizeof(pc));
425 while ((opt = getopt(argc, argv, "aDHsc:C:")) != -1) {
428 pc.show_all_datasets = true;
431 pc.show_space = true;
434 pc.script_fmt = true;
437 pc.show_snaps = true;
442 column = strdup(optarg);
448 column = strdup(optarg);
452 fprintf(stderr, "bectl list: unknown option '-%c'\n",
454 return (usage(false));
461 fprintf(stderr, "bectl list: extra argument provided\n");
462 return (usage(false));
465 if (be_prop_list_alloc(&props) != 0) {
466 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n");
469 if (be_get_bootenv_props(be, props) != 0) {
470 /* XXX TODO: Real errors */
471 fprintf(stderr, "bectl list: failed to fetch boot environments\n");
475 /* List boot environments in alphabetical order by default */
477 column = strdup("name");
479 prop_list_sort(props, column, reverse);
481 /* Force -D off if either -a or -s are specified */
482 if (pc.show_all_datasets || pc.show_snaps)
483 pc.show_space = false;
485 print_headers(props, &pc);
487 /* Print boot environments */
488 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
489 cur = nvlist_next_nvpair(props, cur)) {
490 nvpair_value_nvlist(cur, &dsprops);
492 if (printed > 0 && (pc.show_all_datasets || pc.show_snaps))
495 print_info(nvpair_name(cur), dsprops, &pc);
500 be_prop_list_free(props);