2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/mount.h>
48 static int bectl_cmd_activate(int argc, char *argv[]);
49 static int bectl_cmd_check(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[]);
70 fp = explicit ? stdout : stderr;
72 "Usage:\tbectl {-h | -? | subcommand [args...]}\n"
74 "\tbectl add (path)*\n"
76 "\tbectl activate [-t] beName\n"
78 "\tbectl create [-r] [-e {nonActiveBe | beName@snapshot}] beName\n"
79 "\tbectl create [-r] beName@snapshot\n"
80 "\tbectl destroy [-F] {beName | beName@snapshot}\n"
81 "\tbectl export sourceBe\n"
82 "\tbectl import targetBe\n"
83 "\tbectl jail {-b | -U} [{-o key=value | -u key}]... "
84 "{jailID | jailName}\n"
85 "\t bootenv [utility [argument ...]]\n"
86 "\tbectl list [-DHas] [{-c property | -C property}]\n"
87 "\tbectl mount beName [mountpoint]\n"
88 "\tbectl rename origBeName newBeName\n"
89 "\tbectl {ujail | unjail} {jailID | jailName} bootenv\n"
90 "\tbectl {umount | unmount} [-f] beName\n");
92 return (explicit ? 0 : EX_USAGE);
97 * Represents a relationship between the command name and the parser action
100 struct command_map_entry {
102 int (*fn)(int argc, char *argv[]);
103 /* True if libbe_print_on_error should be disabled */
107 static struct command_map_entry command_map[] =
109 { "activate", bectl_cmd_activate,false },
110 { "create", bectl_cmd_create, false },
111 { "destroy", bectl_cmd_destroy, false },
112 { "export", bectl_cmd_export, false },
113 { "import", bectl_cmd_import, false },
115 { "add", bectl_cmd_add, false },
117 { "jail", bectl_cmd_jail, false },
118 { "list", bectl_cmd_list, false },
119 { "mount", bectl_cmd_mount, false },
120 { "rename", bectl_cmd_rename, false },
121 { "unjail", bectl_cmd_unjail, false },
122 { "unmount", bectl_cmd_unmount, false },
123 { "check", bectl_cmd_check, true },
126 static struct command_map_entry *
127 get_cmd_info(const char *cmd)
131 for (i = 0; i < nitems(command_map); ++i) {
132 if (strcmp(cmd, command_map[i].command) == 0)
133 return (&command_map[i]);
141 bectl_cmd_activate(int argc, char *argv[])
147 while ((opt = getopt(argc, argv, "t")) != -1) {
153 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
155 return (usage(false));
163 fprintf(stderr, "bectl activate: wrong number of arguments\n");
164 return (usage(false));
168 /* activate logic goes here */
169 if ((err = be_activate(be, argv[0], temp)) != 0)
170 /* XXX TODO: more specific error msg based on err */
171 printf("Did not successfully activate boot environment %s\n",
174 printf("Successfully activated boot environment %s\n", argv[0]);
177 printf("for next boot\n");
184 * TODO: when only one arg is given, and it contains an "@" the this should
185 * create that snapshot
188 bectl_cmd_create(int argc, char *argv[])
190 char snapshot[BE_MAXPATHLEN];
191 char *atpos, *bootenv, *snapname;
197 while ((opt = getopt(argc, argv, "e:r")) != -1) {
206 fprintf(stderr, "bectl create: unknown option '-%c'\n",
208 return (usage(false));
216 fprintf(stderr, "bectl create: wrong number of arguments\n");
217 return (usage(false));
222 err = BE_ERR_SUCCESS;
223 if ((atpos = strchr(bootenv, '@')) != NULL) {
225 * This is the "create a snapshot variant". No new boot
226 * environment is to be created here.
229 err = be_snapshot(be, bootenv, atpos, recursive, NULL);
231 if (snapname == NULL)
232 /* Create from currently booted BE */
233 err = be_snapshot(be, be_active_path(be), NULL,
234 recursive, snapshot);
235 else if (strchr(snapname, '@') != NULL)
236 /* Create from given snapshot */
237 strlcpy(snapshot, snapname, sizeof(snapshot));
239 /* Create from given BE */
240 err = be_snapshot(be, snapname, NULL, recursive,
243 if (err == BE_ERR_SUCCESS)
244 err = be_create_depth(be, bootenv, snapshot,
245 recursive == true ? -1 : 0);
254 "Failed to create a snapshot '%s' of '%s'\n",
256 else if (snapname == NULL)
258 "Failed to create bootenv %s\n", bootenv);
261 "Failed to create bootenv %s from snapshot %s\n",
270 bectl_cmd_export(int argc, char *argv[])
275 fprintf(stderr, "bectl export: missing boot environment name\n");
276 return (usage(false));
280 fprintf(stderr, "bectl export: extra arguments provided\n");
281 return (usage(false));
286 if (isatty(STDOUT_FILENO)) {
287 fprintf(stderr, "bectl export: must redirect output\n");
291 be_export(be, bootenv, STDOUT_FILENO);
298 bectl_cmd_import(int argc, char *argv[])
304 fprintf(stderr, "bectl import: missing boot environment name\n");
305 return (usage(false));
309 fprintf(stderr, "bectl import: extra arguments provided\n");
310 return (usage(false));
315 if (isatty(STDIN_FILENO)) {
316 fprintf(stderr, "bectl import: input can not be from terminal\n");
320 err = be_import(be, bootenv, STDIN_FILENO);
327 bectl_cmd_add(int argc, char *argv[])
331 fprintf(stderr, "bectl add: must provide at least one path\n");
332 return (usage(false));
335 for (int i = 1; i < argc; ++i) {
336 printf("arg %d: %s\n", i, argv[i]);
337 /* XXX TODO catch err */
338 be_add_child(be, argv[i], true);
346 bectl_cmd_destroy(int argc, char *argv[])
349 char *origin, *target, targetds[BE_MAXPATHLEN];
353 while ((opt = getopt(argc, argv, "Fo")) != -1) {
356 flags |= BE_DESTROY_FORCE;
359 flags |= BE_DESTROY_ORIGIN;
362 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
364 return (usage(false));
372 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
373 return (usage(false));
378 /* We'll emit a notice if there's an origin to be cleaned up */
379 if ((flags & BE_DESTROY_ORIGIN) == 0 && strchr(target, '@') == NULL) {
380 flags |= BE_DESTROY_AUTOORIGIN;
381 if (be_root_concat(be, target, targetds) != 0)
383 if (be_prop_list_alloc(&props) != 0)
385 if (be_get_dataset_props(be, targetds, props) != 0) {
386 be_prop_list_free(props);
389 if (nvlist_lookup_string(props, "origin", &origin) == 0 &&
390 !be_is_auto_snapshot_name(be, origin))
391 fprintf(stderr, "bectl destroy: leaving origin '%s' intact\n",
393 be_prop_list_free(props);
397 err = be_destroy(be, target, flags);
403 bectl_cmd_mount(int argc, char *argv[])
405 char result_loc[BE_MAXPATHLEN];
406 char *bootenv, *mountpoint;
409 /* XXX TODO: Allow shallow */
410 mntflags = BE_MNT_DEEP;
412 fprintf(stderr, "bectl mount: missing argument(s)\n");
413 return (usage(false));
417 fprintf(stderr, "bectl mount: too many arguments\n");
418 return (usage(false));
422 mountpoint = ((argc == 3) ? argv[2] : NULL);
424 err = be_mount(be, bootenv, mountpoint, mntflags, result_loc);
428 printf("Successfully mounted %s at %s\n", bootenv, result_loc);
432 (argc == 3) ? "Failed to mount bootenv %s at %s\n" :
433 "Failed to mount bootenv %s at temporary path %s\n",
434 bootenv, mountpoint);
442 bectl_cmd_rename(int argc, char *argv[])
448 fprintf(stderr, "bectl rename: missing argument\n");
449 return (usage(false));
453 fprintf(stderr, "bectl rename: too many arguments\n");
454 return (usage(false));
460 err = be_rename(be, src, dest);
466 fprintf(stderr, "Failed to rename bootenv %s to %s\n",
474 bectl_cmd_unmount(int argc, char *argv[])
479 /* Store alias used */
483 while ((opt = getopt(argc, argv, "f")) != -1) {
486 flags |= BE_MNT_FORCE;
489 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
491 return (usage(false));
499 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
500 return (usage(false));
505 err = be_unmount(be, bootenv, flags);
511 fprintf(stderr, "Failed to unmount bootenv %s\n", bootenv);
518 bectl_cmd_check(int argc, char *argv[] __unused)
521 /* The command is left as argv[0] */
523 fprintf(stderr, "bectl check: wrong number of arguments\n");
524 return (usage(false));
531 main(int argc, char *argv[])
533 struct command_map_entry *cmd;
541 return (usage(false));
543 if (strcmp(argv[1], "-r") == 0) {
545 return (usage(false));
546 root = strdup(argv[2]);
556 /* Handle command aliases */
557 if (strcmp(command, "umount") == 0)
560 if (strcmp(command, "ujail") == 0)
563 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
564 return (usage(true));
566 if ((cmd = get_cmd_info(command)) == NULL) {
567 fprintf(stderr, "Unknown command: %s\n", command);
568 return (usage(false));
571 if ((be = libbe_init(root)) == NULL)
574 libbe_print_on_error(be, !cmd->silent);
576 rc = cmd->fn(argc, argv);