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 [-bU] [{-o key=value | -u key}]... beName\n"
82 "\t [utility [argument ...]]\n"
83 "\tbectl list [-aDHs] [{-c property | -C property}]\n"
84 "\tbectl mount beName [mountpoint]\n"
85 "\tbectl rename origBeName newBeName\n"
86 "\tbectl {ujail | unjail} {jailID | jailName | beName}\n"
87 "\tbectl {umount | unmount} [-f] beName\n");
89 return (explicit ? 0 : EX_USAGE);
94 * Represents a relationship between the command name and the parser action
97 struct command_map_entry {
99 int (*fn)(int argc, char *argv[]);
100 /* True if libbe_print_on_error should be disabled */
104 static struct command_map_entry command_map[] =
106 { "activate", bectl_cmd_activate,false },
107 { "create", bectl_cmd_create, false },
108 { "destroy", bectl_cmd_destroy, false },
109 { "export", bectl_cmd_export, false },
110 { "import", bectl_cmd_import, false },
112 { "add", bectl_cmd_add, false },
114 { "jail", bectl_cmd_jail, false },
115 { "list", bectl_cmd_list, false },
116 { "mount", bectl_cmd_mount, false },
117 { "rename", bectl_cmd_rename, false },
118 { "unjail", bectl_cmd_unjail, false },
119 { "unmount", bectl_cmd_unmount, false },
120 { "check", bectl_cmd_check, true },
123 static struct command_map_entry *
124 get_cmd_info(const char *cmd)
128 for (i = 0; i < nitems(command_map); ++i) {
129 if (strcmp(cmd, command_map[i].command) == 0)
130 return (&command_map[i]);
138 bectl_cmd_activate(int argc, char *argv[])
144 while ((opt = getopt(argc, argv, "t")) != -1) {
150 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
152 return (usage(false));
160 fprintf(stderr, "bectl activate: wrong number of arguments\n");
161 return (usage(false));
165 /* activate logic goes here */
166 if ((err = be_activate(be, argv[0], temp)) != 0)
167 /* XXX TODO: more specific error msg based on err */
168 printf("Did not successfully activate boot environment %s\n",
171 printf("Successfully activated boot environment %s\n", argv[0]);
174 printf("for next boot\n");
181 * TODO: when only one arg is given, and it contains an "@" the this should
182 * create that snapshot
185 bectl_cmd_create(int argc, char *argv[])
187 char snapshot[BE_MAXPATHLEN];
188 char *atpos, *bootenv, *snapname;
194 while ((opt = getopt(argc, argv, "e:r")) != -1) {
203 fprintf(stderr, "bectl create: unknown option '-%c'\n",
205 return (usage(false));
213 fprintf(stderr, "bectl create: wrong number of arguments\n");
214 return (usage(false));
219 err = BE_ERR_SUCCESS;
220 if (strchr(bootenv, ' ') != NULL)
221 /* BE datasets with spaces are not bootable */
222 err = BE_ERR_INVALIDNAME;
223 else 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);
251 case BE_ERR_INVALIDNAME:
253 "bectl create: boot environment name must not contain spaces\n");
258 "Failed to create a snapshot '%s' of '%s'\n",
260 else if (snapname == NULL)
262 "Failed to create bootenv %s\n", bootenv);
265 "Failed to create bootenv %s from snapshot %s\n",
274 bectl_cmd_export(int argc, char *argv[])
279 fprintf(stderr, "bectl export: missing boot environment name\n");
280 return (usage(false));
284 fprintf(stderr, "bectl export: extra arguments provided\n");
285 return (usage(false));
290 if (isatty(STDOUT_FILENO)) {
291 fprintf(stderr, "bectl export: must redirect output\n");
295 be_export(be, bootenv, STDOUT_FILENO);
302 bectl_cmd_import(int argc, char *argv[])
308 fprintf(stderr, "bectl import: missing boot environment name\n");
309 return (usage(false));
313 fprintf(stderr, "bectl import: extra arguments provided\n");
314 return (usage(false));
319 if (isatty(STDIN_FILENO)) {
320 fprintf(stderr, "bectl import: input can not be from terminal\n");
324 err = be_import(be, bootenv, STDIN_FILENO);
331 bectl_cmd_add(int argc, char *argv[])
335 fprintf(stderr, "bectl add: must provide at least one path\n");
336 return (usage(false));
339 for (int i = 1; i < argc; ++i) {
340 printf("arg %d: %s\n", i, argv[i]);
341 /* XXX TODO catch err */
342 be_add_child(be, argv[i], true);
350 bectl_cmd_destroy(int argc, char *argv[])
353 char *origin, *target, targetds[BE_MAXPATHLEN];
357 while ((opt = getopt(argc, argv, "Fo")) != -1) {
360 flags |= BE_DESTROY_FORCE;
363 flags |= BE_DESTROY_ORIGIN;
366 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
368 return (usage(false));
376 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
377 return (usage(false));
382 /* We'll emit a notice if there's an origin to be cleaned up */
383 if ((flags & BE_DESTROY_ORIGIN) == 0 && strchr(target, '@') == NULL) {
384 flags |= BE_DESTROY_AUTOORIGIN;
385 if (be_root_concat(be, target, targetds) != 0)
387 if (be_prop_list_alloc(&props) != 0)
389 if (be_get_dataset_props(be, targetds, props) != 0) {
390 be_prop_list_free(props);
393 if (nvlist_lookup_string(props, "origin", &origin) == 0 &&
394 !be_is_auto_snapshot_name(be, origin))
395 fprintf(stderr, "bectl destroy: leaving origin '%s' intact\n",
397 be_prop_list_free(props);
401 err = be_destroy(be, target, flags);
407 bectl_cmd_mount(int argc, char *argv[])
409 char result_loc[BE_MAXPATHLEN];
410 char *bootenv, *mountpoint;
413 /* XXX TODO: Allow shallow */
414 mntflags = BE_MNT_DEEP;
416 fprintf(stderr, "bectl mount: missing argument(s)\n");
417 return (usage(false));
421 fprintf(stderr, "bectl mount: too many arguments\n");
422 return (usage(false));
426 mountpoint = ((argc == 3) ? argv[2] : NULL);
428 err = be_mount(be, bootenv, mountpoint, mntflags, result_loc);
432 printf("Successfully mounted %s at %s\n", bootenv, result_loc);
436 (argc == 3) ? "Failed to mount bootenv %s at %s\n" :
437 "Failed to mount bootenv %s at temporary path %s\n",
438 bootenv, mountpoint);
446 bectl_cmd_rename(int argc, char *argv[])
452 fprintf(stderr, "bectl rename: missing argument\n");
453 return (usage(false));
457 fprintf(stderr, "bectl rename: too many arguments\n");
458 return (usage(false));
464 err = be_rename(be, src, dest);
470 fprintf(stderr, "Failed to rename bootenv %s to %s\n",
478 bectl_cmd_unmount(int argc, char *argv[])
483 /* Store alias used */
487 while ((opt = getopt(argc, argv, "f")) != -1) {
490 flags |= BE_MNT_FORCE;
493 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
495 return (usage(false));
503 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
504 return (usage(false));
509 err = be_unmount(be, bootenv, flags);
515 fprintf(stderr, "Failed to unmount bootenv %s\n", bootenv);
522 bectl_cmd_check(int argc, char *argv[] __unused)
525 /* The command is left as argv[0] */
527 fprintf(stderr, "bectl check: wrong number of arguments\n");
528 return (usage(false));
535 main(int argc, char *argv[])
537 struct command_map_entry *cmd;
545 return (usage(false));
547 if (strcmp(argv[1], "-r") == 0) {
549 return (usage(false));
550 root = strdup(argv[2]);
560 /* Handle command aliases */
561 if (strcmp(command, "umount") == 0)
564 if (strcmp(command, "ujail") == 0)
567 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
568 return (usage(true));
570 if ((cmd = get_cmd_info(command)) == NULL) {
571 fprintf(stderr, "Unknown command: %s\n", command);
572 return (usage(false));
575 if ((be = libbe_init(root)) == NULL)
578 libbe_print_on_error(be, !cmd->silent);
580 rc = cmd->fn(argc, argv);