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 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");
461 print_padding(const char *fval, int colsz, struct printc *pc)
464 if (pc->script_fmt) {
470 colsz -= strlen(fval);
471 printf("%*s ", colsz, "");
475 print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
479 unsigned long long ctimenum, space;
480 nvlist_t *originprops;
484 boolean_t active_now, active_reboot;
487 printf("%*s%s", pc->current_indent, "", name);
489 /* Recurse at the base level if we're breaking info down */
490 if (pc->current_indent == 0 && (pc->show_all_datasets ||
493 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
494 /* XXX TODO: Error? */
496 pc->current_indent += INDENT_INCREMENT;
497 print_info(propstr, dsprops, pc);
498 pc->current_indent += INDENT_INCREMENT;
499 if ((oname = get_origin_props(dsprops, &originprops)) != NULL) {
500 print_info(oname, originprops, pc);
501 nvlist_free(originprops);
503 pc->current_indent = 0;
506 print_padding(name, pc->be_colsz - pc->current_indent, pc);
508 active_colsz = pc->active_colsz_def;
509 if (nvlist_lookup_boolean_value(dsprops, "active",
510 &active_now) == 0 && active_now) {
514 if (nvlist_lookup_boolean_value(dsprops, "nextboot",
515 &active_reboot) == 0 && active_reboot) {
519 if (active_colsz == pc->active_colsz_def) {
523 print_padding(NULL, active_colsz, pc);
524 if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0){
525 printf("%s", propstr);
526 print_padding(propstr, pc->mount_colsz, pc);
529 print_padding("-", pc->mount_colsz, pc);
532 get_origin_props(dsprops, &originprops);
534 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) {
535 space = strtoull(propstr, NULL, 10);
537 if (!pc->show_all_datasets && originprops != NULL &&
538 nvlist_lookup_string(originprops, "used", &propstr) == 0)
539 space += strtoull(propstr, NULL, 10);
541 /* Alas, there's more to it,. */
542 humanize_number(buf, 6, space, "", HN_AUTOSCALE,
543 HN_DECIMAL | HN_NOSPACE | HN_B);
545 print_padding(buf, pc->space_colsz, pc);
548 print_padding("-", pc->space_colsz, pc);
551 if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) {
552 ctimenum = strtoull(propstr, NULL, 10);
553 strftime(buf, BUFSZ, "%Y-%m-%d %H:%M",
554 localtime((time_t *)&ctimenum));
559 if (originprops != NULL) {
560 /*if (pc->show_all_datasets) {
562 be_prop_list_free(originprops);
568 print_headers(nvlist_t *props, struct printc *pc)
570 const char *chosen_be_header;
576 if (pc->show_all_datasets || pc->show_snaps)
577 chosen_be_header = HEADER_BEPLUS;
579 chosen_be_header = HEADER_BE;
580 be_maxcol = strlen(chosen_be_header);
581 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
582 cur = nvlist_next_nvpair(props, cur)) {
583 be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur)));
584 if (!pc->show_all_datasets && !pc->show_snaps)
586 nvpair_value_nvlist(cur, &dsprops);
587 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
589 be_maxcol = MAX(be_maxcol, strlen(propstr) + INDENT_INCREMENT);
590 if (nvlist_lookup_string(dsprops, "origin", &propstr) != 0)
592 be_maxcol = MAX(be_maxcol,
593 strlen(propstr) + INDENT_INCREMENT * 2);
596 pc->be_colsz = be_maxcol;
597 pc->active_colsz_def = strlen(HEADER_ACTIVE);
598 pc->mount_colsz = strlen(HEADER_MOUNT);
599 pc->space_colsz = strlen(HEADER_SPACE);
600 printf("%*s %s %s %s %s\n", -pc->be_colsz, chosen_be_header,
601 HEADER_ACTIVE, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
604 * All other invocations in which we aren't using the default header
605 * will produce quite a bit of input. Throw an extra blank line after
606 * the header to make it look nicer.
608 if (chosen_be_header != HEADER_BE)
613 bectl_cmd_list(int argc, char *argv[])
617 nvlist_t *dsprops, *props;
619 boolean_t active_now, active_reboot;
623 bzero(&pc, sizeof(pc));
624 while ((opt = getopt(argc, argv, "aDHs")) != -1) {
627 pc.show_all_datasets = true;
630 pc.show_space = true;
633 pc.script_fmt = true;
636 pc.show_snaps = true;
639 fprintf(stderr, "bectl list: unknown option '-%c'\n",
641 return (usage(false));
648 fprintf(stderr, "bectl list: extra argument provided\n");
649 return (usage(false));
652 if (be_prop_list_alloc(&props) != 0) {
653 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n");
656 if (be_get_bootenv_props(be, props) != 0) {
657 /* XXX TODO: Real errors */
658 fprintf(stderr, "bectl list: failed to fetch boot environments\n");
663 print_headers(props, &pc);
664 /* Do a first pass to print active and next active first */
665 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
666 cur = nvlist_next_nvpair(props, cur)) {
667 nvpair_value_nvlist(cur, &dsprops);
668 active_now = active_reboot = false;
670 nvlist_lookup_boolean_value(dsprops, "active", &active_now);
671 nvlist_lookup_boolean_value(dsprops, "nextboot",
673 if (!active_now && !active_reboot)
675 if (printed > 0 && (pc.show_all_datasets || pc.show_snaps))
677 print_info(nvpair_name(cur), dsprops, &pc);
681 /* Now pull everything else */
682 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
683 cur = nvlist_next_nvpair(props, cur)) {
684 nvpair_value_nvlist(cur, &dsprops);
685 active_now = active_reboot = false;
687 nvlist_lookup_boolean_value(dsprops, "active", &active_now);
688 nvlist_lookup_boolean_value(dsprops, "nextboot",
690 if (active_now || active_reboot)
692 if (printed > 0 && (pc.show_all_datasets || pc.show_snaps))
694 print_info(nvpair_name(cur), dsprops, &pc);
697 be_prop_list_free(props);
704 bectl_cmd_mount(int argc, char *argv[])
706 char result_loc[BE_MAXPATHLEN];
707 char *bootenv, *mountpoint;
711 fprintf(stderr, "bectl mount: missing argument(s)\n");
712 return (usage(false));
716 fprintf(stderr, "bectl mount: too many arguments\n");
717 return (usage(false));
721 mountpoint = ((argc == 3) ? argv[2] : NULL);
723 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
727 printf("successfully mounted %s at %s\n", bootenv, result_loc);
731 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
732 "failed to mount bootenv %s at temporary path %s\n",
733 bootenv, mountpoint);
741 bectl_cmd_rename(int argc, char *argv[])
747 fprintf(stderr, "bectl rename: missing argument\n");
748 return (usage(false));
752 fprintf(stderr, "bectl rename: too many arguments\n");
753 return (usage(false));
759 err = be_rename(be, src, dest);
765 fprintf(stderr, "failed to rename bootenv %s to %s\n",
773 bectl_search_jail_paths(const char *mnt)
775 char jailpath[MAXPATHLEN + 1];
780 while ((jid = jail_getv(0, "lastjid", &jid, "path", &jailpath,
782 if (strcmp(jailpath, mnt) == 0)
790 * Locate a jail based on an arbitrary identifier. This may be either a name,
791 * a jid, or a BE name. Returns the jid or -1 on failure.
794 bectl_locate_jail(const char *ident)
796 nvlist_t *belist, *props;
800 /* Try the easy-match first */
801 jid = jail_getid(ident);
805 /* Attempt to try it as a BE name, first */
806 if (be_prop_list_alloc(&belist) != 0)
809 if (be_get_bootenv_props(be, belist) != 0)
812 if (nvlist_lookup_nvlist(belist, ident, &props) == 0) {
813 /* We'll attempt to resolve the jid by way of mountpoint */
814 if (nvlist_lookup_string(props, "mountpoint", &mnt) == 0) {
815 jid = bectl_search_jail_paths(mnt);
816 be_prop_list_free(belist);
820 be_prop_list_free(belist);
827 bectl_cmd_unjail(int argc, char *argv[])
829 char path[MAXPATHLEN + 1];
830 char *cmd, *name, *target;
833 /* Store alias used */
837 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
838 return (usage(false));
843 /* Locate the jail */
844 if ((jid = bectl_locate_jail(target)) == -1) {
845 fprintf(stderr, "bectl %s: failed to locate BE by '%s'\n", cmd, target);
849 bzero(&path, MAXPATHLEN + 1);
850 name = jail_getname(jid);
851 if (jail_getv(0, "name", name, "path", path, NULL) != jid) {
853 fprintf(stderr, "bectl %s: failed to get path for jail requested by '%s'\n", cmd, target);
859 if (be_mounted_at(be, path, NULL) != 0) {
860 fprintf(stderr, "bectl %s: jail requested by '%s' not a BE\n", cmd, target);
872 bectl_cmd_unmount(int argc, char *argv[])
877 /* Store alias used */
881 while ((opt = getopt(argc, argv, "f")) != -1) {
884 flags |= BE_MNT_FORCE;
887 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
889 return (usage(false));
897 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
898 return (usage(false));
903 err = be_unmount(be, bootenv, flags);
909 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
917 main(int argc, char *argv[])
920 int command_index, rc;
923 fprintf(stderr, "missing command\n");
924 return (usage(false));
929 /* Handle command aliases */
930 if (strcmp(command, "umount") == 0)
933 if (strcmp(command, "ujail") == 0)
936 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
937 return (usage(true));
939 if (get_cmd_index(command, &command_index)) {
940 fprintf(stderr, "unknown command: %s\n", command);
941 return (usage(false));
945 if ((be = libbe_init()) == NULL)
948 libbe_print_on_error(be, true);
950 /* XXX TODO: can be simplified if offset by 2 instead of one */
951 rc = command_map[command_index].fn(argc-1, argv+1);