remove xvm/ipagent
[unleashed.git] / usr / src / cmd / beadm / beadm.c
blob0dedfd2b4126dbc0a96e5ba3d6e64434d7e76aca
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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.
31 * System includes
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>
44 #include "libbe.h"
46 #define _(x) gettext(x)
48 #ifndef TEXT_DOMAIN
49 #define TEXT_DOMAIN "SYS_TEST"
50 #endif
52 #define DT_BUF_LEN (128)
53 #define NUM_COLS (6)
55 static int be_do_activate(int argc, char **argv);
56 static int be_do_create(int argc, char **argv);
57 static int be_do_destroy(int argc, char **argv);
58 static int be_do_list(int argc, char **argv);
59 static int be_do_mount(int argc, char **argv);
60 static int be_do_unmount(int argc, char **argv);
61 static int be_do_rename(int argc, char **argv);
62 static int be_do_rollback(int argc, char **argv);
63 static void usage(void);
66 * single column name/width output format description
68 struct col_info {
69 const char *col_name;
70 size_t width;
74 * all columns output format
76 struct hdr_info {
77 struct col_info cols[NUM_COLS];
81 * type of possible output formats
83 enum be_fmt {
84 BE_FMT_DEFAULT,
85 BE_FMT_DATASET,
86 BE_FMT_SNAPSHOT,
87 BE_FMT_ALL
91 * command handler description
93 typedef struct be_command {
94 const char *name;
95 int (*func)(int argc, char **argv);
96 } be_command_t;
99 * sorted list of be commands
101 static const be_command_t be_command_tbl[] = {
102 { "activate", be_do_activate },
103 { "create", be_do_create },
104 { "destroy", be_do_destroy },
105 { "list", be_do_list },
106 { "mount", be_do_mount },
107 { "unmount", be_do_unmount },
108 { "umount", be_do_unmount }, /* unmount alias */
109 { "rename", be_do_rename },
110 { "rollback", be_do_rollback },
111 { NULL, NULL },
114 static void
115 usage(void)
117 (void) fprintf(stderr, _("usage:\n"
118 "\tbeadm subcommand cmd_options\n"
119 "\n"
120 "\tsubcommands:\n"
121 "\n"
122 "\tbeadm activate [-v] beName\n"
123 "\tbeadm create [-a] [-d BE_desc]\n"
124 "\t\t[-o property=value] ... [-p zpool] \n"
125 "\t\t[-e nonActiveBe | beName@snapshot] [-v] beName\n"
126 "\tbeadm create [-d BE_desc]\n"
127 "\t\t[-o property=value] ... [-p zpool] [-v] beName@snapshot\n"
128 "\tbeadm destroy [-Ffsv] beName \n"
129 "\tbeadm destroy [-Fv] beName@snapshot \n"
130 "\tbeadm list [[-a] | [-d] [-s]] [-H]\n"
131 "\t\t[-k|-K date | name | space] [-v] [beName]\n"
132 "\tbeadm mount [-s ro|rw] [-v] beName [mountpoint]\n"
133 "\tbeadm unmount [-fv] beName | mountpoint\n"
134 "\tbeadm umount [-fv] beName | mountpoint\n"
135 "\tbeadm rename [-v] origBeName newBeName\n"
136 "\tbeadm rollback [-v] beName snapshot\n"
137 "\tbeadm rollback [-v] beName@snapshot\n"));
140 static int
141 run_be_cmd(const char *cmdname, int argc, char **argv)
143 const be_command_t *command;
145 for (command = &be_command_tbl[0]; command->name != NULL; command++)
146 if (strcmp(command->name, cmdname) == 0)
147 return (command->func(argc, argv));
149 (void) fprintf(stderr, _("Invalid command: %s\n"), cmdname);
150 usage();
151 return (1);
155 main(int argc, char **argv)
157 const char *cmdname;
159 (void) setlocale(LC_ALL, "");
160 (void) textdomain(TEXT_DOMAIN);
162 if (argc < 2) {
163 usage();
164 return (1);
167 cmdname = argv[1];
169 /* Turn error printing off */
170 libbe_print_errors(B_FALSE);
172 return (run_be_cmd(cmdname, --argc, ++argv));
175 static void
176 print_hdr(struct hdr_info *hdr_info)
178 boolean_t first = B_TRUE;
179 size_t i;
180 for (i = 0; i < NUM_COLS; i++) {
181 struct col_info *col_info = &hdr_info->cols[i];
182 const char *name = col_info->col_name;
183 size_t width = col_info->width;
184 if (name == NULL)
185 continue;
187 if (first) {
188 (void) printf("%-*s", width, name);
189 first = B_FALSE;
190 } else
191 (void) printf(" %-*s", width, name);
193 (void) putchar('\n');
196 static void
197 init_hdr_cols(enum be_fmt be_fmt, struct hdr_info *hdr)
199 struct col_info *col = hdr->cols;
200 size_t i;
202 col[1].col_name = _("Active");
203 col[2].col_name = _("Mountpoint");
204 col[3].col_name = _("Space");
205 col[4].col_name = _("Policy");
206 col[5].col_name = _("Created");
207 col[6].col_name = NULL;
209 switch (be_fmt) {
210 case BE_FMT_ALL:
211 col[0].col_name = _("BE/Dataset/Snapshot");
212 break;
213 case BE_FMT_DATASET:
214 col[0].col_name = _("BE/Dataset");
215 break;
216 case BE_FMT_SNAPSHOT:
217 col[0].col_name = _("BE/Snapshot");
218 col[1].col_name = NULL;
219 col[2].col_name = NULL;
220 break;
221 case BE_FMT_DEFAULT:
222 default:
223 col[0].col_name = _("BE");
226 for (i = 0; i < NUM_COLS; i++) {
227 const char *name = col[i].col_name;
228 col[i].width = 0;
230 if (name != NULL) {
231 wchar_t wname[128];
232 size_t sz = mbstowcs(wname, name, sizeof (wname) /
233 sizeof (wchar_t));
234 if (sz > 0) {
235 int wcsw = wcswidth(wname, sz);
236 if (wcsw > 0)
237 col[i].width = wcsw;
238 else
239 col[i].width = sz;
240 } else {
241 col[i].width = strlen(name);
247 static void
248 nicenum(uint64_t num, char *buf, size_t buflen)
250 uint64_t n = num;
251 int index = 0;
252 char u;
254 while (n >= 1024) {
255 n /= 1024;
256 index++;
259 u = " KMGTPE"[index];
261 if (index == 0) {
262 (void) snprintf(buf, buflen, "%llu", n);
263 } else {
264 int i;
265 for (i = 2; i >= 0; i--) {
266 if (snprintf(buf, buflen, "%.*f%c", i,
267 (double)num / (1ULL << 10 * index), u) <= 5)
268 break;
273 static void
274 count_widths(enum be_fmt be_fmt, struct hdr_info *hdr, be_node_list_t *be_nodes)
276 size_t len[NUM_COLS];
277 char buf[DT_BUF_LEN];
278 int i;
279 be_node_list_t *cur_be;
281 for (i = 0; i < NUM_COLS; i++)
282 len[i] = hdr->cols[i].width;
284 for (cur_be = be_nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
285 char name[ZFS_MAX_DATASET_NAME_LEN + 1];
286 const char *be_name = cur_be->be_node_name;
287 const char *root_ds = cur_be->be_root_ds;
288 char *pos;
289 size_t node_name_len = strlen(cur_be->be_node_name);
290 size_t root_ds_len = strlen(cur_be->be_root_ds);
291 size_t mntpt_len = 0;
292 size_t policy_len = 0;
293 size_t used_len;
294 uint64_t used = cur_be->be_space_used;
295 be_snapshot_list_t *snap = NULL;
297 if (cur_be->be_mntpt != NULL)
298 mntpt_len = strlen(cur_be->be_mntpt);
299 if (cur_be->be_policy_type != NULL)
300 policy_len = strlen(cur_be->be_policy_type);
302 (void) strlcpy(name, root_ds, sizeof (name));
303 pos = strstr(name, be_name);
305 if (be_fmt == BE_FMT_DEFAULT) {
306 if (node_name_len > len[0])
307 len[0] = node_name_len;
308 } else {
309 if (root_ds_len + 3 > len[0])
310 len[0] = root_ds_len + 3;
313 if (mntpt_len > len[2])
314 len[2] = mntpt_len;
315 if (policy_len > len[4])
316 len[4] = policy_len;
318 for (snap = cur_be->be_node_snapshots; snap != NULL;
319 snap = snap->be_next_snapshot) {
320 uint64_t snap_used = snap->be_snapshot_space_used;
321 const char *snap_name = snap->be_snapshot_name;
322 (void) strcpy(pos, snap_name);
324 if (be_fmt == BE_FMT_DEFAULT)
325 used += snap_used;
326 else if (be_fmt & BE_FMT_SNAPSHOT) {
327 int snap_len = strlen(name) + 3;
328 if (be_fmt == BE_FMT_SNAPSHOT)
329 snap_len -= pos - name;
330 if (snap_len > len[0])
331 len[0] = snap_len;
332 nicenum(snap_used, buf, sizeof (buf));
333 used_len = strlen(buf);
334 if (used_len > len[3])
335 len[3] = used_len;
339 if (be_fmt == BE_FMT_DEFAULT) {
340 int used_len;
341 nicenum(used, buf, sizeof (buf));
342 used_len = strlen(buf);
343 if (used_len > len[3])
344 len[3] = used_len;
347 nicenum(used, buf, sizeof (buf));
350 for (i = 0; i < NUM_COLS; i++)
351 hdr->cols[i].width = len[i];
354 static void
355 print_be_nodes(const char *be_name, boolean_t parsable, struct hdr_info *hdr,
356 be_node_list_t *nodes)
358 char buf[64];
359 char datetime[DT_BUF_LEN];
360 be_node_list_t *cur_be;
362 for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
363 char active[3] = "-\0";
364 int ai = 0;
365 const char *datetime_fmt = "%F %R";
366 const char *name = cur_be->be_node_name;
367 const char *mntpt = cur_be->be_mntpt;
368 const char *uuid_str = cur_be->be_uuid_str;
369 be_snapshot_list_t *snap = NULL;
370 uint64_t used = cur_be->be_space_used;
371 time_t creation = cur_be->be_node_creation;
372 struct tm *tm;
374 if (be_name != NULL && strcmp(be_name, name) != 0)
375 continue;
377 if (parsable)
378 active[0] = '\0';
380 tm = localtime(&creation);
381 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
383 for (snap = cur_be->be_node_snapshots; snap != NULL;
384 snap = snap->be_next_snapshot)
385 used += snap->be_snapshot_space_used;
387 if (!cur_be->be_global_active)
388 active[ai++] = 'x';
390 if (cur_be->be_active)
391 active[ai++] = 'N';
392 if (cur_be->be_active_on_boot) {
393 if (!cur_be->be_global_active)
394 active[ai] = 'b';
395 else
396 active[ai] = 'R';
399 nicenum(used, buf, sizeof (buf));
400 if (parsable)
401 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
402 name,
403 (uuid_str != NULL ? uuid_str: ""),
404 active,
405 (cur_be->be_mounted ? mntpt: ""),
406 used,
407 cur_be->be_policy_type,
408 creation);
409 else
410 (void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
411 hdr->cols[0].width, name,
412 hdr->cols[1].width, active,
413 hdr->cols[2].width, (cur_be->be_mounted ? mntpt:
414 "-"),
415 hdr->cols[3].width, buf,
416 hdr->cols[4].width, cur_be->be_policy_type,
417 hdr->cols[5].width, datetime);
421 static void
422 print_be_snapshots(be_node_list_t *be, struct hdr_info *hdr, boolean_t parsable)
424 char buf[64];
425 char datetime[DT_BUF_LEN];
426 be_snapshot_list_t *snap = NULL;
428 for (snap = be->be_node_snapshots; snap != NULL;
429 snap = snap->be_next_snapshot) {
430 char name[ZFS_MAX_DATASET_NAME_LEN + 1];
431 const char *datetime_fmt = "%F %R";
432 const char *be_name = be->be_node_name;
433 const char *root_ds = be->be_root_ds;
434 const char *snap_name = snap->be_snapshot_name;
435 char *pos;
436 uint64_t used = snap->be_snapshot_space_used;
437 time_t creation = snap->be_snapshot_creation;
438 struct tm *tm = localtime(&creation);
440 (void) strncpy(name, root_ds, sizeof (name));
441 pos = strstr(name, be_name);
442 (void) strcpy(pos, snap_name);
444 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
445 nicenum(used, buf, sizeof (buf));
447 if (parsable)
448 if (hdr->cols[1].width != 0)
449 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
450 be_name,
451 snap_name,
454 used,
455 be->be_policy_type,
456 creation);
457 else
458 (void) printf("%s;%s;%llu;%s;%ld\n",
459 be_name,
460 snap_name,
461 used,
462 be->be_policy_type,
463 creation);
464 else
465 if (hdr->cols[1].width != 0)
466 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
467 "%-*s\n",
468 hdr->cols[0].width-3, name,
469 hdr->cols[1].width, "-",
470 hdr->cols[2].width, "-",
471 hdr->cols[3].width, buf,
472 hdr->cols[4].width, be->be_policy_type,
473 hdr->cols[5].width, datetime);
474 else
475 (void) printf(" %-*s %-*s %-*s %-*s\n",
476 hdr->cols[0].width-3, snap_name,
477 hdr->cols[3].width, buf,
478 hdr->cols[4].width, be->be_policy_type,
479 hdr->cols[5].width, datetime);
483 static void
484 print_fmt_nodes(const char *be_name, enum be_fmt be_fmt, boolean_t parsable,
485 struct hdr_info *hdr, be_node_list_t *nodes)
487 char buf[64];
488 char datetime[DT_BUF_LEN];
489 be_node_list_t *cur_be;
491 for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
492 char active[3] = "-\0";
493 int ai = 0;
494 const char *datetime_fmt = "%F %R";
495 const char *name = cur_be->be_node_name;
496 const char *mntpt = cur_be->be_mntpt;
497 uint64_t used = cur_be->be_space_used;
498 time_t creation = cur_be->be_node_creation;
499 struct tm *tm;
501 if (be_name != NULL && strcmp(be_name, name) != 0)
502 continue;
504 if (!parsable)
505 (void) printf("%-s\n", name);
506 else
507 active[0] = '\0';
509 tm = localtime(&creation);
510 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
512 if (cur_be->be_active)
513 active[ai++] = 'N';
514 if (cur_be->be_active_on_boot)
515 active[ai] = 'R';
517 nicenum(used, buf, sizeof (buf));
518 if (be_fmt & BE_FMT_DATASET)
519 if (parsable)
520 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
521 cur_be->be_node_name,
522 cur_be->be_root_ds,
523 active,
524 (cur_be->be_mounted ? mntpt: ""),
525 used,
526 cur_be->be_policy_type,
527 creation);
528 else
529 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
530 "%-*s\n",
531 hdr->cols[0].width-3, cur_be->be_root_ds,
532 hdr->cols[1].width, active,
533 hdr->cols[2].width, (cur_be->be_mounted ?
534 mntpt: "-"),
535 hdr->cols[3].width, buf,
536 hdr->cols[4].width, cur_be->be_policy_type,
537 hdr->cols[5].width, datetime);
539 if (be_fmt & BE_FMT_SNAPSHOT)
540 print_be_snapshots(cur_be, hdr, parsable);
544 static void
545 print_nodes(const char *be_name, boolean_t dsets, boolean_t snaps,
546 boolean_t parsable, be_node_list_t *be_nodes)
548 struct hdr_info hdr;
549 enum be_fmt be_fmt = BE_FMT_DEFAULT;
551 if (dsets)
552 be_fmt |= BE_FMT_DATASET;
553 if (snaps)
554 be_fmt |= BE_FMT_SNAPSHOT;
556 if (!parsable) {
557 init_hdr_cols(be_fmt, &hdr);
558 count_widths(be_fmt, &hdr, be_nodes);
559 print_hdr(&hdr);
562 if (be_fmt == BE_FMT_DEFAULT)
563 print_be_nodes(be_name, parsable, &hdr, be_nodes);
564 else
565 print_fmt_nodes(be_name, be_fmt, parsable, &hdr, be_nodes);
568 static boolean_t
569 confirm_destroy(const char *name)
571 boolean_t res = B_FALSE;
572 const char *yesre = nl_langinfo(YESEXPR);
573 const char *nore = nl_langinfo(NOEXPR);
574 regex_t yes_re;
575 regex_t no_re;
576 char buf[128];
577 char *answer;
578 int cflags = REG_EXTENDED;
580 if (regcomp(&yes_re, yesre, cflags) != 0) {
581 /* should not happen */
582 (void) fprintf(stderr, _("Failed to compile 'yes' regexp\n"));
583 return (res);
585 if (regcomp(&no_re, nore, cflags) != 0) {
586 /* should not happen */
587 (void) fprintf(stderr, _("Failed to compile 'no' regexp\n"));
588 regfree(&yes_re);
589 return (res);
592 (void) printf(_("Are you sure you want to destroy %s?\n"
593 "This action cannot be undone (y/[n]): "), name);
595 answer = fgets(buf, sizeof (buf), stdin);
596 if (answer == NULL || *answer == '\0' || *answer == 10)
597 goto out;
599 if (regexec(&yes_re, answer, 0, NULL, 0) == 0) {
600 res = B_TRUE;
601 } else if (regexec(&no_re, answer, 0, NULL, 0) != 0) {
602 (void) fprintf(stderr, _("Invalid response. "
603 "Please enter 'y' or 'n'.\n"));
606 out:
607 regfree(&yes_re);
608 regfree(&no_re);
609 return (res);
612 static int
613 be_nvl_alloc(nvlist_t **nvlp)
615 assert(nvlp != NULL);
617 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) {
618 (void) perror(_("nvlist_alloc failed.\n"));
619 return (1);
622 return (0);
625 static int
626 be_nvl_add_string(nvlist_t *nvl, const char *name, const char *val)
628 assert(nvl != NULL);
630 if (nvlist_add_string(nvl, name, val) != 0) {
631 (void) fprintf(stderr, _("nvlist_add_string failed for "
632 "%s (%s).\n"), name, val);
633 return (1);
636 return (0);
639 static int
640 be_nvl_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
642 assert(nvl != NULL);
644 if (nvlist_add_nvlist(nvl, name, val) != 0) {
645 (void) fprintf(stderr, _("nvlist_add_nvlist failed for %s.\n"),
646 name);
647 return (1);
650 return (0);
653 static int
654 be_nvl_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
656 assert(nvl != NULL);
658 if (nvlist_add_uint16(nvl, name, val) != 0) {
659 (void) fprintf(stderr, _("nvlist_add_uint16 failed for "
660 "%s (%hu).\n"), name, val);
661 return (1);
664 return (0);
667 static int
668 be_do_activate(int argc, char **argv)
670 nvlist_t *be_attrs;
671 int err = 1;
672 int c;
673 char *obe_name;
675 while ((c = getopt(argc, argv, "v")) != -1) {
676 switch (c) {
677 case 'v':
678 libbe_print_errors(B_TRUE);
679 break;
680 default:
681 usage();
682 return (1);
686 argc -= optind;
687 argv += optind;
689 if (argc != 1) {
690 usage();
691 return (1);
694 obe_name = argv[0];
696 if (be_nvl_alloc(&be_attrs) != 0)
697 return (1);
699 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
700 goto out;
702 err = be_activate(be_attrs);
704 switch (err) {
705 case BE_SUCCESS:
706 (void) printf(_("Activated successfully\n"));
707 break;
708 case BE_ERR_BE_NOENT:
709 (void) fprintf(stderr, _("%s does not exist or appear "
710 "to be a valid BE.\nPlease check that the name of "
711 "the BE provided is correct.\n"), obe_name);
712 break;
713 case BE_ERR_PERM:
714 case BE_ERR_ACCESS:
715 (void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
716 (void) fprintf(stderr, _("You have insufficient privileges to "
717 "execute this command.\n"));
718 break;
719 case BE_ERR_ACTIVATE_CURR:
720 default:
721 (void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
722 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
725 out:
726 nvlist_free(be_attrs);
727 return (err);
730 static int
731 be_do_create(int argc, char **argv)
733 nvlist_t *be_attrs;
734 nvlist_t *zfs_props = NULL;
735 boolean_t activate = B_FALSE;
736 boolean_t is_snap = B_FALSE;
737 int c;
738 int err = 1;
739 char *obe_name = NULL;
740 char *snap_name = NULL;
741 char *nbe_zpool = NULL;
742 char *nbe_name = NULL;
743 char *nbe_desc = NULL;
744 char *propname = NULL;
745 char *propval = NULL;
746 char *strval = NULL;
748 while ((c = getopt(argc, argv, "ad:e:io:p:v")) != -1) {
749 switch (c) {
750 case 'a':
751 activate = B_TRUE;
752 break;
753 case 'd':
754 nbe_desc = optarg;
755 break;
756 case 'e':
757 obe_name = optarg;
758 break;
759 case 'o':
760 if (zfs_props == NULL && be_nvl_alloc(&zfs_props) != 0)
761 return (1);
763 propname = optarg;
764 if ((propval = strchr(propname, '=')) == NULL) {
765 (void) fprintf(stderr, _("missing "
766 "'=' for -o option\n"));
767 goto out2;
769 *propval = '\0';
770 propval++;
771 if (nvlist_lookup_string(zfs_props, propname,
772 &strval) == 0) {
773 (void) fprintf(stderr, _("property '%s' "
774 "specified multiple times\n"), propname);
775 goto out2;
778 if (be_nvl_add_string(zfs_props, propname, propval)
779 != 0)
780 goto out2;
782 break;
783 case 'p':
784 nbe_zpool = optarg;
785 break;
786 case 'v':
787 libbe_print_errors(B_TRUE);
788 break;
789 default:
790 usage();
791 goto out2;
795 argc -= optind;
796 argv += optind;
798 if (argc != 1) {
799 usage();
800 goto out2;
803 nbe_name = argv[0];
805 if ((snap_name = strrchr(nbe_name, '@')) != NULL) {
806 if (snap_name[1] == '\0') {
807 usage();
808 goto out2;
811 snap_name[0] = '\0';
812 snap_name++;
813 is_snap = B_TRUE;
816 if (obe_name) {
817 if (is_snap) {
818 usage();
819 goto out2;
823 * Check if obe_name is really a snapshot name.
824 * If so, split it out.
826 if ((snap_name = strrchr(obe_name, '@')) != NULL) {
827 if (snap_name[1] == '\0') {
828 usage();
829 goto out2;
832 snap_name[0] = '\0';
833 snap_name++;
835 } else if (is_snap) {
836 obe_name = nbe_name;
837 nbe_name = NULL;
840 if (be_nvl_alloc(&be_attrs) != 0)
841 goto out2;
844 if (zfs_props != NULL && be_nvl_add_nvlist(be_attrs,
845 BE_ATTR_ORIG_BE_NAME, zfs_props) != 0)
846 goto out;
848 if (obe_name != NULL && be_nvl_add_string(be_attrs,
849 BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
850 goto out;
852 if (snap_name != NULL && be_nvl_add_string(be_attrs,
853 BE_ATTR_SNAP_NAME, snap_name) != 0)
854 goto out;
856 if (nbe_zpool != NULL && be_nvl_add_string(be_attrs,
857 BE_ATTR_NEW_BE_POOL, nbe_zpool) != 0)
858 goto out;
860 if (nbe_name != NULL && be_nvl_add_string(be_attrs,
861 BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
862 goto out;
864 if (nbe_desc != NULL && be_nvl_add_string(be_attrs,
865 BE_ATTR_NEW_BE_DESC, nbe_desc) != 0)
866 goto out;
868 if (is_snap)
869 err = be_create_snapshot(be_attrs);
870 else
871 err = be_copy(be_attrs);
873 switch (err) {
874 case BE_SUCCESS:
875 if (!is_snap && !nbe_name) {
877 * We requested an auto named BE; find out the
878 * name of the BE that was created for us and
879 * the auto snapshot created from the original BE.
881 if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME,
882 &nbe_name) != 0) {
883 (void) fprintf(stderr, _("failed to get %s "
884 "attribute\n"), BE_ATTR_NEW_BE_NAME);
885 break;
886 } else
887 (void) printf(_("Auto named BE: %s\n"),
888 nbe_name);
890 if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME,
891 &snap_name) != 0) {
892 (void) fprintf(stderr, _("failed to get %s "
893 "attribute\n"), BE_ATTR_SNAP_NAME);
894 break;
895 } else
896 (void) printf(_("Auto named snapshot: %s\n"),
897 snap_name);
900 if (!is_snap && activate) {
901 char *args[] = { "activate", "", NULL };
902 args[1] = nbe_name;
903 optind = 1;
905 err = be_do_activate(2, args);
906 goto out;
909 (void) printf(_("Created successfully\n"));
910 break;
911 case BE_ERR_BE_EXISTS:
912 (void) fprintf(stderr, _("BE %s already exists\n."
913 "Please choose a different BE name.\n"), nbe_name);
914 break;
915 case BE_ERR_SS_EXISTS:
916 (void) fprintf(stderr, _("BE %s snapshot %s already exists.\n"
917 "Please choose a different snapshot name.\n"), obe_name,
918 snap_name);
919 break;
920 case BE_ERR_PERM:
921 case BE_ERR_ACCESS:
922 if (is_snap)
923 (void) fprintf(stderr, _("Unable to create snapshot "
924 "%s.\n"), snap_name);
925 else
926 (void) fprintf(stderr, _("Unable to create %s.\n"),
927 nbe_name);
928 (void) fprintf(stderr, _("You have insufficient privileges to "
929 "execute this command.\n"));
930 break;
931 default:
932 if (is_snap)
933 (void) fprintf(stderr, _("Unable to create snapshot "
934 "%s.\n"), snap_name);
935 else
936 (void) fprintf(stderr, _("Unable to create %s.\n"),
937 nbe_name);
938 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
941 out:
942 nvlist_free(be_attrs);
943 out2:
944 nvlist_free(zfs_props);
946 return (err);
949 static int
950 be_do_destroy(int argc, char **argv)
952 nvlist_t *be_attrs;
953 boolean_t is_snap = B_FALSE;
954 boolean_t suppress_prompt = B_FALSE;
955 int err = 1;
956 int c;
957 int destroy_flags = 0;
958 char *snap_name;
959 char *be_name;
961 while ((c = getopt(argc, argv, "fFsv")) != -1) {
962 switch (c) {
963 case 'f':
964 destroy_flags |= BE_DESTROY_FLAG_FORCE_UNMOUNT;
965 break;
966 case 's':
967 destroy_flags |= BE_DESTROY_FLAG_SNAPSHOTS;
968 break;
969 case 'v':
970 libbe_print_errors(B_TRUE);
971 break;
972 case 'F':
973 suppress_prompt = B_TRUE;
974 break;
975 default:
976 usage();
977 return (1);
981 argc -= optind;
982 argv += optind;
984 if (argc != 1) {
985 usage();
986 return (1);
989 be_name = argv[0];
990 if (!suppress_prompt && !confirm_destroy(be_name)) {
991 (void) printf(_("%s has not been destroyed.\n"), be_name);
992 return (0);
995 if ((snap_name = strrchr(be_name, '@')) != NULL) {
996 if (snap_name[1] == '\0') {
997 usage();
998 return (1);
1001 is_snap = B_TRUE;
1002 *snap_name = '\0';
1003 snap_name++;
1006 if (be_nvl_alloc(&be_attrs) != 0)
1007 return (1);
1010 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, be_name) != 0)
1011 goto out;
1013 if (is_snap) {
1014 if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME,
1015 snap_name) != 0)
1016 goto out;
1018 err = be_destroy_snapshot(be_attrs);
1019 } else {
1020 if (be_nvl_add_uint16(be_attrs, BE_ATTR_DESTROY_FLAGS,
1021 destroy_flags) != 0)
1022 goto out;
1024 err = be_destroy(be_attrs);
1027 switch (err) {
1028 case BE_SUCCESS:
1029 (void) printf(_("Destroyed successfully\n"));
1030 break;
1031 case BE_ERR_MOUNTED:
1032 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1033 (void) fprintf(stderr, _("It is currently mounted and must be "
1034 "unmounted before it can be destroyed.\n" "Use 'beadm "
1035 "unmount %s' to unmount the BE before destroying\nit or "
1036 "'beadm destroy -f %s'.\n"), be_name, be_name);
1037 break;
1038 case BE_ERR_DESTROY_CURR_BE:
1039 (void) fprintf(stderr, _("%s is the currently active BE and "
1040 "cannot be destroyed.\nYou must boot from another BE in "
1041 "order to destroy %s.\n"), be_name, be_name);
1042 break;
1043 case BE_ERR_ZONES_UNMOUNT:
1044 (void) fprintf(stderr, _("Unable to destroy one of " "%s's "
1045 "zone BE's.\nUse 'beadm destroy -f %s' or "
1046 "'zfs -f destroy <dataset>'.\n"), be_name, be_name);
1047 break;
1048 case BE_ERR_SS_NOENT:
1049 (void) fprintf(stderr, _("%s does not exist or appear "
1050 "to be a valid snapshot.\nPlease check that the name of "
1051 "the snapshot provided is correct.\n"), snap_name);
1052 break;
1053 case BE_ERR_PERM:
1054 case BE_ERR_ACCESS:
1055 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1056 (void) fprintf(stderr, _("You have insufficient privileges to "
1057 "execute this command.\n"));
1058 break;
1059 case BE_ERR_SS_EXISTS:
1060 (void) fprintf(stderr, _("Unable to destroy %s: "
1061 "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1062 "'zfs -r destroy <dataset>'.\n"), be_name, be_name);
1063 break;
1064 default:
1065 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1066 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1069 out:
1070 nvlist_free(be_attrs);
1071 return (err);
1074 static int
1075 be_do_list(int argc, char **argv)
1077 be_node_list_t *be_nodes = NULL;
1078 boolean_t all = B_FALSE;
1079 boolean_t dsets = B_FALSE;
1080 boolean_t snaps = B_FALSE;
1081 boolean_t parsable = B_FALSE;
1082 int err = 1;
1083 int c = 0;
1084 char *be_name = NULL;
1085 be_sort_t order = BE_SORT_UNSPECIFIED;
1087 while ((c = getopt(argc, argv, "adk:svHK:")) != -1) {
1088 switch (c) {
1089 case 'a':
1090 all = B_TRUE;
1091 break;
1092 case 'd':
1093 dsets = B_TRUE;
1094 break;
1095 case 'k':
1096 case 'K':
1097 if (order != BE_SORT_UNSPECIFIED) {
1098 (void) fprintf(stderr, _("Sort key can be "
1099 "specified only once.\n"));
1100 usage();
1101 return (1);
1103 if (strcmp(optarg, "date") == 0) {
1104 if (c == 'k')
1105 order = BE_SORT_DATE;
1106 else
1107 order = BE_SORT_DATE_REV;
1108 break;
1110 if (strcmp(optarg, "name") == 0) {
1111 if (c == 'k')
1112 order = BE_SORT_NAME;
1113 else
1114 order = BE_SORT_NAME_REV;
1115 break;
1117 if (strcmp(optarg, "space") == 0) {
1118 if (c == 'k')
1119 order = BE_SORT_SPACE;
1120 else
1121 order = BE_SORT_SPACE_REV;
1122 break;
1124 (void) fprintf(stderr, _("Unknown sort key: %s\n"),
1125 optarg);
1126 usage();
1127 return (1);
1128 case 's':
1129 snaps = B_TRUE;
1130 break;
1131 case 'v':
1132 libbe_print_errors(B_TRUE);
1133 break;
1134 case 'H':
1135 parsable = B_TRUE;
1136 break;
1137 default:
1138 usage();
1139 return (1);
1143 if (all) {
1144 if (dsets) {
1145 (void) fprintf(stderr, _("Invalid options: -a and %s "
1146 "are mutually exclusive.\n"), "-d");
1147 usage();
1148 return (1);
1150 if (snaps) {
1151 (void) fprintf(stderr, _("Invalid options: -a and %s "
1152 "are mutually exclusive.\n"), "-s");
1153 usage();
1154 return (1);
1157 dsets = B_TRUE;
1158 snaps = B_TRUE;
1161 argc -= optind;
1162 argv += optind;
1165 if (argc == 1)
1166 be_name = argv[0];
1168 err = be_list(be_name, &be_nodes);
1170 switch (err) {
1171 case BE_SUCCESS:
1172 /* the default sort is ascending date, no need to sort twice */
1173 if (order == BE_SORT_UNSPECIFIED)
1174 order = BE_SORT_DATE;
1176 if (order != BE_SORT_DATE) {
1177 err = be_sort(&be_nodes, order);
1178 if (err != BE_SUCCESS) {
1179 (void) fprintf(stderr, _("Unable to sort Boot "
1180 "Environment\n"));
1181 (void) fprintf(stderr, "%s\n",
1182 be_err_to_str(err));
1183 break;
1187 print_nodes(be_name, dsets, snaps, parsable, be_nodes);
1188 break;
1189 case BE_ERR_BE_NOENT:
1190 if (be_name == NULL)
1191 (void) fprintf(stderr, _("No boot environments found "
1192 "on this system.\n"));
1193 else {
1194 (void) fprintf(stderr, _("%s does not exist or appear "
1195 "to be a valid BE.\nPlease check that the name of "
1196 "the BE provided is correct.\n"), be_name);
1198 break;
1199 default:
1200 (void) fprintf(stderr, _("Unable to display Boot "
1201 "Environment\n"));
1202 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1205 if (be_nodes != NULL)
1206 be_free_list(be_nodes);
1207 return (err);
1210 static int
1211 be_do_mount(int argc, char **argv)
1213 nvlist_t *be_attrs;
1214 boolean_t shared_fs = B_FALSE;
1215 int err = 1;
1216 int c;
1217 int mount_flags = 0;
1218 char *obe_name;
1219 char *mountpoint;
1220 char *tmp_mp = NULL;
1222 while ((c = getopt(argc, argv, "s:v")) != -1) {
1223 switch (c) {
1224 case 's':
1225 shared_fs = B_TRUE;
1227 mount_flags |= BE_MOUNT_FLAG_SHARED_FS;
1229 if (strcmp(optarg, "rw") == 0) {
1230 mount_flags |= BE_MOUNT_FLAG_SHARED_RW;
1231 } else if (strcmp(optarg, "ro") != 0) {
1232 (void) fprintf(stderr, _("The -s flag "
1233 "requires an argument [ rw | ro ]\n"));
1234 usage();
1235 return (1);
1238 break;
1239 case 'v':
1240 libbe_print_errors(B_TRUE);
1241 break;
1242 default:
1243 usage();
1244 return (1);
1248 argc -= optind;
1249 argv += optind;
1251 if (argc < 1 || argc > 2) {
1252 usage();
1253 return (1);
1256 obe_name = argv[0];
1258 if (argc == 2) {
1259 mountpoint = argv[1];
1260 if (mountpoint[0] != '/') {
1261 (void) fprintf(stderr, _("Invalid mount point %s. "
1262 "Mount point must start with a /.\n"), mountpoint);
1263 return (1);
1265 } else {
1266 const char *tmpdir = getenv("TMPDIR");
1267 const char *tmpname = "tmp.XXXXXX";
1268 int sz;
1270 if (tmpdir == NULL)
1271 tmpdir = "/tmp";
1273 sz = asprintf(&tmp_mp, "%s/%s", tmpdir, tmpname);
1274 if (sz < 0) {
1275 (void) fprintf(stderr, _("internal error: "
1276 "out of memory\n"));
1277 return (1);
1280 mountpoint = mkdtemp(tmp_mp);
1283 if (be_nvl_alloc(&be_attrs) != 0)
1284 return (1);
1286 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1287 goto out;
1289 if (be_nvl_add_string(be_attrs, BE_ATTR_MOUNTPOINT, mountpoint) != 0)
1290 goto out;
1292 if (shared_fs && be_nvl_add_uint16(be_attrs, BE_ATTR_MOUNT_FLAGS,
1293 mount_flags) != 0)
1294 goto out;
1296 err = be_mount(be_attrs);
1298 switch (err) {
1299 case BE_SUCCESS:
1300 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint);
1301 break;
1302 case BE_ERR_BE_NOENT:
1303 (void) fprintf(stderr, _("%s does not exist or appear "
1304 "to be a valid BE.\nPlease check that the name of "
1305 "the BE provided is correct.\n"), obe_name);
1306 break;
1307 case BE_ERR_MOUNTED:
1308 (void) fprintf(stderr, _("%s is already mounted.\n"
1309 "Please unmount the BE before mounting it again.\n"),
1310 obe_name);
1311 break;
1312 case BE_ERR_PERM:
1313 case BE_ERR_ACCESS:
1314 (void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1315 (void) fprintf(stderr, _("You have insufficient privileges to "
1316 "execute this command.\n"));
1317 break;
1318 case BE_ERR_NO_MOUNTED_ZONE:
1319 (void) fprintf(stderr, _("Mounted on '%s'.\nUnable to mount "
1320 "one of %s's zone BE's.\n"), mountpoint, obe_name);
1321 break;
1322 default:
1323 (void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1324 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1327 out:
1328 free(tmp_mp);
1329 nvlist_free(be_attrs);
1330 return (err);
1333 static int
1334 be_do_unmount(int argc, char **argv)
1336 nvlist_t *be_attrs;
1337 char *obe_name;
1338 int err = 1;
1339 int c;
1340 int unmount_flags = 0;
1342 while ((c = getopt(argc, argv, "fv")) != -1) {
1343 switch (c) {
1344 case 'f':
1345 unmount_flags |= BE_UNMOUNT_FLAG_FORCE;
1346 break;
1347 case 'v':
1348 libbe_print_errors(B_TRUE);
1349 break;
1350 default:
1351 usage();
1352 return (1);
1356 argc -= optind;
1357 argv += optind;
1359 if (argc != 1) {
1360 usage();
1361 return (1);
1364 obe_name = argv[0];
1366 if (be_nvl_alloc(&be_attrs) != 0)
1367 return (1);
1370 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1371 goto out;
1373 if (be_nvl_add_uint16(be_attrs, BE_ATTR_UNMOUNT_FLAGS,
1374 unmount_flags) != 0)
1375 goto out;
1377 err = be_unmount(be_attrs);
1379 switch (err) {
1380 case BE_SUCCESS:
1381 (void) printf(_("Unmounted successfully\n"));
1382 break;
1383 case BE_ERR_BE_NOENT:
1384 (void) fprintf(stderr, _("%s does not exist or appear "
1385 "to be a valid BE.\nPlease check that the name of "
1386 "the BE provided is correct.\n"), obe_name);
1387 break;
1388 case BE_ERR_UMOUNT_CURR_BE:
1389 (void) fprintf(stderr, _("%s is the currently active BE.\n"
1390 "It cannot be unmounted unless another BE is the "
1391 "currently active BE.\n"), obe_name);
1392 break;
1393 case BE_ERR_UMOUNT_SHARED:
1394 (void) fprintf(stderr, _("%s is a shared file system and it "
1395 "cannot be unmounted.\n"), obe_name);
1396 break;
1397 case BE_ERR_PERM:
1398 case BE_ERR_ACCESS:
1399 (void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1400 (void) fprintf(stderr, _("You have insufficient privileges to "
1401 "execute this command.\n"));
1402 break;
1403 default:
1404 (void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1405 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1408 out:
1409 nvlist_free(be_attrs);
1410 return (err);
1413 static int
1414 be_do_rename(int argc, char **argv)
1416 nvlist_t *be_attrs;
1417 char *obe_name;
1418 char *nbe_name;
1419 int err = 1;
1420 int c;
1422 while ((c = getopt(argc, argv, "v")) != -1) {
1423 switch (c) {
1424 case 'v':
1425 libbe_print_errors(B_TRUE);
1426 break;
1427 default:
1428 usage();
1429 return (1);
1433 argc -= optind;
1434 argv += optind;
1436 if (argc != 2) {
1437 usage();
1438 return (1);
1441 obe_name = argv[0];
1442 nbe_name = argv[1];
1444 if (be_nvl_alloc(&be_attrs) != 0)
1445 return (1);
1447 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1448 goto out;
1450 if (be_nvl_add_string(be_attrs, BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
1451 goto out;
1453 err = be_rename(be_attrs);
1455 switch (err) {
1456 case BE_SUCCESS:
1457 (void) printf(_("Renamed successfully\n"));
1458 break;
1459 case BE_ERR_BE_NOENT:
1460 (void) fprintf(stderr, _("%s does not exist or appear "
1461 "to be a valid BE.\nPlease check that the name of "
1462 "the BE provided is correct.\n"), obe_name);
1463 break;
1464 case BE_ERR_PERM:
1465 case BE_ERR_ACCESS:
1466 (void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1467 obe_name);
1468 (void) fprintf(stderr, _("You have insufficient privileges to "
1469 "execute this command.\n"));
1470 break;
1471 default:
1472 (void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1473 obe_name);
1474 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1477 out:
1478 nvlist_free(be_attrs);
1479 return (err);
1482 static int
1483 be_do_rollback(int argc, char **argv)
1485 nvlist_t *be_attrs;
1486 char *obe_name;
1487 char *snap_name;
1488 int err = 1;
1489 int c;
1491 while ((c = getopt(argc, argv, "v")) != -1) {
1492 switch (c) {
1493 case 'v':
1494 libbe_print_errors(B_TRUE);
1495 break;
1496 default:
1497 usage();
1498 return (1);
1502 argc -= optind;
1503 argv += optind;
1505 if (argc < 1 || argc > 2) {
1506 usage();
1507 return (1);
1510 obe_name = argv[0];
1511 if (argc == 2)
1512 snap_name = argv[1];
1513 else { /* argc == 1 */
1514 if ((snap_name = strrchr(obe_name, '@')) != NULL) {
1515 if (snap_name[1] == '\0') {
1516 usage();
1517 return (1);
1520 snap_name[0] = '\0';
1521 snap_name++;
1522 } else {
1523 usage();
1524 return (1);
1528 if (be_nvl_alloc(&be_attrs) != 0)
1529 return (1);
1531 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1532 goto out;
1534 if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME, snap_name) != 0)
1535 goto out;
1537 err = be_rollback(be_attrs);
1539 switch (err) {
1540 case BE_SUCCESS:
1541 (void) printf(_("Rolled back successfully\n"));
1542 break;
1543 case BE_ERR_BE_NOENT:
1544 (void) fprintf(stderr, _("%s does not exist or appear "
1545 "to be a valid BE.\nPlease check that the name of "
1546 "the BE provided is correct.\n"), obe_name);
1547 break;
1548 case BE_ERR_SS_NOENT:
1549 (void) fprintf(stderr, _("%s does not exist or appear "
1550 "to be a valid snapshot.\nPlease check that the name of "
1551 "the snapshot provided is correct.\n"), snap_name);
1552 break;
1553 case BE_ERR_PERM:
1554 case BE_ERR_ACCESS:
1555 (void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1556 "failed.\n"), obe_name, snap_name);
1557 (void) fprintf(stderr, _("You have insufficient privileges to "
1558 "execute this command.\n"));
1559 break;
1560 default:
1561 (void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1562 "failed.\n"), obe_name, snap_name);
1563 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1566 out:
1567 nvlist_free(be_attrs);
1568 return (err);