]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/bectl/bectl.c
bectl(8): Implement the 'create a snapshot' variant of create
[FreeBSD/FreeBSD.git] / sbin / bectl / bectl.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/mount.h>
34 #include <errno.h>
35 #include <libutil.h>
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 #include <be.h>
46
47 #include "bectl.h"
48
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[]);
54 #if SOON
55 static int bectl_cmd_add(int argc, char *argv[]);
56 #endif
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[]);
60
61 libbe_handle_t *be;
62
63 int
64 usage(bool explicit)
65 {
66         FILE *fp;
67
68         fp =  explicit ? stdout : stderr;
69         fprintf(fp,
70             "usage:\tbectl ( -h | -? | subcommand [args...] )\n"
71             "\tbectl activate [-t] beName\n"
72             "\tbectl create [-e nonActiveBe | -e beName@snapshot] beName\n"
73             "\tbectl create beName@snapshot\n"
74             "\tbectl destroy [-F] beName | beName@snapshot⟩\n"
75             "\tbectl export sourceBe\n"
76             "\tbectl import targetBe\n"
77 #if SOON
78             "\tbectl add (path)*\n"
79 #endif
80             "\tbectl jail [ -o key=value | -u key ]... bootenv\n"
81             "\tbectl list [-a] [-D] [-H] [-s]\n"
82             "\tbectl mount beName [mountpoint]\n"
83             "\tbectl rename origBeName newBeName\n"
84             "\tbectl { ujail | unjail } ⟨jailID | jailName | bootenv)\n"
85             "\tbectl { umount | unmount } [-f] beName\n");
86
87         return (explicit ? 0 : EX_USAGE);
88 }
89
90
91 /*
92  * Represents a relationship between the command name and the parser action
93  * that handles it.
94  */
95 struct command_map_entry {
96         const char *command;
97         int (*fn)(int argc, char *argv[]);
98 };
99
100 static struct command_map_entry command_map[] =
101 {
102         { "activate", bectl_cmd_activate },
103         { "create",   bectl_cmd_create   },
104         { "destroy",  bectl_cmd_destroy  },
105         { "export",   bectl_cmd_export   },
106         { "import",   bectl_cmd_import   },
107 #if SOON
108         { "add",      bectl_cmd_add      },
109 #endif
110         { "jail",     bectl_cmd_jail     },
111         { "list",     bectl_cmd_list     },
112         { "mount",    bectl_cmd_mount    },
113         { "rename",   bectl_cmd_rename   },
114         { "unjail",   bectl_cmd_unjail   },
115         { "unmount",  bectl_cmd_unmount  },
116 };
117
118 static int
119 get_cmd_index(const char *cmd, int *idx)
120 {
121         int map_size;
122
123         map_size = nitems(command_map);
124         for (int i = 0; i < map_size; ++i) {
125                 if (strcmp(cmd, command_map[i].command) == 0) {
126                         *idx = i;
127                         return (0);
128                 }
129         }
130
131         return (1);
132 }
133
134
135 static int
136 bectl_cmd_activate(int argc, char *argv[])
137 {
138         int err, opt;
139         bool temp;
140
141         temp = false;
142         while ((opt = getopt(argc, argv, "t")) != -1) {
143                 switch (opt) {
144                 case 't':
145                         temp = true;
146                         break;
147                 default:
148                         fprintf(stderr, "bectl activate: unknown option '-%c'\n",
149                             optopt);
150                         return (usage(false));
151                 }
152         }
153
154         argc -= optind;
155         argv += optind;
156
157         if (argc != 1) {
158                 fprintf(stderr, "bectl activate: wrong number of arguments\n");
159                 return (usage(false));
160         }
161
162
163         /* activate logic goes here */
164         if ((err = be_activate(be, argv[0], temp)) != 0)
165                 /* XXX TODO: more specific error msg based on err */
166                 printf("did not successfully activate boot environment %s\n",
167                     argv[0]);
168         else
169                 printf("successfully activated boot environment %s\n", argv[0]);
170
171         if (temp)
172                 printf("for next boot\n");
173
174         return (err);
175 }
176
177
178 /*
179  * TODO: when only one arg is given, and it contains an "@" the this should
180  * create that snapshot
181  */
182 static int
183 bectl_cmd_create(int argc, char *argv[])
184 {
185         char *atpos, *bootenv, *snapname, *source;
186         int err, opt;
187         bool recursive;
188
189         snapname = NULL;
190         recursive = false;
191         while ((opt = getopt(argc, argv, "re:")) != -1) {
192                 switch (opt) {
193                 case 'e':
194                         snapname = optarg;
195                         break;
196                 case 'r':
197                         recursive = true;
198                 default:
199                         fprintf(stderr, "bectl create: unknown option '-%c'\n",
200                             optopt);
201                         return (usage(false));
202                 }
203         }
204
205         argc -= optind;
206         argv += optind;
207
208         if (argc != 1) {
209                 fprintf(stderr, "bectl create: wrong number of arguments\n");
210                 return (usage(false));
211         }
212
213         bootenv = *argv;
214         if ((atpos = strchr(bootenv, '@')) != NULL) {
215                 /*
216                  * This is the "create a snapshot variant". No new boot
217                  * environment is to be created here.
218                  */
219                 *atpos++ = '\0';
220                 err = be_snapshot(be, bootenv, atpos, recursive, NULL);
221         } else if (snapname != NULL) {
222                 if (strchr(snapname, '@') != NULL)
223                         err = be_create_from_existing_snap(be, bootenv,
224                             snapname);
225                 else
226                         err = be_create_from_existing(be, bootenv, snapname);
227         } else {
228                 if ((snapname = strchr(bootenv, '@')) != NULL) {
229                         *(snapname++) = '\0';
230                         if ((err = be_snapshot(be, be_active_path(be),
231                             snapname, true, NULL)) != BE_ERR_SUCCESS)
232                                 fprintf(stderr, "failed to create snapshot\n");
233                         asprintf(&source, "%s@%s", be_active_path(be), snapname);
234                         err = be_create_from_existing_snap(be, bootenv,
235                             source);
236                         return (err);
237                 } else
238                         err = be_create(be, bootenv);
239         }
240
241         switch (err) {
242         case BE_ERR_SUCCESS:
243                 break;
244         default:
245                 if (atpos != NULL)
246                         fprintf(stderr,
247                             "failed to create a snapshot '%s' of '%s'\n",
248                             atpos, bootenv);
249                 else if (snapname == NULL)
250                         fprintf(stderr,
251                             "failed to create bootenv %s\n", bootenv);
252                 else
253                         fprintf(stderr,
254                             "failed to create bootenv %s from snapshot %s\n",
255                             bootenv, snapname);
256         }
257
258         return (err);
259 }
260
261
262 static int
263 bectl_cmd_export(int argc, char *argv[])
264 {
265         char *bootenv;
266
267         if (argc == 1) {
268                 fprintf(stderr, "bectl export: missing boot environment name\n");
269                 return (usage(false));
270         }
271
272         if (argc > 2) {
273                 fprintf(stderr, "bectl export: extra arguments provided\n");
274                 return (usage(false));
275         }
276
277         bootenv = argv[1];
278
279         if (isatty(STDOUT_FILENO)) {
280                 fprintf(stderr, "bectl export: must redirect output\n");
281                 return (EX_USAGE);
282         }
283
284         be_export(be, bootenv, STDOUT_FILENO);
285
286         return (0);
287 }
288
289
290 static int
291 bectl_cmd_import(int argc, char *argv[])
292 {
293         char *bootenv;
294         int err;
295
296         if (argc == 1) {
297                 fprintf(stderr, "bectl import: missing boot environment name\n");
298                 return (usage(false));
299         }
300
301         if (argc > 2) {
302                 fprintf(stderr, "bectl import: extra arguments provided\n");
303                 return (usage(false));
304         }
305
306         bootenv = argv[1];
307
308         if (isatty(STDIN_FILENO)) {
309                 fprintf(stderr, "bectl import: input can not be from terminal\n");
310                 return (EX_USAGE);
311         }
312
313         err = be_import(be, bootenv, STDIN_FILENO);
314
315         return (err);
316 }
317
318 #if SOON
319 static int
320 bectl_cmd_add(int argc, char *argv[])
321 {
322
323         if (argc < 2) {
324                 fprintf(stderr, "bectl add: must provide at least one path\n");
325                 return (usage(false));
326         }
327
328         for (int i = 1; i < argc; ++i) {
329                 printf("arg %d: %s\n", i, argv[i]);
330                 /* XXX TODO catch err */
331                 be_add_child(be, argv[i], true);
332         }
333
334         return (0);
335 }
336 #endif
337
338 static int
339 bectl_cmd_destroy(int argc, char *argv[])
340 {
341         char *target;
342         int opt, err;
343         bool force;
344
345         force = false;
346         while ((opt = getopt(argc, argv, "F")) != -1) {
347                 switch (opt) {
348                 case 'F':
349                         force = true;
350                         break;
351                 default:
352                         fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
353                             optopt);
354                         return (usage(false));
355                 }
356         }
357
358         argc -= optind;
359         argv += optind;
360
361         if (argc != 1) {
362                 fprintf(stderr, "bectl destroy: wrong number of arguments\n");
363                 return (usage(false));
364         }
365
366         target = argv[0];
367
368         err = be_destroy(be, target, force);
369
370         return (err);
371 }
372
373 static int
374 bectl_cmd_mount(int argc, char *argv[])
375 {
376         char result_loc[BE_MAXPATHLEN];
377         char *bootenv, *mountpoint;
378         int err;
379
380         if (argc < 2) {
381                 fprintf(stderr, "bectl mount: missing argument(s)\n");
382                 return (usage(false));
383         }
384
385         if (argc > 3) {
386                 fprintf(stderr, "bectl mount: too many arguments\n");
387                 return (usage(false));
388         }
389
390         bootenv = argv[1];
391         mountpoint = ((argc == 3) ? argv[2] : NULL);
392
393         err = be_mount(be, bootenv, mountpoint, 0, result_loc);
394
395         switch (err) {
396         case BE_ERR_SUCCESS:
397                 printf("successfully mounted %s at %s\n", bootenv, result_loc);
398                 break;
399         default:
400                 fprintf(stderr,
401                     (argc == 3) ? "failed to mount bootenv %s at %s\n" :
402                     "failed to mount bootenv %s at temporary path %s\n",
403                     bootenv, mountpoint);
404         }
405
406         return (err);
407 }
408
409
410 static int
411 bectl_cmd_rename(int argc, char *argv[])
412 {
413         char *dest, *src;
414         int err;
415
416         if (argc < 3) {
417                 fprintf(stderr, "bectl rename: missing argument\n");
418                 return (usage(false));
419         }
420
421         if (argc > 3) {
422                 fprintf(stderr, "bectl rename: too many arguments\n");
423                 return (usage(false));
424         }
425
426         src = argv[1];
427         dest = argv[2];
428
429         err = be_rename(be, src, dest);
430
431         switch (err) {
432         case BE_ERR_SUCCESS:
433                 break;
434         default:
435                 fprintf(stderr, "failed to rename bootenv %s to %s\n",
436                     src, dest);
437         }
438
439         return (0);
440 }
441
442 static int
443 bectl_cmd_unmount(int argc, char *argv[])
444 {
445         char *bootenv, *cmd;
446         int err, flags, opt;
447
448         /* Store alias used */
449         cmd = argv[0];
450
451         flags = 0;
452         while ((opt = getopt(argc, argv, "f")) != -1) {
453                 switch (opt) {
454                 case 'f':
455                         flags |= BE_MNT_FORCE;
456                         break;
457                 default:
458                         fprintf(stderr, "bectl %s: unknown option '-%c'\n",
459                             cmd, optopt);
460                         return (usage(false));
461                 }
462         }
463
464         argc -= optind;
465         argv += optind;
466
467         if (argc != 1) {
468                 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
469                 return (usage(false));
470         }
471
472         bootenv = argv[0];
473
474         err = be_unmount(be, bootenv, flags);
475
476         switch (err) {
477         case BE_ERR_SUCCESS:
478                 break;
479         default:
480                 fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
481         }
482
483         return (err);
484 }
485
486
487 int
488 main(int argc, char *argv[])
489 {
490         const char *command;
491         int command_index, rc;
492
493         if (argc < 2) {
494                 fprintf(stderr, "missing command\n");
495                 return (usage(false));
496         }
497
498         command = argv[1];
499
500         /* Handle command aliases */
501         if (strcmp(command, "umount") == 0)
502                 command = "unmount";
503
504         if (strcmp(command, "ujail") == 0)
505                 command = "unjail";
506
507         if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
508                 return (usage(true));
509
510         if (get_cmd_index(command, &command_index)) {
511                 fprintf(stderr, "unknown command: %s\n", command);
512                 return (usage(false));
513         }
514
515
516         if ((be = libbe_init()) == NULL)
517                 return (-1);
518
519         libbe_print_on_error(be, true);
520
521         /* XXX TODO: can be simplified if offset by 2 instead of one */
522         rc = command_map[command_index].fn(argc-1, argv+1);
523
524         libbe_close(be);
525         return (rc);
526 }