]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/bectl/bectl.c
bectl(8): Add some relevant `bectl list -a` information
[FreeBSD/FreeBSD.git] / sbin / bectl / bectl.c
1 /*
2  * be.c
3  *
4  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/param.h>
30 #include <sys/jail.h>
31 #include <sys/mount.h>
32 #include <errno.h>
33 #include <jail.h>
34 #include <libutil.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sysexits.h>
41 #include <time.h>
42 #include <unistd.h>
43
44 #include <be.h>
45
46 #define HEADER_BE       "BE"
47 #define HEADER_BEPLUS   "BE/Dataset/Snapshot"
48 #define HEADER_ACTIVE   "Active"
49 #define HEADER_MOUNT    "Mountpoint"
50 #define HEADER_SPACE    "Space"
51 #define HEADER_CREATED  "Created"
52
53 /* Spaces */
54 #define INDENT_INCREMENT        2
55
56 struct printc {
57         int     active_colsz_def;
58         int     be_colsz;
59         int     current_indent;
60         int     mount_colsz;
61         int     space_colsz;
62         bool    final_be;
63         bool    hide_headers;
64         bool    show_all_datasets;
65         bool    show_snaps;
66         bool    show_space;
67 };
68
69 static int bectl_cmd_activate(int argc, char *argv[]);
70 static int bectl_cmd_create(int argc, char *argv[]);
71 static int bectl_cmd_destroy(int argc, char *argv[]);
72 static int bectl_cmd_export(int argc, char *argv[]);
73 static int bectl_cmd_import(int argc, char *argv[]);
74 static int bectl_cmd_add(int argc, char *argv[]);
75 static int bectl_cmd_jail(int argc, char *argv[]);
76 static const char *get_origin_props(nvlist_t *dsprops, nvlist_t **originprops);
77 static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc);
78 static void print_headers(nvlist_t *props, struct printc *pc);
79 static int bectl_cmd_list(int argc, char *argv[]);
80 static int bectl_cmd_mount(int argc, char *argv[]);
81 static int bectl_cmd_rename(int argc, char *argv[]);
82 static int bectl_search_jail_paths(const char *mnt);
83 static int bectl_locate_jail(const char *ident);
84 static int bectl_cmd_unjail(int argc, char *argv[]);
85 static int bectl_cmd_unmount(int argc, char *argv[]);
86
87 static libbe_handle_t *be;
88
89 static int
90 usage(bool explicit)
91 {
92         FILE *fp;
93
94         fp =  explicit ? stdout : stderr;
95         fprintf(fp,
96             "usage:\tbectl ( -h | -? | subcommand [args...] )\n"
97             "\tbectl activate [-t] beName\n"
98             "\tbectl create [-e nonActiveBe | -e beName@snapshot] beName\n"
99             "\tbectl create beName@snapshot\n"
100             "\tbectl destroy [-F] beName | beName@snapshot⟩\n"
101             "\tbectl export sourceBe\n"
102             "\tbectl import targetBe\n"
103             "\tbectl add (path)*\n"
104             "\tbectl jail bootenv\n"
105             "\tbectl list [-a] [-D] [-H] [-s]\n"
106             "\tbectl mount beName [mountpoint]\n"
107             "\tbectl rename origBeName newBeName\n"
108             "\tbectl { ujail | unjail } ⟨jailID | jailName | bootenv)\n"
109             "\tbectl { umount | unmount } [-f] beName\n");
110
111         return (explicit ? 0 : EX_USAGE);
112 }
113
114
115 /*
116  * Represents a relationship between the command name and the parser action
117  * that handles it.
118  */
119 struct command_map_entry {
120         const char *command;
121         int (*fn)(int argc, char *argv[]);
122 };
123
124 static struct command_map_entry command_map[] =
125 {
126         { "activate", bectl_cmd_activate },
127         { "create",   bectl_cmd_create   },
128         { "destroy",  bectl_cmd_destroy  },
129         { "export",   bectl_cmd_export   },
130         { "import",   bectl_cmd_import   },
131         { "add",      bectl_cmd_add      },
132         { "jail",     bectl_cmd_jail     },
133         { "list",     bectl_cmd_list     },
134         { "mount",    bectl_cmd_mount    },
135         { "rename",   bectl_cmd_rename   },
136         { "unjail",   bectl_cmd_unjail   },
137         { "unmount",  bectl_cmd_unmount  },
138 };
139
140 static int
141 get_cmd_index(const char *cmd, int *index)
142 {
143         int map_size;
144
145         map_size = nitems(command_map);
146         for (int i = 0; i < map_size; ++i) {
147                 if (strcmp(cmd, command_map[i].command) == 0) {
148                         *index = i;
149                         return (0);
150                 }
151         }
152
153         return (1);
154 }
155
156
157 static int
158 bectl_cmd_activate(int argc, char *argv[])
159 {
160         int err, opt;
161         bool temp;
162
163         temp = false;
164         while ((opt = getopt(argc, argv, "t")) != -1) {
165                 switch (opt) {
166                 case 't':
167                         temp = true;
168                         break;
169                 default:
170                         fprintf(stderr, "bectl activate: unknown option '-%c'\n",
171                             optopt);
172                         return (usage(false));
173                 }
174         }
175
176         argc -= optind;
177         argv += optind;
178
179         if (argc != 1) {
180                 fprintf(stderr, "bectl activate: wrong number of arguments\n");
181                 return (usage(false));
182         }
183
184
185         /* activate logic goes here */
186         if ((err = be_activate(be, argv[0], temp)) != 0)
187                 /* XXX TODO: more specific error msg based on err */
188                 printf("did not successfully activate boot environment %s\n",
189                     argv[0]);
190         else
191                 printf("successfully activated boot environment %s\n", argv[0]);
192
193         if (temp)
194                 printf("for next boot\n");
195
196         return (err);
197 }
198
199
200 /*
201  * TODO: when only one arg is given, and it contains an "@" the this should
202  * create that snapshot
203  */
204 static int
205 bectl_cmd_create(int argc, char *argv[])
206 {
207         char *bootenv, *snapname, *source;
208         int err, opt;
209
210         snapname = NULL;
211         while ((opt = getopt(argc, argv, "e:")) != -1) {
212                 switch (opt) {
213                 case 'e':
214                         snapname = optarg;
215                         break;
216                 default:
217                         fprintf(stderr, "bectl create: unknown option '-%c'\n",
218                             optopt);
219                         return (usage(false));
220                 }
221         }
222
223         argc -= optind;
224         argv += optind;
225
226         if (argc != 1) {
227                 fprintf(stderr, "bectl create: wrong number of arguments\n");
228                 return (usage(false));
229         }
230
231         bootenv = *argv;
232
233         if (snapname != NULL) {
234                 if (strchr(snapname, '@') != NULL)
235                         err = be_create_from_existing_snap(be, bootenv,
236                             snapname);
237                 else
238                         err = be_create_from_existing(be, bootenv, snapname);
239         } else {
240                 if ((snapname = strchr(bootenv, '@')) != NULL) {
241                         *(snapname++) = '\0';
242                         if ((err = be_snapshot(be, be_active_path(be),
243                             snapname, true, NULL)) != BE_ERR_SUCCESS)
244                                 fprintf(stderr, "failed to create snapshot\n");
245                         asprintf(&source, "%s@%s", be_active_path(be), snapname);
246                         err = be_create_from_existing_snap(be, bootenv,
247                             source);
248                         return (err);
249                 } else
250                         err = be_create(be, bootenv);
251         }
252
253         switch (err) {
254         case BE_ERR_SUCCESS:
255                 break;
256         default:
257                 if (snapname == NULL)
258                         fprintf(stderr,
259                             "failed to create bootenv %s\n", bootenv);
260                 else
261                         fprintf(stderr,
262                             "failed to create bootenv %s from snapshot %s\n",
263                             bootenv, snapname);
264         }
265
266         return (err);
267 }
268
269
270 static int
271 bectl_cmd_export(int argc, char *argv[])
272 {
273         char *bootenv;
274
275         if (argc == 1) {
276                 fprintf(stderr, "bectl export: missing boot environment name\n");
277                 return (usage(false));
278         }
279
280         if (argc > 2) {
281                 fprintf(stderr, "bectl export: extra arguments provided\n");
282                 return (usage(false));
283         }
284
285         bootenv = argv[1];
286
287         if (isatty(STDOUT_FILENO)) {
288                 fprintf(stderr, "bectl export: must redirect output\n");
289                 return (EX_USAGE);
290         }
291
292         be_export(be, bootenv, STDOUT_FILENO);
293
294         return (0);
295 }
296
297
298 static int
299 bectl_cmd_import(int argc, char *argv[])
300 {
301         char *bootenv;
302         int err;
303
304         if (argc == 1) {
305                 fprintf(stderr, "bectl import: missing boot environment name\n");
306                 return (usage(false));
307         }
308
309         if (argc > 2) {
310                 fprintf(stderr, "bectl import: extra arguments provided\n");
311                 return (usage(false));
312         }
313
314         bootenv = argv[1];
315
316         if (isatty(STDIN_FILENO)) {
317                 fprintf(stderr, "bectl import: input can not be from terminal\n");
318                 return (EX_USAGE);
319         }
320
321         err = be_import(be, bootenv, STDIN_FILENO);
322
323         return (err);
324 }
325
326
327 static int
328 bectl_cmd_add(int argc, char *argv[])
329 {
330
331         if (argc < 2) {
332                 fprintf(stderr, "bectl add: must provide at least one path\n");
333                 return (usage(false));
334         }
335
336         for (int i = 1; i < argc; ++i) {
337                 printf("arg %d: %s\n", i, argv[i]);
338                 /* XXX TODO catch err */
339                 be_add_child(be, argv[i], true);
340         }
341
342         return (0);
343 }
344
345
346 static int
347 bectl_cmd_destroy(int argc, char *argv[])
348 {
349         char *target;
350         int opt, err;
351         bool force;
352
353         force = false;
354         while ((opt = getopt(argc, argv, "F")) != -1) {
355                 switch (opt) {
356                 case 'F':
357                         force = true;
358                         break;
359                 default:
360                         fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
361                             optopt);
362                         return (usage(false));
363                 }
364         }
365
366         argc -= optind;
367         argv += optind;
368
369         if (argc != 1) {
370                 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
371                 return (usage(false));
372         }
373
374         target = argv[0];
375
376         err = be_destroy(be, target, force);
377
378         return (err);
379 }
380
381
382 static int
383 bectl_cmd_jail(int argc, char *argv[])
384 {
385         char *bootenv;
386         char mnt_loc[BE_MAXPATHLEN];
387         int err, jid;
388
389         /* struct jail be_jail = { 0 }; */
390
391         if (argc == 1) {
392                 fprintf(stderr, "bectl jail: missing boot environment name\n");
393                 return (usage(false));
394         }
395         if (argc > 2) {
396                 fprintf(stderr, "bectl jail: too many arguments\n");
397                 return (usage(false));
398         }
399
400         bootenv = argv[1];
401
402         /*
403          * XXX TODO: if its already mounted, perhaps there should be a flag to
404          * indicate its okay to proceed??
405          */
406         if ((err = be_mount(be, bootenv, NULL, 0, mnt_loc)) != BE_ERR_SUCCESS) {
407                 fprintf(stderr, "could not mount bootenv\n");
408                 return (1);
409         }
410
411         /* XXX TODO: Make the IP/hostname configurable? */
412         jid = jail_setv(JAIL_CREATE | JAIL_ATTACH,
413             "name", bootenv,
414             "path", mnt_loc,
415             "host.hostname", bootenv,
416             "persist", "true",
417             "ip4.addr", "10.20.30.40",
418             "allow.mount", "true",
419             "allow.mount.devfs", "true",
420             "enforce_statfs", "1",
421             NULL);
422         if (jid == -1) {
423                 fprintf(stderr, "unable to create jail.  error: %d\n", errno);
424                 return (1);
425         }
426
427         /* We're attached within the jail... good bye! */
428         chdir("/");
429         execl("/bin/sh", "/bin/sh", NULL);
430         return (0);
431 }
432
433 /*
434  * Given a set of dataset properties (for a BE dataset), populate originprops
435  * with the origin's properties.
436  */
437 static const char *
438 get_origin_props(nvlist_t *dsprops, nvlist_t **originprops)
439 {
440         char *propstr;
441
442         if (nvlist_lookup_string(dsprops, "origin", &propstr) == 0) {
443                 if (be_prop_list_alloc(originprops) != 0) {
444                         fprintf(stderr,
445                             "bectl list: failed to allocate origin prop nvlist\n");
446                         return (NULL);
447                 }
448                 if (be_get_snapshot_props(be, propstr, *originprops) != 0) {
449                         /* XXX TODO: Real errors */
450                         fprintf(stderr,
451                             "bectl list: failed to fetch origin properties\n");
452                         return (NULL);
453                 }
454
455                 return (propstr);
456         }
457         return (NULL);
458 }
459
460
461 static void
462 print_info(const char *name, nvlist_t *dsprops, struct printc *pc)
463 {
464 #define BUFSZ   64
465         char buf[BUFSZ];
466         unsigned long long ctimenum, space;
467         nvlist_t *originprops;
468         const char *oname;
469         char *propstr;
470         int active_colsz;
471         boolean_t active_now, active_reboot;
472
473         originprops = NULL;
474         printf("%*s%*s ", pc->current_indent, "",
475             pc->be_colsz + pc->current_indent, name);
476
477         /* Recurse at the base level if we're breaking info down */
478         if (pc->current_indent == 0 && (pc->show_all_datasets ||
479             pc->show_snaps)) {
480                 printf("\n");
481                 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
482                         /* XXX TODO: Error? */
483                         return;
484                 pc->current_indent += INDENT_INCREMENT;
485                 print_info(propstr, dsprops, pc);
486                 pc->current_indent += INDENT_INCREMENT;
487                 if ((oname = get_origin_props(dsprops, &originprops)) != NULL) {
488                         print_info(oname, originprops, pc);
489                         nvlist_free(originprops);
490                 }
491                 pc->current_indent = 0;
492                 if (!pc->final_be)
493                         printf("\n");
494                 return;
495         }
496
497         active_colsz = pc->active_colsz_def;
498         if (nvlist_lookup_boolean_value(dsprops, "active",
499             &active_now) == 0 && active_now) {
500                 printf("N");
501                 active_colsz--;
502         }
503         if (nvlist_lookup_boolean_value(dsprops, "nextboot",
504             &active_reboot) == 0 && active_reboot) {
505                 printf("R");
506                 active_colsz--;
507         }
508         if (active_colsz == pc->active_colsz_def) {
509                 printf("-");
510                 active_colsz--;
511         }
512         printf("%*s ", -active_colsz, " ");
513         if (nvlist_lookup_string(dsprops, "mountpoint", &propstr) == 0)
514                 printf("%*s ", pc->mount_colsz, propstr);
515         else
516                 printf("%*s ", pc->mount_colsz, "-");
517
518         get_origin_props(dsprops, &originprops);
519
520         if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) {
521                 space = strtoull(propstr, NULL, 10);
522
523                 if (!pc->show_all_datasets && originprops != NULL &&
524                     nvlist_lookup_string(originprops, "used", &propstr) == 0)
525                         space += strtoull(propstr, NULL, 10);
526
527                 /* Alas, there's more to it,. */
528                 humanize_number(buf, 6, space, "", HN_AUTOSCALE,
529                         HN_DECIMAL | HN_NOSPACE | HN_B);
530                 printf("%*s ", pc->space_colsz, buf);
531         } else
532                 printf("%*s ", pc->space_colsz, "-");
533
534         if (nvlist_lookup_string(dsprops, "creation", &propstr) == 0) {
535                 ctimenum = strtoull(propstr, NULL, 10);
536                 strftime(buf, BUFSZ, "%Y-%m-%d %H:%M",
537                     localtime((time_t *)&ctimenum));
538                 printf("%s", buf);
539         }
540
541         printf("\n");
542         if (originprops != NULL) {
543                 /*if (pc->show_all_datasets) {
544                 }*/
545                 be_prop_list_free(originprops);
546         }
547 #undef BUFSZ
548 }
549
550 static void
551 print_headers(nvlist_t *props, struct printc *pc)
552 {
553         const char *chosen_be_header;
554         nvpair_t *cur;
555         nvlist_t *dsprops;
556         char *propstr;
557         size_t be_maxcol;
558
559         if (pc->show_all_datasets || pc->show_snaps)
560                 chosen_be_header = HEADER_BEPLUS;
561         else
562                 chosen_be_header = HEADER_BE;
563         be_maxcol = strlen(chosen_be_header);
564         for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
565             cur = nvlist_next_nvpair(props, cur)) {
566                 be_maxcol = MAX(be_maxcol, strlen(nvpair_name(cur)));
567                 if (!pc->show_all_datasets && !pc->show_snaps)
568                         continue;
569                 nvpair_value_nvlist(cur, &dsprops);
570                 if (nvlist_lookup_string(dsprops, "dataset", &propstr) != 0)
571                         continue;
572                 be_maxcol = MAX(be_maxcol, strlen(propstr) + INDENT_INCREMENT);
573                 if (nvlist_lookup_string(dsprops, "origin", &propstr) != 0)
574                         continue;
575                 be_maxcol = MAX(be_maxcol,
576                     strlen(propstr) + INDENT_INCREMENT * 2);
577         }
578
579         pc->be_colsz = -be_maxcol;
580         /* To be made negative after calculating final col sz */
581         pc->active_colsz_def = strlen(HEADER_ACTIVE);
582         pc->mount_colsz = -(int)strlen(HEADER_MOUNT);
583         pc->space_colsz = -(int)strlen(HEADER_SPACE);
584         /* XXX TODO: Take -H into account */
585         printf("%*s %s %s %s %s\n", pc->be_colsz, chosen_be_header,
586             HEADER_ACTIVE, HEADER_MOUNT, HEADER_SPACE, HEADER_CREATED);
587
588         /*
589          * All other invocations in which we aren't using the default header
590          * will produce quite a bit of input.  Throw an extra blank line after
591          * the header to make it look nicer.
592          */
593         if (chosen_be_header != HEADER_BE)
594                 printf("\n");
595 }
596
597 static int
598 bectl_cmd_list(int argc, char *argv[])
599 {
600         struct printc pc;
601         nvpair_t *cur;
602         nvlist_t *dsprops, *props;
603         int opt;
604
605         props = NULL;
606         bzero(&pc, sizeof(pc));
607         while ((opt = getopt(argc, argv, "aDHs")) != -1) {
608                 switch (opt) {
609                 case 'a':
610                         pc.show_all_datasets = true;
611                         break;
612                 case 'D':
613                         pc.show_space = true;
614                         break;
615                 case 'H':
616                         pc.hide_headers = true;
617                         break;
618                 case 's':
619                         pc.show_snaps = true;
620                         break;
621                 default:
622                         fprintf(stderr, "bectl list: unknown option '-%c'\n",
623                             optopt);
624                         return (usage(false));
625                 }
626         }
627
628         argc -= optind;
629
630         if (argc != 0) {
631                 fprintf(stderr, "bectl list: extra argument provided\n");
632                 return (usage(false));
633         }
634
635         if (be_prop_list_alloc(&props) != 0) {
636                 fprintf(stderr, "bectl list: failed to allocate prop nvlist\n");
637                 return (1);
638         }
639         if (be_get_bootenv_props(be, props) != 0) {
640                 /* XXX TODO: Real errors */
641                 fprintf(stderr, "bectl list: failed to fetch boot environments\n");
642                 return (1);
643         }
644
645         print_headers(props, &pc);
646         for (cur = nvlist_next_nvpair(props, NULL); cur != NULL;
647             cur = nvlist_next_nvpair(props, cur)) {
648                 nvpair_value_nvlist(cur, &dsprops);
649                 pc.final_be = nvlist_next_nvpair(props, cur) == NULL;
650                 print_info(nvpair_name(cur), dsprops, &pc);
651         }
652         be_prop_list_free(props);
653
654         return (0);
655 }
656
657
658 static int
659 bectl_cmd_mount(int argc, char *argv[])
660 {
661         char result_loc[BE_MAXPATHLEN];
662         char *bootenv, *mountpoint;
663         int err;
664
665         if (argc < 2) {
666                 fprintf(stderr, "bectl mount: missing argument(s)\n");
667                 return (usage(false));
668         }
669
670         if (argc > 3) {
671                 fprintf(stderr, "bectl mount: too many arguments\n");
672                 return (usage(false));
673         }
674
675         bootenv = argv[1];
676         mountpoint = ((argc == 3) ? argv[2] : NULL);
677
678         err = be_mount(be, bootenv, mountpoint, 0, result_loc);
679
680         switch (err) {
681         case BE_ERR_SUCCESS:
682                 printf("successfully mounted %s at %s\n", bootenv, result_loc);
683                 break;
684         default:
685                 fprintf(stderr,
686                     (argc == 3) ? "failed to mount bootenv %s at %s\n" :
687                     "failed to mount bootenv %s at temporary path %s\n",
688                     bootenv, mountpoint);
689         }
690
691         return (err);
692 }
693
694
695 static int
696 bectl_cmd_rename(int argc, char *argv[])
697 {
698         char *dest, *src;
699         int err;
700
701         if (argc < 3) {
702                 fprintf(stderr, "bectl rename: missing argument\n");
703                 return (usage(false));
704         }
705
706         if (argc > 3) {
707                 fprintf(stderr, "bectl rename: too many arguments\n");
708                 return (usage(false));
709         }
710
711         src = argv[1];
712         dest = argv[2];
713
714         err = be_rename(be, src, dest);
715
716         switch (err) {
717         case BE_ERR_SUCCESS:
718                 break;
719         default:
720                 fprintf(stderr, "failed to rename bootenv %s to %s\n",
721                     src, dest);
722         }
723
724         return (0);
725 }
726
727 static int
728 bectl_search_jail_paths(const char *mnt)
729 {
730         char jailpath[MAXPATHLEN + 1];
731         int jid;
732
733         jid = 0;
734         (void)mnt;
735         while ((jid = jail_getv(0, "lastjid", &jid, "path", &jailpath,
736             NULL)) != -1) {
737                 if (strcmp(jailpath, mnt) == 0)
738                         return (jid);
739         }
740
741         return (-1);
742 }
743
744 /*
745  * Locate a jail based on an arbitrary identifier.  This may be either a name,
746  * a jid, or a BE name.  Returns the jid or -1 on failure.
747  */
748 static int
749 bectl_locate_jail(const char *ident)
750 {
751         nvlist_t *belist, *props;
752         char *mnt;
753         int jid;
754
755         /* Try the easy-match first */
756         jid = jail_getid(ident);
757         if (jid != -1)
758                 return (jid);
759
760         /* Attempt to try it as a BE name, first */
761         if (be_prop_list_alloc(&belist) != 0)
762                 return (-1);
763
764         if (be_get_bootenv_props(be, belist) != 0)
765                 return (-1);
766
767         if (nvlist_lookup_nvlist(belist, ident, &props) == 0) {
768                 /* We'll attempt to resolve the jid by way of mountpoint */
769                 if (nvlist_lookup_string(props, "mountpoint", &mnt) == 0) {
770                         jid = bectl_search_jail_paths(mnt);
771                         be_prop_list_free(belist);
772                         return (jid);
773                 }
774
775                 be_prop_list_free(belist);
776         }
777
778         return (-1);
779 }
780
781 static int
782 bectl_cmd_unjail(int argc, char *argv[])
783 {
784         char path[MAXPATHLEN + 1];
785         char *cmd, *name, *target;
786         int jid;
787
788         /* Store alias used */
789         cmd = argv[0];
790
791         if (argc != 2) {
792                 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
793                 return (usage(false));
794         }
795
796         target = argv[1];
797
798         /* Locate the jail */
799         if ((jid = bectl_locate_jail(target)) == -1) {
800                 fprintf(stderr, "bectl %s: failed to locate BE by '%s'\n", cmd, target);
801                 return (1);
802         }
803
804         bzero(&path, MAXPATHLEN + 1);
805         name = jail_getname(jid);
806         if (jail_getv(0, "name", name, "path", path, NULL) != jid) {
807                 free(name);
808                 fprintf(stderr, "bectl %s: failed to get path for jail requested by '%s'\n", cmd, target);
809                 return (1);
810         }
811
812         free(name);
813
814         if (be_mounted_at(be, path, NULL) != 0) {
815                 fprintf(stderr, "bectl %s: jail requested by '%s' not a BE\n", cmd, target);
816                 return (1);
817         }
818
819         jail_remove(jid);
820         unmount(path, 0);
821
822         return (0);
823 }
824
825
826 static int
827 bectl_cmd_unmount(int argc, char *argv[])
828 {
829         char *bootenv, *cmd;
830         int err, flags, opt;
831
832         /* Store alias used */
833         cmd = argv[0];
834
835         flags = 0;
836         while ((opt = getopt(argc, argv, "f")) != -1) {
837                 switch (opt) {
838                 case 'f':
839                         flags |= BE_MNT_FORCE;
840                         break;
841                 default:
842                         fprintf(stderr, "bectl %s: unknown option '-%c'\n",
843                             cmd, optopt);
844                         return (usage(false));
845                 }
846         }
847
848         argc -= optind;
849         argv += optind;
850
851         if (argc != 1) {
852                 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
853                 return (usage(false));
854         }
855
856         bootenv = argv[0];
857
858         err = be_unmount(be, bootenv, flags);
859
860         switch (err) {
861         case BE_ERR_SUCCESS:
862                 break;
863         default:
864                 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
865         }
866
867         return (err);
868 }
869
870
871 int
872 main(int argc, char *argv[])
873 {
874         const char *command;
875         int command_index, rc;
876
877         if (argc < 2) {
878                 fprintf(stderr, "missing command\n");
879                 return (usage(false));
880         }
881
882         command = argv[1];
883
884         /* Handle command aliases */
885         if (strcmp(command, "umount") == 0)
886                 command = "unmount";
887
888         if (strcmp(command, "ujail") == 0)
889                 command = "unjail";
890
891         if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
892                 return (usage(true));
893
894         if (get_cmd_index(command, &command_index)) {
895                 fprintf(stderr, "unknown command: %s\n", command);
896                 return (usage(false));
897         }
898
899
900         if ((be = libbe_init()) == NULL)
901                 return (-1);
902
903         libbe_print_on_error(be, true);
904
905         /* XXX TODO: can be simplified if offset by 2 instead of one */
906         rc = command_map[command_index].fn(argc-1, argv+1);
907
908         libbe_close(be);
909         return (rc);
910 }