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]
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.
42 #include <sys/types.h>
46 #define _(x) gettext(x)
49 #define TEXT_DOMAIN "SYS_TEST"
52 #define DT_BUF_LEN (128)
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
74 * all columns output format
77 struct col_info cols
[NUM_COLS
];
81 * type of possible output formats
91 * command handler description
93 typedef struct be_command
{
95 int (*func
)(int argc
, char **argv
);
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
},
117 (void) fprintf(stderr
, _("usage:\n"
118 "\tbeadm subcommand cmd_options\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"));
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
);
155 main(int argc
, char **argv
)
159 (void) setlocale(LC_ALL
, "");
160 (void) textdomain(TEXT_DOMAIN
);
169 /* Turn error printing off */
170 libbe_print_errors(B_FALSE
);
172 return (run_be_cmd(cmdname
, --argc
, ++argv
));
176 print_hdr(struct hdr_info
*hdr_info
)
178 boolean_t first
= B_TRUE
;
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
;
188 (void) printf("%-*s", width
, name
);
191 (void) printf(" %-*s", width
, name
);
193 (void) putchar('\n');
197 init_hdr_cols(enum be_fmt be_fmt
, struct hdr_info
*hdr
)
199 struct col_info
*col
= hdr
->cols
;
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
;
211 col
[0].col_name
= _("BE/Dataset/Snapshot");
214 col
[0].col_name
= _("BE/Dataset");
216 case BE_FMT_SNAPSHOT
:
217 col
[0].col_name
= _("BE/Snapshot");
218 col
[1].col_name
= NULL
;
219 col
[2].col_name
= NULL
;
223 col
[0].col_name
= _("BE");
226 for (i
= 0; i
< NUM_COLS
; i
++) {
227 const char *name
= col
[i
].col_name
;
232 size_t sz
= mbstowcs(wname
, name
, sizeof (wname
) /
235 int wcsw
= wcswidth(wname
, sz
);
241 col
[i
].width
= strlen(name
);
248 nicenum(uint64_t num
, char *buf
, size_t buflen
)
259 u
= " KMGTPE"[index
];
262 (void) snprintf(buf
, buflen
, "%llu", n
);
265 for (i
= 2; i
>= 0; i
--) {
266 if (snprintf(buf
, buflen
, "%.*f%c", i
,
267 (double)num
/ (1ULL << 10 * index
), u
) <= 5)
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
];
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
;
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;
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
;
309 if (root_ds_len
+ 3 > len
[0])
310 len
[0] = root_ds_len
+ 3;
313 if (mntpt_len
> len
[2])
315 if (policy_len
> len
[4])
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
)
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])
332 nicenum(snap_used
, buf
, sizeof (buf
));
333 used_len
= strlen(buf
);
334 if (used_len
> len
[3])
339 if (be_fmt
== BE_FMT_DEFAULT
) {
341 nicenum(used
, buf
, sizeof (buf
));
342 used_len
= strlen(buf
);
343 if (used_len
> len
[3])
347 nicenum(used
, buf
, sizeof (buf
));
350 for (i
= 0; i
< NUM_COLS
; i
++)
351 hdr
->cols
[i
].width
= len
[i
];
355 print_be_nodes(const char *be_name
, boolean_t parsable
, struct hdr_info
*hdr
,
356 be_node_list_t
*nodes
)
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";
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
;
374 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 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
)
390 if (cur_be
->be_active
)
392 if (cur_be
->be_active_on_boot
) {
393 if (!cur_be
->be_global_active
)
399 nicenum(used
, buf
, sizeof (buf
));
401 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
403 (uuid_str
!= NULL
? uuid_str
: ""),
405 (cur_be
->be_mounted
? mntpt
: ""),
407 cur_be
->be_policy_type
,
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
:
415 hdr
->cols
[3].width
, buf
,
416 hdr
->cols
[4].width
, cur_be
->be_policy_type
,
417 hdr
->cols
[5].width
, datetime
);
422 print_be_snapshots(be_node_list_t
*be
, struct hdr_info
*hdr
, boolean_t parsable
)
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
;
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
));
448 if (hdr
->cols
[1].width
!= 0)
449 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
458 (void) printf("%s;%s;%llu;%s;%ld\n",
465 if (hdr
->cols
[1].width
!= 0)
466 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
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
);
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
);
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
)
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";
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
;
501 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 0)
505 (void) printf("%-s\n", name
);
509 tm
= localtime(&creation
);
510 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
512 if (cur_be
->be_active
)
514 if (cur_be
->be_active_on_boot
)
517 nicenum(used
, buf
, sizeof (buf
));
518 if (be_fmt
& BE_FMT_DATASET
)
520 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
521 cur_be
->be_node_name
,
524 (cur_be
->be_mounted
? mntpt
: ""),
526 cur_be
->be_policy_type
,
529 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
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
?
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
);
545 print_nodes(const char *be_name
, boolean_t dsets
, boolean_t snaps
,
546 boolean_t parsable
, be_node_list_t
*be_nodes
)
549 enum be_fmt be_fmt
= BE_FMT_DEFAULT
;
552 be_fmt
|= BE_FMT_DATASET
;
554 be_fmt
|= BE_FMT_SNAPSHOT
;
557 init_hdr_cols(be_fmt
, &hdr
);
558 count_widths(be_fmt
, &hdr
, be_nodes
);
562 if (be_fmt
== BE_FMT_DEFAULT
)
563 print_be_nodes(be_name
, parsable
, &hdr
, be_nodes
);
565 print_fmt_nodes(be_name
, be_fmt
, parsable
, &hdr
, be_nodes
);
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
);
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"));
585 if (regcomp(&no_re
, nore
, cflags
) != 0) {
586 /* should not happen */
587 (void) fprintf(stderr
, _("Failed to compile 'no' regexp\n"));
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)
599 if (regexec(&yes_re
, answer
, 0, NULL
, 0) == 0) {
601 } else if (regexec(&no_re
, answer
, 0, NULL
, 0) != 0) {
602 (void) fprintf(stderr
, _("Invalid response. "
603 "Please enter 'y' or 'n'.\n"));
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"));
626 be_nvl_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
630 if (nvlist_add_string(nvl
, name
, val
) != 0) {
631 (void) fprintf(stderr
, _("nvlist_add_string failed for "
632 "%s (%s).\n"), name
, val
);
640 be_nvl_add_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
*val
)
644 if (nvlist_add_nvlist(nvl
, name
, val
) != 0) {
645 (void) fprintf(stderr
, _("nvlist_add_nvlist failed for %s.\n"),
654 be_nvl_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
658 if (nvlist_add_uint16(nvl
, name
, val
) != 0) {
659 (void) fprintf(stderr
, _("nvlist_add_uint16 failed for "
660 "%s (%hu).\n"), name
, val
);
668 be_do_activate(int argc
, char **argv
)
675 while ((c
= getopt(argc
, argv
, "v")) != -1) {
678 libbe_print_errors(B_TRUE
);
696 if (be_nvl_alloc(&be_attrs
) != 0)
699 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
702 err
= be_activate(be_attrs
);
706 (void) printf(_("Activated successfully\n"));
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
);
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"));
719 case BE_ERR_ACTIVATE_CURR
:
721 (void) fprintf(stderr
, _("Unable to activate %s.\n"), obe_name
);
722 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
726 nvlist_free(be_attrs
);
731 be_do_create(int argc
, char **argv
)
734 nvlist_t
*zfs_props
= NULL
;
735 boolean_t activate
= B_FALSE
;
736 boolean_t is_snap
= B_FALSE
;
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
;
748 while ((c
= getopt(argc
, argv
, "ad:e:io:p:v")) != -1) {
760 if (zfs_props
== NULL
&& be_nvl_alloc(&zfs_props
) != 0)
764 if ((propval
= strchr(propname
, '=')) == NULL
) {
765 (void) fprintf(stderr
, _("missing "
766 "'=' for -o option\n"));
771 if (nvlist_lookup_string(zfs_props
, propname
,
773 (void) fprintf(stderr
, _("property '%s' "
774 "specified multiple times\n"), propname
);
778 if (be_nvl_add_string(zfs_props
, propname
, propval
)
787 libbe_print_errors(B_TRUE
);
805 if ((snap_name
= strrchr(nbe_name
, '@')) != NULL
) {
806 if (snap_name
[1] == '\0') {
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') {
835 } else if (is_snap
) {
840 if (be_nvl_alloc(&be_attrs
) != 0)
844 if (zfs_props
!= NULL
&& be_nvl_add_nvlist(be_attrs
,
845 BE_ATTR_ORIG_BE_NAME
, zfs_props
) != 0)
848 if (obe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
849 BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
852 if (snap_name
!= NULL
&& be_nvl_add_string(be_attrs
,
853 BE_ATTR_SNAP_NAME
, snap_name
) != 0)
856 if (nbe_zpool
!= NULL
&& be_nvl_add_string(be_attrs
,
857 BE_ATTR_NEW_BE_POOL
, nbe_zpool
) != 0)
860 if (nbe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
861 BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
864 if (nbe_desc
!= NULL
&& be_nvl_add_string(be_attrs
,
865 BE_ATTR_NEW_BE_DESC
, nbe_desc
) != 0)
869 err
= be_create_snapshot(be_attrs
);
871 err
= be_copy(be_attrs
);
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
,
883 (void) fprintf(stderr
, _("failed to get %s "
884 "attribute\n"), BE_ATTR_NEW_BE_NAME
);
887 (void) printf(_("Auto named BE: %s\n"),
890 if (nvlist_lookup_string(be_attrs
, BE_ATTR_SNAP_NAME
,
892 (void) fprintf(stderr
, _("failed to get %s "
893 "attribute\n"), BE_ATTR_SNAP_NAME
);
896 (void) printf(_("Auto named snapshot: %s\n"),
900 if (!is_snap
&& activate
) {
901 char *args
[] = { "activate", "", NULL
};
905 err
= be_do_activate(2, args
);
909 (void) printf(_("Created successfully\n"));
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
);
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
,
923 (void) fprintf(stderr
, _("Unable to create snapshot "
924 "%s.\n"), snap_name
);
926 (void) fprintf(stderr
, _("Unable to create %s.\n"),
928 (void) fprintf(stderr
, _("You have insufficient privileges to "
929 "execute this command.\n"));
933 (void) fprintf(stderr
, _("Unable to create snapshot "
934 "%s.\n"), snap_name
);
936 (void) fprintf(stderr
, _("Unable to create %s.\n"),
938 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
942 nvlist_free(be_attrs
);
944 nvlist_free(zfs_props
);
950 be_do_destroy(int argc
, char **argv
)
953 boolean_t is_snap
= B_FALSE
;
954 boolean_t suppress_prompt
= B_FALSE
;
957 int destroy_flags
= 0;
961 while ((c
= getopt(argc
, argv
, "fFsv")) != -1) {
964 destroy_flags
|= BE_DESTROY_FLAG_FORCE_UNMOUNT
;
967 destroy_flags
|= BE_DESTROY_FLAG_SNAPSHOTS
;
970 libbe_print_errors(B_TRUE
);
973 suppress_prompt
= B_TRUE
;
990 if (!suppress_prompt
&& !confirm_destroy(be_name
)) {
991 (void) printf(_("%s has not been destroyed.\n"), be_name
);
995 if ((snap_name
= strrchr(be_name
, '@')) != NULL
) {
996 if (snap_name
[1] == '\0') {
1006 if (be_nvl_alloc(&be_attrs
) != 0)
1010 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, be_name
) != 0)
1014 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
,
1018 err
= be_destroy_snapshot(be_attrs
);
1020 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_DESTROY_FLAGS
,
1021 destroy_flags
) != 0)
1024 err
= be_destroy(be_attrs
);
1029 (void) printf(_("Destroyed successfully\n"));
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
);
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
);
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
);
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
);
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"));
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
);
1065 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1066 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1070 nvlist_free(be_attrs
);
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
;
1084 char *be_name
= NULL
;
1085 be_sort_t order
= BE_SORT_UNSPECIFIED
;
1087 while ((c
= getopt(argc
, argv
, "adk:svHK:")) != -1) {
1097 if (order
!= BE_SORT_UNSPECIFIED
) {
1098 (void) fprintf(stderr
, _("Sort key can be "
1099 "specified only once.\n"));
1103 if (strcmp(optarg
, "date") == 0) {
1105 order
= BE_SORT_DATE
;
1107 order
= BE_SORT_DATE_REV
;
1110 if (strcmp(optarg
, "name") == 0) {
1112 order
= BE_SORT_NAME
;
1114 order
= BE_SORT_NAME_REV
;
1117 if (strcmp(optarg
, "space") == 0) {
1119 order
= BE_SORT_SPACE
;
1121 order
= BE_SORT_SPACE_REV
;
1124 (void) fprintf(stderr
, _("Unknown sort key: %s\n"),
1132 libbe_print_errors(B_TRUE
);
1145 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1146 "are mutually exclusive.\n"), "-d");
1151 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1152 "are mutually exclusive.\n"), "-s");
1168 err
= be_list(be_name
, &be_nodes
);
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 "
1181 (void) fprintf(stderr
, "%s\n",
1182 be_err_to_str(err
));
1187 print_nodes(be_name
, dsets
, snaps
, parsable
, be_nodes
);
1189 case BE_ERR_BE_NOENT
:
1190 if (be_name
== NULL
)
1191 (void) fprintf(stderr
, _("No boot environments found "
1192 "on this system.\n"));
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
);
1200 (void) fprintf(stderr
, _("Unable to display Boot "
1202 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1205 if (be_nodes
!= NULL
)
1206 be_free_list(be_nodes
);
1211 be_do_mount(int argc
, char **argv
)
1214 boolean_t shared_fs
= B_FALSE
;
1217 int mount_flags
= 0;
1220 char *tmp_mp
= NULL
;
1222 while ((c
= getopt(argc
, argv
, "s:v")) != -1) {
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"));
1240 libbe_print_errors(B_TRUE
);
1251 if (argc
< 1 || 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
);
1266 const char *tmpdir
= getenv("TMPDIR");
1267 const char *tmpname
= "tmp.XXXXXX";
1273 sz
= asprintf(&tmp_mp
, "%s/%s", tmpdir
, tmpname
);
1275 (void) fprintf(stderr
, _("internal error: "
1276 "out of memory\n"));
1280 mountpoint
= mkdtemp(tmp_mp
);
1283 if (be_nvl_alloc(&be_attrs
) != 0)
1286 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1289 if (be_nvl_add_string(be_attrs
, BE_ATTR_MOUNTPOINT
, mountpoint
) != 0)
1292 if (shared_fs
&& be_nvl_add_uint16(be_attrs
, BE_ATTR_MOUNT_FLAGS
,
1296 err
= be_mount(be_attrs
);
1300 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint
);
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
);
1307 case BE_ERR_MOUNTED
:
1308 (void) fprintf(stderr
, _("%s is already mounted.\n"
1309 "Please unmount the BE before mounting it again.\n"),
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"));
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
);
1323 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1324 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1329 nvlist_free(be_attrs
);
1334 be_do_unmount(int argc
, char **argv
)
1340 int unmount_flags
= 0;
1342 while ((c
= getopt(argc
, argv
, "fv")) != -1) {
1345 unmount_flags
|= BE_UNMOUNT_FLAG_FORCE
;
1348 libbe_print_errors(B_TRUE
);
1366 if (be_nvl_alloc(&be_attrs
) != 0)
1370 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1373 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_UNMOUNT_FLAGS
,
1374 unmount_flags
) != 0)
1377 err
= be_unmount(be_attrs
);
1381 (void) printf(_("Unmounted successfully\n"));
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
);
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
);
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
);
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"));
1404 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1405 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1409 nvlist_free(be_attrs
);
1414 be_do_rename(int argc
, char **argv
)
1422 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1425 libbe_print_errors(B_TRUE
);
1444 if (be_nvl_alloc(&be_attrs
) != 0)
1447 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1450 if (be_nvl_add_string(be_attrs
, BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
1453 err
= be_rename(be_attrs
);
1457 (void) printf(_("Renamed successfully\n"));
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
);
1466 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1468 (void) fprintf(stderr
, _("You have insufficient privileges to "
1469 "execute this command.\n"));
1472 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1474 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1478 nvlist_free(be_attrs
);
1483 be_do_rollback(int argc
, char **argv
)
1491 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1494 libbe_print_errors(B_TRUE
);
1505 if (argc
< 1 || 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') {
1520 snap_name
[0] = '\0';
1528 if (be_nvl_alloc(&be_attrs
) != 0)
1531 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1534 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
, snap_name
) != 0)
1537 err
= be_rollback(be_attrs
);
1541 (void) printf(_("Rolled back successfully\n"));
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
);
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
);
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"));
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
));
1567 nvlist_free(be_attrs
);