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"
75 "\tbectl activate [-T]\n"
77 "\tbectl create [-r] [-e {nonActiveBe | beName@snapshot}] beName\n"
78 "\tbectl create [-r] beName@snapshot\n"
79 "\tbectl destroy [-Fo] {beName | beName@snapshot}\n"
80 "\tbectl export sourceBe\n"
81 "\tbectl import targetBe\n"
82 "\tbectl jail [-bU] [{-o key=value | -u key}]... beName\n"
83 "\t [utility [argument ...]]\n"
84 "\tbectl list [-aDHs] [{-c property | -C property}]\n"
85 "\tbectl mount beName [mountpoint]\n"
86 "\tbectl rename origBeName newBeName\n"
87 "\tbectl {ujail | unjail} {jailID | jailName | beName}\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[])
146 while ((opt = getopt(argc, argv, "tT")) != -1) {
150 return (usage(false));
155 return (usage(false));
159 fprintf(stderr, "bectl activate: unknown option '-%c'\n",
161 return (usage(false));
168 if (argc != 1 && (!reset || argc != 0)) {
169 fprintf(stderr, "bectl activate: wrong number of arguments\n");
170 return (usage(false));
174 if ((err = be_deactivate(be, NULL, reset)) == 0)
175 printf("Temporary activation removed\n");
177 printf("Failed to remove temporary activation\n");
181 /* activate logic goes here */
182 if ((err = be_activate(be, argv[0], temp)) != 0)
183 /* XXX TODO: more specific error msg based on err */
184 printf("Did not successfully activate boot environment %s\n",
187 printf("Successfully activated boot environment %s\n", argv[0]);
190 printf("for next boot\n");
197 * TODO: when only one arg is given, and it contains an "@" the this should
198 * create that snapshot
201 bectl_cmd_create(int argc, char *argv[])
203 char snapshot[BE_MAXPATHLEN];
204 char *atpos, *bootenv, *snapname;
210 while ((opt = getopt(argc, argv, "e:r")) != -1) {
219 fprintf(stderr, "bectl create: unknown option '-%c'\n",
221 return (usage(false));
229 fprintf(stderr, "bectl create: wrong number of arguments\n");
230 return (usage(false));
235 err = BE_ERR_SUCCESS;
236 if (strchr(bootenv, ' ') != NULL)
237 /* BE datasets with spaces are not bootable */
238 err = BE_ERR_INVALIDNAME;
239 else if ((atpos = strchr(bootenv, '@')) != NULL) {
241 * This is the "create a snapshot variant". No new boot
242 * environment is to be created here.
245 err = be_snapshot(be, bootenv, atpos, recursive, NULL);
247 if (snapname == NULL)
248 /* Create from currently booted BE */
249 err = be_snapshot(be, be_active_path(be), NULL,
250 recursive, snapshot);
251 else if (strchr(snapname, '@') != NULL)
252 /* Create from given snapshot */
253 strlcpy(snapshot, snapname, sizeof(snapshot));
255 /* Create from given BE */
256 err = be_snapshot(be, snapname, NULL, recursive,
259 if (err == BE_ERR_SUCCESS)
260 err = be_create_depth(be, bootenv, snapshot,
261 recursive == true ? -1 : 0);
267 case BE_ERR_INVALIDNAME:
269 "bectl create: boot environment name must not contain spaces\n");
274 "Failed to create a snapshot '%s' of '%s'\n",
276 else if (snapname == NULL)
278 "Failed to create bootenv %s\n", bootenv);
281 "Failed to create bootenv %s from snapshot %s\n",
290 bectl_cmd_export(int argc, char *argv[])
295 fprintf(stderr, "bectl export: missing boot environment name\n");
296 return (usage(false));
300 fprintf(stderr, "bectl export: extra arguments provided\n");
301 return (usage(false));
306 if (isatty(STDOUT_FILENO)) {
307 fprintf(stderr, "bectl export: must redirect output\n");
311 be_export(be, bootenv, STDOUT_FILENO);
318 bectl_cmd_import(int argc, char *argv[])
324 fprintf(stderr, "bectl import: missing boot environment name\n");
325 return (usage(false));
329 fprintf(stderr, "bectl import: extra arguments provided\n");
330 return (usage(false));
335 if (isatty(STDIN_FILENO)) {
336 fprintf(stderr, "bectl import: input can not be from terminal\n");
340 err = be_import(be, bootenv, STDIN_FILENO);
347 bectl_cmd_add(int argc, char *argv[])
351 fprintf(stderr, "bectl add: must provide at least one path\n");
352 return (usage(false));
355 for (int i = 1; i < argc; ++i) {
356 printf("arg %d: %s\n", i, argv[i]);
357 /* XXX TODO catch err */
358 be_add_child(be, argv[i], true);
366 bectl_cmd_destroy(int argc, char *argv[])
369 char *origin, *target, targetds[BE_MAXPATHLEN];
373 while ((opt = getopt(argc, argv, "Fo")) != -1) {
376 flags |= BE_DESTROY_FORCE;
379 flags |= BE_DESTROY_ORIGIN;
382 fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
384 return (usage(false));
392 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
393 return (usage(false));
398 /* We'll emit a notice if there's an origin to be cleaned up */
399 if ((flags & BE_DESTROY_ORIGIN) == 0 && strchr(target, '@') == NULL) {
400 flags |= BE_DESTROY_AUTOORIGIN;
401 if (be_root_concat(be, target, targetds) != 0)
403 if (be_prop_list_alloc(&props) != 0)
405 if (be_get_dataset_props(be, targetds, props) != 0) {
406 be_prop_list_free(props);
409 if (nvlist_lookup_string(props, "origin", &origin) == 0 &&
410 !be_is_auto_snapshot_name(be, origin))
411 fprintf(stderr, "bectl destroy: leaving origin '%s' intact\n",
413 be_prop_list_free(props);
417 err = be_destroy(be, target, flags);
423 bectl_cmd_mount(int argc, char *argv[])
425 char result_loc[BE_MAXPATHLEN];
426 char *bootenv, *mountpoint;
429 /* XXX TODO: Allow shallow */
430 mntflags = BE_MNT_DEEP;
432 fprintf(stderr, "bectl mount: missing argument(s)\n");
433 return (usage(false));
437 fprintf(stderr, "bectl mount: too many arguments\n");
438 return (usage(false));
442 mountpoint = ((argc == 3) ? argv[2] : NULL);
444 err = be_mount(be, bootenv, mountpoint, mntflags, result_loc);
448 printf("Successfully mounted %s at %s\n", bootenv, result_loc);
452 (argc == 3) ? "Failed to mount bootenv %s at %s\n" :
453 "Failed to mount bootenv %s at temporary path %s\n",
454 bootenv, mountpoint);
462 bectl_cmd_rename(int argc, char *argv[])
468 fprintf(stderr, "bectl rename: missing argument\n");
469 return (usage(false));
473 fprintf(stderr, "bectl rename: too many arguments\n");
474 return (usage(false));
480 err = be_rename(be, src, dest);
486 fprintf(stderr, "Failed to rename bootenv %s to %s\n",
494 bectl_cmd_unmount(int argc, char *argv[])
499 /* Store alias used */
503 while ((opt = getopt(argc, argv, "f")) != -1) {
506 flags |= BE_MNT_FORCE;
509 fprintf(stderr, "bectl %s: unknown option '-%c'\n",
511 return (usage(false));
519 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
520 return (usage(false));
525 err = be_unmount(be, bootenv, flags);
531 fprintf(stderr, "Failed to unmount bootenv %s\n", bootenv);
538 bectl_cmd_check(int argc, char *argv[] __unused)
541 /* The command is left as argv[0] */
543 fprintf(stderr, "bectl check: wrong number of arguments\n");
544 return (usage(false));
551 main(int argc, char *argv[])
553 struct command_map_entry *cmd;
561 return (usage(false));
563 if (strcmp(argv[1], "-r") == 0) {
565 return (usage(false));
566 root = strdup(argv[2]);
576 /* Handle command aliases */
577 if (strcmp(command, "umount") == 0)
580 if (strcmp(command, "ujail") == 0)
583 if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
584 return (usage(true));
586 if ((cmd = get_cmd_info(command)) == NULL) {
587 fprintf(stderr, "Unknown command: %s\n", command);
588 return (usage(false));
591 if ((be = libbe_init(root)) == NULL) {
593 fprintf(stderr, "libbe_init(\"%s\") failed.\n",
594 root != NULL ? root : "");
598 libbe_print_on_error(be, !cmd->silent);
600 rc = cmd->fn(argc, argv);