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
63 bool show_all_datasets;
68 static int bectl_cmd_activate(int argc, char *argv[]);
69 static int bectl_cmd_create(int argc, char *argv[]);
70 static int bectl_cmd_destroy(int argc, char *argv[]);
71 static int bectl_cmd_export(int argc, char *argv[]);
72 static int bectl_cmd_import(int argc, char *argv[]);
73 static int bectl_cmd_add(int argc, char *argv[]);
74 static int bectl_cmd_jail(int argc, char *argv[]);
75 static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops);
76 static void print_padding(const char *fval, int colsz, struct printc *pc);
77 static int print_snapshots(const char *dsname, struct printc *pc);
78 static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc);
79 static void print_headers(nvlist_t *props, struct printc *pc);
80 static int bectl_cmd_list(int argc, char *argv[]);
81 static int bectl_cmd_mount(int argc, char *argv[]);
82 static int bectl_cmd_rename(int argc, char *argv[]);
83 static int bectl_search_jail_paths(const char *mnt);
84 static int bectl_locate_jail(const char *ident);
85 static int bectl_cmd_unjail(int argc, char *argv[]);
86 static int bectl_cmd_unmount(int argc, char *argv[]);
88 static libbe_handle_t *be;
95 fp = explicit ? stdout : stderr;
97 "usage:\tbectl ( -h | -? | subcommand [args...] )\n"
98 "\tbectl activate [-t] beName\n"
99 "\tbectl create [-e nonActiveBe | -e beName@snapshot] beName\n"
100 "\tbectl create beName@snapshot\n"
101 "\tbectl destroy [-F] beName | beName@snapshot⟩\n"
102 "\tbectl export sourceBe\n"
103 "\tbectl import targetBe\n"
104 "\tbectl add (path)*\n"
105 "\tbectl jail bootenv\n"
106 "\tbectl list [-a] [-D] [-H] [-s]\n"
107 "\tbectl mount beName [mountpoint]\n"
108 "\tbectl rename origBeName newBeName\n"
109 "\tbectl { ujail | unjail } ⟨jailID | jailName | bootenv)\n"
110 "\tbectl { umount | unmount } [-f] beName\n");
112 return (explicit ? 0 : EX_USAGE);
117 * Represents a relationship between the command name and the parser action
120 struct command_map_entry {
122 int (*fn)(int argc, char *argv[]);
125 static struct command_map_entry command_map[] =
127 { "activate", bectl_cmd_activate },
128 { "create", bectl_cmd_create },
129 { "destroy", bectl_cmd_destroy },
130 { "export", bectl_cmd_export },
131 { "import", bectl_cmd_import },
132 { "add", bectl_cmd_add },
133 { "jail", bectl_cmd_jail },
134 { "list", bectl_cmd_list },
135 { "mount", bectl_cmd_mount },
136 { "rename", bectl_cmd_rename },
137 { "unjail", bectl_cmd_unjail },
138 { "unmount", bectl_cmd_unmount },
142 get_cmd_index(const char *cmd, int *index)
146 map_size = nitems(command_map);
147 for (int i = 0; i < map_size; ++i) {
148 if (strcmp(cmd, command_map[i].command) == 0) {
159 bectl_cmd_activate(int argc, char *argv[])
165 while ((opt = getopt(argc, argv, "t")) != -1) {
171 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
173 return (usage(false));
181 fprintf(stderr, "bectl activate: wrong number of arguments\n");
182 return (usage(false));
186 /* activate logic goes here */
187 if ((err = be_activate(be, argv[0], temp)) != 0)
188 /* XXX TODO: more specific error msg based on err */
189 printf("did not successfully activate boot environment %s\n",
192 printf("successfully activated boot environment %s\n", argv[0]);
195 printf("for next boot\n");
202 * TODO: when only one arg is given, and it contains an "@" the this should
203 * create that snapshot
206 bectl_cmd_create(int argc, char *argv[])
208 char *bootenv, *snapname, *source;
212 while ((opt = getopt(argc, argv, "e:")) != -1) {
218 fprintf(stderr, "bectl create: unknown option '-%c'\n",
220 return (usage(false));
228 fprintf(stderr, "bectl create: wrong number of arguments\n");
229 return (usage(false));
234 if (snapname != NULL) {
235 if (strchr(snapname, '@') != NULL)
236 err = be_create_from_existing_snap(be, bootenv,
239 err = be_create_from_existing(be, bootenv, snapname);
241 if ((snapname = strchr(bootenv, '@')) != NULL) {
242 *(snapname++) = '\0';
243 if ((err = be_snapshot(be, be_active_path(be),
244 snapname, true, NULL)) != BE_ERR_SUCCESS)
245 fprintf(stderr, "failed to create snapshot\n");
246 asprintf(&source, "%s@%s", be_active_path(be), snapname);
247 err = be_create_from_existing_snap(be, bootenv,
251 err = be_create(be, bootenv);
258 if (snapname == NULL)
260 "failed to create bootenv %s\n", bootenv);
263 "failed to create bootenv %s from snapshot %s\n",
272 bectl_cmd_export(int argc, char *argv[])
277 fprintf(stderr, "bectl export: missing boot environment name\n");
278 return (usage(false));
282 fprintf(stderr, "bectl export: extra arguments provided\n");
283 return (usage(false));
288 if (isatty(STDOUT_FILENO)) {
289 fprintf(stderr, "bectl export: must redirect output\n");
293 be_export(be, bootenv, STDOUT_FILENO);
300 bectl_cmd_import(int argc, char *argv[])
306 fprintf(stderr, "bectl import: missing boot environment name\n");
307 return (usage(false));
311 fprintf(stderr, "bectl import: extra arguments provided\n");
312 return (usage(false));
317 if (isatty(STDIN_FILENO)) {
318 fprintf(stderr, "bectl import: input can not be from terminal\n");
322 err = be_import(be, bootenv, STDIN_FILENO);
329 bectl_cmd_add(int argc, char *argv[])
333 fprintf(stderr, "bectl add: must provide at least one path\n");
334 return (usage(false));
337 for (int i = 1; i < argc; ++i) {
338 printf("arg %d: %s\n", i, argv[i]);
339 /* XXX TODO catch err */
340 be_add_child(be, argv[i], true);
348 bectl_cmd_destroy(int argc, char *argv[])
355 while ((opt = getopt(argc, argv, "F")) != -1) {
361 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
363 return (usage(false));
371 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
372 return (usage(false));
377 err = be_destroy(be, target, force);
384 bectl_cmd_jail(int argc, char *argv[])
387 char mnt_loc[BE_MAXPATHLEN];
390 /* struct jail be_jail = { 0 }; */
393 fprintf(stderr, "bectl jail: missing boot environment name\n");
394 return (usage(false));
397 fprintf(stderr, "bectl jail: too many arguments\n");
398 return (usage(false));
404 * XXX TODO: if its already mounted, perhaps there should be a flag to
405 * indicate its okay to proceed??
407 if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS) {
408 fprintf(stderr, "could not mount bootenv\n");
412 /* XXX TODO: Make the IP/hostname configurable? */
413 jid = jail_setv(JAIL_CREATE | JAIL_ATTACH,
416 "host.hostname", bootenv,
418 "ip4.addr", "10.20.30.40",
419 "allow.mount", "true",
420 "allow.mount.devfs", "true",
421 "enforce_statfs", "1",
424 fprintf(stderr, "unable to create jail. error: %d\n", errno);
428 /* We're attached within the jail... good bye! */
430 execl("/bin/sh", "/bin/sh", NULL);
435 * Given a set of dataset properties (for a BE dataset), populate originprops
436 * with the origin's properties.
439 get_origin_props(nvlist_t *dsprops, nvlist_t **originprops)
443 if (nvlist_lookup_string(dsprops, "origin", &propstr) == 0) {
444 if (be_prop_list_alloc(originprops) != 0) {
446 "bectl list: failed to allocate origin prop nvlist\n");
449 if (be_get_dataset_props(be, propstr, *originprops) != 0) {
450 /* XXX TODO: Real errors */
452 "bectl list: failed to fetch origin properties\n");
462 print_padding(const char *fval, int colsz, struct printc *pc)
465 /* -H flag handling; all delimiters/padding are a single tab */
466 if (pc->script_fmt) {
472 colsz -= strlen(fval);
473 printf("%*s ", colsz, "");
476 static unsigned long long
477 dataset_space(const char *oname)
479 unsigned long long space;
480 char *dsname, *propstr, *sep;
484 dsname = strdup(oname);
488 /* Truncate snapshot to dataset name, as needed */
489 if ((sep = strchr(dsname, '@')) != NULL)
492 if (be_prop_list_alloc(&dsprops) != 0) {
497 if (be_get_dataset_props(be, dsname, dsprops) != 0) {
498 nvlist_free(dsprops);
503 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0)
504 space = strtoull(propstr, NULL, 10);
506 nvlist_free(dsprops);
512 print_snapshots(const char *dsname, struct printc *pc)
515 nvlist_t *props, *sprops;
517 if (be_prop_list_alloc(&props) != 0) {
518 fprintf(stderr, "bectl list: failed to allocate snapshot nvlist\n");
521 if (be_get_dataset_snapshots(be, dsname, props) != 0) {
522 fprintf(stderr, "bectl list: failed to fetch boot ds snapshots\n");
525 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
526 cur = nvlist_next_nvpair(props, cur)) {
527 nvpair_value_nvlist(cur, &sprops);
528 print_info(nvpair_name(cur), sprops, pc);
534 print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
538 unsigned long long ctimenum, space;
539 nvlist_t *originprops;
541 char *dsname, *propstr;
543 boolean_t active_now, active_reboot;
547 printf("%*s%s", pc->current_indent, "", name);
548 nvlist_lookup_string(dsprops, "dataset", &dsname);
550 /* Recurse at the base level if we're breaking info down */
551 if (pc->current_indent == 0 && (pc->show_all_datasets ||
555 /* XXX TODO: Error? */
558 * Whether we're dealing with -a or -s, we'll always print the
559 * dataset name/information followed by its origin. For -s, we
560 * additionally iterate through all snapshots of this boot
561 * environment and also print their information.
563 pc->current_indent += INDENT_INCREMENT;
564 print_info(dsname, dsprops, pc);
565 pc->current_indent += INDENT_INCREMENT;
566 if ((oname = get_origin_props(dsprops, &originprops)) != NULL) {
567 print_info(oname, originprops, pc);
568 nvlist_free(originprops);
571 /* Back up a level; snapshots at the same level as dataset */
572 pc->current_indent -= INDENT_INCREMENT;
574 print_snapshots(dsname, pc);
575 pc->current_indent = 0;
578 print_padding(name, pc->be_colsz - pc->current_indent, pc);
580 active_colsz = pc->active_colsz_def;
581 if (nvlist_lookup_boolean_value(dsprops, "active",
582 &active_now) == 0 && active_now) {
586 if (nvlist_lookup_boolean_value(dsprops, "nextboot",
587 &active_reboot) == 0 && active_reboot) {
591 if (active_colsz == pc->active_colsz_def) {
595 print_padding(NULL, active_colsz, pc);
596 if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0){
597 printf("%s", propstr);
598 print_padding(propstr, pc->mount_colsz, pc);
601 print_padding("-", pc->mount_colsz, pc);
604 oname = get_origin_props(dsprops, &originprops);
605 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) {
607 * The space used column is some composition of:
608 * - The "used" property of the dataset
609 * - The "used" property of the origin snapshot (not -a or -s)
610 * - The "used" property of the origin dataset (-D flag only)
612 * The -D flag is ignored if -a or -s are specified.
614 space = strtoull(propstr, NULL, 10);
616 if (!pc->show_all_datasets && !pc->show_snaps &&
617 originprops != NULL &&
618 nvlist_lookup_string(originprops, "used", &propstr) == 0)
619 space += strtoull(propstr, NULL, 10);
621 if (pc->show_space && oname != NULL)
622 space += dataset_space(oname);
624 /* Alas, there's more to it,. */
625 be_nicenum(space, buf, 6);
627 print_padding(buf, pc->space_colsz, pc);
630 print_padding("-", pc->space_colsz, pc);
633 if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) {
634 ctimenum = strtoull(propstr, NULL, 10);
635 strftime(buf, BUFSZ, "%Y-%m-%d %H:%M",
636 localtime((time_t *)&ctimenum));
641 if (originprops != NULL)
642 be_prop_list_free(originprops);
647 print_headers(nvlist_t *props, struct printc *pc)
649 const char *chosen_be_header;
655 if (pc->show_all_datasets || pc->show_snaps)
656 chosen_be_header = HEADER_BEPLUS;
658 chosen_be_header = HEADER_BE;
659 be_maxcol = strlen(chosen_be_header);
660 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
661 cur = nvlist_next_nvpair(props, cur)) {
662 be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur)));
663 if (!pc->show_all_datasets && !pc->show_snaps)
665 nvpair_value_nvlist(cur, &dsprops);
666 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
668 be_maxcol = MAX(be_maxcol, strlen(propstr) + INDENT_INCREMENT);
669 if (nvlist_lookup_string(dsprops, "origin", &propstr) != 0)
671 be_maxcol = MAX(be_maxcol,
672 strlen(propstr) + INDENT_INCREMENT * 2);
675 pc->be_colsz = be_maxcol;
676 pc->active_colsz_def = strlen(HEADER_ACTIVE);
677 pc->mount_colsz = strlen(HEADER_MOUNT);
678 pc->space_colsz = strlen(HEADER_SPACE);
679 printf("%*s %s %s %s %s\n", -pc->be_colsz, chosen_be_header,
680 HEADER_ACTIVE, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
683 * All other invocations in which we aren't using the default header
684 * will produce quite a bit of input. Throw an extra blank line after
685 * the header to make it look nicer.
687 if (chosen_be_header != HEADER_BE)
692 bectl_cmd_list(int argc, char *argv[])
696 nvlist_t *dsprops, *props;
698 boolean_t active_now, active_reboot;
702 bzero(&pc, sizeof(pc));
703 while ((opt = getopt(argc, argv, "aDHs")) != -1) {
706 pc.show_all_datasets = true;
709 pc.show_space = true;
712 pc.script_fmt = true;
715 pc.show_snaps = true;
718 fprintf(stderr, "bectl list: unknown option '-%c'\n",
720 return (usage(false));
727 fprintf(stderr, "bectl list: extra argument provided\n");
728 return (usage(false));
731 if (be_prop_list_alloc(&props) != 0) {
732 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n");
735 if (be_get_bootenv_props(be, props) != 0) {
736 /* XXX TODO: Real errors */
737 fprintf(stderr, "bectl list: failed to fetch boot environments\n");
741 /* Force -D off if either -a or -s are specified */
742 if (pc.show_all_datasets || pc.show_snaps)
743 pc.show_space = false;
745 print_headers(props, &pc);
746 /* Do a first pass to print active and next active first */
747 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
748 cur = nvlist_next_nvpair(props, cur)) {
749 nvpair_value_nvlist(cur, &dsprops);
750 active_now = active_reboot = false;
752 nvlist_lookup_boolean_value(dsprops, "active", &active_now);
753 nvlist_lookup_boolean_value(dsprops, "nextboot",
755 if (!active_now && !active_reboot)
757 if (printed > 0 && (pc.show_all_datasets || pc.show_snaps))
759 print_info(nvpair_name(cur), dsprops, &pc);
763 /* Now pull everything else */
764 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
765 cur = nvlist_next_nvpair(props, cur)) {
766 nvpair_value_nvlist(cur, &dsprops);
767 active_now = active_reboot = false;
769 nvlist_lookup_boolean_value(dsprops, "active", &active_now);
770 nvlist_lookup_boolean_value(dsprops, "nextboot",
772 if (active_now || active_reboot)
774 if (printed > 0 && (pc.show_all_datasets || pc.show_snaps))
776 print_info(nvpair_name(cur), dsprops, &pc);
779 be_prop_list_free(props);
786 bectl_cmd_mount(int argc, char *argv[])
788 char result_loc[BE_MAXPATHLEN];
789 char *bootenv, *mountpoint;
793 fprintf(stderr, "bectl mount: missing argument(s)\n");
794 return (usage(false));
798 fprintf(stderr, "bectl mount: too many arguments\n");
799 return (usage(false));
803 mountpoint = ((argc == 3) ? argv[2] : NULL);
805 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
809 printf("successfully mounted %s at %s\n", bootenv, result_loc);
813 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
814 "failed to mount bootenv %s at temporary path %s\n",
815 bootenv, mountpoint);
823 bectl_cmd_rename(int argc, char *argv[])
829 fprintf(stderr, "bectl rename: missing argument\n");
830 return (usage(false));
834 fprintf(stderr, "bectl rename: too many arguments\n");
835 return (usage(false));
841 err = be_rename(be, src, dest);
847 fprintf(stderr, "failed to rename bootenv %s to %s\n",
855 bectl_search_jail_paths(const char *mnt)
857 char jailpath[MAXPATHLEN + 1];
862 while ((jid = jail_getv(0, "lastjid", &jid, "path", &jailpath,
864 if (strcmp(jailpath, mnt) == 0)
872 * Locate a jail based on an arbitrary identifier. This may be either a name,
873 * a jid, or a BE name. Returns the jid or -1 on failure.
876 bectl_locate_jail(const char *ident)
878 nvlist_t *belist, *props;
882 /* Try the easy-match first */
883 jid = jail_getid(ident);
887 /* Attempt to try it as a BE name, first */
888 if (be_prop_list_alloc(&belist) != 0)
891 if (be_get_bootenv_props(be, belist) != 0)
894 if (nvlist_lookup_nvlist(belist, ident, &props) == 0) {
895 /* We'll attempt to resolve the jid by way of mountpoint */
896 if (nvlist_lookup_string(props, "mountpoint", &mnt) == 0) {
897 jid = bectl_search_jail_paths(mnt);
898 be_prop_list_free(belist);
902 be_prop_list_free(belist);
909 bectl_cmd_unjail(int argc, char *argv[])
911 char path[MAXPATHLEN + 1];
912 char *cmd, *name, *target;
915 /* Store alias used */
919 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
920 return (usage(false));
925 /* Locate the jail */
926 if ((jid = bectl_locate_jail(target)) == -1) {
927 fprintf(stderr, "bectl %s: failed to locate BE by '%s'\n", cmd, target);
931 bzero(&path, MAXPATHLEN + 1);
932 name = jail_getname(jid);
933 if (jail_getv(0, "name", name, "path", path, NULL) != jid) {
935 fprintf(stderr, "bectl %s: failed to get path for jail requested by '%s'\n", cmd, target);
941 if (be_mounted_at(be, path, NULL) != 0) {
942 fprintf(stderr, "bectl %s: jail requested by '%s' not a BE\n", cmd, target);
954 bectl_cmd_unmount(int argc, char *argv[])
959 /* Store alias used */
963 while ((opt = getopt(argc, argv, "f")) != -1) {
966 flags |= BE_MNT_FORCE;
969 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
971 return (usage(false));
979 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
980 return (usage(false));
985 err = be_unmount(be, bootenv, flags);
991 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
999 main(int argc, char *argv[])
1001 const char *command;
1002 int command_index, rc;
1005 fprintf(stderr, "missing command\n");
1006 return (usage(false));
1011 /* Handle command aliases */
1012 if (strcmp(command, "umount") == 0)
1013 command = "unmount";
1015 if (strcmp(command, "ujail") == 0)
1018 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
1019 return (usage(true));
1021 if (get_cmd_index(command, &command_index)) {
1022 fprintf(stderr, "unknown command: %s\n", command);
1023 return (usage(false));
1027 if ((be = libbe_init()) == NULL)
1030 libbe_print_on_error(be, true);
1032 /* XXX TODO: can be simplified if offset by 2 instead of one */
1033 rc = command_map[command_index].fn(argc-1, argv+1);