xref: /titanic_41/usr/src/cmd/beadm/beadm.c (revision 5e2c3ae0c70f6eb4a42ae670882aabac983cb5f1)
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 		be_snapshot_list_t *snap = NULL;
373 		uint64_t used = cur_be->be_space_used;
374 		time_t creation = cur_be->be_node_creation;
375 		struct tm *tm;
376 
377 		if (be_name != NULL && strcmp(be_name, name) != 0)
378 			continue;
379 
380 		if (parsable)
381 			active[0] = '\0';
382 
383 		tm = localtime(&creation);
384 		(void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
385 
386 		for (snap = cur_be->be_node_snapshots; snap != NULL;
387 		    snap = snap->be_next_snapshot)
388 			used += snap->be_snapshot_space_used;
389 
390 		if (!cur_be->be_global_active)
391 			active[ai++] = 'x';
392 
393 		if (cur_be->be_active)
394 			active[ai++] = 'N';
395 		if (cur_be->be_active_on_boot) {
396 			if (!cur_be->be_global_active)
397 				active[ai] = 'b';
398 			else
399 				active[ai] = 'R';
400 		}
401 
402 		nicenum(used, buf, sizeof (buf));
403 		if (parsable)
404 			(void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
405 			    name,
406 			    cur_be->be_uuid_str,
407 			    active,
408 			    (cur_be->be_mounted ? mntpt: ""),
409 			    used,
410 			    cur_be->be_policy_type,
411 			    creation);
412 		else
413 			(void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
414 			    hdr->cols[0].width, name,
415 			    hdr->cols[1].width, active,
416 			    hdr->cols[2].width, (cur_be->be_mounted ? mntpt:
417 			    "-"),
418 			    hdr->cols[3].width, buf,
419 			    hdr->cols[4].width, cur_be->be_policy_type,
420 			    hdr->cols[5].width, datetime);
421 	}
422 }
423 
424 static void
425 print_be_snapshots(be_node_list_t *be, struct hdr_info *hdr, boolean_t parsable)
426 {
427 	char buf[64];
428 	char datetime[DT_BUF_LEN];
429 	be_snapshot_list_t *snap = NULL;
430 
431 	for (snap = be->be_node_snapshots; snap != NULL;
432 	    snap = snap->be_next_snapshot) {
433 		char name[ZFS_MAX_DATASET_NAME_LEN + 1];
434 		const char *datetime_fmt = "%F %R";
435 		const char *be_name = be->be_node_name;
436 		const char *root_ds = be->be_root_ds;
437 		const char *snap_name = snap->be_snapshot_name;
438 		char *pos;
439 		uint64_t used = snap->be_snapshot_space_used;
440 		time_t creation = snap->be_snapshot_creation;
441 		struct tm *tm = localtime(&creation);
442 
443 		(void) strncpy(name, root_ds, sizeof (name));
444 		pos = strstr(name, be_name);
445 		(void) strcpy(pos, snap_name);
446 
447 		(void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
448 		nicenum(used, buf, sizeof (buf));
449 
450 		if (parsable)
451 			if (hdr->cols[1].width != 0)
452 				(void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
453 				    be_name,
454 				    snap_name,
455 				    "",
456 				    "",
457 				    used,
458 				    be->be_policy_type,
459 				    creation);
460 			else
461 				(void) printf("%s;%s;%llu;%s;%ld\n",
462 				    be_name,
463 				    snap_name,
464 				    used,
465 				    be->be_policy_type,
466 				    creation);
467 		else
468 			if (hdr->cols[1].width != 0)
469 				(void) printf("   %-*s %-*s %-*s %-*s %-*s "
470 				    "%-*s\n",
471 				    hdr->cols[0].width-3, name,
472 				    hdr->cols[1].width, "-",
473 				    hdr->cols[2].width, "-",
474 				    hdr->cols[3].width, buf,
475 				    hdr->cols[4].width, be->be_policy_type,
476 				    hdr->cols[5].width, datetime);
477 			else
478 				(void) printf("   %-*s %-*s %-*s %-*s\n",
479 				    hdr->cols[0].width-3, snap_name,
480 				    hdr->cols[3].width, buf,
481 				    hdr->cols[4].width, be->be_policy_type,
482 				    hdr->cols[5].width, datetime);
483 	}
484 }
485 
486 static void
487 print_fmt_nodes(const char *be_name, enum be_fmt be_fmt, boolean_t parsable,
488     struct hdr_info *hdr, be_node_list_t *nodes)
489 {
490 	char buf[64];
491 	char datetime[DT_BUF_LEN];
492 	be_node_list_t	*cur_be;
493 
494 	for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
495 		char active[3] = "-\0";
496 		int ai = 0;
497 		const char *datetime_fmt = "%F %R";
498 		const char *name = cur_be->be_node_name;
499 		const char *mntpt = cur_be->be_mntpt;
500 		uint64_t used = cur_be->be_space_used;
501 		time_t creation = cur_be->be_node_creation;
502 		struct tm *tm;
503 
504 		if (be_name != NULL && strcmp(be_name, name) != 0)
505 			continue;
506 
507 		if (!parsable)
508 			(void) printf("%-s\n", name);
509 		else
510 			active[0] = '\0';
511 
512 		tm = localtime(&creation);
513 		(void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
514 
515 		if (cur_be->be_active)
516 			active[ai++] = 'N';
517 		if (cur_be->be_active_on_boot)
518 			active[ai] = 'R';
519 
520 		nicenum(used, buf, sizeof (buf));
521 		if (be_fmt & BE_FMT_DATASET)
522 			if (parsable)
523 				(void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
524 				    cur_be->be_node_name,
525 				    cur_be->be_root_ds,
526 				    active,
527 				    (cur_be->be_mounted ? mntpt: ""),
528 				    used,
529 				    cur_be->be_policy_type,
530 				    creation);
531 			else
532 				(void) printf("   %-*s %-*s %-*s %-*s %-*s "
533 				    "%-*s\n",
534 				    hdr->cols[0].width-3, cur_be->be_root_ds,
535 				    hdr->cols[1].width, active,
536 				    hdr->cols[2].width, (cur_be->be_mounted ?
537 				    mntpt: "-"),
538 				    hdr->cols[3].width, buf,
539 				    hdr->cols[4].width, cur_be->be_policy_type,
540 				    hdr->cols[5].width, datetime);
541 
542 		if (be_fmt & BE_FMT_SNAPSHOT)
543 			print_be_snapshots(cur_be, hdr, parsable);
544 	}
545 }
546 
547 static void
548 print_nodes(const char *be_name, boolean_t dsets, boolean_t snaps,
549     boolean_t parsable, be_node_list_t *be_nodes)
550 {
551 	struct hdr_info hdr;
552 	enum be_fmt be_fmt  = BE_FMT_DEFAULT;
553 
554 	if (dsets)
555 		be_fmt |= BE_FMT_DATASET;
556 	if (snaps)
557 		be_fmt |= BE_FMT_SNAPSHOT;
558 
559 	if (!parsable) {
560 		init_hdr_cols(be_fmt, &hdr);
561 		count_widths(be_fmt, &hdr, be_nodes);
562 		print_hdr(&hdr);
563 	}
564 
565 	if (be_fmt == BE_FMT_DEFAULT)
566 		print_be_nodes(be_name, parsable, &hdr, be_nodes);
567 	else
568 		print_fmt_nodes(be_name, be_fmt, parsable, &hdr, be_nodes);
569 }
570 
571 static boolean_t
572 confirm_destroy(const char *name)
573 {
574 	boolean_t res = B_FALSE;
575 	const char *yesre = nl_langinfo(YESEXPR);
576 	const char *nore = nl_langinfo(NOEXPR);
577 	regex_t yes_re;
578 	regex_t no_re;
579 	char buf[128];
580 	char *answer;
581 	int cflags = REG_EXTENDED;
582 
583 	if (regcomp(&yes_re, yesre, cflags) != 0) {
584 		/* should not happen */
585 		(void) fprintf(stderr, _("Failed to compile 'yes' regexp\n"));
586 		return (res);
587 	}
588 	if (regcomp(&no_re, nore, cflags) != 0) {
589 		/* should not happen */
590 		(void) fprintf(stderr, _("Failed to compile 'no' regexp\n"));
591 		regfree(&yes_re);
592 		return (res);
593 	}
594 
595 	(void) printf(_("Are you sure you want to destroy %s?\n"
596 	    "This action cannot be undone (y/[n]): "), name);
597 
598 	answer = fgets(buf, sizeof (buf), stdin);
599 	if (answer == NULL || *answer == '\0' || *answer == 10)
600 		goto out;
601 
602 	if (regexec(&yes_re, answer, 0, NULL, 0) == 0) {
603 		res = B_TRUE;
604 	} else if (regexec(&no_re, answer, 0, NULL, 0) != 0) {
605 		(void) fprintf(stderr, _("Invalid response. "
606 		    "Please enter 'y' or 'n'.\n"));
607 	}
608 
609 out:
610 	regfree(&yes_re);
611 	regfree(&no_re);
612 	return (res);
613 }
614 
615 static int
616 be_nvl_alloc(nvlist_t **nvlp)
617 {
618 	assert(nvlp != NULL);
619 
620 	if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) {
621 		(void) perror(_("nvlist_alloc failed.\n"));
622 		return (1);
623 	}
624 
625 	return (0);
626 }
627 
628 static int
629 be_nvl_add_string(nvlist_t *nvl, const char *name, const char *val)
630 {
631 	assert(nvl != NULL);
632 
633 	if (nvlist_add_string(nvl, name, val) != 0) {
634 		(void) fprintf(stderr, _("nvlist_add_string failed for "
635 		    "%s (%s).\n"), name, val);
636 		return (1);
637 	}
638 
639 	return (0);
640 }
641 
642 static int
643 be_nvl_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
644 {
645 	assert(nvl != NULL);
646 
647 	if (nvlist_add_nvlist(nvl, name, val) != 0) {
648 		(void) fprintf(stderr, _("nvlist_add_nvlist failed for %s.\n"),
649 		    name);
650 		return (1);
651 	}
652 
653 	return (0);
654 }
655 
656 static int
657 be_nvl_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
658 {
659 	assert(nvl != NULL);
660 
661 	if (nvlist_add_uint16(nvl, name, val) != 0) {
662 		(void) fprintf(stderr, _("nvlist_add_uint16 failed for "
663 		    "%s (%hu).\n"), name, val);
664 		return (1);
665 	}
666 
667 	return (0);
668 }
669 
670 static int
671 be_do_activate(int argc, char **argv)
672 {
673 	nvlist_t	*be_attrs;
674 	int		err = 1;
675 	int		c;
676 	char		*obe_name;
677 
678 	while ((c = getopt(argc, argv, "v")) != -1) {
679 		switch (c) {
680 		case 'v':
681 			libbe_print_errors(B_TRUE);
682 			break;
683 		default:
684 			usage();
685 			return (1);
686 		}
687 	}
688 
689 	argc -= optind;
690 	argv += optind;
691 
692 	if (argc != 1) {
693 		usage();
694 		return (1);
695 	}
696 
697 	obe_name = argv[0];
698 
699 	if (be_nvl_alloc(&be_attrs) != 0)
700 		return (1);
701 
702 	if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
703 		goto out;
704 
705 	err = be_activate(be_attrs);
706 
707 	switch (err) {
708 	case BE_SUCCESS:
709 		(void) printf(_("Activated successfully\n"));
710 		break;
711 	case BE_ERR_BE_NOENT:
712 		(void) fprintf(stderr, _("%s does not exist or appear "
713 		    "to be a valid BE.\nPlease check that the name of "
714 		    "the BE provided is correct.\n"), obe_name);
715 		break;
716 	case BE_ERR_PERM:
717 	case BE_ERR_ACCESS:
718 		(void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
719 		(void) fprintf(stderr, _("You have insufficient privileges to "
720 		    "execute this command.\n"));
721 		break;
722 	case BE_ERR_ACTIVATE_CURR:
723 	default:
724 		(void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
725 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
726 	}
727 
728 out:
729 	nvlist_free(be_attrs);
730 	return (err);
731 }
732 
733 static int
734 be_do_create(int argc, char **argv)
735 {
736 	nvlist_t	*be_attrs;
737 	nvlist_t	*zfs_props = NULL;
738 	boolean_t	activate = B_FALSE;
739 	boolean_t	is_snap = B_FALSE;
740 	int		c;
741 	int		err = 1;
742 	char		*obe_name = NULL;
743 	char		*snap_name = NULL;
744 	char		*nbe_zpool = NULL;
745 	char		*nbe_name = NULL;
746 	char		*nbe_desc = NULL;
747 	char		*propname = NULL;
748 	char		*propval = NULL;
749 	char		*strval = NULL;
750 
751 	while ((c = getopt(argc, argv, "ad:e:io:p:v")) != -1) {
752 		switch (c) {
753 		case 'a':
754 			activate = B_TRUE;
755 			break;
756 		case 'd':
757 			nbe_desc = optarg;
758 			break;
759 		case 'e':
760 			obe_name = optarg;
761 			break;
762 		case 'o':
763 			if (zfs_props == NULL && be_nvl_alloc(&zfs_props) != 0)
764 				return (1);
765 
766 			propname = optarg;
767 			if ((propval = strchr(propname, '=')) == NULL) {
768 				(void) fprintf(stderr, _("missing "
769 				    "'=' for -o option\n"));
770 				goto out2;
771 			}
772 			*propval = '\0';
773 			propval++;
774 			if (nvlist_lookup_string(zfs_props, propname,
775 			    &strval) == 0) {
776 				(void) fprintf(stderr, _("property '%s' "
777 				    "specified multiple times\n"), propname);
778 				goto out2;
779 
780 			}
781 			if (be_nvl_add_string(zfs_props, propname, propval)
782 			    != 0)
783 				goto out2;
784 
785 			break;
786 		case 'p':
787 			nbe_zpool = optarg;
788 			break;
789 		case 'v':
790 			libbe_print_errors(B_TRUE);
791 			break;
792 		default:
793 			usage();
794 			goto out2;
795 		}
796 	}
797 
798 	argc -= optind;
799 	argv += optind;
800 
801 	if (argc != 1) {
802 		usage();
803 		goto out2;
804 	}
805 
806 	nbe_name = argv[0];
807 
808 	if ((snap_name = strrchr(nbe_name, '@')) != NULL) {
809 		if (snap_name[1] == '\0') {
810 			usage();
811 			goto out2;
812 		}
813 
814 		snap_name[0] = '\0';
815 		snap_name++;
816 		is_snap = B_TRUE;
817 	}
818 
819 	if (obe_name) {
820 		if (is_snap) {
821 			usage();
822 			goto out2;
823 		}
824 
825 		/*
826 		 * Check if obe_name is really a snapshot name.
827 		 * If so, split it out.
828 		 */
829 		if ((snap_name = strrchr(obe_name, '@')) != NULL) {
830 			if (snap_name[1] == '\0') {
831 				usage();
832 				goto out2;
833 			}
834 
835 			snap_name[0] = '\0';
836 			snap_name++;
837 		}
838 	} else if (is_snap) {
839 		obe_name = nbe_name;
840 		nbe_name = NULL;
841 	}
842 
843 	if (be_nvl_alloc(&be_attrs) != 0)
844 		goto out2;
845 
846 
847 	if (zfs_props != NULL && be_nvl_add_nvlist(be_attrs,
848 	    BE_ATTR_ORIG_BE_NAME, zfs_props) != 0)
849 		goto out;
850 
851 	if (obe_name != NULL && be_nvl_add_string(be_attrs,
852 	    BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
853 		goto out;
854 
855 	if (snap_name != NULL && be_nvl_add_string(be_attrs,
856 	    BE_ATTR_SNAP_NAME, snap_name) != 0)
857 		goto out;
858 
859 	if (nbe_zpool != NULL && be_nvl_add_string(be_attrs,
860 	    BE_ATTR_NEW_BE_POOL, nbe_zpool) != 0)
861 		goto out;
862 
863 	if (nbe_name != NULL && be_nvl_add_string(be_attrs,
864 	    BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
865 		goto out;
866 
867 	if (nbe_desc != NULL && be_nvl_add_string(be_attrs,
868 	    BE_ATTR_NEW_BE_DESC, nbe_desc) != 0)
869 		goto out;
870 
871 	if (is_snap)
872 		err = be_create_snapshot(be_attrs);
873 	else
874 		err = be_copy(be_attrs);
875 
876 	switch (err) {
877 	case BE_SUCCESS:
878 		if (!is_snap && !nbe_name) {
879 			/*
880 			 * We requested an auto named BE; find out the
881 			 * name of the BE that was created for us and
882 			 * the auto snapshot created from the original BE.
883 			 */
884 			if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME,
885 			    &nbe_name) != 0) {
886 				(void) fprintf(stderr, _("failed to get %s "
887 				    "attribute\n"), BE_ATTR_NEW_BE_NAME);
888 				break;
889 			} else
890 				(void) printf(_("Auto named BE: %s\n"),
891 				    nbe_name);
892 
893 			if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME,
894 			    &snap_name) != 0) {
895 				(void) fprintf(stderr, _("failed to get %s "
896 				    "attribute\n"), BE_ATTR_SNAP_NAME);
897 				break;
898 			} else
899 				(void) printf(_("Auto named snapshot: %s\n"),
900 				    snap_name);
901 		}
902 
903 		if (!is_snap && activate) {
904 			char *args[] = { "activate", "", NULL };
905 			args[1] = nbe_name;
906 			optind = 1;
907 
908 			err = be_do_activate(2, args);
909 			goto out;
910 		}
911 
912 		(void) printf(_("Created successfully\n"));
913 		break;
914 	case BE_ERR_BE_EXISTS:
915 		(void) fprintf(stderr, _("BE %s already exists\n."
916 		    "Please choose a different BE name.\n"), nbe_name);
917 		break;
918 	case BE_ERR_SS_EXISTS:
919 		(void) fprintf(stderr, _("BE %s snapshot %s already exists.\n"
920 		    "Please choose a different snapshot name.\n"), obe_name,
921 		    snap_name);
922 		break;
923 	case BE_ERR_PERM:
924 	case BE_ERR_ACCESS:
925 		if (is_snap)
926 			(void) fprintf(stderr, _("Unable to create snapshot "
927 			    "%s.\n"), snap_name);
928 		else
929 			(void) fprintf(stderr, _("Unable to create %s.\n"),
930 			    nbe_name);
931 		(void) fprintf(stderr, _("You have insufficient privileges to "
932 		    "execute this command.\n"));
933 		break;
934 	default:
935 		if (is_snap)
936 			(void) fprintf(stderr, _("Unable to create snapshot "
937 			    "%s.\n"), snap_name);
938 		else
939 			(void) fprintf(stderr, _("Unable to create %s.\n"),
940 			    nbe_name);
941 		(void) fprintf(stderr, "%s\n", be_err_to_str(err));
942 	}
943 
944 out:
945 	nvlist_free(be_attrs);
946 out2:
947 	if (zfs_props != NULL)
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