2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
30 #include <sys/mount.h>
46 static int bectl_cmd_activate(int argc, char *argv[]);
47 static int bectl_cmd_create(int argc, char *argv[]);
48 static int bectl_cmd_destroy(int argc, char *argv[]);
49 static int bectl_cmd_export(int argc, char *argv[]);
50 static int bectl_cmd_import(int argc, char *argv[]);
51 static int bectl_cmd_add(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_unmount(int argc, char *argv[]);
63 fp = explicit ? stdout : stderr;
65 "usage:\tbectl ( -h | -? | subcommand [args...] )\n"
66 "\tbectl activate [-t] beName\n"
67 "\tbectl create [-e nonActiveBe | -e beName@snapshot] beName\n"
68 "\tbectl create beName@snapshot\n"
69 "\tbectl destroy [-F] beName | beName@snapshot⟩\n"
70 "\tbectl export sourceBe\n"
71 "\tbectl import targetBe\n"
72 "\tbectl add (path)*\n"
73 "\tbectl jail [ -o key=value | -u key ]... bootenv\n"
74 "\tbectl list [-a] [-D] [-H] [-s]\n"
75 "\tbectl mount beName [mountpoint]\n"
76 "\tbectl rename origBeName newBeName\n"
77 "\tbectl { ujail | unjail } ⟨jailID | jailName | bootenv)\n"
78 "\tbectl { umount | unmount } [-f] beName\n");
80 return (explicit ? 0 : EX_USAGE);
85 * Represents a relationship between the command name and the parser action
88 struct command_map_entry {
90 int (*fn)(int argc, char *argv[]);
93 static struct command_map_entry command_map[] =
95 { "activate", bectl_cmd_activate },
96 { "create", bectl_cmd_create },
97 { "destroy", bectl_cmd_destroy },
98 { "export", bectl_cmd_export },
99 { "import", bectl_cmd_import },
100 { "add", bectl_cmd_add },
101 { "jail", bectl_cmd_jail },
102 { "list", bectl_cmd_list },
103 { "mount", bectl_cmd_mount },
104 { "rename", bectl_cmd_rename },
105 { "unjail", bectl_cmd_unjail },
106 { "unmount", bectl_cmd_unmount },
110 get_cmd_index(const char *cmd, int *index)
114 map_size = nitems(command_map);
115 for (int i = 0; i < map_size; ++i) {
116 if (strcmp(cmd, command_map[i].command) == 0) {
127 bectl_cmd_activate(int argc, char *argv[])
133 while ((opt = getopt(argc, argv, "t")) != -1) {
139 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
141 return (usage(false));
149 fprintf(stderr, "bectl activate: wrong number of arguments\n");
150 return (usage(false));
154 /* activate logic goes here */
155 if ((err = be_activate(be, argv[0], temp)) != 0)
156 /* XXX TODO: more specific error msg based on err */
157 printf("did not successfully activate boot environment %s\n",
160 printf("successfully activated boot environment %s\n", argv[0]);
163 printf("for next boot\n");
170 * TODO: when only one arg is given, and it contains an "@" the this should
171 * create that snapshot
174 bectl_cmd_create(int argc, char *argv[])
176 char *bootenv, *snapname, *source;
180 while ((opt = getopt(argc, argv, "e:")) != -1) {
186 fprintf(stderr, "bectl create: unknown option '-%c'\n",
188 return (usage(false));
196 fprintf(stderr, "bectl create: wrong number of arguments\n");
197 return (usage(false));
202 if (snapname != NULL) {
203 if (strchr(snapname, '@') != NULL)
204 err = be_create_from_existing_snap(be, bootenv,
207 err = be_create_from_existing(be, bootenv, snapname);
209 if ((snapname = strchr(bootenv, '@')) != NULL) {
210 *(snapname++) = '\0';
211 if ((err = be_snapshot(be, be_active_path(be),
212 snapname, true, NULL)) != BE_ERR_SUCCESS)
213 fprintf(stderr, "failed to create snapshot\n");
214 asprintf(&source, "%s@%s", be_active_path(be), snapname);
215 err = be_create_from_existing_snap(be, bootenv,
219 err = be_create(be, bootenv);
226 if (snapname == NULL)
228 "failed to create bootenv %s\n", bootenv);
231 "failed to create bootenv %s from snapshot %s\n",
240 bectl_cmd_export(int argc, char *argv[])
245 fprintf(stderr, "bectl export: missing boot environment name\n");
246 return (usage(false));
250 fprintf(stderr, "bectl export: extra arguments provided\n");
251 return (usage(false));
256 if (isatty(STDOUT_FILENO)) {
257 fprintf(stderr, "bectl export: must redirect output\n");
261 be_export(be, bootenv, STDOUT_FILENO);
268 bectl_cmd_import(int argc, char *argv[])
274 fprintf(stderr, "bectl import: missing boot environment name\n");
275 return (usage(false));
279 fprintf(stderr, "bectl import: extra arguments provided\n");
280 return (usage(false));
285 if (isatty(STDIN_FILENO)) {
286 fprintf(stderr, "bectl import: input can not be from terminal\n");
290 err = be_import(be, bootenv, STDIN_FILENO);
297 bectl_cmd_add(int argc, char *argv[])
301 fprintf(stderr, "bectl add: must provide at least one path\n");
302 return (usage(false));
305 for (int i = 1; i < argc; ++i) {
306 printf("arg %d: %s\n", i, argv[i]);
307 /* XXX TODO catch err */
308 be_add_child(be, argv[i], true);
316 bectl_cmd_destroy(int argc, char *argv[])
323 while ((opt = getopt(argc, argv, "F")) != -1) {
329 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
331 return (usage(false));
339 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
340 return (usage(false));
345 err = be_destroy(be, target, force);
351 bectl_cmd_mount(int argc, char *argv[])
353 char result_loc[BE_MAXPATHLEN];
354 char *bootenv, *mountpoint;
358 fprintf(stderr, "bectl mount: missing argument(s)\n");
359 return (usage(false));
363 fprintf(stderr, "bectl mount: too many arguments\n");
364 return (usage(false));
368 mountpoint = ((argc == 3) ? argv[2] : NULL);
370 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
374 printf("successfully mounted %s at %s\n", bootenv, result_loc);
378 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
379 "failed to mount bootenv %s at temporary path %s\n",
380 bootenv, mountpoint);
388 bectl_cmd_rename(int argc, char *argv[])
394 fprintf(stderr, "bectl rename: missing argument\n");
395 return (usage(false));
399 fprintf(stderr, "bectl rename: too many arguments\n");
400 return (usage(false));
406 err = be_rename(be, src, dest);
412 fprintf(stderr, "failed to rename bootenv %s to %s\n",
420 bectl_cmd_unmount(int argc, char *argv[])
425 /* Store alias used */
429 while ((opt = getopt(argc, argv, "f")) != -1) {
432 flags |= BE_MNT_FORCE;
435 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
437 return (usage(false));
445 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
446 return (usage(false));
451 err = be_unmount(be, bootenv, flags);
457 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
465 main(int argc, char *argv[])
468 int command_index, rc;
471 fprintf(stderr, "missing command\n");
472 return (usage(false));
477 /* Handle command aliases */
478 if (strcmp(command, "umount") == 0)
481 if (strcmp(command, "ujail") == 0)
484 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
485 return (usage(true));
487 if (get_cmd_index(command, &command_index)) {
488 fprintf(stderr, "unknown command: %s\n", command);
489 return (usage(false));
493 if ((be = libbe_init()) == NULL)
496 libbe_print_on_error(be, true);
498 /* XXX TODO: can be simplified if offset by 2 instead of one */
499 rc = command_map[command_index].fn(argc-1, argv+1);