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
29 #include <sys/param.h>
31 #include <sys/mount.h>
46 #define HEADER_BE "BE"
47 #define HEADER_BEPLUS "BE/Dataset/Snapshot"
48 #define HEADER_ACTIVE "Active"
49 #define HEADER_MOUNT "Mountpoint"
50 #define HEADER_SPACE "Space"
51 #define HEADER_CREATED "Created"
54 #define INDENT_INCREMENT 2
64 bool show_all_datasets;
69 static int bectl_cmd_activate(int argc, char *argv[]);
70 static int bectl_cmd_create(int argc, char *argv[]);
71 static int bectl_cmd_destroy(int argc, char *argv[]);
72 static int bectl_cmd_export(int argc, char *argv[]);
73 static int bectl_cmd_import(int argc, char *argv[]);
74 static int bectl_cmd_add(int argc, char *argv[]);
75 static int bectl_cmd_jail(int argc, char *argv[]);
76 static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops);
77 static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc);
78 static void print_headers(nvlist_t *props, struct printc *pc);
79 static int bectl_cmd_list(int argc, char *argv[]);
80 static int bectl_cmd_mount(int argc, char *argv[]);
81 static int bectl_cmd_rename(int argc, char *argv[]);
82 static int bectl_search_jail_paths(const char *mnt);
83 static int bectl_locate_jail(const char *ident);
84 static int bectl_cmd_unjail(int argc, char *argv[]);
85 static int bectl_cmd_unmount(int argc, char *argv[]);
87 static libbe_handle_t *be;
94 fp = explicit ? stdout : stderr;
96 "usage:\tbectl ( -h | -? | subcommand [args...] )\n"
97 "\tbectl activate [-t] beName\n"
98 "\tbectl create [-e nonActiveBe | -e beName@snapshot] beName\n"
99 "\tbectl create beName@snapshot\n"
100 "\tbectl destroy [-F] beName | beName@snapshot⟩\n"
101 "\tbectl export sourceBe\n"
102 "\tbectl import targetBe\n"
103 "\tbectl add (path)*\n"
104 "\tbectl jail bootenv\n"
105 "\tbectl list [-a] [-D] [-H] [-s]\n"
106 "\tbectl mount beName [mountpoint]\n"
107 "\tbectl rename origBeName newBeName\n"
108 "\tbectl { ujail | unjail } ⟨jailID | jailName | bootenv)\n"
109 "\tbectl { umount | unmount } [-f] beName\n");
111 return (explicit ? 0 : EX_USAGE);
116 * Represents a relationship between the command name and the parser action
119 struct command_map_entry {
121 int (*fn)(int argc, char *argv[]);
124 static struct command_map_entry command_map[] =
126 { "activate", bectl_cmd_activate },
127 { "create", bectl_cmd_create },
128 { "destroy", bectl_cmd_destroy },
129 { "export", bectl_cmd_export },
130 { "import", bectl_cmd_import },
131 { "add", bectl_cmd_add },
132 { "jail", bectl_cmd_jail },
133 { "list", bectl_cmd_list },
134 { "mount", bectl_cmd_mount },
135 { "rename", bectl_cmd_rename },
136 { "unjail", bectl_cmd_unjail },
137 { "unmount", bectl_cmd_unmount },
141 get_cmd_index(const char *cmd, int *index)
145 map_size = nitems(command_map);
146 for (int i = 0; i < map_size; ++i) {
147 if (strcmp(cmd, command_map[i].command) == 0) {
158 bectl_cmd_activate(int argc, char *argv[])
164 while ((opt = getopt(argc, argv, "t")) != -1) {
170 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
172 return (usage(false));
180 fprintf(stderr, "bectl activate: wrong number of arguments\n");
181 return (usage(false));
185 /* activate logic goes here */
186 if ((err = be_activate(be, argv[0], temp)) != 0)
187 /* XXX TODO: more specific error msg based on err */
188 printf("did not successfully activate boot environment %s\n",
191 printf("successfully activated boot environment %s\n", argv[0]);
194 printf("for next boot\n");
201 * TODO: when only one arg is given, and it contains an "@" the this should
202 * create that snapshot
205 bectl_cmd_create(int argc, char *argv[])
207 char *bootenv, *snapname, *source;
211 while ((opt = getopt(argc, argv, "e:")) != -1) {
217 fprintf(stderr, "bectl create: unknown option '-%c'\n",
219 return (usage(false));
227 fprintf(stderr, "bectl create: wrong number of arguments\n");
228 return (usage(false));
233 if (snapname != NULL) {
234 if (strchr(snapname, '@') != NULL)
235 err = be_create_from_existing_snap(be, bootenv,
238 err = be_create_from_existing(be, bootenv, snapname);
240 if ((snapname = strchr(bootenv, '@')) != NULL) {
241 *(snapname++) = '\0';
242 if ((err = be_snapshot(be, be_active_path(be),
243 snapname, true, NULL)) != BE_ERR_SUCCESS)
244 fprintf(stderr, "failed to create snapshot\n");
245 asprintf(&source, "%s@%s", be_active_path(be), snapname);
246 err = be_create_from_existing_snap(be, bootenv,
250 err = be_create(be, bootenv);
257 if (snapname == NULL)
259 "failed to create bootenv %s\n", bootenv);
262 "failed to create bootenv %s from snapshot %s\n",
271 bectl_cmd_export(int argc, char *argv[])
276 fprintf(stderr, "bectl export: missing boot environment name\n");
277 return (usage(false));
281 fprintf(stderr, "bectl export: extra arguments provided\n");
282 return (usage(false));
287 if (isatty(STDOUT_FILENO)) {
288 fprintf(stderr, "bectl export: must redirect output\n");
292 be_export(be, bootenv, STDOUT_FILENO);
299 bectl_cmd_import(int argc, char *argv[])
305 fprintf(stderr, "bectl import: missing boot environment name\n");
306 return (usage(false));
310 fprintf(stderr, "bectl import: extra arguments provided\n");
311 return (usage(false));
316 if (isatty(STDIN_FILENO)) {
317 fprintf(stderr, "bectl import: input can not be from terminal\n");
321 err = be_import(be, bootenv, STDIN_FILENO);
328 bectl_cmd_add(int argc, char *argv[])
332 fprintf(stderr, "bectl add: must provide at least one path\n");
333 return (usage(false));
336 for (int i = 1; i < argc; ++i) {
337 printf("arg %d: %s\n", i, argv[i]);
338 /* XXX TODO catch err */
339 be_add_child(be, argv[i], true);
347 bectl_cmd_destroy(int argc, char *argv[])
354 while ((opt = getopt(argc, argv, "F")) != -1) {
360 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
362 return (usage(false));
370 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
371 return (usage(false));
376 err = be_destroy(be, target, force);
383 bectl_cmd_jail(int argc, char *argv[])
386 char mnt_loc[BE_MAXPATHLEN];
389 /* struct jail be_jail = { 0 }; */
392 fprintf(stderr, "bectl jail: missing boot environment name\n");
393 return (usage(false));
396 fprintf(stderr, "bectl jail: too many arguments\n");
397 return (usage(false));
403 * XXX TODO: if its already mounted, perhaps there should be a flag to
404 * indicate its okay to proceed??
406 if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS) {
407 fprintf(stderr, "could not mount bootenv\n");
411 /* XXX TODO: Make the IP/hostname configurable? */
412 jid = jail_setv(JAIL_CREATE | JAIL_ATTACH,
415 "host.hostname", bootenv,
417 "ip4.addr", "10.20.30.40",
418 "allow.mount", "true",
419 "allow.mount.devfs", "true",
420 "enforce_statfs", "1",
423 fprintf(stderr, "unable to create jail. error: %d\n", errno);
427 /* We're attached within the jail... good bye! */
429 execl("/bin/sh", "/bin/sh", NULL);
434 * Given a set of dataset properties (for a BE dataset), populate originprops
435 * with the origin's properties.
438 get_origin_props(nvlist_t *dsprops, nvlist_t **originprops)
442 if (nvlist_lookup_string(dsprops, "origin", &propstr) == 0) {
443 if (be_prop_list_alloc(originprops) != 0) {
445 "bectl list: failed to allocate origin prop nvlist\n");
448 if (be_get_snapshot_props(be, propstr, *originprops) != 0) {
449 /* XXX TODO: Real errors */
451 "bectl list: failed to fetch origin properties\n");
462 print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
466 unsigned long long ctimenum, space;
467 nvlist_t *originprops;
471 boolean_t active_now, active_reboot;
474 printf("%*s%*s ", pc->current_indent, "",
475 pc->be_colsz + pc->current_indent, name);
477 /* Recurse at the base level if we're breaking info down */
478 if (pc->current_indent == 0 && (pc->show_all_datasets ||
481 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
482 /* XXX TODO: Error? */
484 pc->current_indent += INDENT_INCREMENT;
485 print_info(propstr, dsprops, pc);
486 pc->current_indent += INDENT_INCREMENT;
487 if ((oname = get_origin_props(dsprops, &originprops)) != NULL) {
488 print_info(oname, originprops, pc);
489 nvlist_free(originprops);
491 pc->current_indent = 0;
497 active_colsz = pc->active_colsz_def;
498 if (nvlist_lookup_boolean_value(dsprops, "active",
499 &active_now) == 0 && active_now) {
503 if (nvlist_lookup_boolean_value(dsprops, "nextboot",
504 &active_reboot) == 0 && active_reboot) {
508 if (active_colsz == pc->active_colsz_def) {
512 printf("%*s ", -active_colsz, " ");
513 if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0)
514 printf("%*s ", pc->mount_colsz, propstr);
516 printf("%*s ", pc->mount_colsz, "-");
518 get_origin_props(dsprops, &originprops);
520 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) {
521 space = strtoull(propstr, NULL, 10);
523 if (!pc->show_all_datasets && originprops != NULL &&
524 nvlist_lookup_string(originprops, "used", &propstr) == 0)
525 space += strtoull(propstr, NULL, 10);
527 /* Alas, there's more to it,. */
528 humanize_number(buf, 6, space, "", HN_AUTOSCALE,
529 HN_DECIMAL | HN_NOSPACE | HN_B);
530 printf("%*s ", pc->space_colsz, buf);
532 printf("%*s ", pc->space_colsz, "-");
534 if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) {
535 ctimenum = strtoull(propstr, NULL, 10);
536 strftime(buf, BUFSZ, "%Y-%m-%d %H:%M",
537 localtime((time_t *)&ctimenum));
542 if (originprops != NULL) {
543 /*if (pc->show_all_datasets) {
545 be_prop_list_free(originprops);
551 print_headers(nvlist_t *props, struct printc *pc)
553 const char *chosen_be_header;
559 if (pc->show_all_datasets || pc->show_snaps)
560 chosen_be_header = HEADER_BEPLUS;
562 chosen_be_header = HEADER_BE;
563 be_maxcol = strlen(chosen_be_header);
564 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
565 cur = nvlist_next_nvpair(props, cur)) {
566 be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur)));
567 if (!pc->show_all_datasets && !pc->show_snaps)
569 nvpair_value_nvlist(cur, &dsprops);
570 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
572 be_maxcol = MAX(be_maxcol, strlen(propstr) + INDENT_INCREMENT);
573 if (nvlist_lookup_string(dsprops, "origin", &propstr) != 0)
575 be_maxcol = MAX(be_maxcol,
576 strlen(propstr) + INDENT_INCREMENT * 2);
579 pc->be_colsz = -be_maxcol;
580 /* To be made negative after calculating final col sz */
581 pc->active_colsz_def = strlen(HEADER_ACTIVE);
582 pc->mount_colsz = -(int)strlen(HEADER_MOUNT);
583 pc->space_colsz = -(int)strlen(HEADER_SPACE);
584 /* XXX TODO: Take -H into account */
585 printf("%*s %s %s %s %s\n", pc->be_colsz, chosen_be_header,
586 HEADER_ACTIVE, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
589 * All other invocations in which we aren't using the default header
590 * will produce quite a bit of input. Throw an extra blank line after
591 * the header to make it look nicer.
593 if (chosen_be_header != HEADER_BE)
598 bectl_cmd_list(int argc, char *argv[])
602 nvlist_t *dsprops, *props;
606 bzero(&pc, sizeof(pc));
607 while ((opt = getopt(argc, argv, "aDHs")) != -1) {
610 pc.show_all_datasets = true;
613 pc.show_space = true;
616 pc.hide_headers = true;
619 pc.show_snaps = true;
622 fprintf(stderr, "bectl list: unknown option '-%c'\n",
624 return (usage(false));
631 fprintf(stderr, "bectl list: extra argument provided\n");
632 return (usage(false));
635 if (be_prop_list_alloc(&props) != 0) {
636 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n");
639 if (be_get_bootenv_props(be, props) != 0) {
640 /* XXX TODO: Real errors */
641 fprintf(stderr, "bectl list: failed to fetch boot environments\n");
645 print_headers(props, &pc);
646 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
647 cur = nvlist_next_nvpair(props, cur)) {
648 nvpair_value_nvlist(cur, &dsprops);
649 pc.final_be = nvlist_next_nvpair(props, cur) == NULL;
650 print_info(nvpair_name(cur), dsprops, &pc);
652 be_prop_list_free(props);
659 bectl_cmd_mount(int argc, char *argv[])
661 char result_loc[BE_MAXPATHLEN];
662 char *bootenv, *mountpoint;
666 fprintf(stderr, "bectl mount: missing argument(s)\n");
667 return (usage(false));
671 fprintf(stderr, "bectl mount: too many arguments\n");
672 return (usage(false));
676 mountpoint = ((argc == 3) ? argv[2] : NULL);
678 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
682 printf("successfully mounted %s at %s\n", bootenv, result_loc);
686 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
687 "failed to mount bootenv %s at temporary path %s\n",
688 bootenv, mountpoint);
696 bectl_cmd_rename(int argc, char *argv[])
702 fprintf(stderr, "bectl rename: missing argument\n");
703 return (usage(false));
707 fprintf(stderr, "bectl rename: too many arguments\n");
708 return (usage(false));
714 err = be_rename(be, src, dest);
720 fprintf(stderr, "failed to rename bootenv %s to %s\n",
728 bectl_search_jail_paths(const char *mnt)
730 char jailpath[MAXPATHLEN + 1];
735 while ((jid = jail_getv(0, "lastjid", &jid, "path", &jailpath,
737 if (strcmp(jailpath, mnt) == 0)
745 * Locate a jail based on an arbitrary identifier. This may be either a name,
746 * a jid, or a BE name. Returns the jid or -1 on failure.
749 bectl_locate_jail(const char *ident)
751 nvlist_t *belist, *props;
755 /* Try the easy-match first */
756 jid = jail_getid(ident);
760 /* Attempt to try it as a BE name, first */
761 if (be_prop_list_alloc(&belist) != 0)
764 if (be_get_bootenv_props(be, belist) != 0)
767 if (nvlist_lookup_nvlist(belist, ident, &props) == 0) {
768 /* We'll attempt to resolve the jid by way of mountpoint */
769 if (nvlist_lookup_string(props, "mountpoint", &mnt) == 0) {
770 jid = bectl_search_jail_paths(mnt);
771 be_prop_list_free(belist);
775 be_prop_list_free(belist);
782 bectl_cmd_unjail(int argc, char *argv[])
784 char path[MAXPATHLEN + 1];
785 char *cmd, *name, *target;
788 /* Store alias used */
792 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
793 return (usage(false));
798 /* Locate the jail */
799 if ((jid = bectl_locate_jail(target)) == -1) {
800 fprintf(stderr, "bectl %s: failed to locate BE by '%s'\n", cmd, target);
804 bzero(&path, MAXPATHLEN + 1);
805 name = jail_getname(jid);
806 if (jail_getv(0, "name", name, "path", path, NULL) != jid) {
808 fprintf(stderr, "bectl %s: failed to get path for jail requested by '%s'\n", cmd, target);
814 if (be_mounted_at(be, path, NULL) != 0) {
815 fprintf(stderr, "bectl %s: jail requested by '%s' not a BE\n", cmd, target);
827 bectl_cmd_unmount(int argc, char *argv[])
832 /* Store alias used */
836 while ((opt = getopt(argc, argv, "f")) != -1) {
839 flags |= BE_MNT_FORCE;
842 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
844 return (usage(false));
852 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
853 return (usage(false));
858 err = be_unmount(be, bootenv, flags);
864 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
872 main(int argc, char *argv[])
875 int command_index, rc;
878 fprintf(stderr, "missing command\n");
879 return (usage(false));
884 /* Handle command aliases */
885 if (strcmp(command, "umount") == 0)
888 if (strcmp(command, "ujail") == 0)
891 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
892 return (usage(true));
894 if (get_cmd_index(command, &command_index)) {
895 fprintf(stderr, "unknown command: %s\n", command);
896 return (usage(false));
900 if ((be = libbe_init()) == NULL)
903 libbe_print_on_error(be, true);
905 /* XXX TODO: can be simplified if offset by 2 instead of one */
906 rc = command_map[command_index].fn(argc-1, argv+1);