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/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/mount.h>
49 static int bectl_cmd_activate(int argc, char *argv[]);
50 static int bectl_cmd_create(int argc, char *argv[]);
51 static int bectl_cmd_destroy(int argc, char *argv[]);
52 static int bectl_cmd_export(int argc, char *argv[]);
53 static int bectl_cmd_import(int argc, char *argv[]);
55 static int bectl_cmd_add(int argc, char *argv[]);
57 static int bectl_cmd_mount(int argc, char *argv[]);
58 static int bectl_cmd_rename(int argc, char *argv[]);
59 static int bectl_cmd_unmount(int argc, char *argv[]);
68 fp = explicit ? stdout : stderr;
70 "usage:\tbectl {-h | -? | subcommand [args...]}\n"
71 "\tbectl activate [-t] beName\n"
72 "\tbectl create [-e {nonActiveBe | -e beName@snapshot}] beName\n"
73 "\tbectl create beName@snapshot\n"
74 "\tbectl destroy [-F] {beName | beName@snapshot}\n"
75 "\tbectl export sourceBe\n"
76 "\tbectl import targetBe\n"
78 "\tbectl add (path)*\n"
80 "\tbectl jail [{-b | -U}] [{-o key=value | -u key}]... bootenv [utility [argument ...]]\n"
81 "\tbectl list [-a] [-D] [-H] [-s]\n"
82 "\tbectl mount beName [mountpoint]\n"
83 "\tbectl rename origBeName newBeName\n"
84 "\tbectl {ujail | unjail} ⟨jailID | jailName | bootenv)\n"
85 "\tbectl {umount | unmount} [-f] beName\n");
87 return (explicit ? 0 : EX_USAGE);
92 * Represents a relationship between the command name and the parser action
95 struct command_map_entry {
97 int (*fn)(int argc, char *argv[]);
100 static struct command_map_entry command_map[] =
102 { "activate", bectl_cmd_activate },
103 { "create", bectl_cmd_create },
104 { "destroy", bectl_cmd_destroy },
105 { "export", bectl_cmd_export },
106 { "import", bectl_cmd_import },
108 { "add", bectl_cmd_add },
110 { "jail", bectl_cmd_jail },
111 { "list", bectl_cmd_list },
112 { "mount", bectl_cmd_mount },
113 { "rename", bectl_cmd_rename },
114 { "unjail", bectl_cmd_unjail },
115 { "unmount", bectl_cmd_unmount },
119 get_cmd_index(const char *cmd, int *idx)
123 map_size = nitems(command_map);
124 for (int i = 0; i < map_size; ++i) {
125 if (strcmp(cmd, command_map[i].command) == 0) {
136 bectl_cmd_activate(int argc, char *argv[])
142 while ((opt = getopt(argc, argv, "t")) != -1) {
148 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
150 return (usage(false));
158 fprintf(stderr, "bectl activate: wrong number of arguments\n");
159 return (usage(false));
163 /* activate logic goes here */
164 if ((err = be_activate(be, argv[0], temp)) != 0)
165 /* XXX TODO: more specific error msg based on err */
166 printf("did not successfully activate boot environment %s\n",
169 printf("successfully activated boot environment %s\n", argv[0]);
172 printf("for next boot\n");
179 * TODO: when only one arg is given, and it contains an "@" the this should
180 * create that snapshot
183 bectl_cmd_create(int argc, char *argv[])
185 char *atpos, *bootenv, *snapname, *source;
191 while ((opt = getopt(argc, argv, "e:r")) != -1) {
200 fprintf(stderr, "bectl create: unknown option '-%c'\n",
202 return (usage(false));
210 fprintf(stderr, "bectl create: wrong number of arguments\n");
211 return (usage(false));
215 if ((atpos = strchr(bootenv, '@')) != NULL) {
217 * This is the "create a snapshot variant". No new boot
218 * environment is to be created here.
221 err = be_snapshot(be, bootenv, atpos, recursive, NULL);
222 } else if (snapname != NULL) {
223 if (strchr(snapname, '@') != NULL)
224 err = be_create_from_existing_snap(be, bootenv,
227 err = be_create_from_existing(be, bootenv, snapname);
229 if ((snapname = strchr(bootenv, '@')) != NULL) {
230 *(snapname++) = '\0';
231 if ((err = be_snapshot(be, be_active_path(be),
232 snapname, true, NULL)) != BE_ERR_SUCCESS)
233 fprintf(stderr, "failed to create snapshot\n");
234 asprintf(&source, "%s@%s", be_active_path(be), snapname);
235 err = be_create_from_existing_snap(be, bootenv,
239 err = be_create(be, bootenv);
248 "failed to create a snapshot '%s' of '%s'\n",
250 else if (snapname == NULL)
252 "failed to create bootenv %s\n", bootenv);
255 "failed to create bootenv %s from snapshot %s\n",
264 bectl_cmd_export(int argc, char *argv[])
269 fprintf(stderr, "bectl export: missing boot environment name\n");
270 return (usage(false));
274 fprintf(stderr, "bectl export: extra arguments provided\n");
275 return (usage(false));
280 if (isatty(STDOUT_FILENO)) {
281 fprintf(stderr, "bectl export: must redirect output\n");
285 be_export(be, bootenv, STDOUT_FILENO);
292 bectl_cmd_import(int argc, char *argv[])
298 fprintf(stderr, "bectl import: missing boot environment name\n");
299 return (usage(false));
303 fprintf(stderr, "bectl import: extra arguments provided\n");
304 return (usage(false));
309 if (isatty(STDIN_FILENO)) {
310 fprintf(stderr, "bectl import: input can not be from terminal\n");
314 err = be_import(be, bootenv, STDIN_FILENO);
321 bectl_cmd_add(int argc, char *argv[])
325 fprintf(stderr, "bectl add: must provide at least one path\n");
326 return (usage(false));
329 for (int i = 1; i < argc; ++i) {
330 printf("arg %d: %s\n", i, argv[i]);
331 /* XXX TODO catch err */
332 be_add_child(be, argv[i], true);
340 bectl_cmd_destroy(int argc, char *argv[])
347 while ((opt = getopt(argc, argv, "F")) != -1) {
353 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
355 return (usage(false));
363 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
364 return (usage(false));
369 err = be_destroy(be, target, force);
375 bectl_cmd_mount(int argc, char *argv[])
377 char result_loc[BE_MAXPATHLEN];
378 char *bootenv, *mountpoint;
382 fprintf(stderr, "bectl mount: missing argument(s)\n");
383 return (usage(false));
387 fprintf(stderr, "bectl mount: too many arguments\n");
388 return (usage(false));
392 mountpoint = ((argc == 3) ? argv[2] : NULL);
394 err = be_mount(be, bootenv, mountpoint, 0, result_loc);
398 printf("successfully mounted %s at %s\n", bootenv, result_loc);
402 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
403 "failed to mount bootenv %s at temporary path %s\n",
404 bootenv, mountpoint);
412 bectl_cmd_rename(int argc, char *argv[])
418 fprintf(stderr, "bectl rename: missing argument\n");
419 return (usage(false));
423 fprintf(stderr, "bectl rename: too many arguments\n");
424 return (usage(false));
430 err = be_rename(be, src, dest);
436 fprintf(stderr, "failed to rename bootenv %s to %s\n",
444 bectl_cmd_unmount(int argc, char *argv[])
449 /* Store alias used */
453 while ((opt = getopt(argc, argv, "f")) != -1) {
456 flags |= BE_MNT_FORCE;
459 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
461 return (usage(false));
469 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
470 return (usage(false));
475 err = be_unmount(be, bootenv, flags);
481 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
489 main(int argc, char *argv[])
492 int command_index, rc;
495 return (usage(false));
499 /* Handle command aliases */
500 if (strcmp(command, "umount") == 0)
503 if (strcmp(command, "ujail") == 0)
506 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
507 return (usage(true));
509 if (get_cmd_index(command, &command_index)) {
510 fprintf(stderr, "unknown command: %s\n", command);
511 return (usage(false));
515 if ((be = libbe_init()) == NULL)
518 libbe_print_on_error(be, true);
520 /* XXX TODO: can be simplified if offset by 2 instead of one */
521 rc = command_map[command_index].fn(argc-1, argv+1);