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"
72 "\tbectl add (path)*\n"
74 "\tbectl activate [-t] beName\n"
75 "\tbectl create [-r] [-e {nonActiveBe | beName@snapshot}] beName\n"
76 "\tbectl create [-r] beName@snapshot\n"
77 "\tbectl destroy [-F] {beName | beName@snapshot}\n"
78 "\tbectl export sourceBe\n"
79 "\tbectl import targetBe\n"
80 "\tbectl jail {-b | -U} [{-o key=value | -u key}]... "
81 "{jailID | jailName}\n"
82 "\t bootenv [utility [argument ...]]\n"
83 "\tbectl list [-DHas] [{-c property | -C property}]\n"
84 "\tbectl mount beName [mountpoint]\n"
85 "\tbectl rename origBeName newBeName\n"
86 "\tbectl {ujail | unjail} {jailID | jailName} bootenv\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[]);
102 static struct command_map_entry command_map[] =
104 { "activate", bectl_cmd_activate },
105 { "create", bectl_cmd_create },
106 { "destroy", bectl_cmd_destroy },
107 { "export", bectl_cmd_export },
108 { "import", bectl_cmd_import },
110 { "add", bectl_cmd_add },
112 { "jail", bectl_cmd_jail },
113 { "list", bectl_cmd_list },
114 { "mount", bectl_cmd_mount },
115 { "rename", bectl_cmd_rename },
116 { "unjail", bectl_cmd_unjail },
117 { "unmount", bectl_cmd_unmount },
121 get_cmd_index(const char *cmd, int *idx)
125 map_size = nitems(command_map);
126 for (int i = 0; i < map_size; ++i) {
127 if (strcmp(cmd, command_map[i].command) == 0) {
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 ((atpos = strchr(bootenv, '@')) != NULL) {
222 * This is the "create a snapshot variant". No new boot
223 * environment is to be created here.
226 err = be_snapshot(be, bootenv, atpos, recursive, NULL);
228 if (snapname == NULL)
229 /* Create from currently booted BE */
230 err = be_snapshot(be, be_active_path(be), NULL,
231 recursive, snapshot);
232 else if (strchr(snapname, '@') != NULL)
233 /* Create from given snapshot */
234 strlcpy(snapshot, snapname, sizeof(snapshot));
236 /* Create from given BE */
237 err = be_snapshot(be, snapname, NULL, recursive,
240 if (err == BE_ERR_SUCCESS)
241 err = be_create_depth(be, bootenv, snapshot,
242 recursive == true ? -1 : 0);
251 "failed to create a snapshot '%s' of '%s'\n",
253 else if (snapname == NULL)
255 "failed to create bootenv %s\n", bootenv);
258 "failed to create bootenv %s from snapshot %s\n",
267 bectl_cmd_export(int argc, char *argv[])
272 fprintf(stderr, "bectl export: missing boot environment name\n");
273 return (usage(false));
277 fprintf(stderr, "bectl export: extra arguments provided\n");
278 return (usage(false));
283 if (isatty(STDOUT_FILENO)) {
284 fprintf(stderr, "bectl export: must redirect output\n");
288 be_export(be, bootenv, STDOUT_FILENO);
295 bectl_cmd_import(int argc, char *argv[])
301 fprintf(stderr, "bectl import: missing boot environment name\n");
302 return (usage(false));
306 fprintf(stderr, "bectl import: extra arguments provided\n");
307 return (usage(false));
312 if (isatty(STDIN_FILENO)) {
313 fprintf(stderr, "bectl import: input can not be from terminal\n");
317 err = be_import(be, bootenv, STDIN_FILENO);
324 bectl_cmd_add(int argc, char *argv[])
328 fprintf(stderr, "bectl add: must provide at least one path\n");
329 return (usage(false));
332 for (int i = 1; i < argc; ++i) {
333 printf("arg %d: %s\n", i, argv[i]);
334 /* XXX TODO catch err */
335 be_add_child(be, argv[i], true);
343 bectl_cmd_destroy(int argc, char *argv[])
346 char *origin, *target, targetds[BE_MAXPATHLEN];
350 while ((opt = getopt(argc, argv, "Fo")) != -1) {
353 flags |= BE_DESTROY_FORCE;
356 flags |= BE_DESTROY_ORIGIN;
359 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
361 return (usage(false));
369 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
370 return (usage(false));
375 /* We'll emit a notice if there's an origin to be cleaned up */
376 if ((flags & BE_DESTROY_ORIGIN) == 0 && strchr(target, '@') == NULL) {
377 if (be_root_concat(be, target, targetds) != 0)
379 if (be_prop_list_alloc(&props) != 0)
381 if (be_get_dataset_props(be, targetds, props) != 0) {
382 be_prop_list_free(props);
385 if (nvlist_lookup_string(props, "origin", &origin) == 0)
386 fprintf(stderr, "bectl destroy: leaving origin '%s' intact\n",
388 be_prop_list_free(props);
392 err = be_destroy(be, target, flags);
398 bectl_cmd_mount(int argc, char *argv[])
400 char result_loc[BE_MAXPATHLEN];
401 char *bootenv, *mountpoint;
404 /* XXX TODO: Allow shallow */
405 mntflags = BE_MNT_DEEP;
407 fprintf(stderr, "bectl mount: missing argument(s)\n");
408 return (usage(false));
412 fprintf(stderr, "bectl mount: too many arguments\n");
413 return (usage(false));
417 mountpoint = ((argc == 3) ? argv[2] : NULL);
419 err = be_mount(be, bootenv, mountpoint, mntflags, result_loc);
423 printf("successfully mounted %s at %s\n", bootenv, result_loc);
427 (argc == 3) ? "failed to mount bootenv %s at %s\n" :
428 "failed to mount bootenv %s at temporary path %s\n",
429 bootenv, mountpoint);
437 bectl_cmd_rename(int argc, char *argv[])
443 fprintf(stderr, "bectl rename: missing argument\n");
444 return (usage(false));
448 fprintf(stderr, "bectl rename: too many arguments\n");
449 return (usage(false));
455 err = be_rename(be, src, dest);
461 fprintf(stderr, "failed to rename bootenv %s to %s\n",
469 bectl_cmd_unmount(int argc, char *argv[])
474 /* Store alias used */
478 while ((opt = getopt(argc, argv, "f")) != -1) {
481 flags |= BE_MNT_FORCE;
484 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
486 return (usage(false));
494 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
495 return (usage(false));
500 err = be_unmount(be, bootenv, flags);
506 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
514 main(int argc, char *argv[])
518 int command_index, rc;
522 return (usage(false));
524 if (strcmp(argv[1], "-r") == 0) {
526 return (usage(false));
527 root = strdup(argv[2]);
537 /* Handle command aliases */
538 if (strcmp(command, "umount") == 0)
541 if (strcmp(command, "ujail") == 0)
544 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
545 return (usage(true));
547 if (get_cmd_index(command, &command_index)) {
548 fprintf(stderr, "unknown command: %s\n", command);
549 return (usage(false));
553 if ((be = libbe_init(root)) == NULL)
556 libbe_print_on_error(be, true);
558 rc = command_map[command_index].fn(argc, argv);