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[]);
68 fp = explicit ? stdout : stderr;
70 "Usage:\tbectl {-h | -? | subcommand [args...]}\n"
72 "\tbectl add (path)*\n"
74 "\tbectl activate [-t] beName\n"
76 "\tbectl create [-r] [-e {nonActiveBe | beName@snapshot}] beName\n"
77 "\tbectl create [-r] beName@snapshot\n"
78 "\tbectl destroy [-F] {beName | beName@snapshot}\n"
79 "\tbectl export sourceBe\n"
80 "\tbectl import targetBe\n"
81 "\tbectl jail {-b | -U} [{-o key=value | -u key}]... "
82 "{jailID | jailName}\n"
83 "\t bootenv [utility [argument ...]]\n"
84 "\tbectl list [-DHas] [{-c property | -C property}]\n"
85 "\tbectl mount beName [mountpoint]\n"
86 "\tbectl rename origBeName newBeName\n"
87 "\tbectl {ujail | unjail} {jailID | jailName} bootenv\n"
88 "\tbectl {umount | unmount} [-f] beName\n");
90 return (explicit ? 0 : EX_USAGE);
95 * Represents a relationship between the command name and the parser action
98 struct command_map_entry {
100 int (*fn)(int argc, char *argv[]);
101 /* True if libbe_print_on_error should be disabled */
105 static struct command_map_entry command_map[] =
107 { "activate", bectl_cmd_activate,false },
108 { "create", bectl_cmd_create, false },
109 { "destroy", bectl_cmd_destroy, false },
110 { "export", bectl_cmd_export, false },
111 { "import", bectl_cmd_import, false },
113 { "add", bectl_cmd_add, false },
115 { "jail", bectl_cmd_jail, false },
116 { "list", bectl_cmd_list, false },
117 { "mount", bectl_cmd_mount, false },
118 { "rename", bectl_cmd_rename, false },
119 { "unjail", bectl_cmd_unjail, false },
120 { "unmount", bectl_cmd_unmount, false },
121 { "check", bectl_cmd_check, true },
124 static struct command_map_entry *
125 get_cmd_info(const char *cmd)
129 for (i = 0; i < nitems(command_map); ++i) {
130 if (strcmp(cmd, command_map[i].command) == 0)
131 return (&command_map[i]);
139 bectl_cmd_activate(int argc, char *argv[])
145 while ((opt = getopt(argc, argv, "t")) != -1) {
151 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
153 return (usage(false));
161 fprintf(stderr, "bectl activate: wrong number of arguments\n");
162 return (usage(false));
166 /* activate logic goes here */
167 if ((err = be_activate(be, argv[0], temp)) != 0)
168 /* XXX TODO: more specific error msg based on err */
169 printf("Did not successfully activate boot environment %s\n",
172 printf("Successfully activated boot environment %s\n", argv[0]);
175 printf("for next boot\n");
182 * TODO: when only one arg is given, and it contains an "@" the this should
183 * create that snapshot
186 bectl_cmd_create(int argc, char *argv[])
188 char snapshot[BE_MAXPATHLEN];
189 char *atpos, *bootenv, *snapname;
195 while ((opt = getopt(argc, argv, "e:r")) != -1) {
204 fprintf(stderr, "bectl create: unknown option '-%c'\n",
206 return (usage(false));
214 fprintf(stderr, "bectl create: wrong number of arguments\n");
215 return (usage(false));
220 err = BE_ERR_SUCCESS;
221 if ((atpos = strchr(bootenv, '@')) != NULL) {
223 * This is the "create a snapshot variant". No new boot
224 * environment is to be created here.
227 err = be_snapshot(be, bootenv, atpos, recursive, NULL);
229 if (snapname == NULL)
230 /* Create from currently booted BE */
231 err = be_snapshot(be, be_active_path(be), NULL,
232 recursive, snapshot);
233 else if (strchr(snapname, '@') != NULL)
234 /* Create from given snapshot */
235 strlcpy(snapshot, snapname, sizeof(snapshot));
237 /* Create from given BE */
238 err = be_snapshot(be, snapname, NULL, recursive,
241 if (err == BE_ERR_SUCCESS)
242 err = be_create_depth(be, bootenv, snapshot,
243 recursive == true ? -1 : 0);
252 "Failed to create a snapshot '%s' of '%s'\n",
254 else if (snapname == NULL)
256 "Failed to create bootenv %s\n", bootenv);
259 "Failed to create bootenv %s from snapshot %s\n",
268 bectl_cmd_export(int argc, char *argv[])
273 fprintf(stderr, "bectl export: missing boot environment name\n");
274 return (usage(false));
278 fprintf(stderr, "bectl export: extra arguments provided\n");
279 return (usage(false));
284 if (isatty(STDOUT_FILENO)) {
285 fprintf(stderr, "bectl export: must redirect output\n");
289 be_export(be, bootenv, STDOUT_FILENO);
296 bectl_cmd_import(int argc, char *argv[])
302 fprintf(stderr, "bectl import: missing boot environment name\n");
303 return (usage(false));
307 fprintf(stderr, "bectl import: extra arguments provided\n");
308 return (usage(false));
313 if (isatty(STDIN_FILENO)) {
314 fprintf(stderr, "bectl import: input can not be from terminal\n");
318 err = be_import(be, bootenv, STDIN_FILENO);
325 bectl_cmd_add(int argc, char *argv[])
329 fprintf(stderr, "bectl add: must provide at least one path\n");
330 return (usage(false));
333 for (int i = 1; i < argc; ++i) {
334 printf("arg %d: %s\n", i, argv[i]);
335 /* XXX TODO catch err */
336 be_add_child(be, argv[i], true);
344 bectl_cmd_destroy(int argc, char *argv[])
347 char *origin, *target, targetds[BE_MAXPATHLEN];
351 while ((opt = getopt(argc, argv, "Fo")) != -1) {
354 flags |= BE_DESTROY_FORCE;
357 flags |= BE_DESTROY_ORIGIN;
360 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
362 return (usage(false));
370 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
371 return (usage(false));
376 /* We'll emit a notice if there's an origin to be cleaned up */
377 if ((flags & BE_DESTROY_ORIGIN) == 0 && strchr(target, '@') == NULL) {
378 flags |= BE_DESTROY_AUTOORIGIN;
379 if (be_root_concat(be, target, targetds) != 0)
381 if (be_prop_list_alloc(&props) != 0)
383 if (be_get_dataset_props(be, targetds, props) != 0) {
384 be_prop_list_free(props);
387 if (nvlist_lookup_string(props, "origin", &origin) == 0 &&
388 !be_is_auto_snapshot_name(be, origin))
389 fprintf(stderr, "bectl destroy: leaving origin '%s' intact\n",
391 be_prop_list_free(props);
395 err = be_destroy(be, target, flags);
401 bectl_cmd_mount(int argc, char *argv[])
403 char result_loc[BE_MAXPATHLEN];
404 char *bootenv, *mountpoint;
407 /* XXX TODO: Allow shallow */
408 mntflags = BE_MNT_DEEP;
410 fprintf(stderr, "bectl mount: missing argument(s)\n");
411 return (usage(false));
415 fprintf(stderr, "bectl mount: too many arguments\n");
416 return (usage(false));
420 mountpoint = ((argc == 3) ? argv[2] : NULL);
422 err = be_mount(be, bootenv, mountpoint, mntflags, result_loc);
426 printf("Successfully mounted %s at %s\n", bootenv, result_loc);
430 (argc == 3) ? "Failed to mount bootenv %s at %s\n" :
431 "Failed to mount bootenv %s at temporary path %s\n",
432 bootenv, mountpoint);
440 bectl_cmd_rename(int argc, char *argv[])
446 fprintf(stderr, "bectl rename: missing argument\n");
447 return (usage(false));
451 fprintf(stderr, "bectl rename: too many arguments\n");
452 return (usage(false));
458 err = be_rename(be, src, dest);
464 fprintf(stderr, "Failed to rename bootenv %s to %s\n",
472 bectl_cmd_unmount(int argc, char *argv[])
477 /* Store alias used */
481 while ((opt = getopt(argc, argv, "f")) != -1) {
484 flags |= BE_MNT_FORCE;
487 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
489 return (usage(false));
497 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
498 return (usage(false));
503 err = be_unmount(be, bootenv, flags);
509 fprintf(stderr, "Failed to unmount bootenv %s\n", bootenv);
516 bectl_cmd_check(int argc, char *argv[] __unused)
519 /* The command is left as argv[0] */
521 fprintf(stderr, "bectl check: wrong number of arguments\n");
522 return (usage(false));
529 main(int argc, char *argv[])
531 struct command_map_entry *cmd;
539 return (usage(false));
541 if (strcmp(argv[1], "-r") == 0) {
543 return (usage(false));
544 root = strdup(argv[2]);
554 /* Handle command aliases */
555 if (strcmp(command, "umount") == 0)
558 if (strcmp(command, "ujail") == 0)
561 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
562 return (usage(true));
564 if ((cmd = get_cmd_info(command)) == NULL) {
565 fprintf(stderr, "Unknown command: %s\n", command);
566 return (usage(false));
569 if ((be = libbe_init(root)) == NULL)
572 libbe_print_on_error(be, !cmd->silent);
574 rc = cmd->fn(argc, argv);