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/malloc.h>
32 #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[]);
57 static libbe_handle_t *be;
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(const 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[])
134 while ((opt = getopt(argc, argv, "t")) != -1) {
140 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
142 return (usage(false));
150 fprintf(stderr, "bectl activate: wrong number of arguments\n");
151 return (usage(false));
155 /* activate logic goes here */
156 if ((err = be_activate(be, argv[0], temp)) != 0)
157 /* XXX TODO: more specific error msg based on err */
158 printf("did not successfully activate boot environment %s\n",
161 printf("successfully activated boot environment %s\n", argv[0]);
164 printf("for next boot\n");
171 * TODO: when only one arg is given, and it contains an "@" the this should
172 * create that snapshot
175 bectl_cmd_create(int argc, char *argv[])
177 char *bootenv, *snapname, *source;
181 while ((opt = getopt(argc, argv, "e:")) != -1) {
187 fprintf(stderr, "bectl create: unknown option '-%c'\n",
189 return (usage(false));
197 fprintf(stderr, "bectl create: wrong number of arguments\n");
198 return (usage(false));
203 if (snapname != NULL) {
204 if (strchr(snapname, '@') != NULL)
205 err = be_create_from_existing_snap(be, bootenv,
208 err = be_create_from_existing(be, bootenv, snapname);
210 if ((snapname = strchr(bootenv, '@')) != NULL) {
211 *(snapname++) = '\0';
212 if ((err = be_snapshot(be, be_active_path(be),
213 snapname, true, NULL)) != BE_ERR_SUCCESS)
214 fprintf(stderr, "failed to create snapshot\n");
215 asprintf(&source, "%s@%s", be_active_path(be), snapname);
216 err = be_create_from_existing_snap(be, bootenv,
220 err = be_create(be, bootenv);
227 if (snapname == NULL)
229 "failed to create bootenv %s\n", bootenv);
232 "failed to create bootenv %s from snapshot %s\n",
241 bectl_cmd_export(int argc, char *argv[])
246 fprintf(stderr, "bectl export: missing boot environment name\n");
247 return (usage(false));
251 fprintf(stderr, "bectl export: extra arguments provided\n");
252 return (usage(false));
257 if (isatty(STDOUT_FILENO)) {
258 fprintf(stderr, "bectl export: must redirect output\n");
262 be_export(be, bootenv, STDOUT_FILENO);
269 bectl_cmd_import(int argc, char *argv[])
275 fprintf(stderr, "bectl import: missing boot environment name\n");
276 return (usage(false));
280 fprintf(stderr, "bectl import: extra arguments provided\n");
281 return (usage(false));
286 if (isatty(STDIN_FILENO)) {
287 fprintf(stderr, "bectl import: input can not be from terminal\n");
291 err = be_import(be, bootenv, STDIN_FILENO);
298 bectl_cmd_add(int argc, char *argv[])
302 fprintf(stderr, "bectl add: must provide at least one path\n");
303 return (usage(false));
306 for (int i = 1; i < argc; ++i) {
307 printf("arg %d: %s\n", i, argv[i]);
308 /* XXX TODO catch err */
309 be_add_child(be, argv[i], true);
317 bectl_cmd_destroy(int argc, char *argv[])
324 while ((opt = getopt(argc, argv, "F")) != -1) {
330 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
332 return (usage(false));
340 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
341 return (usage(false));
346 err = be_destroy(be, target, force);
353 bectl_cmd_jail(int argc, char *argv[])
356 char mnt_loc[BE_MAXPATHLEN];
357 char buf[BE_MAXPATHLEN*2];
360 /* struct jail be_jail = { 0 }; */
363 fprintf(stderr, "bectl jail: missing boot environment name\n");
364 return (usage(false));
367 fprintf(stderr, "bectl jail: too many arguments\n");
368 return (usage(false));
374 * XXX TODO: if its already mounted, perhaps there should be a flag to
375 * indicate its okay to proceed??
377 if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS)
378 fprintf(stderr, "could not mount bootenv\n");
381 * NOTE: this is not quite functional:
382 * see https://github.com/vermaden/beadm/blob/master/HOWTO.htm on
383 * neccesary modifications to correctly boot the jail
387 * snprintf(buf, BE_MAXPATHLEN*2, "jail %s %s %s /bin/sh /etc/rc",
388 * mnt_loc, bootenv, "192.168.1.123");
390 snprintf(buf, BE_MAXPATHLEN*2, "jail %s %s %s /bin/sh", mnt_loc,
391 bootenv, "192.168.1.123");
397 * be_jail.version = JAIL_API_VERSION;
398 * be_jail.path = "/tmp/be_mount.hCCk";
399 * be_jail.jailname = "sdfs";
401 * if ((jid = jail(&be_jail)) != -1) {
402 * printf("jail %d created at %s\n", jid, mnt_loc);
405 * fprintf(stderr, "unable to create jail. error: %d\n", errno);
415 bectl_cmd_list(int argc, char *argv[])
419 bool show_all_datasets, show_space, hide_headers, show_snaps;
421 show_all_datasets = show_space = hide_headers = show_snaps = false;
422 while ((opt = getopt(argc, argv, "aDHs")) != -1) {
425 show_all_datasets = true;
437 fprintf(stderr, "bectl list: unknown option '-%c'\n",
439 return (usage(false));
446 fprintf(stderr, "bectl list: extra argument provided\n");
447 return (usage(false));
451 if (nvlist_alloc(&props, NV_UNIQUE_NAME, M_WAITOK) != 0) {
452 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n");
455 if (be_get_bootenv_props(be, props) != 0) {
456 /* XXX TODO: Real errors */
457 fprintf(stderr, "bectl list: failed to fetch boot environments\n");
461 dump_nvlist(props, 0);
469 bectl_cmd_mount(int argc, char *argv[])
471 char result_loc[BE_MAXPATHLEN];
472 char *bootenv, *mountpoint;
476 fprintf(stderr, "bectl mount: missing argument(s)\n");
477 return (usage(false));
481 fprintf(stderr, "bectl mount: too many arguments\n");
482 return (usage(false));
486 mountpoint = ((argc == 3) ? argv[2] : NULL);
488 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
492 printf("successfully mounted %s at %s\n", bootenv, result_loc);
496 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
497 "failed to mount bootenv %s at temporary path %s\n",
498 bootenv, mountpoint);
506 bectl_cmd_rename(int argc, char *argv[])
512 fprintf(stderr, "bectl rename: missing argument\n");
513 return (usage(false));
517 fprintf(stderr, "bectl rename: too many arguments\n");
518 return (usage(false));
524 err = be_rename(be, src, dest);
530 fprintf(stderr, "failed to rename bootenv %s to %s\n",
539 bectl_cmd_unjail(int argc, char *argv[])
545 /* Store alias used */
549 while ((opt = getopt(argc, argv, "f")) != -1) {
555 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
557 return (usage(false));
565 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
566 return (usage(false));
571 /* unjail logic goes here */
577 bectl_cmd_unmount(int argc, char *argv[])
582 /* Store alias used */
586 while ((opt = getopt(argc, argv, "f")) != -1) {
589 flags |= BE_MNT_FORCE;
592 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
594 return (usage(false));
602 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
603 return (usage(false));
608 err = be_unmount(be, bootenv, flags);
614 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
622 main(int argc, char *argv[])
625 int command_index, rc;
628 fprintf(stderr, "missing command\n");
629 return (usage(false));
634 /* Handle command aliases */
635 if (strcmp(command, "umount") == 0)
638 if (strcmp(command, "ujail") == 0)
641 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
642 return (usage(true));
644 if (get_cmd_index(command, &command_index)) {
645 fprintf(stderr, "unknown command: %s\n", command);
646 return (usage(false));
650 if ((be = libbe_init()) == NULL)
653 libbe_print_on_error(be, true);
655 /* XXX TODO: can be simplified if offset by 2 instead of one */
656 rc = command_map[command_index].fn(argc-1, argv+1);