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>
44 static int bectl_cmd_activate(int argc, char *argv[]);
45 static int bectl_cmd_create(int argc, char *argv[]);
46 static int bectl_cmd_destroy(int argc, char *argv[]);
47 static int bectl_cmd_export(int argc, char *argv[]);
48 static int bectl_cmd_import(int argc, char *argv[]);
49 static int bectl_cmd_add(int argc, char *argv[]);
50 static int bectl_cmd_jail(int argc, char *argv[]);
51 static int bectl_cmd_list(int argc, char *argv[]);
52 static int bectl_cmd_mount(int argc, char *argv[]);
53 static int bectl_cmd_rename(int argc, char *argv[]);
54 static int bectl_cmd_unjail(int argc, char *argv[]);
55 static int bectl_cmd_unmount(int argc, char *argv[]);
64 fp = explicit ? stdout : stderr;
66 "usage:\tbectl ( -h | -? | subcommand [args...] )\n"
67 "\tbectl activate [-t] beName\n"
68 "\tbectl create [-e nonActiveBe | -e beName@snapshot] beName\n"
69 "\tbectl create beName@snapshot\n"
70 "\tbectl destroy [-F] beName | beName@snapshot⟩\n"
71 "\tbectl export sourceBe\n"
72 "\tbectl import targetBe\n"
73 "\tbectl add (path)*\n"
74 "\tbectl jail bootenv\n"
75 "\tbectl list [-a] [-D] [-H] [-s]\n"
76 "\tbectl mount beName [mountpoint]\n"
77 "\tbectl rename origBeName newBeName\n"
78 "\tbectl { ujail | unjail } ⟨jailID | jailName⟩ bootenv\n"
79 "\tbectl { umount | unmount } [-f] beName\n");
81 return (explicit ? 0 : EX_USAGE);
86 * Represents a relationship between the command name and the parser action
89 struct command_map_entry {
91 int (*fn)(int argc, char *argv[]);
94 static struct command_map_entry command_map[] =
96 { "activate", bectl_cmd_activate },
97 { "create", bectl_cmd_create },
98 { "destroy", bectl_cmd_destroy },
99 { "export", bectl_cmd_export },
100 { "import", bectl_cmd_import },
101 { "add", bectl_cmd_add },
102 { "jail", bectl_cmd_jail },
103 { "list", bectl_cmd_list },
104 { "mount", bectl_cmd_mount },
105 { "rename", bectl_cmd_rename },
106 { "unjail", bectl_cmd_unjail },
107 { "unmount", bectl_cmd_unmount },
111 get_cmd_index(char *cmd, int *index)
115 map_size = nitems(command_map);
116 for (int i = 0; i < map_size; ++i) {
117 if (strcmp(cmd, command_map[i].command) == 0) {
128 bectl_cmd_activate(int argc, char *argv[])
135 while ((opt = getopt(argc, argv, "t")) != -1) {
141 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
143 return (usage(false));
151 fprintf(stderr, "bectl activate: wrong number of arguments\n");
152 return (usage(false));
156 /* activate logic goes here */
157 if ((err = be_activate(be, argv[0], temp)) != 0)
158 /* XXX TODO: more specific error msg based on err */
159 printf("did not successfully activate boot environment %s\n",
162 printf("successfully activated boot environment %s\n", argv[0]);
165 printf("for next boot\n");
172 * TODO: when only one arg is given, and it contains an "@" the this should
173 * create that snapshot
176 bectl_cmd_create(int argc, char *argv[])
178 char *bootenv, *snapname, *source;
182 while ((opt = getopt(argc, argv, "e:")) != -1) {
188 fprintf(stderr, "bectl create: unknown option '-%c'\n",
190 return (usage(false));
198 fprintf(stderr, "bectl create: wrong number of arguments\n");
199 return (usage(false));
204 if (snapname != NULL) {
205 if (strchr(snapname, '@') != NULL)
206 err = be_create_from_existing_snap(be, bootenv,
209 err = be_create_from_existing(be, bootenv, snapname);
211 if ((snapname = strchr(bootenv, '@')) != NULL) {
212 *(snapname++) = '\0';
213 if ((err = be_snapshot(be, (char *)be_active_path(be),
214 snapname, true, NULL)) != BE_ERR_SUCCESS)
215 fprintf(stderr, "failed to create snapshot\n");
216 asprintf(&source, "%s@%s", be_active_path(be), snapname);
217 err = be_create_from_existing_snap(be, bootenv,
221 err = be_create(be, bootenv);
228 if (snapname == NULL)
230 "failed to create bootenv %s\n", bootenv);
233 "failed to create bootenv %s from snapshot %s\n",
242 bectl_cmd_export(int argc, char *argv[])
248 fprintf(stderr, "bectl export: missing boot environment name\n");
249 return (usage(false));
253 fprintf(stderr, "bectl export: extra arguments provided\n");
254 return (usage(false));
259 if (isatty(STDOUT_FILENO)) {
260 fprintf(stderr, "bectl export: must redirect output\n");
264 be_export(be, bootenv, STDOUT_FILENO);
271 bectl_cmd_import(int argc, char *argv[])
277 fprintf(stderr, "bectl import: missing boot environment name\n");
278 return (usage(false));
282 fprintf(stderr, "bectl import: extra arguments provided\n");
283 return (usage(false));
288 if (isatty(STDIN_FILENO)) {
289 fprintf(stderr, "bectl import: input can not be from terminal\n");
293 err = be_import(be, bootenv, STDIN_FILENO);
300 bectl_cmd_add(int argc, char *argv[])
305 fprintf(stderr, "bectl add: must provide at least one path\n");
306 return (usage(false));
309 for (int i = 1; i < argc; ++i) {
310 printf("arg %d: %s\n", i, argv[i]);
311 /* XXX TODO catch err */
312 be_add_child(be, argv[i], true);
320 bectl_cmd_destroy(int argc, char *argv[])
327 while ((opt = getopt(argc, argv, "F")) != -1) {
333 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
335 return (usage(false));
343 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
344 return (usage(false));
349 err = be_destroy(be, target, force);
356 bectl_cmd_jail(int argc, char *argv[])
359 char mnt_loc[BE_MAXPATHLEN];
360 char buf[BE_MAXPATHLEN*2];
363 /* struct jail be_jail = { 0 }; */
366 fprintf(stderr, "bectl jail: missing boot environment name\n");
367 return (usage(false));
370 fprintf(stderr, "bectl jail: too many arguments\n");
371 return (usage(false));
377 * XXX TODO: if its already mounted, perhaps there should be a flag to
378 * indicate its okay to proceed??
380 if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS)
381 fprintf(stderr, "could not mount bootenv\n");
384 * NOTE: this is not quite functional:
385 * see https://github.com/vermaden/beadm/blob/master/HOWTO.htm on
386 * neccesary modifications to correctly boot the jail
390 * snprintf(buf, BE_MAXPATHLEN*2, "jail %s %s %s /bin/sh /etc/rc",
391 * mnt_loc, bootenv, "192.168.1.123");
393 snprintf(buf, BE_MAXPATHLEN*2, "jail %s %s %s /bin/sh", mnt_loc,
394 bootenv, "192.168.1.123");
400 * be_jail.version = JAIL_API_VERSION;
401 * be_jail.path = "/tmp/be_mount.hCCk";
402 * be_jail.jailname = "sdfs";
404 * if ((jid = jail(&be_jail)) != -1) {
405 * printf("jail %d created at %s\n", jid, mnt_loc);
408 * fprintf(stderr, "unable to create jail. error: %d\n", errno);
418 bectl_cmd_list(int argc, char *argv[])
423 bool show_all_datasets, show_space, hide_headers, show_snaps;
425 show_all_datasets = show_space = hide_headers = show_snaps = false;
426 while ((opt = getopt(argc, argv, "aDHs")) != -1) {
429 show_all_datasets = true;
441 fprintf(stderr, "bectl list: unknown option '-%c'\n",
443 return (usage(false));
450 fprintf(stderr, "bectl list: extra argument provided\n");
451 return (usage(false));
454 /* props = be_get_bootenv_props(be); */
460 bectl_cmd_mount(int argc, char *argv[])
462 char result_loc[BE_MAXPATHLEN];
463 char *bootenv, *mountpoint;
467 fprintf(stderr, "bectl mount: missing argument(s)\n");
468 return (usage(false));
472 fprintf(stderr, "bectl mount: too many arguments\n");
473 return (usage(false));
477 mountpoint = ((argc == 3) ? argv[2] : NULL);
479 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
483 printf("successfully mounted %s at %s\n", bootenv, result_loc);
487 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
488 "failed to mount bootenv %s at temporary path %s\n",
489 bootenv, mountpoint);
497 bectl_cmd_rename(int argc, char *argv[])
503 fprintf(stderr, "bectl rename: missing argument\n");
504 return (usage(false));
508 fprintf(stderr, "bectl rename: too many arguments\n");
509 return (usage(false));
515 err = be_rename(be, src, dest);
521 fprintf(stderr, "failed to rename bootenv %s to %s\n",
530 bectl_cmd_unjail(int argc, char *argv[])
536 /* Store alias used */
540 while ((opt = getopt(argc, argv, "f")) != -1) {
546 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
548 return (usage(false));
556 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
557 return (usage(false));
562 /* unjail logic goes here */
568 bectl_cmd_unmount(int argc, char *argv[])
573 /* Store alias used */
577 while ((opt = getopt(argc, argv, "f")) != -1) {
580 flags |= BE_MNT_FORCE;
583 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
585 return (usage(false));
593 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
594 return (usage(false));
599 err = be_unmount(be, bootenv, flags);
605 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
613 main(int argc, char *argv[])
616 int command_index, rc;
619 fprintf(stderr, "missing command\n");
620 return (usage(false));
625 /* Handle command aliases */
626 if (strcmp(command, "umount") == 0)
629 if (strcmp(command, "ujail") == 0)
632 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
633 return (usage(true));
635 if (get_cmd_index(command, &command_index)) {
636 fprintf(stderr, "unknown command: %s\n", command);
637 return (usage(false));
641 if ((be = libbe_init()) == NULL)
644 libbe_print_on_error(be, true);
646 /* XXX TODO: can be simplified if offset by 2 instead of one */
647 rc = command_map[command_index].fn(argc-1, argv+1);