xref: /illumos-gate/usr/src/cmd/beadm/beadm.c (revision cd277642e0bdc71a7f57c550df1279b0c091d6e2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25  * Copyright 2015 Toomas Soome <tsoome@me.com>
26  * Copyright 2015 Gary Mills
27  * Copyright (c) 2015 by Delphix. All rights reserved.
28  */
29 
30 /*
31  * System includes
32  */
33 
34 #include <assert.h>
35 #include <stdio.h>
36 #include <strings.h>
37 #include <libzfs.h>
38 #include <locale.h>
39 #include <langinfo.h>
40 #include <stdlib.h>
41 #include <wchar.h>
42 #include <sys/types.h>
43 
44 #include "libbe.h"
45 
46 #ifndef lint
47 #define	_(x) gettext(x)
48 #else
49 #define	_(x) (x)
50 #endif
51 
52 #ifndef TEXT_DOMAIN
53 #define	TEXT_DOMAIN "SYS_TEST"
54 #endif
55 
56 #define	DT_BUF_LEN (128)
57 #define	NUM_COLS (6)
58 
59 static int be_do_activate(int argc, char **argv);
60 static int be_do_create(int argc, char **argv);
61 static int be_do_destroy(int argc, char **argv);
62 static int be_do_list(int argc, char **argv);
63 static int be_do_mount(int argc, char **argv);
64 static int be_do_unmount(int argc, char **argv);
65 static int be_do_rename(int argc, char **argv);
66 static int be_do_rollback(int argc, char **argv);
67 static void usage(void);
68 
69 /*
70  * single column name/width output format description
71  */
72 struct col_info {
73 	const char *col_name;
74 	size_t width;
75 };
76 
77 /*
78  * all columns output format
79  */
80 struct hdr_info {
81 	struct col_info cols[NUM_COLS];
82 };
83 
84 /*
85  * type of possible output formats
86  */
87 enum be_fmt {
88 	BE_FMT_DEFAULT,
89 	BE_FMT_DATASET,
90 	BE_FMT_SNAPSHOT,
91 	BE_FMT_ALL
92 };
93 
94 /*
95  * command handler description
96  */
97 typedef struct be_command {
98 	const char	*name;
99 	int		(*func)(int argc, char **argv);
100 } be_command_t;
101 
102 /*
103  * sorted list of be commands
104  */
105 static const be_command_t be_command_tbl[] = {
106 	{ "activate",		be_do_activate },
107 	{ "create",		be_do_create },
108 	{ "destroy",		be_do_destroy },
109 	{ "list",		be_do_list },
110 	{ "mount",		be_do_mount },
111 	{ "unmount",		be_do_unmount },
112 	{ "umount",		be_do_unmount }, /* unmount alias */
113 	{ "rename",		be_do_rename },
114 	{ "rollback",		be_do_rollback },
115 	{ NULL,			NULL },
116 };
117 
118 static void
119 usage(void)
120 {
121 	(void) fprintf(stderr, _("usage:\n"
122 	    "\tbeadm subcommand cmd_options\n"
123 	    "\n"
124 	    "\tsubcommands:\n"
125 	    "\n"
126 	    "\tbeadm activate [-v] beName\n"
127 	    "\tbeadm create [-a] [-d BE_desc]\n"
128 	    "\t\t[-o property=value] ... [-p zpool] \n"
129 	    "\t\t[-e nonActiveBe | beName@snapshot] [-v] beName\n"
130 	    "\tbeadm create [-d BE_desc]\n"
131 	    "\t\t[-o property=value] ... [-p zpool] [-v] beName@snapshot\n"
132 	    "\tbeadm destroy [-Ffsv] beName \n"
133 	    "\tbeadm destroy [-Fv] beName@snapshot \n"
134 	    "\tbeadm list [[-a] | [-d] [-s]] [-H]\n"
135 	    "\t\t[-k|-K date | name | space] [-v] [beName]\n"
136 	    "\tbeadm mount [-s ro|rw] [-v] beName [mountpoint]\n"
137 	    "\tbeadm unmount [-fv] beName | mountpoint\n"
138 	    "\tbeadm umount [-fv] beName | mountpoint\n"
139 	    "\tbeadm rename [-v] origBeName newBeName\n"
140 	    "\tbeadm rollback [-v] beName snapshot\n"
141 	    "\tbeadm rollback [-v] beName@snapshot\n"));
142 }
143 
144 static int
145 run_be_cmd(const char *cmdname, int argc, char **argv)
146 {
147 	const be_command_t *command;
148 
149 	for (command = &be_command_tbl[0]; command->name != NULL; command++)
150 		if (strcmp(command->name, cmdname) == 0)
151 			return (command->func(argc, argv));
152 
153 	(void) fprintf(stderr, _("Invalid command: %s\n"), cmdname);
154 	usage();
155 	return (1);
156 }
157 
158 int
159 main(int argc, char **argv)
160 {
161 	const char *cmdname;
162 
163 	(void) setlocale(LC_ALL, "");
164 	(void) textdomain(TEXT_DOMAIN);
165 
166 	if (argc < 2) {
167 		usage();
168 		return (1);
169 	}
170 
171 	cmdname = argv[1];
172 
173 	/* Turn error printing off */
174 	libbe_print_errors(B_FALSE);
175 
176 	return (run_be_cmd(cmdname, --argc, ++argv));
177 }
178 
179 static void
180 print_hdr(struct hdr_info *hdr_info)
181 {
182 	boolean_t first = B_TRUE;
183 	size_t i;
184 	for (i = 0; i < NUM_COLS; i++) {
185 		struct col_info *col_info = &hdr_info->cols[i];
186 		const char *name = col_info->col_name;
187 		size_t width = col_info->width;
188 		if (name == NULL)
189 			continue;
190 
191 		if (first) {
192 			(void) printf("%-*s", width, name);
193 			first = B_FALSE;
194 		} else
195 			(void) printf(" %-*s", width, name);
196 	}
197 	(void) putchar('\n');
198 }
199 
200 static void
201 init_hdr_cols(enum be_fmt be_fmt, struct hdr_info *hdr)
202 {
203 	struct col_info *col = hdr->cols;
204 	size_t i;
205 
206 	col[1].col_name = _("Active");
207 	col[2].col_name = _("Mountpoint");
208 	col[3].col_name = _("Space");
209 	col[4].col_name = _("Policy");
210 	col[5].col_name = _("Created");
211 	col[6].col_name = NULL;
212 
213 	switch (be_fmt) {
214 	case BE_FMT_ALL:
215 		col[0].col_name = _("BE/Dataset/Snapshot");
216 		break;
217 	case BE_FMT_DATASET:
218 		col[0].col_name = _("BE/Dataset");
219 		break;
220 	case BE_FMT_SNAPSHOT:
221 		col[0].col_name = _("BE/Snapshot");
222 		col[1].col_name = NULL;
223 		col[2].col_name = NULL;
224 		break;
225 	case BE_FMT_DEFAULT:
226 	default:
227 		col[0].col_name = _("BE");
228 	}
229 
230 	for (i = 0; i < NUM_COLS; i++) {
231 		const char *name = col[i].col_name;
232 		col[i].width = 0;
233 
234 		if (name != NULL) {
235 			wchar_t wname[128];
236 			size_t sz = mbstowcs(wname, name, sizeof (wname) /
237 			    sizeof (wchar_t));
238 			if (sz > 0) {
239 				int wcsw = wcswidth(wname, sz);
240 				if (wcsw > 0)
241 					col[i].width = wcsw;
242 				else
243 					col[i].width = sz;
244 			} else {
245 				col[i].width = strlen(name);
246 			}
247 		}
248 	}
249 }
250 
251 static void
252 nicenum(uint64_t num, char *buf, size_t buflen)
253 {
254 	uint64_t n = num;
255 	int index = 0;
256 	char u;
257 
258 	while (n >= 1024) {
259 		n /= 1024;
260 		index++;
261 	}
262 
263 	u = " KMGTPE"[index];
264 
265 	if (index == 0) {
266 		(void) snprintf(buf, buflen, "%llu", n);
267 	} else {
268 		int i;
269 		for (i = 2; i >= 0; i--) {
270 			if (snprintf(buf, buflen, "%.*f%c", i,
271 			    (double)num / (1ULL << 10 * index), u) <= 5)
272 				break;
273 		}
274 	}
275 }
276 
277 static void
278 count_widths(enum be_fmt be_fmt, struct hdr_info *hdr, be_node_list_t *be_nodes)
279 {
280 	size_t len[NUM_COLS];
281 	char buf[DT_BUF_LEN];
282 	int i;
283 	be_node_list_t *cur_be;
284 
285 	for (i = 0; i < NUM_COLS; i++)
286 		len[i] = hdr->cols[i].width;
287 
288 	for (cur_be = be_nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
289 		char name[ZFS_MAX_DATASET_NAME_LEN + 1];
290 		const char *be_name = cur_be->be_node_name;
291 		const char *root_ds = cur_be->be_root_ds;
292 		char *pos;
293 		size_t node_name_len = strlen(cur_be->be_node_name);
294 		size_t root_ds_len = strlen(cur_be->be_root_ds);
295 		size_t mntpt_len = 0;
296 		size_t policy_len = 0;
297 		size_t used_len;
298 		uint64_t used = cur_be->be_space_used;
299 		be_snapshot_list_t *snap = NULL;
300 
301 		if (cur_be->be_mntpt != NULL)
302 			mntpt_len = strlen(cur_be->be_mntpt);
303 		if (cur_be->be_policy_type != NULL)
304 			policy_len = strlen(cur_be->be_policy_type);
305 
306 		(void) strlcpy(name, root_ds, sizeof (name));
307 		pos = strstr(name, be_name);
308 
309 		if (be_fmt == BE_FMT_DEFAULT) {
310 			if (node_name_len > len[0])
311 				len[0] = node_name_len;
312 		} else {
313 			if (root_ds_len + 3 > len[0])
314 				len[0] = root_ds_len + 3;
315 		}
316 
317 		if (mntpt_len > len[2])
318 			len[2] = mntpt_len;
319 		if (policy_len > len[4])
320 			len[4] = policy_len;
321 
322 		for (snap = cur_be->be_node_snapshots; snap != NULL;
323 		    snap = snap->be_next_snapshot) {
324 			uint64_t snap_used = snap->be_snapshot_space_used;
325 			const char *snap_name = snap->be_snapshot_name;
326 			(void) strcpy(pos, snap_name);
327 
328 			if (be_fmt == BE_FMT_DEFAULT)
329 				used += snap_used;
330 			else if (be_fmt & BE_FMT_SNAPSHOT) {
331 				int snap_len = strlen(name) + 3;
332 				if (be_fmt == BE_FMT_SNAPSHOT)
333 					snap_len -= pos - name;
334 				if (snap_len > len[0])
335 					len[0] = snap_len;
336 				nicenum(snap_used, buf, sizeof (buf));
337 				used_len = strlen(buf);
338 				if (used_len > len[3])
339 					len[3] = used_len;
340 			}
341 		}
342 
343 		if (be_fmt == BE_FMT_DEFAULT) {
344 			int used_len;
345 			nicenum(used, buf, sizeof (buf));
346 			used_len = strlen(buf);
347 			if (used_len > len[3])
348 				len[3] = used_len;
349 		}
350 
351 		nicenum(used, buf, sizeof (buf));
352 	}
353 
354 	for (i = 0; i < NUM_COLS; i++)
355 		hdr->cols[i].width = len[i];
356 }
357 
358 static void
359 print_be_nodes(const char *be_name, boolean_t parsable, struct hdr_info *hdr,
360     be_node_list_t *nodes)
361 {
362 	char buf[64];
363 	char datetime[DT_BUF_LEN];
364 	be_node_list_t	*cur_be;
365 
366 	for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
367 		char active[3] = "-\0";
368 		int ai = 0;
369 		const char *datetime_fmt = "%F %R";
370 		const char *name = cur_be->be_node_name;
371 		const char *mntpt = cur_be->be_mntpt;
372 		const char *uuid_str = cur_be->be_uuid_str;
373 		be_snapshot_list_t *snap = NULL;
374 		uint64_t used = cur_be->be_space_used;
375 		time_t creation = cur_be->be_node_creation;
376 		struct tm *tm;
377 
378 		if (be_name != NULL && strcmp(be_name, name) != 0)
379 			continue;
380 
381 		if (parsable)
382 			active[0] = '\0';
383 
384 		tm = localtime(&creation);
385 		(void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
386 
387 		for (snap = cur_be->be_node_snapshots; snap != NULL;
388 		    snap = snap->be_next_snapshot)
389 			used += snap->be_snapshot_space_used;
390 
391 		if (!cur_be->be_global_active)
392 			active[ai++] = 'x';
393 
394 		if (cur_be->be_active)
395 			active[ai++] = 'N';
396 		if (cur_be->be_active_on_boot) {
397 			if (!cur_be->be_global_active)
398 				active[ai] = 'b';
399 			else
400 				active[ai] = 'R';
401 		}
402 
403 		nicenum(used, buf, sizeof (buf));
404 		if (parsable)
405 			(void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
406 			    name,
407 			    (uuid_str != NULL ? uuid_str: ""),
408 			    active,
409 			    (cur_be->be_mounted ? mntpt: ""),
410 			    used,
411 			    cur_be->be_policy_type,
412 			    creation);
413 		else
414 			(void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
415 			    hdr->cols[0].width, name,
416 			    hdr->cols[1].width, active,
417 			    hdr->cols[2].width, (cur_be->be_mounted ? mntpt:
418 			    "-"),
419 			    hdr->cols[3].width, buf,
420 			    hdr->cols[4].width, cur_be->be_policy_type,
421 			    hdr->cols[5].width, datetime);
422 	}
423 }
424 
425 static void
426 print_be_snapshots(be_node_list_t *be, struct hdr_info *hdr, boolean_t parsable)
427 {
428 	char buf[64];
429 	char datetime[DT_BUF_LEN];
430 	be_snapshot_list_t *snap = NULL;
431 
432 	for (snap = be->be_node_snapshots; snap != NULL;
433 	    snap = snap->be_next_snapshot) {
434 		char name[ZFS_MAX_DATASET_NAME_LEN + 1];
435 		const char *datetime_fmt = "%F %R";
436 		const char *be_name = be->be_node_name;
437 		const char *root_ds = be->be_root_ds;
438 		const char *snap_name = snap->be_snapshot_name;
439 		char *pos;
440 		uint64_t used = snap->be_snapshot_space_used;
441 		time_t creation = snap->be_snapshot_creation;
442 		struct tm *tm = localtime(&creation);
443 
444 		(void) strncpy(name, root_ds, sizeof (name));
445 		pos = strstr(name, be_name);
446 		(void) strcpy(pos, snap_name);
447 
448 		(void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
449 		nicenum(used, buf, sizeof (buf));
450 
451 		if (parsable)
452 			if (hdr->cols[1].width != 0)
453 				(void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
454 				    be_name,
455 				    snap_name,
456 				    "",
457 				    "",
458 				    used,
459 				    be->be_policy_type,
460 				    creation);
461 			else
462 				(void) printf("%s;%s;%llu;%s;%ld\n",
463 				    be_name,
464 				    snap_name,
465 				    used,
466 				    be->be_policy_type,
467 				    creation);
468 		else
469 			if (hdr->cols[1].width != 0)
470 				(void) printf("   %-*s %-*s %-*s %-*s %-*s "
471 				    "%-*s\n",
472 				    hdr->cols[0].width-3, name,
473 				    hdr->cols[1].width, "-",
474 				    hdr->cols[2].width, "-",
475 				    hdr->cols[3].width, buf,
476 				    hdr->cols[4].width, be->be_policy_type,
477 				    hdr->cols[5].width, datetime);
478 			else
479 				(void) printf("   %-*s %-*s %-*s %-*s\n",
480 				    hdr->cols[0].width-3, snap_name,
481 				    hdr->cols[3].width, buf,
482 				    hdr->cols[4].width, be->be_policy_type,
483 				    hdr->cols[5].width, datetime);
484 	}
485 }
486 
487 static void
488 print_fmt_nodes(const char *be_name, enum be_fmt be_fmt, boolean_t parsable,
489     struct hdr_info *hdr, be_node_list_t *nodes)
490 {
491 	char buf[64];
492 	char datetime[DT_BUF_LEN];
493 	be_node_list_t	*cur_be;
494 
495 	for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
496 		char active[3] = "-\0";
497 		int ai = 0;
498 		const char *datetime_fmt = "%F %R";
499 		const char *name = cur_be->be_node_name;
500 		const char *mntpt = cur_be->be_mntpt;
501 		uint64_t used = cur_be->be_space_used;
502 		time_t creation = cur_be->be_node_creation;
503 		struct tm *tm;
504 
505 		if (be_name != NULL && strcmp(be_name, name) != 0)
506 			continue;
507 
508 		if (!parsable)
509 			(void) printf("%-s\n", name);
510 		else
511 			active[0] = '\0';
512 
513 		tm = localtime(&creation);
514 		(void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
515 
516 		if (cur_be->be_active)
517 			active[ai++] = 'N';
518 		if (cur_be->be_active_on_boot)
519 			active[ai] = 'R';
520 
521 		nicenum(used, buf, sizeof (buf));
522 		if (be_fmt & BE_FMT_DATASET)
523 			if (parsable)
524 				(void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
525 				    cur_be->be_node_name,
526 				    cur_be->be_root_ds,
527 				    active,
528 				    (cur_be->be_mounted ? mntpt: ""),
529 				    used,
530 				    cur_be->be_policy_type,
531 				    creation);
532 			else
533 				(void) printf("   %-*s %-*s %-*s %-*s %-*s "
534 				    "%-*s\n",
535 				    hdr->cols[0].width-3, cur_be->be_root_ds,
536 				    hdr->cols[1].width, active,
537 				    hdr->cols[2].width, (cur_be->be_mounted ?
538 				    mntpt: "-"),
539 				    hdr->cols[3].width, buf,
540 				    hdr->cols[4].width, cur_be->be_policy_type,
541 				    hdr->cols[5].width, datetime);
542 
543 		if (be_fmt & BE_FMT_SNAPSHOT)
544 			print_be_snapshots(cur_be, hdr, parsable);
545 	}
546 }
547 
548 static void
549 print_nodes(const char *be_name, boolean_t dsets, boolean_t snaps,
550     boolean_t parsable, be_node_list_t *be_nodes)
551 {
552 	struct hdr_info hdr;
553 	enum be_fmt be_fmt  = BE_FMT_DEFAULT;
554 
555 	if (dsets)
556 		be_fmt |= BE_FMT_DATASET;
557 	if (snaps)
558 		be_fmt |= BE_FMT_SNAPSHOT;
559 
560 	if (!parsable) {
561 		init_hdr_cols(be_fmt, &hdr);
562 		count_widths(be_fmt, &hdr, be_nodes);
563 		print_hdr(&hdr);
564 	}
565 
566 	if (be_fmt == BE_FMT_DEFAULT)
567 		print_be_nodes(be_name, parsable, &hdr, be_nodes);
568 	else
569 		print_fmt_nodes(be_name, be_fmt, parsable, &hdr, be_nodes);
570 }
571 
572 static boolean_t
573 confirm_destroy(const char *name)
574 {
575 	boolean_t res = B_FALSE;
576 	const char *yesre = nl_langinfo(YESEXPR);
577 	const char *nore = nl_langinfo(NOEXPR);
578 	regex_t yes_re;
579 	regex_t no_re;
580 	char buf[128];
581 	char *answer;
582 	int cflags = REG_EXTENDED;
583 
584 	if (regcomp(&yes_re, yesre, cflags) != 0) {
585 		/* should not happen */
586 		(void) fprintf(stderr, _("Failed to compile 'yes' regexp\n"));
587 		return (res);
588 	}
589 	if (regcomp(&no_re, nore, cflags) != 0) {
590 		/* should not happen */
591 		(void) fprintf(stderr, _("Failed to compile 'no' regexp\n"));
592 		regfree(&yes_re);
593 		return (res);
594 	}
595 
596 	(void) printf(_("Are you sure you want to destroy %s?\n"
597 	    "This action cannot be undone (y/[n]): "), name);
598 
599 	answer = fgets(buf, sizeof (buf), stdin);
600 	if (answer == NULL || *answer == '\0' || *answer == 10)
601 		goto out;
602 
603 	if (regexec(&yes_re, answer, 0, NULL, 0) == 0) {
604 		res = B_TRUE;
605 	} else if (regexec(&no_re, answer, 0, NULL, 0) != 0) {
606 		(void) fprintf(stderr, _("Invalid response. "
607 		    "Please enter 'y' or 'n'.\n"));
608 	}
609 
610 out:
611 	regfree(&yes_re);
612 	regfree(&no_re);
613 	return (res);
614 }
615 
616 static int
617 be_nvl_alloc(nvlist_t **nvlp)
618 {
619 	assert(nvlp != NULL);
620 
621 	if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) {
622 		(void) perror(_("nvlist_alloc failed.\n"));
623 		return (1);
624 	}
625 
626 	return (0);
627 }
628 
629 static int
630 be_nvl_add_string(nvlist_t *nvl, const char *name, const char *val)
631 {
632 	assert(nvl != NULL);
633 
634 	if (nvlist_add_string(nvl, name, val) != 0) {
635 		(void) fprintf(stderr, _("nvlist_add_string failed for "
636 		    "%s (%s).\n"), name, val);
637 		return (1);
638 	}
639 
640 	return (0);
641 }
642 
643 static int
644 be_nvl_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
645 {
646 	assert(nvl != NULL);
647 
648 	if (nvlist_add_nvlist(nvl, name, val) != 0) {
649 		(void) fprintf(stderr, _("nvlist_add_nvlist failed for %s.\n"),
650 		    name);
651 		return (1);
652 	}
653 
654 	return (0);
655 }
656 
657 static int
658 be_nvl_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
659 {
660 	assert(nvl != NULL);
661 
662 	if (nvlist_add_uint16(nvl, name, val) != 0) {
663 		(void) fprintf(stderr, _("nvlist_add_uint16 failed for "
664 		    "%s (%hu).\n"), name, val);
665 		return (1);
666 	}
667 
668 	return (0);
669 }
670 
671 static int
672 be_do_activate(int argc, char **argv)
673 {
674 	nvlist_t	*be_attrs;
675 	int		err = 1;
676 	int		c;
677 	char		*obe_name;
678 
679 	while ((c = getopt(argc, argv, "v")) != -1) {
680 		switch (c) {
681 		case 'v':
682 			libbe_print_errors(B_TRUE);
683 			break;
684 		default:
685 			usage();
686 			return (1);
687 		}
688 	}
689 
690 	argc -= optind;
691 	argv += optind;
692 
693 	if (argc != 1) {
694 		usage();
695 		return (1);
696 	}
697 
698 	obe_name = argv[0];
699 
700 	if (be_nvl_alloc(&be_attrs) != 0)
701 		return (1);
702 
703 	if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
704 		goto out;
705 
706 	err = be_activate(be_attrs);
707 
708 	switch (err) {
709 	case BE_SUCCESS:
710 		(void) printf(_("Activated successfully\n"));
711 		break;
712 	case BE_ERR_BE_NOENT:
713 		(void) fprintf(stderr, _("%s does not exist or appear "
714 		    "to be a valid BE.\nPlease check that the name of "
715 		    "the BE provided is correct.\n"), obe_name);
716 		break;
717 	case BE_ERR_PERM:
718 	case BE_ERR_ACCESS:
719 		(void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
720 		(void) fprintf(stderr, _("You have insufficient privileges to "
721 		    "execute this command.\n"));
722 		break;
723 	case BE_ERR_ACTIVATE_CURR:
724 	default:
725 		(void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
726 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
727 	}
728 
729 out:
730 	nvlist_free(be_attrs);
731 	return (err);
732 }
733 
734 static int
735 be_do_create(int argc, char **argv)
736 {
737 	nvlist_t	*be_attrs;
738 	nvlist_t	*zfs_props = NULL;
739 	boolean_t	activate = B_FALSE;
740 	boolean_t	is_snap = B_FALSE;
741 	int		c;
742 	int		err = 1;
743 	char		*obe_name = NULL;
744 	char		*snap_name = NULL;
745 	char		*nbe_zpool = NULL;
746 	char		*nbe_name = NULL;
747 	char		*nbe_desc = NULL;
748 	char		*propname = NULL;
749 	char		*propval = NULL;
750 	char		*strval = NULL;
751 
752 	while ((c = getopt(argc, argv, "ad:e:io:p:v")) != -1) {
753 		switch (c) {
754 		case 'a':
755 			activate = B_TRUE;
756 			break;
757 		case 'd':
758 			nbe_desc = optarg;
759 			break;
760 		case 'e':
761 			obe_name = optarg;
762 			break;
763 		case 'o':
764 			if (zfs_props == NULL && be_nvl_alloc(&zfs_props) != 0)
765 				return (1);
766 
767 			propname = optarg;
768 			if ((propval = strchr(propname, '=')) == NULL) {
769 				(void) fprintf(stderr, _("missing "
770 				    "'=' for -o option\n"));
771 				goto out2;
772 			}
773 			*propval = '\0';
774 			propval++;
775 			if (nvlist_lookup_string(zfs_props, propname,
776 			    &strval) == 0) {
777 				(void) fprintf(stderr, _("property '%s' "
778 				    "specified multiple times\n"), propname);
779 				goto out2;
780 
781 			}
782 			if (be_nvl_add_string(zfs_props, propname, propval)
783 			    != 0)
784 				goto out2;
785 
786 			break;
787 		case 'p':
788 			nbe_zpool = optarg;
789 			break;
790 		case 'v':
791 			libbe_print_errors(B_TRUE);
792 			break;
793 		default:
794 			usage();
795 			goto out2;
796 		}
797 	}
798 
799 	argc -= optind;
800 	argv += optind;
801 
802 	if (argc != 1) {
803 		usage();
804 		goto out2;
805 	}
806 
807 	nbe_name = argv[0];
808 
809 	if ((snap_name = strrchr(nbe_name, '@')) != NULL) {
810 		if (snap_name[1] == '\0') {
811 			usage();
812 			goto out2;
813 		}
814 
815 		snap_name[0] = '\0';
816 		snap_name++;
817 		is_snap = B_TRUE;
818 	}
819 
820 	if (obe_name) {
821 		if (is_snap) {
822 			usage();
823 			goto out2;
824 		}
825 
826 		/*
827 		 * Check if obe_name is really a snapshot name.
828 		 * If so, split it out.
829 		 */
830 		if ((snap_name = strrchr(obe_name, '@')) != NULL) {
831 			if (snap_name[1] == '\0') {
832 				usage();
833 				goto out2;
834 			}
835 
836 			snap_name[0] = '\0';
837 			snap_name++;
838 		}
839 	} else if (is_snap) {
840 		obe_name = nbe_name;
841 		nbe_name = NULL;
842 	}
843 
844 	if (be_nvl_alloc(&be_attrs) != 0)
845 		goto out2;
846 
847 
848 	if (zfs_props != NULL && be_nvl_add_nvlist(be_attrs,
849 	    BE_ATTR_ORIG_BE_NAME, zfs_props) != 0)
850 		goto out;
851 
852 	if (obe_name != NULL && be_nvl_add_string(be_attrs,
853 	    BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
854 		goto out;
855 
856 	if (snap_name != NULL && be_nvl_add_string(be_attrs,
857 	    BE_ATTR_SNAP_NAME, snap_name) != 0)
858 		goto out;
859 
860 	if (nbe_zpool != NULL && be_nvl_add_string(be_attrs,
861 	    BE_ATTR_NEW_BE_POOL, nbe_zpool) != 0)
862 		goto out;
863 
864 	if (nbe_name != NULL && be_nvl_add_string(be_attrs,
865 	    BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
866 		goto out;
867 
868 	if (nbe_desc != NULL && be_nvl_add_string(be_attrs,
869 	    BE_ATTR_NEW_BE_DESC, nbe_desc) != 0)
870 		goto out;
871 
872 	if (is_snap)
873 		err = be_create_snapshot(be_attrs);
874 	else
875 		err = be_copy(be_attrs);
876 
877 	switch (err) {
878 	case BE_SUCCESS:
879 		if (!is_snap && !nbe_name) {
880 			/*
881 			 * We requested an auto named BE; find out the
882 			 * name of the BE that was created for us and
883 			 * the auto snapshot created from the original BE.
884 			 */
885 			if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME,
886 			    &nbe_name) != 0) {
887 				(void) fprintf(stderr, _("failed to get %s "
888 				    "attribute\n"), BE_ATTR_NEW_BE_NAME);
889 				break;
890 			} else
891 				(void) printf(_("Auto named BE: %s\n"),
892 				    nbe_name);
893 
894 			if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME,
895 			    &snap_name) != 0) {
896 				(void) fprintf(stderr, _("failed to get %s "
897 				    "attribute\n"), BE_ATTR_SNAP_NAME);
898 				break;
899 			} else
900 				(void) printf(_("Auto named snapshot: %s\n"),
901 				    snap_name);
902 		}
903 
904 		if (!is_snap && activate) {
905 			char *args[] = { "activate", "", NULL };
906 			args[1] = nbe_name;
907 			optind = 1;
908 
909 			err = be_do_activate(2, args);
910 			goto out;
911 		}
912 
913 		(void) printf(_("Created successfully\n"));
914 		break;
915 	case BE_ERR_BE_EXISTS:
916 		(void) fprintf(stderr, _("BE %s already exists\n."
917 		    "Please choose a different BE name.\n"), nbe_name);
918 		break;
919 	case BE_ERR_SS_EXISTS:
920 		(void) fprintf(stderr, _("BE %s snapshot %s already exists.\n"
921 		    "Please choose a different snapshot name.\n"), obe_name,
922 		    snap_name);
923 		break;
924 	case BE_ERR_PERM:
925 	case BE_ERR_ACCESS:
926 		if (is_snap)
927 			(void) fprintf(stderr, _("Unable to create snapshot "
928 			    "%s.\n"), snap_name);
929 		else
930 			(void) fprintf(stderr, _("Unable to create %s.\n"),
931 			    nbe_name);
932 		(void) fprintf(stderr, _("You have insufficient privileges to "
933 		    "execute this command.\n"));
934 		break;
935 	default:
936 		if (is_snap)
937 			(void) fprintf(stderr, _("Unable to create snapshot "
938 			    "%s.\n"), snap_name);
939 		else
940 			(void) fprintf(stderr, _("Unable to create %s.\n"),
941 			    nbe_name);
942 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
943 	}
944 
945 out:
946 	nvlist_free(be_attrs);
947 out2:
948 	nvlist_free(zfs_props);
949 
950 	return (err);
951 }
952 
953 static int
954 be_do_destroy(int argc, char **argv)
955 {
956 	nvlist_t	*be_attrs;
957 	boolean_t	is_snap = B_FALSE;
958 	boolean_t	suppress_prompt = B_FALSE;
959 	int		err = 1;
960 	int		c;
961 	int		destroy_flags = 0;
962 	char		*snap_name;
963 	char		*be_name;
964 
965 	while ((c = getopt(argc, argv, "fFsv")) != -1) {
966 		switch (c) {
967 		case 'f':
968 			destroy_flags |= BE_DESTROY_FLAG_FORCE_UNMOUNT;
969 			break;
970 		case 's':
971 			destroy_flags |= BE_DESTROY_FLAG_SNAPSHOTS;
972 			break;
973 		case 'v':
974 			libbe_print_errors(B_TRUE);
975 			break;
976 		case 'F':
977 			suppress_prompt = B_TRUE;
978 			break;
979 		default:
980 			usage();
981 			return (1);
982 		}
983 	}
984 
985 	argc -= optind;
986 	argv += optind;
987 
988 	if (argc != 1) {
989 		usage();
990 		return (1);
991 	}
992 
993 	be_name = argv[0];
994 	if (!suppress_prompt && !confirm_destroy(be_name)) {
995 		(void) printf(_("%s has not been destroyed.\n"), be_name);
996 		return (0);
997 	}
998 
999 	if ((snap_name = strrchr(be_name, '@')) != NULL) {
1000 		if (snap_name[1] == '\0') {
1001 			usage();
1002 			return (1);
1003 		}
1004 
1005 		is_snap = B_TRUE;
1006 		*snap_name = '\0';
1007 		snap_name++;
1008 	}
1009 
1010 	if (be_nvl_alloc(&be_attrs) != 0)
1011 		return (1);
1012 
1013 
1014 	if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, be_name) != 0)
1015 		goto out;
1016 
1017 	if (is_snap) {
1018 		if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME,
1019 		    snap_name) != 0)
1020 			goto out;
1021 
1022 		err = be_destroy_snapshot(be_attrs);
1023 	} else {
1024 		if (be_nvl_add_uint16(be_attrs, BE_ATTR_DESTROY_FLAGS,
1025 		    destroy_flags) != 0)
1026 			goto out;
1027 
1028 		err = be_destroy(be_attrs);
1029 	}
1030 
1031 	switch (err) {
1032 	case BE_SUCCESS:
1033 		(void) printf(_("Destroyed successfully\n"));
1034 		break;
1035 	case BE_ERR_MOUNTED:
1036 		(void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1037 		(void) fprintf(stderr, _("It is currently mounted and must be "
1038 		    "unmounted before it can be destroyed.\n" "Use 'beadm "
1039 		    "unmount %s' to unmount the BE before destroying\nit or "
1040 		    "'beadm destroy -f %s'.\n"), be_name, be_name);
1041 		break;
1042 	case BE_ERR_DESTROY_CURR_BE:
1043 		(void) fprintf(stderr, _("%s is the currently active BE and "
1044 		    "cannot be destroyed.\nYou must boot from another BE in "
1045 		    "order to destroy %s.\n"), be_name, be_name);
1046 		break;
1047 	case BE_ERR_ZONES_UNMOUNT:
1048 		(void) fprintf(stderr, _("Unable to destroy one of " "%s's "
1049 		    "zone BE's.\nUse 'beadm destroy -f %s' or "
1050 		    "'zfs -f destroy <dataset>'.\n"), be_name, be_name);
1051 		break;
1052 	case BE_ERR_SS_NOENT:
1053 		(void) fprintf(stderr, _("%s does not exist or appear "
1054 		    "to be a valid snapshot.\nPlease check that the name of "
1055 		    "the snapshot provided is correct.\n"), snap_name);
1056 		break;
1057 	case BE_ERR_PERM:
1058 	case BE_ERR_ACCESS:
1059 		(void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1060 		(void) fprintf(stderr, _("You have insufficient privileges to "
1061 		    "execute this command.\n"));
1062 		break;
1063 	case BE_ERR_SS_EXISTS:
1064 		(void) fprintf(stderr, _("Unable to destroy %s: "
1065 		    "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1066 		    "'zfs -r destroy <dataset>'.\n"), be_name, be_name);
1067 		break;
1068 	default:
1069 		(void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1070 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
1071 	}
1072 
1073 out:
1074 	nvlist_free(be_attrs);
1075 	return (err);
1076 }
1077 
1078 static int
1079 be_do_list(int argc, char **argv)
1080 {
1081 	be_node_list_t	*be_nodes = NULL;
1082 	boolean_t	all = B_FALSE;
1083 	boolean_t	dsets = B_FALSE;
1084 	boolean_t	snaps = B_FALSE;
1085 	boolean_t	parsable = B_FALSE;
1086 	int		err = 1;
1087 	int		c = 0;
1088 	char		*be_name = NULL;
1089 	be_sort_t	order = BE_SORT_UNSPECIFIED;
1090 
1091 	while ((c = getopt(argc, argv, "adk:svHK:")) != -1) {
1092 		switch (c) {
1093 		case 'a':
1094 			all = B_TRUE;
1095 			break;
1096 		case 'd':
1097 			dsets = B_TRUE;
1098 			break;
1099 		case 'k':
1100 		case 'K':
1101 			if (order != BE_SORT_UNSPECIFIED) {
1102 				(void) fprintf(stderr, _("Sort key can be "
1103 				    "specified only once.\n"));
1104 				usage();
1105 				return (1);
1106 			}
1107 			if (strcmp(optarg, "date") == 0) {
1108 				if (c == 'k')
1109 					order = BE_SORT_DATE;
1110 				else
1111 					order = BE_SORT_DATE_REV;
1112 				break;
1113 			}
1114 			if (strcmp(optarg, "name") == 0) {
1115 				if (c == 'k')
1116 					order = BE_SORT_NAME;
1117 				else
1118 					order = BE_SORT_NAME_REV;
1119 				break;
1120 			}
1121 			if (strcmp(optarg, "space") == 0) {
1122 				if (c == 'k')
1123 					order = BE_SORT_SPACE;
1124 				else
1125 					order = BE_SORT_SPACE_REV;
1126 				break;
1127 			}
1128 			(void) fprintf(stderr, _("Unknown sort key: %s\n"),
1129 			    optarg);
1130 			usage();
1131 			return (1);
1132 		case 's':
1133 			snaps = B_TRUE;
1134 			break;
1135 		case 'v':
1136 			libbe_print_errors(B_TRUE);
1137 			break;
1138 		case 'H':
1139 			parsable = B_TRUE;
1140 			break;
1141 		default:
1142 			usage();
1143 			return (1);
1144 		}
1145 	}
1146 
1147 	if (all) {
1148 		if (dsets) {
1149 			(void) fprintf(stderr, _("Invalid options: -a and %s "
1150 			    "are mutually exclusive.\n"), "-d");
1151 			usage();
1152 			return (1);
1153 		}
1154 		if (snaps) {
1155 			(void) fprintf(stderr, _("Invalid options: -a and %s "
1156 			    "are mutually exclusive.\n"), "-s");
1157 			usage();
1158 			return (1);
1159 		}
1160 
1161 		dsets = B_TRUE;
1162 		snaps = B_TRUE;
1163 	}
1164 
1165 	argc -= optind;
1166 	argv += optind;
1167 
1168 
1169 	if (argc == 1)
1170 		be_name = argv[0];
1171 
1172 	err = be_list(be_name, &be_nodes);
1173 
1174 	switch (err) {
1175 	case BE_SUCCESS:
1176 		/* the default sort is ascending date, no need to sort twice */
1177 		if (order == BE_SORT_UNSPECIFIED)
1178 			order = BE_SORT_DATE;
1179 
1180 		if (order != BE_SORT_DATE) {
1181 			err = be_sort(&be_nodes, order);
1182 			if (err != BE_SUCCESS) {
1183 				(void) fprintf(stderr, _("Unable to sort Boot "
1184 				    "Environment\n"));
1185 				(void) fprintf(stderr, "%s\n",
1186 				    be_err_to_str(err));
1187 				break;
1188 			}
1189 		}
1190 
1191 		print_nodes(be_name, dsets, snaps, parsable, be_nodes);
1192 		break;
1193 	case BE_ERR_BE_NOENT:
1194 		if (be_name == NULL)
1195 			(void) fprintf(stderr, _("No boot environments found "
1196 			    "on this system.\n"));
1197 		else {
1198 			(void) fprintf(stderr, _("%s does not exist or appear "
1199 			    "to be a valid BE.\nPlease check that the name of "
1200 			    "the BE provided is correct.\n"), be_name);
1201 		}
1202 		break;
1203 	default:
1204 		(void) fprintf(stderr, _("Unable to display Boot "
1205 		    "Environment\n"));
1206 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
1207 	}
1208 
1209 	if (be_nodes != NULL)
1210 		be_free_list(be_nodes);
1211 	return (err);
1212 }
1213 
1214 static int
1215 be_do_mount(int argc, char **argv)
1216 {
1217 	nvlist_t	*be_attrs;
1218 	boolean_t	shared_fs = B_FALSE;
1219 	int		err = 1;
1220 	int		c;
1221 	int		mount_flags = 0;
1222 	char		*obe_name;
1223 	char		*mountpoint;
1224 	char		*tmp_mp = NULL;
1225 
1226 	while ((c = getopt(argc, argv, "s:v")) != -1) {
1227 		switch (c) {
1228 		case 's':
1229 			shared_fs = B_TRUE;
1230 
1231 			mount_flags |= BE_MOUNT_FLAG_SHARED_FS;
1232 
1233 			if (strcmp(optarg, "rw") == 0) {
1234 				mount_flags |= BE_MOUNT_FLAG_SHARED_RW;
1235 			} else if (strcmp(optarg, "ro") != 0) {
1236 				(void) fprintf(stderr, _("The -s flag "
1237 				    "requires an argument [ rw | ro ]\n"));
1238 				usage();
1239 				return (1);
1240 			}
1241 
1242 			break;
1243 		case 'v':
1244 			libbe_print_errors(B_TRUE);
1245 			break;
1246 		default:
1247 			usage();
1248 			return (1);
1249 		}
1250 	}
1251 
1252 	argc -= optind;
1253 	argv += optind;
1254 
1255 	if (argc < 1 || argc > 2) {
1256 		usage();
1257 		return (1);
1258 	}
1259 
1260 	obe_name = argv[0];
1261 
1262 	if (argc == 2) {
1263 		mountpoint = argv[1];
1264 		if (mountpoint[0] != '/') {
1265 			(void) fprintf(stderr, _("Invalid mount point %s. "
1266 			    "Mount point must start with a /.\n"), mountpoint);
1267 			return (1);
1268 		}
1269 	} else {
1270 		const char *tmpdir = getenv("TMPDIR");
1271 		const char *tmpname = "tmp.XXXXXX";
1272 		int sz;
1273 
1274 		if (tmpdir == NULL)
1275 			tmpdir = "/tmp";
1276 
1277 		sz = asprintf(&tmp_mp, "%s/%s", tmpdir, tmpname);
1278 		if (sz < 0) {
1279 			(void) fprintf(stderr, _("internal error: "
1280 			    "out of memory\n"));
1281 			return (1);
1282 		}
1283 
1284 		mountpoint = mkdtemp(tmp_mp);
1285 	}
1286 
1287 	if (be_nvl_alloc(&be_attrs) != 0)
1288 		return (1);
1289 
1290 	if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1291 		goto out;
1292 
1293 	if (be_nvl_add_string(be_attrs, BE_ATTR_MOUNTPOINT, mountpoint) != 0)
1294 		goto out;
1295 
1296 	if (shared_fs && be_nvl_add_uint16(be_attrs, BE_ATTR_MOUNT_FLAGS,
1297 	    mount_flags) != 0)
1298 		goto out;
1299 
1300 	err = be_mount(be_attrs);
1301 
1302 	switch (err) {
1303 	case BE_SUCCESS:
1304 		(void) printf(_("Mounted successfully on: '%s'\n"), mountpoint);
1305 		break;
1306 	case BE_ERR_BE_NOENT:
1307 		(void) fprintf(stderr, _("%s does not exist or appear "
1308 		    "to be a valid BE.\nPlease check that the name of "
1309 		    "the BE provided is correct.\n"), obe_name);
1310 		break;
1311 	case BE_ERR_MOUNTED:
1312 		(void) fprintf(stderr, _("%s is already mounted.\n"
1313 		    "Please unmount the BE before mounting it again.\n"),
1314 		    obe_name);
1315 		break;
1316 	case BE_ERR_PERM:
1317 	case BE_ERR_ACCESS:
1318 		(void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1319 		(void) fprintf(stderr, _("You have insufficient privileges to "
1320 		    "execute this command.\n"));
1321 		break;
1322 	case BE_ERR_NO_MOUNTED_ZONE:
1323 		(void) fprintf(stderr, _("Mounted on '%s'.\nUnable to mount "
1324 		    "one of %s's zone BE's.\n"), mountpoint, obe_name);
1325 		break;
1326 	default:
1327 		(void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1328 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
1329 	}
1330 
1331 out:
1332 	if (tmp_mp != NULL)
1333 		free(tmp_mp);
1334 	nvlist_free(be_attrs);
1335 	return (err);
1336 }
1337 
1338 static int
1339 be_do_unmount(int argc, char **argv)
1340 {
1341 	nvlist_t	*be_attrs;
1342 	char		*obe_name;
1343 	int		err = 1;
1344 	int		c;
1345 	int		unmount_flags = 0;
1346 
1347 	while ((c = getopt(argc, argv, "fv")) != -1) {
1348 		switch (c) {
1349 		case 'f':
1350 			unmount_flags |= BE_UNMOUNT_FLAG_FORCE;
1351 			break;
1352 		case 'v':
1353 			libbe_print_errors(B_TRUE);
1354 			break;
1355 		default:
1356 			usage();
1357 			return (1);
1358 		}
1359 	}
1360 
1361 	argc -= optind;
1362 	argv += optind;
1363 
1364 	if (argc != 1) {
1365 		usage();
1366 		return (1);
1367 	}
1368 
1369 	obe_name = argv[0];
1370 
1371 	if (be_nvl_alloc(&be_attrs) != 0)
1372 		return (1);
1373 
1374 
1375 	if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1376 		goto out;
1377 
1378 	if (be_nvl_add_uint16(be_attrs, BE_ATTR_UNMOUNT_FLAGS,
1379 	    unmount_flags) != 0)
1380 		goto out;
1381 
1382 	err = be_unmount(be_attrs);
1383 
1384 	switch (err) {
1385 	case BE_SUCCESS:
1386 		(void) printf(_("Unmounted successfully\n"));
1387 		break;
1388 	case BE_ERR_BE_NOENT:
1389 		(void) fprintf(stderr, _("%s does not exist or appear "
1390 		    "to be a valid BE.\nPlease check that the name of "
1391 		    "the BE provided is correct.\n"), obe_name);
1392 		break;
1393 	case BE_ERR_UMOUNT_CURR_BE:
1394 		(void) fprintf(stderr, _("%s is the currently active BE.\n"
1395 		    "It cannot be unmounted unless another BE is the "
1396 		    "currently active BE.\n"), obe_name);
1397 		break;
1398 	case BE_ERR_UMOUNT_SHARED:
1399 		(void) fprintf(stderr, _("%s is a shared file system and it "
1400 		    "cannot be unmounted.\n"), obe_name);
1401 		break;
1402 	case BE_ERR_PERM:
1403 	case BE_ERR_ACCESS:
1404 		(void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1405 		(void) fprintf(stderr, _("You have insufficient privileges to "
1406 		    "execute this command.\n"));
1407 		break;
1408 	default:
1409 		(void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1410 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
1411 	}
1412 
1413 out:
1414 	nvlist_free(be_attrs);
1415 	return (err);
1416 }
1417 
1418 static int
1419 be_do_rename(int argc, char **argv)
1420 {
1421 	nvlist_t	*be_attrs;
1422 	char		*obe_name;
1423 	char		*nbe_name;
1424 	int err = 1;
1425 	int c;
1426 
1427 	while ((c = getopt(argc, argv, "v")) != -1) {
1428 		switch (c) {
1429 		case 'v':
1430 			libbe_print_errors(B_TRUE);
1431 			break;
1432 		default:
1433 			usage();
1434 			return (1);
1435 		}
1436 	}
1437 
1438 	argc -= optind;
1439 	argv += optind;
1440 
1441 	if (argc != 2) {
1442 		usage();
1443 		return (1);
1444 	}
1445 
1446 	obe_name = argv[0];
1447 	nbe_name = argv[1];
1448 
1449 	if (be_nvl_alloc(&be_attrs) != 0)
1450 		return (1);
1451 
1452 	if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1453 		goto out;
1454 
1455 	if (be_nvl_add_string(be_attrs, BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
1456 		goto out;
1457 
1458 	err = be_rename(be_attrs);
1459 
1460 	switch (err) {
1461 	case BE_SUCCESS:
1462 		(void) printf(_("Renamed successfully\n"));
1463 		break;
1464 	case BE_ERR_BE_NOENT:
1465 		(void) fprintf(stderr, _("%s does not exist or appear "
1466 		    "to be a valid BE.\nPlease check that the name of "
1467 		    "the BE provided is correct.\n"), obe_name);
1468 		break;
1469 	case BE_ERR_PERM:
1470 	case BE_ERR_ACCESS:
1471 		(void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1472 		    obe_name);
1473 		(void) fprintf(stderr, _("You have insufficient privileges to "
1474 		    "execute this command.\n"));
1475 		break;
1476 	default:
1477 		(void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1478 		    obe_name);
1479 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
1480 	}
1481 
1482 out:
1483 	nvlist_free(be_attrs);
1484 	return (err);
1485 }
1486 
1487 static int
1488 be_do_rollback(int argc, char **argv)
1489 {
1490 	nvlist_t	*be_attrs;
1491 	char		*obe_name;
1492 	char		*snap_name;
1493 	int		err = 1;
1494 	int		c;
1495 
1496 	while ((c = getopt(argc, argv, "v")) != -1) {
1497 		switch (c) {
1498 		case 'v':
1499 			libbe_print_errors(B_TRUE);
1500 			break;
1501 		default:
1502 			usage();
1503 			return (1);
1504 		}
1505 	}
1506 
1507 	argc -= optind;
1508 	argv += optind;
1509 
1510 	if (argc < 1 || argc > 2) {
1511 		usage();
1512 		return (1);
1513 	}
1514 
1515 	obe_name = argv[0];
1516 	if (argc == 2)
1517 		snap_name = argv[1];
1518 	else { /* argc == 1 */
1519 		if ((snap_name = strrchr(obe_name, '@')) != NULL) {
1520 			if (snap_name[1] == '\0') {
1521 				usage();
1522 				return (1);
1523 			}
1524 
1525 			snap_name[0] = '\0';
1526 			snap_name++;
1527 		} else {
1528 			usage();
1529 			return (1);
1530 		}
1531 	}
1532 
1533 	if (be_nvl_alloc(&be_attrs) != 0)
1534 		return (1);
1535 
1536 	if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1537 		goto out;
1538 
1539 	if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME, snap_name) != 0)
1540 		goto out;
1541 
1542 	err = be_rollback(be_attrs);
1543 
1544 	switch (err) {
1545 	case BE_SUCCESS:
1546 		(void) printf(_("Rolled back successfully\n"));
1547 		break;
1548 	case BE_ERR_BE_NOENT:
1549 		(void) fprintf(stderr, _("%s does not exist or appear "
1550 		    "to be a valid BE.\nPlease check that the name of "
1551 		    "the BE provided is correct.\n"), obe_name);
1552 		break;
1553 	case BE_ERR_SS_NOENT:
1554 		(void) fprintf(stderr, _("%s does not exist or appear "
1555 		    "to be a valid snapshot.\nPlease check that the name of "
1556 		    "the snapshot provided is correct.\n"), snap_name);
1557 		break;
1558 	case BE_ERR_PERM:
1559 	case BE_ERR_ACCESS:
1560 		(void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1561 		    "failed.\n"), obe_name, snap_name);
1562 		(void) fprintf(stderr, _("You have insufficient privileges to "
1563 		    "execute this command.\n"));
1564 		break;
1565 	default:
1566 		(void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1567 		    "failed.\n"), obe_name, snap_name);
1568 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
1569 	}
1570 
1571 out:
1572 	nvlist_free(be_attrs);
1573 	return (err);
1574 }
1575