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.
27 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
42 #include <sys/types.h>
47 #define _(x) gettext(x)
53 #define TEXT_DOMAIN "SYS_TEST"
56 #define DT_BUF_LEN (128)
59 static int be_do_activate(int argc
, char **argv
);
60 static int be_do_create(int argc
, char **argv
);
61 static int be_do_destroy(int argc
, char **argv
);
62 static int be_do_list(int argc
, char **argv
);
63 static int be_do_mount(int argc
, char **argv
);
64 static int be_do_unmount(int argc
, char **argv
);
65 static int be_do_rename(int argc
, char **argv
);
66 static int be_do_rollback(int argc
, char **argv
);
67 static void usage(void);
70 * single column name/width output format description
78 * all columns output format
81 struct col_info cols
[NUM_COLS
];
85 * type of possible output formats
95 * command handler description
97 typedef struct be_command
{
99 int (*func
)(int argc
, char **argv
);
103 * sorted list of be commands
105 static const be_command_t be_command_tbl
[] = {
106 { "activate", be_do_activate
},
107 { "create", be_do_create
},
108 { "destroy", be_do_destroy
},
109 { "list", be_do_list
},
110 { "mount", be_do_mount
},
111 { "unmount", be_do_unmount
},
112 { "umount", be_do_unmount
}, /* unmount alias */
113 { "rename", be_do_rename
},
114 { "rollback", be_do_rollback
},
121 (void) fprintf(stderr
, _("usage:\n"
122 "\tbeadm subcommand cmd_options\n"
126 "\tbeadm activate beName\n"
127 "\tbeadm create [-d BE_desc]\n"
128 "\t\t[-o property=value] ... [-p zpool] \n"
129 "\t\t[-e nonActiveBe | beName@snapshot] beName\n"
130 "\tbeadm create [-d BE_desc]\n"
131 "\t\t[-o property=value] ... [-p zpool] beName@snapshot\n"
132 "\tbeadm destroy [-Ffs] beName \n"
133 "\tbeadm destroy [-F] beName@snapshot \n"
134 "\tbeadm list [[-a] | [-d] [-s]] [-H] [beName]\n"
135 "\tbeadm mount [-s ro|rw] beName [mountpoint]\n"
136 "\tbeadm unmount [-f] beName\n"
137 "\tbeadm umount [-f] beName\n"
138 "\tbeadm rename origBeName newBeName\n"
139 "\tbeadm rollback beName snapshot\n"
140 "\tbeadm rollback beName@snapshot\n"));
144 run_be_cmd(const char *cmdname
, int argc
, char **argv
)
146 const be_command_t
*command
;
148 for (command
= &be_command_tbl
[0]; command
->name
!= NULL
; command
++)
149 if (strcmp(command
->name
, cmdname
) == 0)
150 return (command
->func(argc
, argv
));
152 (void) fprintf(stderr
, _("Invalid command: %s\n"), cmdname
);
158 main(int argc
, char **argv
)
162 (void) setlocale(LC_ALL
, "");
163 (void) textdomain(TEXT_DOMAIN
);
172 /* Turn error printing off */
173 libbe_print_errors(B_FALSE
);
175 return (run_be_cmd(cmdname
, --argc
, ++argv
));
179 print_hdr(struct hdr_info
*hdr_info
)
181 boolean_t first
= B_TRUE
;
183 for (i
= 0; i
< NUM_COLS
; i
++) {
184 struct col_info
*col_info
= &hdr_info
->cols
[i
];
185 const char *name
= col_info
->col_name
;
186 size_t width
= col_info
->width
;
191 (void) printf("%-*s", width
, name
);
194 (void) printf(" %-*s", width
, name
);
196 (void) putchar('\n');
200 init_hdr_cols(enum be_fmt be_fmt
, struct hdr_info
*hdr
)
202 struct col_info
*col
= hdr
->cols
;
205 col
[1].col_name
= _("Active");
206 col
[2].col_name
= _("Mountpoint");
207 col
[3].col_name
= _("Space");
208 col
[4].col_name
= _("Policy");
209 col
[5].col_name
= _("Created");
210 col
[6].col_name
= NULL
;
214 col
[0].col_name
= _("BE/Dataset/Snapshot");
217 col
[0].col_name
= _("BE/Dataset");
219 case BE_FMT_SNAPSHOT
:
220 col
[0].col_name
= _("BE/Snapshot");
221 col
[1].col_name
= NULL
;
222 col
[2].col_name
= NULL
;
226 col
[0].col_name
= _("BE");
229 for (i
= 0; i
< NUM_COLS
; i
++) {
230 const char *name
= col
[i
].col_name
;
235 size_t sz
= mbstowcs(wname
, name
, sizeof (wname
) /
238 int wcsw
= wcswidth(wname
, sz
);
244 col
[i
].width
= strlen(name
);
251 nicenum(uint64_t num
, char *buf
, size_t buflen
)
262 u
= " KMGTPE"[index
];
265 (void) snprintf(buf
, buflen
, "%llu", n
);
268 for (i
= 2; i
>= 0; i
--) {
269 if (snprintf(buf
, buflen
, "%.*f%c", i
,
270 (double)num
/ (1ULL << 10 * index
), u
) <= 5)
277 count_widths(enum be_fmt be_fmt
, struct hdr_info
*hdr
, be_node_list_t
*be_nodes
)
279 size_t len
[NUM_COLS
];
280 char buf
[DT_BUF_LEN
];
282 be_node_list_t
*cur_be
;
284 for (i
= 0; i
< NUM_COLS
; i
++)
285 len
[i
] = hdr
->cols
[i
].width
;
287 for (cur_be
= be_nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
288 char name
[ZFS_MAXNAMELEN
+1];
289 const char *be_name
= cur_be
->be_node_name
;
290 const char *root_ds
= cur_be
->be_root_ds
;
292 size_t node_name_len
= strlen(cur_be
->be_node_name
);
293 size_t root_ds_len
= strlen(cur_be
->be_root_ds
);
294 size_t mntpt_len
= 0;
295 size_t policy_len
= 0;
297 uint64_t used
= cur_be
->be_space_used
;
298 be_snapshot_list_t
*snap
= NULL
;
300 if (cur_be
->be_mntpt
!= NULL
)
301 mntpt_len
= strlen(cur_be
->be_mntpt
);
302 if (cur_be
->be_policy_type
!= NULL
)
303 policy_len
= strlen(cur_be
->be_policy_type
);
305 (void) strlcpy(name
, root_ds
, sizeof (name
));
306 pos
= strstr(name
, be_name
);
308 if (be_fmt
== BE_FMT_DEFAULT
) {
309 if (node_name_len
> len
[0])
310 len
[0] = node_name_len
;
312 if (root_ds_len
+ 3 > len
[0])
313 len
[0] = root_ds_len
+ 3;
316 if (mntpt_len
> len
[2])
318 if (policy_len
> len
[4])
321 for (snap
= cur_be
->be_node_snapshots
; snap
!= NULL
;
322 snap
= snap
->be_next_snapshot
) {
323 uint64_t snap_used
= snap
->be_snapshot_space_used
;
324 const char *snap_name
= snap
->be_snapshot_name
;
325 (void) strcpy(pos
, snap_name
);
327 if (be_fmt
== BE_FMT_DEFAULT
)
329 else if (be_fmt
& BE_FMT_SNAPSHOT
) {
330 int snap_len
= strlen(name
) + 3;
331 if (be_fmt
== BE_FMT_SNAPSHOT
)
332 snap_len
-= pos
- name
;
333 if (snap_len
> len
[0])
335 nicenum(snap_used
, buf
, sizeof (buf
));
336 used_len
= strlen(buf
);
337 if (used_len
> len
[3])
342 if (be_fmt
== BE_FMT_DEFAULT
) {
344 nicenum(used
, buf
, sizeof (buf
));
345 used_len
= strlen(buf
);
346 if (used_len
> len
[3])
350 nicenum(used
, buf
, sizeof (buf
));
353 for (i
= 0; i
< NUM_COLS
; i
++)
354 hdr
->cols
[i
].width
= len
[i
];
358 print_be_nodes(const char *be_name
, boolean_t parsable
, struct hdr_info
*hdr
,
359 be_node_list_t
*nodes
)
362 char datetime
[DT_BUF_LEN
];
363 be_node_list_t
*cur_be
;
365 for (cur_be
= nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
366 char active
[3] = "-\0";
368 const char *datetime_fmt
= "%F %R";
369 const char *name
= cur_be
->be_node_name
;
370 const char *mntpt
= cur_be
->be_mntpt
;
371 be_snapshot_list_t
*snap
= NULL
;
372 uint64_t used
= cur_be
->be_space_used
;
373 time_t creation
= cur_be
->be_node_creation
;
376 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 0)
382 tm
= localtime(&creation
);
383 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
385 for (snap
= cur_be
->be_node_snapshots
; snap
!= NULL
;
386 snap
= snap
->be_next_snapshot
)
387 used
+= snap
->be_snapshot_space_used
;
389 if (cur_be
->be_active
)
391 if (cur_be
->be_active_on_boot
)
394 nicenum(used
, buf
, sizeof (buf
));
396 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
400 (cur_be
->be_mounted
? mntpt
: ""),
402 cur_be
->be_policy_type
,
405 (void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
406 hdr
->cols
[0].width
, name
,
407 hdr
->cols
[1].width
, active
,
408 hdr
->cols
[2].width
, (cur_be
->be_mounted
? mntpt
:
410 hdr
->cols
[3].width
, buf
,
411 hdr
->cols
[4].width
, cur_be
->be_policy_type
,
412 hdr
->cols
[5].width
, datetime
);
417 print_be_snapshots(be_node_list_t
*be
, struct hdr_info
*hdr
, boolean_t parsable
)
420 char datetime
[DT_BUF_LEN
];
421 be_snapshot_list_t
*snap
= NULL
;
423 for (snap
= be
->be_node_snapshots
; snap
!= NULL
;
424 snap
= snap
->be_next_snapshot
) {
425 char name
[ZFS_MAXNAMELEN
+1];
426 const char *datetime_fmt
= "%F %R";
427 const char *be_name
= be
->be_node_name
;
428 const char *root_ds
= be
->be_root_ds
;
429 const char *snap_name
= snap
->be_snapshot_name
;
431 uint64_t used
= snap
->be_snapshot_space_used
;
432 time_t creation
= snap
->be_snapshot_creation
;
433 struct tm
*tm
= localtime(&creation
);
435 (void) strncpy(name
, root_ds
, sizeof (name
));
436 pos
= strstr(name
, be_name
);
437 (void) strcpy(pos
, snap_name
);
439 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
440 nicenum(used
, buf
, sizeof (buf
));
443 if (hdr
->cols
[1].width
!= 0)
444 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
453 (void) printf("%s;%s;%llu;%s;%ld\n",
460 if (hdr
->cols
[1].width
!= 0)
461 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
463 hdr
->cols
[0].width
-3, name
,
464 hdr
->cols
[1].width
, "-",
465 hdr
->cols
[2].width
, "-",
466 hdr
->cols
[3].width
, buf
,
467 hdr
->cols
[4].width
, be
->be_policy_type
,
468 hdr
->cols
[5].width
, datetime
);
470 (void) printf(" %-*s %-*s %-*s %-*s\n",
471 hdr
->cols
[0].width
-3, snap_name
,
472 hdr
->cols
[3].width
, buf
,
473 hdr
->cols
[4].width
, be
->be_policy_type
,
474 hdr
->cols
[5].width
, datetime
);
479 print_fmt_nodes(const char *be_name
, enum be_fmt be_fmt
, boolean_t parsable
,
480 struct hdr_info
*hdr
, be_node_list_t
*nodes
)
483 char datetime
[DT_BUF_LEN
];
484 be_node_list_t
*cur_be
;
486 for (cur_be
= nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
487 char active
[3] = "-\0";
489 const char *datetime_fmt
= "%F %R";
490 const char *name
= cur_be
->be_node_name
;
491 const char *mntpt
= cur_be
->be_mntpt
;
492 uint64_t used
= cur_be
->be_space_used
;
493 time_t creation
= cur_be
->be_node_creation
;
496 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 0)
500 (void) printf("%-s\n", name
);
504 tm
= localtime(&creation
);
505 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
507 if (cur_be
->be_active
)
509 if (cur_be
->be_active_on_boot
)
512 nicenum(used
, buf
, sizeof (buf
));
513 if (be_fmt
& BE_FMT_DATASET
)
515 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
516 cur_be
->be_node_name
,
519 (cur_be
->be_mounted
? mntpt
: ""),
521 cur_be
->be_policy_type
,
524 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
526 hdr
->cols
[0].width
-3, cur_be
->be_root_ds
,
527 hdr
->cols
[1].width
, active
,
528 hdr
->cols
[2].width
, (cur_be
->be_mounted
?
530 hdr
->cols
[3].width
, buf
,
531 hdr
->cols
[4].width
, cur_be
->be_policy_type
,
532 hdr
->cols
[5].width
, datetime
);
534 if (be_fmt
& BE_FMT_SNAPSHOT
)
535 print_be_snapshots(cur_be
, hdr
, parsable
);
540 print_nodes(const char *be_name
, boolean_t dsets
, boolean_t snaps
,
541 boolean_t parsable
, be_node_list_t
*be_nodes
)
544 enum be_fmt be_fmt
= BE_FMT_DEFAULT
;
547 be_fmt
|= BE_FMT_DATASET
;
549 be_fmt
|= BE_FMT_SNAPSHOT
;
552 init_hdr_cols(be_fmt
, &hdr
);
553 count_widths(be_fmt
, &hdr
, be_nodes
);
557 if (be_fmt
== BE_FMT_DEFAULT
)
558 print_be_nodes(be_name
, parsable
, &hdr
, be_nodes
);
560 print_fmt_nodes(be_name
, be_fmt
, parsable
, &hdr
, be_nodes
);
564 confirm_destroy(const char *name
)
566 boolean_t res
= B_FALSE
;
567 const char *yesre
= nl_langinfo(YESEXPR
);
568 const char *nore
= nl_langinfo(NOEXPR
);
573 int cflags
= REG_EXTENDED
;
575 if (regcomp(&yes_re
, yesre
, cflags
) != 0) {
576 /* should not happen */
577 (void) fprintf(stderr
, _("Failed to compile 'yes' regexp\n"));
580 if (regcomp(&no_re
, nore
, cflags
) != 0) {
581 /* should not happen */
582 (void) fprintf(stderr
, _("Failed to compile 'no' regexp\n"));
587 (void) printf(_("Are you sure you want to destroy %s?\n"
588 "This action cannot be undone (y/[n]): "), name
);
590 answer
= fgets(buf
, sizeof (buf
), stdin
);
591 if (answer
== NULL
|| *answer
== '\0' || *answer
== 10)
594 if (regexec(&yes_re
, answer
, 0, NULL
, 0) == 0) {
596 } else if (regexec(&no_re
, answer
, 0, NULL
, 0) != 0) {
597 (void) fprintf(stderr
, _("Invalid response. "
598 "Please enter 'y' or 'n'.\n"));
608 be_nvl_alloc(nvlist_t
**nvlp
)
610 assert(nvlp
!= NULL
);
612 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0) {
613 (void) perror(_("nvlist_alloc failed.\n"));
621 be_nvl_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
625 if (nvlist_add_string(nvl
, name
, val
) != 0) {
626 (void) fprintf(stderr
, _("nvlist_add_string failed for "
627 "%s (%s).\n"), name
, val
);
635 be_nvl_add_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
*val
)
639 if (nvlist_add_nvlist(nvl
, name
, val
) != 0) {
640 (void) fprintf(stderr
, _("nvlist_add_nvlist failed for %s.\n"),
649 be_nvl_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
653 if (nvlist_add_uint16(nvl
, name
, val
) != 0) {
654 (void) fprintf(stderr
, _("nvlist_add_uint16 failed for "
655 "%s (%hu).\n"), name
, val
);
663 be_do_activate(int argc
, char **argv
)
679 if (be_nvl_alloc(&be_attrs
) != 0)
682 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
685 err
= be_activate(be_attrs
);
689 (void) printf(_("Activated successfully\n"));
691 case BE_ERR_BE_NOENT
:
692 (void) fprintf(stderr
, _("%s does not exist or appear "
693 "to be a valid BE.\nPlease check that the name of "
694 "the BE provided is correct.\n"), obe_name
);
698 (void) fprintf(stderr
, _("Unable to activate %s.\n"), obe_name
);
699 (void) fprintf(stderr
, _("You have insufficient privileges to "
700 "execute this command.\n"));
702 case BE_ERR_ACTIVATE_CURR
:
704 (void) fprintf(stderr
, _("Unable to activate %s.\n"), obe_name
);
705 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
709 nvlist_free(be_attrs
);
714 be_do_create(int argc
, char **argv
)
717 nvlist_t
*zfs_props
= NULL
;
718 boolean_t activate
= B_FALSE
;
719 boolean_t is_snap
= B_FALSE
;
722 char *obe_name
= NULL
;
723 char *snap_name
= NULL
;
724 char *nbe_zpool
= NULL
;
725 char *nbe_name
= NULL
;
726 char *nbe_desc
= NULL
;
727 char *propname
= NULL
;
728 char *propval
= NULL
;
731 while ((c
= getopt(argc
, argv
, "ad:e:io:p:")) != -1) {
743 if (zfs_props
== NULL
&& be_nvl_alloc(&zfs_props
) != 0)
747 if ((propval
= strchr(propname
, '=')) == NULL
) {
748 (void) fprintf(stderr
, _("missing "
749 "'=' for -o option\n"));
754 if (nvlist_lookup_string(zfs_props
, propname
,
756 (void) fprintf(stderr
, _("property '%s' "
757 "specified multiple times\n"), propname
);
761 if (be_nvl_add_string(zfs_props
, propname
, propval
)
785 if ((snap_name
= strrchr(nbe_name
, '@')) != NULL
) {
786 if (snap_name
[1] == '\0') {
803 * Check if obe_name is really a snapshot name.
804 * If so, split it out.
806 if ((snap_name
= strrchr(obe_name
, '@')) != NULL
) {
807 if (snap_name
[1] == '\0') {
815 } else if (is_snap
) {
820 if (be_nvl_alloc(&be_attrs
) != 0)
824 if (zfs_props
!= NULL
&& be_nvl_add_nvlist(be_attrs
,
825 BE_ATTR_ORIG_BE_NAME
, zfs_props
) != 0)
828 if (obe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
829 BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
832 if (snap_name
!= NULL
&& be_nvl_add_string(be_attrs
,
833 BE_ATTR_SNAP_NAME
, snap_name
) != 0)
836 if (nbe_zpool
!= NULL
&& be_nvl_add_string(be_attrs
,
837 BE_ATTR_NEW_BE_POOL
, nbe_zpool
) != 0)
840 if (nbe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
841 BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
844 if (nbe_desc
!= NULL
&& be_nvl_add_string(be_attrs
,
845 BE_ATTR_NEW_BE_DESC
, nbe_desc
) != 0)
849 err
= be_create_snapshot(be_attrs
);
851 err
= be_copy(be_attrs
);
855 if (!is_snap
&& !nbe_name
) {
857 * We requested an auto named BE; find out the
858 * name of the BE that was created for us and
859 * the auto snapshot created from the original BE.
861 if (nvlist_lookup_string(be_attrs
, BE_ATTR_NEW_BE_NAME
,
863 (void) fprintf(stderr
, _("failed to get %s "
864 "attribute\n"), BE_ATTR_NEW_BE_NAME
);
867 (void) printf(_("Auto named BE: %s\n"),
870 if (nvlist_lookup_string(be_attrs
, BE_ATTR_SNAP_NAME
,
872 (void) fprintf(stderr
, _("failed to get %s "
873 "attribute\n"), BE_ATTR_SNAP_NAME
);
876 (void) printf(_("Auto named snapshot: %s\n"),
880 if (!is_snap
&& activate
) {
881 char *args
[] = { "activate", "", NULL
};
885 err
= be_do_activate(2, args
);
889 (void) printf(_("Created successfully\n"));
891 case BE_ERR_BE_EXISTS
:
892 (void) fprintf(stderr
, _("BE %s already exists\n."
893 "Please choose a different BE name.\n"), nbe_name
);
895 case BE_ERR_SS_EXISTS
:
896 (void) fprintf(stderr
, _("BE %s snapshot %s already exists.\n"
897 "Please choose a different snapshot name.\n"), obe_name
,
903 (void) fprintf(stderr
, _("Unable to create snapshot "
904 "%s.\n"), snap_name
);
906 (void) fprintf(stderr
, _("Unable to create %s.\n"),
908 (void) fprintf(stderr
, _("You have insufficient privileges to "
909 "execute this command.\n"));
913 (void) fprintf(stderr
, _("Unable to create snapshot "
914 "%s.\n"), snap_name
);
916 (void) fprintf(stderr
, _("Unable to create %s.\n"),
918 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
922 nvlist_free(be_attrs
);
924 if (zfs_props
!= NULL
)
925 nvlist_free(zfs_props
);
931 be_do_destroy(int argc
, char **argv
)
934 boolean_t is_snap
= B_FALSE
;
935 boolean_t suppress_prompt
= B_FALSE
;
938 int destroy_flags
= 0;
942 while ((c
= getopt(argc
, argv
, "fFs")) != -1) {
945 destroy_flags
|= BE_DESTROY_FLAG_FORCE_UNMOUNT
;
948 destroy_flags
|= BE_DESTROY_FLAG_SNAPSHOTS
;
951 suppress_prompt
= B_TRUE
;
968 if (!suppress_prompt
&& !confirm_destroy(be_name
)) {
969 (void) printf(_("%s has not been destroyed.\n"), be_name
);
973 if ((snap_name
= strrchr(be_name
, '@')) != NULL
) {
974 if (snap_name
[1] == '\0') {
984 if (be_nvl_alloc(&be_attrs
) != 0)
988 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, be_name
) != 0)
992 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
,
996 err
= be_destroy_snapshot(be_attrs
);
998 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_DESTROY_FLAGS
,
1002 err
= be_destroy(be_attrs
);
1007 (void) printf(_("Destroyed successfully\n"));
1009 case BE_ERR_MOUNTED
:
1010 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1011 (void) fprintf(stderr
, _("It is currently mounted and must be "
1012 "unmounted before it can be destroyed.\n" "Use 'beadm "
1013 "unmount %s' to unmount the BE before destroying\nit or "
1014 "'beadm destroy -f %s'.\n"), be_name
, be_name
);
1016 case BE_ERR_DESTROY_CURR_BE
:
1017 (void) fprintf(stderr
, _("%s is the currently active BE and "
1018 "cannot be destroyed.\nYou must boot from another BE in "
1019 "order to destroy %s.\n"), be_name
, be_name
);
1021 case BE_ERR_ZONES_UNMOUNT
:
1022 (void) fprintf(stderr
, _("Unable to destroy one of " "%s's "
1023 "zone BE's.\nUse 'beadm destroy -f %s' or "
1024 "'zfs -f destroy <dataset>'.\n"), be_name
, be_name
);
1026 case BE_ERR_SS_NOENT
:
1027 (void) fprintf(stderr
, _("%s does not exist or appear "
1028 "to be a valid snapshot.\nPlease check that the name of "
1029 "the snapshot provided is correct.\n"), snap_name
);
1033 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1034 (void) fprintf(stderr
, _("You have insufficient privileges to "
1035 "execute this command.\n"));
1037 case BE_ERR_SS_EXISTS
:
1038 (void) fprintf(stderr
, _("Unable to destroy %s: "
1039 "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1040 "'zfs -r destroy <dataset>'.\n"), be_name
, be_name
);
1043 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1044 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1048 nvlist_free(be_attrs
);
1053 be_do_list(int argc
, char **argv
)
1055 be_node_list_t
*be_nodes
= NULL
;
1056 boolean_t all
= B_FALSE
;
1057 boolean_t dsets
= B_FALSE
;
1058 boolean_t snaps
= B_FALSE
;
1059 boolean_t parsable
= B_FALSE
;
1062 char *be_name
= NULL
;
1064 while ((c
= getopt(argc
, argv
, "nadsH")) != -1) {
1086 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1087 "are mutually exclusive.\n"), "-d");
1092 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1093 "are mutually exclusive.\n"), "-s");
1109 err
= be_list(be_name
, &be_nodes
);
1113 print_nodes(be_name
, dsets
, snaps
, parsable
, be_nodes
);
1115 case BE_ERR_BE_NOENT
:
1116 if (be_name
== NULL
)
1117 (void) fprintf(stderr
, _("No boot environments found "
1118 "on this system.\n"));
1120 (void) fprintf(stderr
, _("%s does not exist or appear "
1121 "to be a valid BE.\nPlease check that the name of "
1122 "the BE provided is correct.\n"), be_name
);
1126 (void) fprintf(stderr
, _("Unable to display Boot "
1128 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1131 if (be_nodes
!= NULL
)
1132 be_free_list(be_nodes
);
1137 be_do_mount(int argc
, char **argv
)
1140 boolean_t shared_fs
= B_FALSE
;
1143 int mount_flags
= 0;
1146 char *tmp_mp
= NULL
;
1148 while ((c
= getopt(argc
, argv
, "s:")) != -1) {
1153 mount_flags
|= BE_MOUNT_FLAG_SHARED_FS
;
1155 if (strcmp(optarg
, "rw") == 0) {
1156 mount_flags
|= BE_MOUNT_FLAG_SHARED_RW
;
1157 } else if (strcmp(optarg
, "ro") != 0) {
1158 (void) fprintf(stderr
, _("The -s flag "
1159 "requires an argument [ rw | ro ]\n"));
1174 if (argc
< 1 || argc
> 2) {
1182 mountpoint
= argv
[1];
1183 if (mountpoint
[0] != '/') {
1184 (void) fprintf(stderr
, _("Invalid mount point %s. "
1185 "Mount point must start with a /.\n"), mountpoint
);
1189 const char *tmpdir
= getenv("TMPDIR");
1190 const char *tmpname
= "tmp.XXXXXX";
1196 sz
= asprintf(&tmp_mp
, "%s/%s", tmpdir
, tmpname
);
1198 (void) fprintf(stderr
, _("internal error: "
1199 "out of memory\n"));
1203 mountpoint
= mkdtemp(tmp_mp
);
1206 if (be_nvl_alloc(&be_attrs
) != 0)
1209 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1212 if (be_nvl_add_string(be_attrs
, BE_ATTR_MOUNTPOINT
, mountpoint
) != 0)
1215 if (shared_fs
&& be_nvl_add_uint16(be_attrs
, BE_ATTR_MOUNT_FLAGS
,
1219 err
= be_mount(be_attrs
);
1223 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint
);
1225 case BE_ERR_BE_NOENT
:
1227 (void) fprintf(stderr
, _("%s does not exist or appear "
1228 "to be a valid BE.\nPlease check that the name of "
1229 "the BE provided is correct.\n"), obe_name
);
1231 case BE_ERR_MOUNTED
:
1232 (void) fprintf(stderr
, _("%s is already mounted.\n"
1233 "Please unmount the BE before mounting it again.\n"),
1239 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1240 (void) fprintf(stderr
, _("You have insufficient privileges to "
1241 "execute this command.\n"));
1245 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1246 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1252 nvlist_free(be_attrs
);
1257 be_do_unmount(int argc
, char **argv
)
1263 int unmount_flags
= 0;
1265 while ((c
= getopt(argc
, argv
, "f")) != -1) {
1268 unmount_flags
|= BE_UNMOUNT_FLAG_FORCE
;
1286 if (be_nvl_alloc(&be_attrs
) != 0)
1290 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1293 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_UNMOUNT_FLAGS
,
1294 unmount_flags
) != 0)
1297 err
= be_unmount(be_attrs
);
1301 (void) printf(_("Unmounted successfully\n"));
1303 case BE_ERR_BE_NOENT
:
1304 (void) fprintf(stderr
, _("%s does not exist or appear "
1305 "to be a valid BE.\nPlease check that the name of "
1306 "the BE provided is correct.\n"), obe_name
);
1308 case BE_ERR_UMOUNT_CURR_BE
:
1309 (void) fprintf(stderr
, _("%s is the currently active BE.\n"
1310 "It cannot be unmounted unless another BE is the "
1311 "currently active BE.\n"), obe_name
);
1313 case BE_ERR_UMOUNT_SHARED
:
1314 (void) fprintf(stderr
, _("%s is a shared file system and it "
1315 "cannot be unmounted.\n"), obe_name
);
1319 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1320 (void) fprintf(stderr
, _("You have insufficient privileges to "
1321 "execute this command.\n"));
1324 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1325 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1329 nvlist_free(be_attrs
);
1334 be_do_rename(int argc
, char **argv
)
1352 if (be_nvl_alloc(&be_attrs
) != 0)
1355 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1358 if (be_nvl_add_string(be_attrs
, BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
1361 err
= be_rename(be_attrs
);
1365 (void) printf(_("Renamed successfully\n"));
1367 case BE_ERR_BE_NOENT
:
1368 (void) fprintf(stderr
, _("%s does not exist or appear "
1369 "to be a valid BE.\nPlease check that the name of "
1370 "the BE provided is correct.\n"), obe_name
);
1374 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1376 (void) fprintf(stderr
, _("You have insufficient privileges to "
1377 "execute this command.\n"));
1380 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1382 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1386 nvlist_free(be_attrs
);
1391 be_do_rollback(int argc
, char **argv
)
1401 if (argc
< 1 || argc
> 2) {
1408 snap_name
= argv
[1];
1409 else { /* argc == 1 */
1410 if ((snap_name
= strrchr(obe_name
, '@')) != NULL
) {
1411 if (snap_name
[1] == '\0') {
1416 snap_name
[0] = '\0';
1424 if (be_nvl_alloc(&be_attrs
) != 0)
1427 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1430 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
, snap_name
) != 0)
1433 err
= be_rollback(be_attrs
);
1437 (void) printf(_("Rolled back successfully\n"));
1439 case BE_ERR_BE_NOENT
:
1440 (void) fprintf(stderr
, _("%s does not exist or appear "
1441 "to be a valid BE.\nPlease check that the name of "
1442 "the BE provided is correct.\n"), obe_name
);
1444 case BE_ERR_SS_NOENT
:
1445 (void) fprintf(stderr
, _("%s does not exist or appear "
1446 "to be a valid snapshot.\nPlease check that the name of "
1447 "the snapshot provided is correct.\n"), snap_name
);
1451 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1452 "failed.\n"), obe_name
, snap_name
);
1453 (void) fprintf(stderr
, _("You have insufficient privileges to "
1454 "execute this command.\n"));
1457 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1458 "failed.\n"), obe_name
, snap_name
);
1459 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1463 nvlist_free(be_attrs
);