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.
28 * Copyright 2017 Jason King
43 #include <sys/types.h>
44 #include <sys/debug.h>
45 #include <libcmdutils.h>
50 #define _(x) gettext(x)
56 #define TEXT_DOMAIN "SYS_TEST"
59 #define DT_BUF_LEN (128)
61 CTASSERT(DT_BUF_LEN
>= NN_NUMBUF_SZ
);
63 static int be_do_activate(int argc
, char **argv
);
64 static int be_do_create(int argc
, char **argv
);
65 static int be_do_destroy(int argc
, char **argv
);
66 static int be_do_list(int argc
, char **argv
);
67 static int be_do_mount(int argc
, char **argv
);
68 static int be_do_unmount(int argc
, char **argv
);
69 static int be_do_rename(int argc
, char **argv
);
70 static int be_do_rollback(int argc
, char **argv
);
71 static void usage(void);
74 * single column name/width output format description
82 * all columns output format
85 struct col_info cols
[NUM_COLS
];
89 * type of possible output formats
99 * command handler description
101 typedef struct be_command
{
103 int (*func
)(int argc
, char **argv
);
107 * sorted list of be commands
109 static const be_command_t be_command_tbl
[] = {
110 { "activate", be_do_activate
},
111 { "create", be_do_create
},
112 { "destroy", be_do_destroy
},
113 { "list", be_do_list
},
114 { "mount", be_do_mount
},
115 { "unmount", be_do_unmount
},
116 { "umount", be_do_unmount
}, /* unmount alias */
117 { "rename", be_do_rename
},
118 { "rollback", be_do_rollback
},
125 (void) fprintf(stderr
, _("usage:\n"
126 "\tbeadm subcommand cmd_options\n"
130 "\tbeadm activate [-v] beName\n"
131 "\tbeadm create [-a] [-d BE_desc]\n"
132 "\t\t[-o property=value] ... [-p zpool] \n"
133 "\t\t[-e nonActiveBe | beName@snapshot] [-v] beName\n"
134 "\tbeadm create [-d BE_desc]\n"
135 "\t\t[-o property=value] ... [-p zpool] [-v] beName@snapshot\n"
136 "\tbeadm destroy [-Ffsv] beName \n"
137 "\tbeadm destroy [-Fv] beName@snapshot \n"
138 "\tbeadm list [[-a] | [-d] [-s]] [-H]\n"
139 "\t\t[-k|-K date | name | space] [-v] [beName]\n"
140 "\tbeadm mount [-s ro|rw] [-v] beName [mountpoint]\n"
141 "\tbeadm unmount [-fv] beName | mountpoint\n"
142 "\tbeadm umount [-fv] beName | mountpoint\n"
143 "\tbeadm rename [-v] origBeName newBeName\n"
144 "\tbeadm rollback [-v] beName snapshot\n"
145 "\tbeadm rollback [-v] beName@snapshot\n"));
149 run_be_cmd(const char *cmdname
, int argc
, char **argv
)
151 const be_command_t
*command
;
153 for (command
= &be_command_tbl
[0]; command
->name
!= NULL
; command
++)
154 if (strcmp(command
->name
, cmdname
) == 0)
155 return (command
->func(argc
, argv
));
157 (void) fprintf(stderr
, _("Invalid command: %s\n"), cmdname
);
163 main(int argc
, char **argv
)
167 (void) setlocale(LC_ALL
, "");
168 (void) textdomain(TEXT_DOMAIN
);
177 /* Turn error printing off */
178 libbe_print_errors(B_FALSE
);
180 return (run_be_cmd(cmdname
, --argc
, ++argv
));
184 print_hdr(struct hdr_info
*hdr_info
)
186 boolean_t first
= B_TRUE
;
188 for (i
= 0; i
< NUM_COLS
; i
++) {
189 struct col_info
*col_info
= &hdr_info
->cols
[i
];
190 const char *name
= col_info
->col_name
;
191 size_t width
= col_info
->width
;
196 (void) printf("%-*s", width
, name
);
199 (void) printf(" %-*s", width
, name
);
201 (void) putchar('\n');
205 init_hdr_cols(enum be_fmt be_fmt
, struct hdr_info
*hdr
)
207 struct col_info
*col
= hdr
->cols
;
210 col
[1].col_name
= _("Active");
211 col
[2].col_name
= _("Mountpoint");
212 col
[3].col_name
= _("Space");
213 col
[4].col_name
= _("Policy");
214 col
[5].col_name
= _("Created");
215 col
[6].col_name
= NULL
;
219 col
[0].col_name
= _("BE/Dataset/Snapshot");
222 col
[0].col_name
= _("BE/Dataset");
224 case BE_FMT_SNAPSHOT
:
225 col
[0].col_name
= _("BE/Snapshot");
226 col
[1].col_name
= NULL
;
227 col
[2].col_name
= NULL
;
231 col
[0].col_name
= _("BE");
234 for (i
= 0; i
< NUM_COLS
; i
++) {
235 const char *name
= col
[i
].col_name
;
240 size_t sz
= mbstowcs(wname
, name
, sizeof (wname
) /
243 int wcsw
= wcswidth(wname
, sz
);
249 col
[i
].width
= strlen(name
);
256 count_widths(enum be_fmt be_fmt
, struct hdr_info
*hdr
, be_node_list_t
*be_nodes
)
258 size_t len
[NUM_COLS
];
259 char buf
[DT_BUF_LEN
];
261 be_node_list_t
*cur_be
;
263 for (i
= 0; i
< NUM_COLS
; i
++)
264 len
[i
] = hdr
->cols
[i
].width
;
266 for (cur_be
= be_nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
267 char name
[ZFS_MAX_DATASET_NAME_LEN
+ 1];
268 const char *be_name
= cur_be
->be_node_name
;
269 const char *root_ds
= cur_be
->be_root_ds
;
271 size_t node_name_len
= strlen(cur_be
->be_node_name
);
272 size_t root_ds_len
= strlen(cur_be
->be_root_ds
);
273 size_t mntpt_len
= 0;
274 size_t policy_len
= 0;
276 uint64_t used
= cur_be
->be_space_used
;
277 be_snapshot_list_t
*snap
= NULL
;
279 if (cur_be
->be_mntpt
!= NULL
)
280 mntpt_len
= strlen(cur_be
->be_mntpt
);
281 if (cur_be
->be_policy_type
!= NULL
)
282 policy_len
= strlen(cur_be
->be_policy_type
);
284 (void) strlcpy(name
, root_ds
, sizeof (name
));
285 pos
= strstr(name
, be_name
);
287 if (be_fmt
== BE_FMT_DEFAULT
) {
288 if (node_name_len
> len
[0])
289 len
[0] = node_name_len
;
291 if (root_ds_len
+ 3 > len
[0])
292 len
[0] = root_ds_len
+ 3;
295 if (mntpt_len
> len
[2])
297 if (policy_len
> len
[4])
300 for (snap
= cur_be
->be_node_snapshots
; snap
!= NULL
;
301 snap
= snap
->be_next_snapshot
) {
302 uint64_t snap_used
= snap
->be_snapshot_space_used
;
303 const char *snap_name
= snap
->be_snapshot_name
;
304 (void) strcpy(pos
, snap_name
);
306 if (be_fmt
== BE_FMT_DEFAULT
)
308 else if (be_fmt
& BE_FMT_SNAPSHOT
) {
309 int snap_len
= strlen(name
) + 3;
310 if (be_fmt
== BE_FMT_SNAPSHOT
)
311 snap_len
-= pos
- name
;
312 if (snap_len
> len
[0])
314 nicenum(snap_used
, buf
, sizeof (buf
));
315 used_len
= strlen(buf
);
316 if (used_len
> len
[3])
321 if (be_fmt
== BE_FMT_DEFAULT
) {
323 nicenum(used
, buf
, sizeof (buf
));
324 used_len
= strlen(buf
);
325 if (used_len
> len
[3])
329 nicenum(used
, buf
, sizeof (buf
));
332 for (i
= 0; i
< NUM_COLS
; i
++)
333 hdr
->cols
[i
].width
= len
[i
];
337 print_be_nodes(const char *be_name
, boolean_t parsable
, struct hdr_info
*hdr
,
338 be_node_list_t
*nodes
)
341 char datetime
[DT_BUF_LEN
];
342 be_node_list_t
*cur_be
;
344 for (cur_be
= nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
345 char active
[3] = "-\0";
347 const char *datetime_fmt
= "%F %R";
348 const char *name
= cur_be
->be_node_name
;
349 const char *mntpt
= cur_be
->be_mntpt
;
350 const char *uuid_str
= cur_be
->be_uuid_str
;
351 be_snapshot_list_t
*snap
= NULL
;
352 uint64_t used
= cur_be
->be_space_used
;
353 time_t creation
= cur_be
->be_node_creation
;
356 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 0)
362 tm
= localtime(&creation
);
363 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
365 for (snap
= cur_be
->be_node_snapshots
; snap
!= NULL
;
366 snap
= snap
->be_next_snapshot
)
367 used
+= snap
->be_snapshot_space_used
;
369 if (!cur_be
->be_global_active
)
372 if (cur_be
->be_active
)
374 if (cur_be
->be_active_on_boot
) {
375 if (!cur_be
->be_global_active
)
381 nicenum(used
, buf
, sizeof (buf
));
383 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
385 (uuid_str
!= NULL
? uuid_str
: ""),
387 (cur_be
->be_mounted
? mntpt
: ""),
389 cur_be
->be_policy_type
,
392 (void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
393 hdr
->cols
[0].width
, name
,
394 hdr
->cols
[1].width
, active
,
395 hdr
->cols
[2].width
, (cur_be
->be_mounted
? mntpt
:
397 hdr
->cols
[3].width
, buf
,
398 hdr
->cols
[4].width
, cur_be
->be_policy_type
,
399 hdr
->cols
[5].width
, datetime
);
404 print_be_snapshots(be_node_list_t
*be
, struct hdr_info
*hdr
, boolean_t parsable
)
407 char datetime
[DT_BUF_LEN
];
408 be_snapshot_list_t
*snap
= NULL
;
410 for (snap
= be
->be_node_snapshots
; snap
!= NULL
;
411 snap
= snap
->be_next_snapshot
) {
412 char name
[ZFS_MAX_DATASET_NAME_LEN
+ 1];
413 const char *datetime_fmt
= "%F %R";
414 const char *be_name
= be
->be_node_name
;
415 const char *root_ds
= be
->be_root_ds
;
416 const char *snap_name
= snap
->be_snapshot_name
;
418 uint64_t used
= snap
->be_snapshot_space_used
;
419 time_t creation
= snap
->be_snapshot_creation
;
420 struct tm
*tm
= localtime(&creation
);
422 (void) strncpy(name
, root_ds
, sizeof (name
));
423 pos
= strstr(name
, be_name
);
424 (void) strcpy(pos
, snap_name
);
426 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
427 nicenum(used
, buf
, sizeof (buf
));
430 if (hdr
->cols
[1].width
!= 0)
431 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
440 (void) printf("%s;%s;%llu;%s;%ld\n",
447 if (hdr
->cols
[1].width
!= 0)
448 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
450 hdr
->cols
[0].width
-3, name
,
451 hdr
->cols
[1].width
, "-",
452 hdr
->cols
[2].width
, "-",
453 hdr
->cols
[3].width
, buf
,
454 hdr
->cols
[4].width
, be
->be_policy_type
,
455 hdr
->cols
[5].width
, datetime
);
457 (void) printf(" %-*s %-*s %-*s %-*s\n",
458 hdr
->cols
[0].width
-3, snap_name
,
459 hdr
->cols
[3].width
, buf
,
460 hdr
->cols
[4].width
, be
->be_policy_type
,
461 hdr
->cols
[5].width
, datetime
);
466 print_fmt_nodes(const char *be_name
, enum be_fmt be_fmt
, boolean_t parsable
,
467 struct hdr_info
*hdr
, be_node_list_t
*nodes
)
470 char datetime
[DT_BUF_LEN
];
471 be_node_list_t
*cur_be
;
473 for (cur_be
= nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
474 char active
[3] = "-\0";
476 const char *datetime_fmt
= "%F %R";
477 const char *name
= cur_be
->be_node_name
;
478 const char *mntpt
= cur_be
->be_mntpt
;
479 uint64_t used
= cur_be
->be_space_used
;
480 time_t creation
= cur_be
->be_node_creation
;
483 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 0)
487 (void) printf("%-s\n", name
);
491 tm
= localtime(&creation
);
492 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
494 if (cur_be
->be_active
)
496 if (cur_be
->be_active_on_boot
)
499 nicenum(used
, buf
, sizeof (buf
));
500 if (be_fmt
& BE_FMT_DATASET
)
502 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
503 cur_be
->be_node_name
,
506 (cur_be
->be_mounted
? mntpt
: ""),
508 cur_be
->be_policy_type
,
511 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
513 hdr
->cols
[0].width
-3, cur_be
->be_root_ds
,
514 hdr
->cols
[1].width
, active
,
515 hdr
->cols
[2].width
, (cur_be
->be_mounted
?
517 hdr
->cols
[3].width
, buf
,
518 hdr
->cols
[4].width
, cur_be
->be_policy_type
,
519 hdr
->cols
[5].width
, datetime
);
521 if (be_fmt
& BE_FMT_SNAPSHOT
)
522 print_be_snapshots(cur_be
, hdr
, parsable
);
527 print_nodes(const char *be_name
, boolean_t dsets
, boolean_t snaps
,
528 boolean_t parsable
, be_node_list_t
*be_nodes
)
531 enum be_fmt be_fmt
= BE_FMT_DEFAULT
;
534 be_fmt
|= BE_FMT_DATASET
;
536 be_fmt
|= BE_FMT_SNAPSHOT
;
539 init_hdr_cols(be_fmt
, &hdr
);
540 count_widths(be_fmt
, &hdr
, be_nodes
);
544 if (be_fmt
== BE_FMT_DEFAULT
)
545 print_be_nodes(be_name
, parsable
, &hdr
, be_nodes
);
547 print_fmt_nodes(be_name
, be_fmt
, parsable
, &hdr
, be_nodes
);
551 confirm_destroy(const char *name
)
553 boolean_t res
= B_FALSE
;
554 const char *yesre
= nl_langinfo(YESEXPR
);
555 const char *nore
= nl_langinfo(NOEXPR
);
560 int cflags
= REG_EXTENDED
;
562 if (regcomp(&yes_re
, yesre
, cflags
) != 0) {
563 /* should not happen */
564 (void) fprintf(stderr
, _("Failed to compile 'yes' regexp\n"));
567 if (regcomp(&no_re
, nore
, cflags
) != 0) {
568 /* should not happen */
569 (void) fprintf(stderr
, _("Failed to compile 'no' regexp\n"));
574 (void) printf(_("Are you sure you want to destroy %s?\n"
575 "This action cannot be undone (y/[n]): "), name
);
577 answer
= fgets(buf
, sizeof (buf
), stdin
);
578 if (answer
== NULL
|| *answer
== '\0' || *answer
== 10)
581 if (regexec(&yes_re
, answer
, 0, NULL
, 0) == 0) {
583 } else if (regexec(&no_re
, answer
, 0, NULL
, 0) != 0) {
584 (void) fprintf(stderr
, _("Invalid response. "
585 "Please enter 'y' or 'n'.\n"));
595 be_nvl_alloc(nvlist_t
**nvlp
)
597 assert(nvlp
!= NULL
);
599 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0) {
600 (void) perror(_("nvlist_alloc failed.\n"));
608 be_nvl_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
612 if (nvlist_add_string(nvl
, name
, val
) != 0) {
613 (void) fprintf(stderr
, _("nvlist_add_string failed for "
614 "%s (%s).\n"), name
, val
);
622 be_nvl_add_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
*val
)
626 if (nvlist_add_nvlist(nvl
, name
, val
) != 0) {
627 (void) fprintf(stderr
, _("nvlist_add_nvlist failed for %s.\n"),
636 be_nvl_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
640 if (nvlist_add_uint16(nvl
, name
, val
) != 0) {
641 (void) fprintf(stderr
, _("nvlist_add_uint16 failed for "
642 "%s (%hu).\n"), name
, val
);
650 be_do_activate(int argc
, char **argv
)
657 while ((c
= getopt(argc
, argv
, "v")) != -1) {
660 libbe_print_errors(B_TRUE
);
678 if (be_nvl_alloc(&be_attrs
) != 0)
681 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
684 err
= be_activate(be_attrs
);
688 (void) printf(_("Activated successfully\n"));
690 case BE_ERR_BE_NOENT
:
691 (void) fprintf(stderr
, _("%s does not exist or appear "
692 "to be a valid BE.\nPlease check that the name of "
693 "the BE provided is correct.\n"), obe_name
);
697 (void) fprintf(stderr
, _("Unable to activate %s.\n"), obe_name
);
698 (void) fprintf(stderr
, _("You have insufficient privileges to "
699 "execute this command.\n"));
701 case BE_ERR_ACTIVATE_CURR
:
703 (void) fprintf(stderr
, _("Unable to activate %s.\n"), obe_name
);
704 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
708 nvlist_free(be_attrs
);
713 be_do_create(int argc
, char **argv
)
716 nvlist_t
*zfs_props
= NULL
;
717 boolean_t activate
= B_FALSE
;
718 boolean_t is_snap
= B_FALSE
;
721 char *obe_name
= NULL
;
722 char *snap_name
= NULL
;
723 char *nbe_zpool
= NULL
;
724 char *nbe_name
= NULL
;
725 char *nbe_desc
= NULL
;
726 char *propname
= NULL
;
727 char *propval
= NULL
;
730 while ((c
= getopt(argc
, argv
, "ad:e:io:p:v")) != -1) {
742 if (zfs_props
== NULL
&& be_nvl_alloc(&zfs_props
) != 0)
746 if ((propval
= strchr(propname
, '=')) == NULL
) {
747 (void) fprintf(stderr
, _("missing "
748 "'=' for -o option\n"));
753 if (nvlist_lookup_string(zfs_props
, propname
,
755 (void) fprintf(stderr
, _("property '%s' "
756 "specified multiple times\n"), propname
);
760 if (be_nvl_add_string(zfs_props
, propname
, propval
)
769 libbe_print_errors(B_TRUE
);
787 if ((snap_name
= strrchr(nbe_name
, '@')) != NULL
) {
788 if (snap_name
[1] == '\0') {
805 * Check if obe_name is really a snapshot name.
806 * If so, split it out.
808 if ((snap_name
= strrchr(obe_name
, '@')) != NULL
) {
809 if (snap_name
[1] == '\0') {
817 } else if (is_snap
) {
822 if (be_nvl_alloc(&be_attrs
) != 0)
826 if (zfs_props
!= NULL
&& be_nvl_add_nvlist(be_attrs
,
827 BE_ATTR_ORIG_BE_NAME
, zfs_props
) != 0)
830 if (obe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
831 BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
834 if (snap_name
!= NULL
&& be_nvl_add_string(be_attrs
,
835 BE_ATTR_SNAP_NAME
, snap_name
) != 0)
838 if (nbe_zpool
!= NULL
&& be_nvl_add_string(be_attrs
,
839 BE_ATTR_NEW_BE_POOL
, nbe_zpool
) != 0)
842 if (nbe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
843 BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
846 if (nbe_desc
!= NULL
&& be_nvl_add_string(be_attrs
,
847 BE_ATTR_NEW_BE_DESC
, nbe_desc
) != 0)
851 err
= be_create_snapshot(be_attrs
);
853 err
= be_copy(be_attrs
);
857 if (!is_snap
&& !nbe_name
) {
859 * We requested an auto named BE; find out the
860 * name of the BE that was created for us and
861 * the auto snapshot created from the original BE.
863 if (nvlist_lookup_string(be_attrs
, BE_ATTR_NEW_BE_NAME
,
865 (void) fprintf(stderr
, _("failed to get %s "
866 "attribute\n"), BE_ATTR_NEW_BE_NAME
);
869 (void) printf(_("Auto named BE: %s\n"),
872 if (nvlist_lookup_string(be_attrs
, BE_ATTR_SNAP_NAME
,
874 (void) fprintf(stderr
, _("failed to get %s "
875 "attribute\n"), BE_ATTR_SNAP_NAME
);
878 (void) printf(_("Auto named snapshot: %s\n"),
882 if (!is_snap
&& activate
) {
883 char *args
[] = { "activate", "", NULL
};
887 err
= be_do_activate(2, args
);
891 (void) printf(_("Created successfully\n"));
893 case BE_ERR_BE_EXISTS
:
894 (void) fprintf(stderr
, _("BE %s already exists\n."
895 "Please choose a different BE name.\n"), nbe_name
);
897 case BE_ERR_SS_EXISTS
:
898 (void) fprintf(stderr
, _("BE %s snapshot %s already exists.\n"
899 "Please choose a different snapshot name.\n"), obe_name
,
905 (void) fprintf(stderr
, _("Unable to create snapshot "
906 "%s.\n"), snap_name
);
908 (void) fprintf(stderr
, _("Unable to create %s.\n"),
910 (void) fprintf(stderr
, _("You have insufficient privileges to "
911 "execute this command.\n"));
915 (void) fprintf(stderr
, _("Unable to create snapshot "
916 "%s.\n"), snap_name
);
918 (void) fprintf(stderr
, _("Unable to create %s.\n"),
920 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
924 nvlist_free(be_attrs
);
926 nvlist_free(zfs_props
);
932 be_do_destroy(int argc
, char **argv
)
935 boolean_t is_snap
= B_FALSE
;
936 boolean_t suppress_prompt
= B_FALSE
;
939 int destroy_flags
= 0;
943 while ((c
= getopt(argc
, argv
, "fFsv")) != -1) {
946 destroy_flags
|= BE_DESTROY_FLAG_FORCE_UNMOUNT
;
949 destroy_flags
|= BE_DESTROY_FLAG_SNAPSHOTS
;
952 libbe_print_errors(B_TRUE
);
955 suppress_prompt
= B_TRUE
;
972 if (!suppress_prompt
&& !confirm_destroy(be_name
)) {
973 (void) printf(_("%s has not been destroyed.\n"), be_name
);
977 if ((snap_name
= strrchr(be_name
, '@')) != NULL
) {
978 if (snap_name
[1] == '\0') {
988 if (be_nvl_alloc(&be_attrs
) != 0)
992 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, be_name
) != 0)
996 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
,
1000 err
= be_destroy_snapshot(be_attrs
);
1002 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_DESTROY_FLAGS
,
1003 destroy_flags
) != 0)
1006 err
= be_destroy(be_attrs
);
1011 (void) printf(_("Destroyed successfully\n"));
1013 case BE_ERR_MOUNTED
:
1014 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1015 (void) fprintf(stderr
, _("It is currently mounted and must be "
1016 "unmounted before it can be destroyed.\n" "Use 'beadm "
1017 "unmount %s' to unmount the BE before destroying\nit or "
1018 "'beadm destroy -f %s'.\n"), be_name
, be_name
);
1020 case BE_ERR_DESTROY_CURR_BE
:
1021 (void) fprintf(stderr
, _("%s is the currently active BE and "
1022 "cannot be destroyed.\nYou must boot from another BE in "
1023 "order to destroy %s.\n"), be_name
, be_name
);
1025 case BE_ERR_ZONES_UNMOUNT
:
1026 (void) fprintf(stderr
, _("Unable to destroy one of " "%s's "
1027 "zone BE's.\nUse 'beadm destroy -f %s' or "
1028 "'zfs -f destroy <dataset>'.\n"), be_name
, be_name
);
1030 case BE_ERR_SS_NOENT
:
1031 (void) fprintf(stderr
, _("%s does not exist or appear "
1032 "to be a valid snapshot.\nPlease check that the name of "
1033 "the snapshot provided is correct.\n"), snap_name
);
1037 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1038 (void) fprintf(stderr
, _("You have insufficient privileges to "
1039 "execute this command.\n"));
1041 case BE_ERR_SS_EXISTS
:
1042 (void) fprintf(stderr
, _("Unable to destroy %s: "
1043 "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1044 "'zfs -r destroy <dataset>'.\n"), be_name
, be_name
);
1047 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1048 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1052 nvlist_free(be_attrs
);
1057 be_do_list(int argc
, char **argv
)
1059 be_node_list_t
*be_nodes
= NULL
;
1060 boolean_t all
= B_FALSE
;
1061 boolean_t dsets
= B_FALSE
;
1062 boolean_t snaps
= B_FALSE
;
1063 boolean_t parsable
= B_FALSE
;
1066 char *be_name
= NULL
;
1067 be_sort_t order
= BE_SORT_UNSPECIFIED
;
1069 while ((c
= getopt(argc
, argv
, "adk:svHK:")) != -1) {
1079 if (order
!= BE_SORT_UNSPECIFIED
) {
1080 (void) fprintf(stderr
, _("Sort key can be "
1081 "specified only once.\n"));
1085 if (strcmp(optarg
, "date") == 0) {
1087 order
= BE_SORT_DATE
;
1089 order
= BE_SORT_DATE_REV
;
1092 if (strcmp(optarg
, "name") == 0) {
1094 order
= BE_SORT_NAME
;
1096 order
= BE_SORT_NAME_REV
;
1099 if (strcmp(optarg
, "space") == 0) {
1101 order
= BE_SORT_SPACE
;
1103 order
= BE_SORT_SPACE_REV
;
1106 (void) fprintf(stderr
, _("Unknown sort key: %s\n"),
1114 libbe_print_errors(B_TRUE
);
1127 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1128 "are mutually exclusive.\n"), "-d");
1133 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1134 "are mutually exclusive.\n"), "-s");
1150 err
= be_list(be_name
, &be_nodes
);
1154 /* the default sort is ascending date, no need to sort twice */
1155 if (order
== BE_SORT_UNSPECIFIED
)
1156 order
= BE_SORT_DATE
;
1158 if (order
!= BE_SORT_DATE
) {
1159 err
= be_sort(&be_nodes
, order
);
1160 if (err
!= BE_SUCCESS
) {
1161 (void) fprintf(stderr
, _("Unable to sort Boot "
1163 (void) fprintf(stderr
, "%s\n",
1164 be_err_to_str(err
));
1169 print_nodes(be_name
, dsets
, snaps
, parsable
, be_nodes
);
1171 case BE_ERR_BE_NOENT
:
1172 if (be_name
== NULL
)
1173 (void) fprintf(stderr
, _("No boot environments found "
1174 "on this system.\n"));
1176 (void) fprintf(stderr
, _("%s does not exist or appear "
1177 "to be a valid BE.\nPlease check that the name of "
1178 "the BE provided is correct.\n"), be_name
);
1182 (void) fprintf(stderr
, _("Unable to display Boot "
1184 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1187 if (be_nodes
!= NULL
)
1188 be_free_list(be_nodes
);
1193 be_do_mount(int argc
, char **argv
)
1196 boolean_t shared_fs
= B_FALSE
;
1199 int mount_flags
= 0;
1202 char *tmp_mp
= NULL
;
1204 while ((c
= getopt(argc
, argv
, "s:v")) != -1) {
1209 mount_flags
|= BE_MOUNT_FLAG_SHARED_FS
;
1211 if (strcmp(optarg
, "rw") == 0) {
1212 mount_flags
|= BE_MOUNT_FLAG_SHARED_RW
;
1213 } else if (strcmp(optarg
, "ro") != 0) {
1214 (void) fprintf(stderr
, _("The -s flag "
1215 "requires an argument [ rw | ro ]\n"));
1222 libbe_print_errors(B_TRUE
);
1233 if (argc
< 1 || argc
> 2) {
1241 mountpoint
= argv
[1];
1242 if (mountpoint
[0] != '/') {
1243 (void) fprintf(stderr
, _("Invalid mount point %s. "
1244 "Mount point must start with a /.\n"), mountpoint
);
1248 const char *tmpdir
= getenv("TMPDIR");
1249 const char *tmpname
= "tmp.XXXXXX";
1255 sz
= asprintf(&tmp_mp
, "%s/%s", tmpdir
, tmpname
);
1257 (void) fprintf(stderr
, _("internal error: "
1258 "out of memory\n"));
1262 mountpoint
= mkdtemp(tmp_mp
);
1265 if (be_nvl_alloc(&be_attrs
) != 0)
1268 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1271 if (be_nvl_add_string(be_attrs
, BE_ATTR_MOUNTPOINT
, mountpoint
) != 0)
1274 if (shared_fs
&& be_nvl_add_uint16(be_attrs
, BE_ATTR_MOUNT_FLAGS
,
1278 err
= be_mount(be_attrs
);
1282 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint
);
1284 case BE_ERR_BE_NOENT
:
1285 (void) fprintf(stderr
, _("%s does not exist or appear "
1286 "to be a valid BE.\nPlease check that the name of "
1287 "the BE provided is correct.\n"), obe_name
);
1289 case BE_ERR_MOUNTED
:
1290 (void) fprintf(stderr
, _("%s is already mounted.\n"
1291 "Please unmount the BE before mounting it again.\n"),
1296 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1297 (void) fprintf(stderr
, _("You have insufficient privileges to "
1298 "execute this command.\n"));
1300 case BE_ERR_NO_MOUNTED_ZONE
:
1301 (void) fprintf(stderr
, _("Mounted on '%s'.\nUnable to mount "
1302 "one of %s's zone BE's.\n"), mountpoint
, obe_name
);
1305 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1306 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1312 nvlist_free(be_attrs
);
1317 be_do_unmount(int argc
, char **argv
)
1323 int unmount_flags
= 0;
1325 while ((c
= getopt(argc
, argv
, "fv")) != -1) {
1328 unmount_flags
|= BE_UNMOUNT_FLAG_FORCE
;
1331 libbe_print_errors(B_TRUE
);
1349 if (be_nvl_alloc(&be_attrs
) != 0)
1353 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1356 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_UNMOUNT_FLAGS
,
1357 unmount_flags
) != 0)
1360 err
= be_unmount(be_attrs
);
1364 (void) printf(_("Unmounted successfully\n"));
1366 case BE_ERR_BE_NOENT
:
1367 (void) fprintf(stderr
, _("%s does not exist or appear "
1368 "to be a valid BE.\nPlease check that the name of "
1369 "the BE provided is correct.\n"), obe_name
);
1371 case BE_ERR_UMOUNT_CURR_BE
:
1372 (void) fprintf(stderr
, _("%s is the currently active BE.\n"
1373 "It cannot be unmounted unless another BE is the "
1374 "currently active BE.\n"), obe_name
);
1376 case BE_ERR_UMOUNT_SHARED
:
1377 (void) fprintf(stderr
, _("%s is a shared file system and it "
1378 "cannot be unmounted.\n"), obe_name
);
1382 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1383 (void) fprintf(stderr
, _("You have insufficient privileges to "
1384 "execute this command.\n"));
1387 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1388 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1392 nvlist_free(be_attrs
);
1397 be_do_rename(int argc
, char **argv
)
1405 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1408 libbe_print_errors(B_TRUE
);
1427 if (be_nvl_alloc(&be_attrs
) != 0)
1430 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1433 if (be_nvl_add_string(be_attrs
, BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
1436 err
= be_rename(be_attrs
);
1440 (void) printf(_("Renamed successfully\n"));
1442 case BE_ERR_BE_NOENT
:
1443 (void) fprintf(stderr
, _("%s does not exist or appear "
1444 "to be a valid BE.\nPlease check that the name of "
1445 "the BE provided is correct.\n"), obe_name
);
1449 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1451 (void) fprintf(stderr
, _("You have insufficient privileges to "
1452 "execute this command.\n"));
1455 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1457 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1461 nvlist_free(be_attrs
);
1466 be_do_rollback(int argc
, char **argv
)
1474 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1477 libbe_print_errors(B_TRUE
);
1488 if (argc
< 1 || argc
> 2) {
1495 snap_name
= argv
[1];
1496 else { /* argc == 1 */
1497 if ((snap_name
= strrchr(obe_name
, '@')) != NULL
) {
1498 if (snap_name
[1] == '\0') {
1503 snap_name
[0] = '\0';
1511 if (be_nvl_alloc(&be_attrs
) != 0)
1514 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1517 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
, snap_name
) != 0)
1520 err
= be_rollback(be_attrs
);
1524 (void) printf(_("Rolled back successfully\n"));
1526 case BE_ERR_BE_NOENT
:
1527 (void) fprintf(stderr
, _("%s does not exist or appear "
1528 "to be a valid BE.\nPlease check that the name of "
1529 "the BE provided is correct.\n"), obe_name
);
1531 case BE_ERR_SS_NOENT
:
1532 (void) fprintf(stderr
, _("%s does not exist or appear "
1533 "to be a valid snapshot.\nPlease check that the name of "
1534 "the snapshot provided is correct.\n"), snap_name
);
1538 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1539 "failed.\n"), obe_name
, snap_name
);
1540 (void) fprintf(stderr
, _("You have insufficient privileges to "
1541 "execute this command.\n"));
1544 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1545 "failed.\n"), obe_name
, snap_name
);
1546 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1550 nvlist_free(be_attrs
);