2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
29 #include <sys/mount.h>
45 static int bectl_cmd_activate(int argc, char *argv[]);
46 static int bectl_cmd_check(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[]);
52 static int bectl_cmd_add(int argc, char *argv[]);
54 static int bectl_cmd_mount(int argc, char *argv[]);
55 static int bectl_cmd_rename(int argc, char *argv[]);
56 static int bectl_cmd_unmount(int argc, char *argv[]);
65 fp = explicit ? stdout : stderr;
67 "Usage:\tbectl {-h | -? | subcommand [args...]}\n"
69 "\tbectl [-r beroot] add (path)*\n"
71 "\tbectl [-r beroot] activate [-t] beName\n"
72 "\tbectl [-r beroot] activate [-T]\n"
73 "\tbectl [-r beroot] check\n"
74 "\tbectl [-r beroot] create [-r] [-e {nonActiveBe | beName@snapshot}] beName\n"
75 "\tbectl [-r beroot] create [-r] beName@snapshot\n"
76 "\tbectl [-r beroot] destroy [-Fo] {beName | beName@snapshot}\n"
77 "\tbectl [-r beroot] export sourceBe\n"
78 "\tbectl [-r beroot] import targetBe\n"
79 "\tbectl [-r beroot] jail [-bU] [{-o key=value | -u key}]... beName\n"
80 "\t [utility [argument ...]]\n"
81 "\tbectl [-r beroot] list [-aDHs] [{-c property | -C property}]\n"
82 "\tbectl [-r beroot] mount beName [mountpoint]\n"
83 "\tbectl [-r beroot] rename origBeName newBeName\n"
84 "\tbectl [-r beroot] {ujail | unjail} {jailID | jailName | beName}\n"
85 "\tbectl [-r beroot] {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[]);
98 /* True if libbe_print_on_error should be disabled */
102 static struct command_map_entry command_map[] =
104 { "activate", bectl_cmd_activate,false },
105 { "create", bectl_cmd_create, false },
106 { "destroy", bectl_cmd_destroy, false },
107 { "export", bectl_cmd_export, false },
108 { "import", bectl_cmd_import, false },
110 { "add", bectl_cmd_add, false },
112 { "jail", bectl_cmd_jail, false },
113 { "list", bectl_cmd_list, false },
114 { "mount", bectl_cmd_mount, false },
115 { "rename", bectl_cmd_rename, false },
116 { "unjail", bectl_cmd_unjail, false },
117 { "unmount", bectl_cmd_unmount, false },
118 { "check", bectl_cmd_check, true },
121 static struct command_map_entry *
122 get_cmd_info(const char *cmd)
126 for (i = 0; i < nitems(command_map); ++i) {
127 if (strcmp(cmd, command_map[i].command) == 0)
128 return (&command_map[i]);
135 bectl_cmd_activate(int argc, char *argv[])
142 while ((opt = getopt(argc, argv, "tT")) != -1) {
146 return (usage(false));
151 return (usage(false));
155 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
157 return (usage(false));
164 if (argc != 1 && (!reset || argc != 0)) {
165 fprintf(stderr, "bectl activate: wrong number of arguments\n");
166 return (usage(false));
170 if ((err = be_deactivate(be, NULL, reset)) == 0)
171 printf("Temporary activation removed\n");
173 printf("Failed to remove temporary activation\n");
177 /* activate logic goes here */
178 if ((err = be_activate(be, argv[0], temp)) != 0)
179 /* XXX TODO: more specific error msg based on err */
180 printf("Did not successfully activate boot environment %s\n",
183 printf("Successfully activated boot environment %s\n", argv[0]);
186 printf("for next boot\n");
193 * TODO: when only one arg is given, and it contains an "@" the this should
194 * create that snapshot
197 bectl_cmd_create(int argc, char *argv[])
199 char snapshot[BE_MAXPATHLEN];
200 char *atpos, *bootenv, *snapname;
206 while ((opt = getopt(argc, argv, "e:r")) != -1) {
215 fprintf(stderr, "bectl create: unknown option '-%c'\n",
217 return (usage(false));
225 fprintf(stderr, "bectl create: wrong number of arguments\n");
226 return (usage(false));
231 err = BE_ERR_SUCCESS;
232 if ((atpos = strchr(bootenv, '@')) != NULL) {
234 * This is the "create a snapshot variant". No new boot
235 * environment is to be created here.
238 err = be_snapshot(be, bootenv, atpos, recursive, NULL);
240 if (snapname == NULL)
241 /* Create from currently booted BE */
242 err = be_snapshot(be, be_active_path(be), NULL,
243 recursive, snapshot);
244 else if (strchr(snapname, '@') != NULL)
245 /* Create from given snapshot */
246 strlcpy(snapshot, snapname, sizeof(snapshot));
248 /* Create from given BE */
249 err = be_snapshot(be, snapname, NULL, recursive,
252 if (err == BE_ERR_SUCCESS)
253 err = be_create_depth(be, bootenv, snapshot,
254 recursive == true ? -1 : 0);
260 case BE_ERR_INVALIDNAME:
262 "bectl create: boot environment name must not contain spaces\n");
267 "Failed to create a snapshot '%s' of '%s'\n",
269 else if (snapname == NULL)
271 "Failed to create bootenv %s\n", bootenv);
274 "Failed to create bootenv %s from snapshot %s\n",
283 bectl_cmd_export(int argc, char *argv[])
288 fprintf(stderr, "bectl export: missing boot environment name\n");
289 return (usage(false));
293 fprintf(stderr, "bectl export: extra arguments provided\n");
294 return (usage(false));
299 if (isatty(STDOUT_FILENO)) {
300 fprintf(stderr, "bectl export: must redirect output\n");
304 be_export(be, bootenv, STDOUT_FILENO);
311 bectl_cmd_import(int argc, char *argv[])
317 fprintf(stderr, "bectl import: missing boot environment name\n");
318 return (usage(false));
322 fprintf(stderr, "bectl import: extra arguments provided\n");
323 return (usage(false));
328 if (isatty(STDIN_FILENO)) {
329 fprintf(stderr, "bectl import: input can not be from terminal\n");
333 err = be_import(be, bootenv, STDIN_FILENO);
340 bectl_cmd_add(int argc, char *argv[])
344 fprintf(stderr, "bectl add: must provide at least one path\n");
345 return (usage(false));
348 for (int i = 1; i < argc; ++i) {
349 printf("arg %d: %s\n", i, argv[i]);
350 /* XXX TODO catch err */
351 be_add_child(be, argv[i], true);
359 bectl_cmd_destroy(int argc, char *argv[])
362 char *target, targetds[BE_MAXPATHLEN];
367 while ((opt = getopt(argc, argv, "Fo")) != -1) {
370 flags |= BE_DESTROY_FORCE;
373 flags |= BE_DESTROY_ORIGIN;
376 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
378 return (usage(false));
386 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
387 return (usage(false));
392 /* We'll emit a notice if there's an origin to be cleaned up */
393 if ((flags & BE_DESTROY_ORIGIN) == 0 && strchr(target, '@') == NULL) {
394 flags |= BE_DESTROY_AUTOORIGIN;
395 if (be_root_concat(be, target, targetds) != 0)
397 if (be_prop_list_alloc(&props) != 0)
399 if (be_get_dataset_props(be, targetds, props) != 0) {
400 be_prop_list_free(props);
403 if (nvlist_lookup_string(props, "origin", &origin) == 0 &&
404 !be_is_auto_snapshot_name(be, origin))
405 fprintf(stderr, "bectl destroy: leaving origin '%s' intact\n",
407 be_prop_list_free(props);
411 err = be_destroy(be, target, flags);
417 bectl_cmd_mount(int argc, char *argv[])
419 char result_loc[BE_MAXPATHLEN];
420 char *bootenv, *mountpoint;
423 /* XXX TODO: Allow shallow */
424 mntflags = BE_MNT_DEEP;
426 fprintf(stderr, "bectl mount: missing argument(s)\n");
427 return (usage(false));
431 fprintf(stderr, "bectl mount: too many arguments\n");
432 return (usage(false));
436 mountpoint = ((argc == 3) ? argv[2] : NULL);
438 err = be_mount(be, bootenv, mountpoint, mntflags, result_loc);
442 printf("%s\n", result_loc);
446 (argc == 3) ? "Failed to mount bootenv %s at %s\n" :
447 "Failed to mount bootenv %s at temporary path %s\n",
448 bootenv, mountpoint);
456 bectl_cmd_rename(int argc, char *argv[])
462 fprintf(stderr, "bectl rename: missing argument\n");
463 return (usage(false));
467 fprintf(stderr, "bectl rename: too many arguments\n");
468 return (usage(false));
474 err = be_rename(be, src, dest);
479 fprintf(stderr, "Failed to rename bootenv %s to %s\n",
487 bectl_cmd_unmount(int argc, char *argv[])
492 /* Store alias used */
496 while ((opt = getopt(argc, argv, "f")) != -1) {
499 flags |= BE_MNT_FORCE;
502 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
504 return (usage(false));
512 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
513 return (usage(false));
518 err = be_unmount(be, bootenv, flags);
524 fprintf(stderr, "Failed to unmount bootenv %s\n", bootenv);
531 bectl_cmd_check(int argc, char *argv[] __unused)
534 /* The command is left as argv[0] */
536 fprintf(stderr, "bectl check: wrong number of arguments\n");
537 return (usage(false));
544 main(int argc, char *argv[])
546 struct command_map_entry *cmd;
554 return (usage(false));
556 if (strcmp(argv[1], "-r") == 0) {
558 return (usage(false));
559 root = strdup(argv[2]);
569 /* Handle command aliases */
570 if (strcmp(command, "umount") == 0)
573 if (strcmp(command, "ujail") == 0)
576 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
577 return (usage(true));
579 if ((cmd = get_cmd_info(command)) == NULL) {
580 fprintf(stderr, "Unknown command: %s\n", command);
581 return (usage(false));
584 if ((be = libbe_init(root)) == NULL) {
586 fprintf(stderr, "libbe_init(\"%s\") failed.\n",
587 root != NULL ? root : "");
591 libbe_print_on_error(be, !cmd->silent);
593 rc = cmd->fn(argc, argv);