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_ACTIVE "Active"
48 #define HEADER_MOUNT "Mountpoint"
49 #define HEADER_SPACE "Space"
50 #define HEADER_CREATED "Created"
59 static int bectl_cmd_activate(int argc, char *argv[]);
60 static int bectl_cmd_create(int argc, char *argv[]);
61 static int bectl_cmd_destroy(int argc, char *argv[]);
62 static int bectl_cmd_export(int argc, char *argv[]);
63 static int bectl_cmd_import(int argc, char *argv[]);
64 static int bectl_cmd_add(int argc, char *argv[]);
65 static int bectl_cmd_jail(int argc, char *argv[]);
66 static void print_dataset(nvpair_t *cur, struct printc *pc);
67 static int bectl_cmd_list(int argc, char *argv[]);
68 static int bectl_cmd_mount(int argc, char *argv[]);
69 static int bectl_cmd_rename(int argc, char *argv[]);
70 static int bectl_search_jail_paths(const char *mnt);
71 static int bectl_locate_jail(const char *ident);
72 static int bectl_cmd_unjail(int argc, char *argv[]);
73 static int bectl_cmd_unmount(int argc, char *argv[]);
75 static libbe_handle_t *be;
82 fp = explicit ? stdout : stderr;
84 "usage:\tbectl ( -h | -? | subcommand [args...] )\n"
85 "\tbectl activate [-t] beName\n"
86 "\tbectl create [-e nonActiveBe | -e beName@snapshot] beName\n"
87 "\tbectl create beName@snapshot\n"
88 "\tbectl destroy [-F] beName | beName@snapshot⟩\n"
89 "\tbectl export sourceBe\n"
90 "\tbectl import targetBe\n"
91 "\tbectl add (path)*\n"
92 "\tbectl jail bootenv\n"
93 "\tbectl list [-a] [-D] [-H] [-s]\n"
94 "\tbectl mount beName [mountpoint]\n"
95 "\tbectl rename origBeName newBeName\n"
96 "\tbectl { ujail | unjail } ⟨jailID | jailName | bootenv)\n"
97 "\tbectl { umount | unmount } [-f] beName\n");
99 return (explicit ? 0 : EX_USAGE);
104 * Represents a relationship between the command name and the parser action
107 struct command_map_entry {
109 int (*fn)(int argc, char *argv[]);
112 static struct command_map_entry command_map[] =
114 { "activate", bectl_cmd_activate },
115 { "create", bectl_cmd_create },
116 { "destroy", bectl_cmd_destroy },
117 { "export", bectl_cmd_export },
118 { "import", bectl_cmd_import },
119 { "add", bectl_cmd_add },
120 { "jail", bectl_cmd_jail },
121 { "list", bectl_cmd_list },
122 { "mount", bectl_cmd_mount },
123 { "rename", bectl_cmd_rename },
124 { "unjail", bectl_cmd_unjail },
125 { "unmount", bectl_cmd_unmount },
129 get_cmd_index(const char *cmd, int *index)
133 map_size = nitems(command_map);
134 for (int i = 0; i < map_size; ++i) {
135 if (strcmp(cmd, command_map[i].command) == 0) {
146 bectl_cmd_activate(int argc, char *argv[])
152 while ((opt = getopt(argc, argv, "t")) != -1) {
158 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
160 return (usage(false));
168 fprintf(stderr, "bectl activate: wrong number of arguments\n");
169 return (usage(false));
173 /* activate logic goes here */
174 if ((err = be_activate(be, argv[0], temp)) != 0)
175 /* XXX TODO: more specific error msg based on err */
176 printf("did not successfully activate boot environment %s\n",
179 printf("successfully activated boot environment %s\n", argv[0]);
182 printf("for next boot\n");
189 * TODO: when only one arg is given, and it contains an "@" the this should
190 * create that snapshot
193 bectl_cmd_create(int argc, char *argv[])
195 char *bootenv, *snapname, *source;
199 while ((opt = getopt(argc, argv, "e:")) != -1) {
205 fprintf(stderr, "bectl create: unknown option '-%c'\n",
207 return (usage(false));
215 fprintf(stderr, "bectl create: wrong number of arguments\n");
216 return (usage(false));
221 if (snapname != NULL) {
222 if (strchr(snapname, '@') != NULL)
223 err = be_create_from_existing_snap(be, bootenv,
226 err = be_create_from_existing(be, bootenv, snapname);
228 if ((snapname = strchr(bootenv, '@')) != NULL) {
229 *(snapname++) = '\0';
230 if ((err = be_snapshot(be, be_active_path(be),
231 snapname, true, NULL)) != BE_ERR_SUCCESS)
232 fprintf(stderr, "failed to create snapshot\n");
233 asprintf(&source, "%s@%s", be_active_path(be), snapname);
234 err = be_create_from_existing_snap(be, bootenv,
238 err = be_create(be, bootenv);
245 if (snapname == NULL)
247 "failed to create bootenv %s\n", bootenv);
250 "failed to create bootenv %s from snapshot %s\n",
259 bectl_cmd_export(int argc, char *argv[])
264 fprintf(stderr, "bectl export: missing boot environment name\n");
265 return (usage(false));
269 fprintf(stderr, "bectl export: extra arguments provided\n");
270 return (usage(false));
275 if (isatty(STDOUT_FILENO)) {
276 fprintf(stderr, "bectl export: must redirect output\n");
280 be_export(be, bootenv, STDOUT_FILENO);
287 bectl_cmd_import(int argc, char *argv[])
293 fprintf(stderr, "bectl import: missing boot environment name\n");
294 return (usage(false));
298 fprintf(stderr, "bectl import: extra arguments provided\n");
299 return (usage(false));
304 if (isatty(STDIN_FILENO)) {
305 fprintf(stderr, "bectl import: input can not be from terminal\n");
309 err = be_import(be, bootenv, STDIN_FILENO);
316 bectl_cmd_add(int argc, char *argv[])
320 fprintf(stderr, "bectl add: must provide at least one path\n");
321 return (usage(false));
324 for (int i = 1; i < argc; ++i) {
325 printf("arg %d: %s\n", i, argv[i]);
326 /* XXX TODO catch err */
327 be_add_child(be, argv[i], true);
335 bectl_cmd_destroy(int argc, char *argv[])
342 while ((opt = getopt(argc, argv, "F")) != -1) {
348 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
350 return (usage(false));
358 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
359 return (usage(false));
364 err = be_destroy(be, target, force);
371 bectl_cmd_jail(int argc, char *argv[])
374 char mnt_loc[BE_MAXPATHLEN];
377 /* struct jail be_jail = { 0 }; */
380 fprintf(stderr, "bectl jail: missing boot environment name\n");
381 return (usage(false));
384 fprintf(stderr, "bectl jail: too many arguments\n");
385 return (usage(false));
391 * XXX TODO: if its already mounted, perhaps there should be a flag to
392 * indicate its okay to proceed??
394 if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS) {
395 fprintf(stderr, "could not mount bootenv\n");
399 /* XXX TODO: Make the IP/hostname configurable? */
400 jid = jail_setv(JAIL_CREATE | JAIL_ATTACH,
403 "host.hostname", bootenv,
405 "ip4.addr", "10.20.30.40",
406 "allow.mount", "true",
407 "allow.mount.devfs", "true",
408 "enforce_statfs", "1",
411 fprintf(stderr, "unable to create jail. error: %d\n", errno);
415 /* We're attached within the jail... good bye! */
417 execl("/bin/sh", "/bin/sh", NULL);
422 print_dataset(nvpair_t *cur, struct printc *pc)
426 unsigned long long ctimenum, space;
427 nvlist_t *dsprops, *originprops;
430 boolean_t active_now, active_reboot;
433 propstr = nvpair_name(cur);
434 /* XXX TODO: Some views show snapshots */
435 if (strchr(propstr, '@') != NULL)
437 printf("%*s ", pc->be_colsz, propstr);
439 active_colsz = pc->active_colsz_def;
440 nvpair_value_nvlist(cur, &dsprops);
441 if (nvlist_lookup_boolean_value(dsprops, "active",
442 &active_now) == 0 && active_now) {
446 if (nvlist_lookup_boolean_value(dsprops, "nextboot",
447 &active_reboot) == 0 && active_reboot) {
451 if (active_colsz == pc->active_colsz_def) {
455 printf("%*s ", -active_colsz, " ");
456 if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0)
457 printf("%*s ", pc->mount_colsz, propstr);
459 printf("%*s ", pc->mount_colsz, "-");
461 if (nvlist_lookup_string(dsprops, "origin", &propstr) == 0) {
462 if (be_prop_list_alloc(&originprops) != 0) {
464 "bectl list: failed to allocate origin prop nvlist\n");
467 if (be_get_snapshot_props(be, propstr, originprops) != 0) {
468 /* XXX TODO: Real errors */
470 "bectl list: failed to fetch origin properties\n");
475 if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) {
476 space = strtoull(propstr, NULL, 10);
478 if (originprops != NULL && nvlist_lookup_string(originprops,
479 "used", &propstr) == 0)
480 space += strtoull(propstr, NULL, 10);
482 /* Alas, there's more to it,. */
483 humanize_number(buf, 6, space, "", HN_AUTOSCALE,
484 HN_DECIMAL | HN_NOSPACE | HN_B);
485 printf("%*s ", pc->space_colsz, buf);
487 printf("%*s ", pc->space_colsz, "-");
489 if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) {
490 ctimenum = strtoull(propstr, NULL, 10);
491 strftime(buf, BUFSZ, "%Y-%m-%d %H:%M",
492 localtime((time_t *)&ctimenum));
497 if (originprops != NULL)
498 be_prop_list_free(originprops);
503 bectl_cmd_list(int argc, char *argv[])
510 bool show_all_datasets, show_space, hide_headers, show_snaps;
513 show_all_datasets = show_space = hide_headers = show_snaps = false;
514 while ((opt = getopt(argc, argv, "aDHs")) != -1) {
517 show_all_datasets = true;
529 fprintf(stderr, "bectl list: unknown option '-%c'\n",
531 return (usage(false));
538 fprintf(stderr, "bectl list: extra argument provided\n");
539 return (usage(false));
542 if (be_prop_list_alloc(&props) != 0) {
543 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n");
546 if (be_get_bootenv_props(be, props) != 0) {
547 /* XXX TODO: Real errors */
548 fprintf(stderr, "bectl list: failed to fetch boot environments\n");
552 be_maxcol = strlen(HEADER_BE);
553 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
554 cur = nvlist_next_nvpair(props, cur)) {
555 be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur)));
558 pc.be_colsz = -be_maxcol;
559 /* To be made negative after calculating final col sz */
560 pc.active_colsz_def = strlen(HEADER_ACTIVE);
561 pc.mount_colsz = -(int)strlen(HEADER_MOUNT);
562 pc.space_colsz = -(int)strlen(HEADER_SPACE);
563 printf("%*s %s %s %s %s\n", pc.be_colsz, HEADER_BE, HEADER_ACTIVE,
564 HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
565 for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
566 cur = nvlist_next_nvpair(props, cur)) {
567 print_dataset(cur, &pc);
569 be_prop_list_free(props);
576 bectl_cmd_mount(int argc, char *argv[])
578 char result_loc[BE_MAXPATHLEN];
579 char *bootenv, *mountpoint;
583 fprintf(stderr, "bectl mount: missing argument(s)\n");
584 return (usage(false));
588 fprintf(stderr, "bectl mount: too many arguments\n");
589 return (usage(false));
593 mountpoint = ((argc == 3) ? argv[2] : NULL);
595 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
599 printf("successfully mounted %s at %s\n", bootenv, result_loc);
603 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
604 "failed to mount bootenv %s at temporary path %s\n",
605 bootenv, mountpoint);
613 bectl_cmd_rename(int argc, char *argv[])
619 fprintf(stderr, "bectl rename: missing argument\n");
620 return (usage(false));
624 fprintf(stderr, "bectl rename: too many arguments\n");
625 return (usage(false));
631 err = be_rename(be, src, dest);
637 fprintf(stderr, "failed to rename bootenv %s to %s\n",
645 bectl_search_jail_paths(const char *mnt)
647 char jailpath[MAXPATHLEN + 1];
652 while ((jid = jail_getv(0, "lastjid", &jid, "path", &jailpath,
654 if (strcmp(jailpath, mnt) == 0)
662 * Locate a jail based on an arbitrary identifier. This may be either a name,
663 * a jid, or a BE name. Returns the jid or -1 on failure.
666 bectl_locate_jail(const char *ident)
668 nvlist_t *belist, *props;
672 /* Try the easy-match first */
673 jid = jail_getid(ident);
677 /* Attempt to try it as a BE name, first */
678 if (be_prop_list_alloc(&belist) != 0)
681 if (be_get_bootenv_props(be, belist) != 0)
684 if (nvlist_lookup_nvlist(belist, ident, &props) == 0) {
685 /* We'll attempt to resolve the jid by way of mountpoint */
686 if (nvlist_lookup_string(props, "mountpoint", &mnt) == 0) {
687 jid = bectl_search_jail_paths(mnt);
688 be_prop_list_free(belist);
692 be_prop_list_free(belist);
699 bectl_cmd_unjail(int argc, char *argv[])
701 char path[MAXPATHLEN + 1];
702 char *cmd, *name, *target;
705 /* Store alias used */
709 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
710 return (usage(false));
715 /* Locate the jail */
716 if ((jid = bectl_locate_jail(target)) == -1) {
717 fprintf(stderr, "bectl %s: failed to locate BE by '%s'\n", cmd, target);
721 bzero(&path, MAXPATHLEN + 1);
722 name = jail_getname(jid);
723 if (jail_getv(0, "name", name, "path", path, NULL) != jid) {
725 fprintf(stderr, "bectl %s: failed to get path for jail requested by '%s'\n", cmd, target);
731 if (be_mounted_at(be, path, NULL) != 0) {
732 fprintf(stderr, "bectl %s: jail requested by '%s' not a BE\n", cmd, target);
744 bectl_cmd_unmount(int argc, char *argv[])
749 /* Store alias used */
753 while ((opt = getopt(argc, argv, "f")) != -1) {
756 flags |= BE_MNT_FORCE;
759 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
761 return (usage(false));
769 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
770 return (usage(false));
775 err = be_unmount(be, bootenv, flags);
781 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
789 main(int argc, char *argv[])
792 int command_index, rc;
795 fprintf(stderr, "missing command\n");
796 return (usage(false));
801 /* Handle command aliases */
802 if (strcmp(command, "umount") == 0)
805 if (strcmp(command, "ujail") == 0)
808 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
809 return (usage(true));
811 if (get_cmd_index(command, &command_index)) {
812 fprintf(stderr, "unknown command: %s\n", command);
813 return (usage(false));
817 if ((be = libbe_init()) == NULL)
820 libbe_print_on_error(be, true);
822 /* XXX TODO: can be simplified if offset by 2 instead of one */
823 rc = command_map[command_index].fn(argc-1, argv+1);