xref: /freebsd/sbin/bectl/bectl.c (revision 07d397d74651435ff6f4d474876589e85967350e)
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 *bootenv, *snapname, *source;
186 	int err, opt;
187 
188 	snapname = NULL;
189 	while ((opt = getopt(argc, argv, "e:")) != -1) {
190 		switch (opt) {
191 		case 'e':
192 			snapname = optarg;
193 			break;
194 		default:
195 			fprintf(stderr, "bectl create: unknown option '-%c'\n",
196 			    optopt);
197 			return (usage(false));
198 		}
199 	}
200 
201 	argc -= optind;
202 	argv += optind;
203 
204 	if (argc != 1) {
205 		fprintf(stderr, "bectl create: wrong number of arguments\n");
206 		return (usage(false));
207 	}
208 
209 	bootenv = *argv;
210 
211 	if (snapname != NULL) {
212 		if (strchr(snapname, '@') != NULL)
213 			err = be_create_from_existing_snap(be, bootenv,
214 			    snapname);
215 		else
216 			err = be_create_from_existing(be, bootenv, snapname);
217 	} else {
218 		if ((snapname = strchr(bootenv, '@')) != NULL) {
219 			*(snapname++) = '\0';
220 			if ((err = be_snapshot(be, be_active_path(be),
221 			    snapname, true, NULL)) != BE_ERR_SUCCESS)
222 				fprintf(stderr, "failed to create snapshot\n");
223 			asprintf(&source, "%s@%s", be_active_path(be), snapname);
224 			err = be_create_from_existing_snap(be, bootenv,
225 			    source);
226 			return (err);
227 		} else
228 			err = be_create(be, bootenv);
229 	}
230 
231 	switch (err) {
232 	case BE_ERR_SUCCESS:
233 		break;
234 	default:
235 		if (snapname == NULL)
236 			fprintf(stderr,
237 			    "failed to create bootenv %s\n", bootenv);
238 		else
239 			fprintf(stderr,
240 			    "failed to create bootenv %s from snapshot %s\n",
241 			    bootenv, snapname);
242 	}
243 
244 	return (err);
245 }
246 
247 
248 static int
249 bectl_cmd_export(int argc, char *argv[])
250 {
251 	char *bootenv;
252 
253 	if (argc == 1) {
254 		fprintf(stderr, "bectl export: missing boot environment name\n");
255 		return (usage(false));
256 	}
257 
258 	if (argc > 2) {
259 		fprintf(stderr, "bectl export: extra arguments provided\n");
260 		return (usage(false));
261 	}
262 
263 	bootenv = argv[1];
264 
265 	if (isatty(STDOUT_FILENO)) {
266 		fprintf(stderr, "bectl export: must redirect output\n");
267 		return (EX_USAGE);
268 	}
269 
270 	be_export(be, bootenv, STDOUT_FILENO);
271 
272 	return (0);
273 }
274 
275 
276 static int
277 bectl_cmd_import(int argc, char *argv[])
278 {
279 	char *bootenv;
280 	int err;
281 
282 	if (argc == 1) {
283 		fprintf(stderr, "bectl import: missing boot environment name\n");
284 		return (usage(false));
285 	}
286 
287 	if (argc > 2) {
288 		fprintf(stderr, "bectl import: extra arguments provided\n");
289 		return (usage(false));
290 	}
291 
292 	bootenv = argv[1];
293 
294 	if (isatty(STDIN_FILENO)) {
295 		fprintf(stderr, "bectl import: input can not be from terminal\n");
296 		return (EX_USAGE);
297 	}
298 
299 	err = be_import(be, bootenv, STDIN_FILENO);
300 
301 	return (err);
302 }
303 
304 #if SOON
305 static int
306 bectl_cmd_add(int argc, char *argv[])
307 {
308 
309 	if (argc < 2) {
310 		fprintf(stderr, "bectl add: must provide at least one path\n");
311 		return (usage(false));
312 	}
313 
314 	for (int i = 1; i < argc; ++i) {
315 		printf("arg %d: %s\n", i, argv[i]);
316 		/* XXX TODO catch err */
317 		be_add_child(be, argv[i], true);
318 	}
319 
320 	return (0);
321 }
322 #endif
323 
324 static int
325 bectl_cmd_destroy(int argc, char *argv[])
326 {
327 	char *target;
328 	int opt, err;
329 	bool force;
330 
331 	force = false;
332 	while ((opt = getopt(argc, argv, "F")) != -1) {
333 		switch (opt) {
334 		case 'F':
335 			force = true;
336 			break;
337 		default:
338 			fprintf(stderr, "bectl destroy: unknown option '-%c'\n",
339 			    optopt);
340 			return (usage(false));
341 		}
342 	}
343 
344 	argc -= optind;
345 	argv += optind;
346 
347 	if (argc != 1) {
348 		fprintf(stderr, "bectl destroy: wrong number of arguments\n");
349 		return (usage(false));
350 	}
351 
352 	target = argv[0];
353 
354 	err = be_destroy(be, target, force);
355 
356 	return (err);
357 }
358 
359 static int
360 bectl_cmd_mount(int argc, char *argv[])
361 {
362 	char result_loc[BE_MAXPATHLEN];
363 	char *bootenv, *mountpoint;
364 	int err;
365 
366 	if (argc < 2) {
367 		fprintf(stderr, "bectl mount: missing argument(s)\n");
368 		return (usage(false));
369 	}
370 
371 	if (argc > 3) {
372 		fprintf(stderr, "bectl mount: too many arguments\n");
373 		return (usage(false));
374 	}
375 
376 	bootenv = argv[1];
377 	mountpoint = ((argc == 3) ? argv[2] : NULL);
378 
379 	err = be_mount(be, bootenv, mountpoint, 0, result_loc);
380 
381 	switch (err) {
382 	case BE_ERR_SUCCESS:
383 		printf("successfully mounted %s at %s\n", bootenv, result_loc);
384 		break;
385 	default:
386 		fprintf(stderr,
387 		    (argc == 3) ? "failed to mount bootenv %s at %s\n" :
388 		    "failed to mount bootenv %s at temporary path %s\n",
389 		    bootenv, mountpoint);
390 	}
391 
392 	return (err);
393 }
394 
395 
396 static int
397 bectl_cmd_rename(int argc, char *argv[])
398 {
399 	char *dest, *src;
400 	int err;
401 
402 	if (argc < 3) {
403 		fprintf(stderr, "bectl rename: missing argument\n");
404 		return (usage(false));
405 	}
406 
407 	if (argc > 3) {
408 		fprintf(stderr, "bectl rename: too many arguments\n");
409 		return (usage(false));
410 	}
411 
412 	src = argv[1];
413 	dest = argv[2];
414 
415 	err = be_rename(be, src, dest);
416 
417 	switch (err) {
418 	case BE_ERR_SUCCESS:
419 		break;
420 	default:
421 		fprintf(stderr, "failed to rename bootenv %s to %s\n",
422 		    src, dest);
423 	}
424 
425 	return (0);
426 }
427 
428 static int
429 bectl_cmd_unmount(int argc, char *argv[])
430 {
431 	char *bootenv, *cmd;
432 	int err, flags, opt;
433 
434 	/* Store alias used */
435 	cmd = argv[0];
436 
437 	flags = 0;
438 	while ((opt = getopt(argc, argv, "f")) != -1) {
439 		switch (opt) {
440 		case 'f':
441 			flags |= BE_MNT_FORCE;
442 			break;
443 		default:
444 			fprintf(stderr, "bectl %s: unknown option '-%c'\n",
445 			    cmd, optopt);
446 			return (usage(false));
447 		}
448 	}
449 
450 	argc -= optind;
451 	argv += optind;
452 
453 	if (argc != 1) {
454 		fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
455 		return (usage(false));
456 	}
457 
458 	bootenv = argv[0];
459 
460 	err = be_unmount(be, bootenv, flags);
461 
462 	switch (err) {
463 	case BE_ERR_SUCCESS:
464 		break;
465 	default:
466 		fprintf(stderr, "failed to unmount bootenv %s\n", bootenv);
467 	}
468 
469 	return (err);
470 }
471 
472 
473 int
474 main(int argc, char *argv[])
475 {
476 	const char *command;
477 	int command_index, rc;
478 
479 	if (argc < 2) {
480 		fprintf(stderr, "missing command\n");
481 		return (usage(false));
482 	}
483 
484 	command = argv[1];
485 
486 	/* Handle command aliases */
487 	if (strcmp(command, "umount") == 0)
488 		command = "unmount";
489 
490 	if (strcmp(command, "ujail") == 0)
491 		command = "unjail";
492 
493 	if ((strcmp(command, "-?") == 0) || (strcmp(command, "-h") == 0))
494 		return (usage(true));
495 
496 	if (get_cmd_index(command, &command_index)) {
497 		fprintf(stderr, "unknown command: %s\n", command);
498 		return (usage(false));
499 	}
500 
501 
502 	if ((be = libbe_init()) == NULL)
503 		return (-1);
504 
505 	libbe_print_on_error(be, true);
506 
507 	/* XXX TODO: can be simplified if offset by 2 instead of one */
508 	rc = command_map[command_index].fn(argc-1, argv+1);
509 
510 	libbe_close(be);
511 	return (rc);
512 }
513