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 2013 Nexenta Systems, Inc. All rights reserved.
28 * Copyright 2015 Toomas Soome <tsoome@me.com>
43 #include <sys/types.h>
48 #define _(x) gettext(x)
54 #define TEXT_DOMAIN "SYS_TEST"
57 #define DT_BUF_LEN (128)
60 static int be_do_activate(int argc
, char **argv
);
61 static int be_do_create(int argc
, char **argv
);
62 static int be_do_destroy(int argc
, char **argv
);
63 static int be_do_list(int argc
, char **argv
);
64 static int be_do_mount(int argc
, char **argv
);
65 static int be_do_unmount(int argc
, char **argv
);
66 static int be_do_rename(int argc
, char **argv
);
67 static int be_do_rollback(int argc
, char **argv
);
68 static void usage(void);
71 * single column name/width output format description
79 * all columns output format
82 struct col_info cols
[NUM_COLS
];
86 * type of possible output formats
96 * command handler description
98 typedef struct be_command
{
100 int (*func
)(int argc
, char **argv
);
104 * sorted list of be commands
106 static const be_command_t be_command_tbl
[] = {
107 { "activate", be_do_activate
},
108 { "create", be_do_create
},
109 { "destroy", be_do_destroy
},
110 { "list", be_do_list
},
111 { "mount", be_do_mount
},
112 { "unmount", be_do_unmount
},
113 { "umount", be_do_unmount
}, /* unmount alias */
114 { "rename", be_do_rename
},
115 { "rollback", be_do_rollback
},
122 (void) fprintf(stderr
, _("usage:\n"
123 "\tbeadm subcommand cmd_options\n"
127 "\tbeadm activate [-v] beName\n"
128 "\tbeadm create [-a] [-d BE_desc]\n"
129 "\t\t[-o property=value] ... [-p zpool] \n"
130 "\t\t[-e nonActiveBe | beName@snapshot] [-v] beName\n"
131 "\tbeadm create [-d BE_desc]\n"
132 "\t\t[-o property=value] ... [-p zpool] [-v] beName@snapshot\n"
133 "\tbeadm destroy [-Ffsv] beName \n"
134 "\tbeadm destroy [-Fv] beName@snapshot \n"
135 "\tbeadm list [[-a] | [-d] [-s]] [-H]\n"
136 "\t\t[-k|-K date | name | space] [-v] [beName]\n"
137 "\tbeadm mount [-s ro|rw] [-v] beName [mountpoint]\n"
138 "\tbeadm unmount [-fv] beName | mountpoint\n"
139 "\tbeadm umount [-fv] beName | mountpoint\n"
140 "\tbeadm rename [-v] origBeName newBeName\n"
141 "\tbeadm rollback [-v] beName snapshot\n"
142 "\tbeadm rollback [-v] beName@snapshot\n"));
146 run_be_cmd(const char *cmdname
, int argc
, char **argv
)
148 const be_command_t
*command
;
150 for (command
= &be_command_tbl
[0]; command
->name
!= NULL
; command
++)
151 if (strcmp(command
->name
, cmdname
) == 0)
152 return (command
->func(argc
, argv
));
154 (void) fprintf(stderr
, _("Invalid command: %s\n"), cmdname
);
160 main(int argc
, char **argv
)
164 (void) setlocale(LC_ALL
, "");
165 (void) textdomain(TEXT_DOMAIN
);
174 /* Turn error printing off */
175 libbe_print_errors(B_FALSE
);
177 return (run_be_cmd(cmdname
, --argc
, ++argv
));
181 print_hdr(struct hdr_info
*hdr_info
)
183 boolean_t first
= B_TRUE
;
185 for (i
= 0; i
< NUM_COLS
; i
++) {
186 struct col_info
*col_info
= &hdr_info
->cols
[i
];
187 const char *name
= col_info
->col_name
;
188 size_t width
= col_info
->width
;
193 (void) printf("%-*s", width
, name
);
196 (void) printf(" %-*s", width
, name
);
198 (void) putchar('\n');
202 init_hdr_cols(enum be_fmt be_fmt
, struct hdr_info
*hdr
)
204 struct col_info
*col
= hdr
->cols
;
207 col
[1].col_name
= _("Active");
208 col
[2].col_name
= _("Mountpoint");
209 col
[3].col_name
= _("Space");
210 col
[4].col_name
= _("Policy");
211 col
[5].col_name
= _("Created");
212 col
[6].col_name
= NULL
;
216 col
[0].col_name
= _("BE/Dataset/Snapshot");
219 col
[0].col_name
= _("BE/Dataset");
221 case BE_FMT_SNAPSHOT
:
222 col
[0].col_name
= _("BE/Snapshot");
223 col
[1].col_name
= NULL
;
224 col
[2].col_name
= NULL
;
228 col
[0].col_name
= _("BE");
231 for (i
= 0; i
< NUM_COLS
; i
++) {
232 const char *name
= col
[i
].col_name
;
237 size_t sz
= mbstowcs(wname
, name
, sizeof (wname
) /
240 int wcsw
= wcswidth(wname
, sz
);
246 col
[i
].width
= strlen(name
);
253 nicenum(uint64_t num
, char *buf
, size_t buflen
)
264 u
= " KMGTPE"[index
];
267 (void) snprintf(buf
, buflen
, "%llu", n
);
270 for (i
= 2; i
>= 0; i
--) {
271 if (snprintf(buf
, buflen
, "%.*f%c", i
,
272 (double)num
/ (1ULL << 10 * index
), u
) <= 5)
279 count_widths(enum be_fmt be_fmt
, struct hdr_info
*hdr
, be_node_list_t
*be_nodes
)
281 size_t len
[NUM_COLS
];
282 char buf
[DT_BUF_LEN
];
284 be_node_list_t
*cur_be
;
286 for (i
= 0; i
< NUM_COLS
; i
++)
287 len
[i
] = hdr
->cols
[i
].width
;
289 for (cur_be
= be_nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
290 char name
[ZFS_MAXNAMELEN
+1];
291 const char *be_name
= cur_be
->be_node_name
;
292 const char *root_ds
= cur_be
->be_root_ds
;
294 size_t node_name_len
= strlen(cur_be
->be_node_name
);
295 size_t root_ds_len
= strlen(cur_be
->be_root_ds
);
296 size_t mntpt_len
= 0;
297 size_t policy_len
= 0;
299 uint64_t used
= cur_be
->be_space_used
;
300 be_snapshot_list_t
*snap
= NULL
;
302 if (cur_be
->be_mntpt
!= NULL
)
303 mntpt_len
= strlen(cur_be
->be_mntpt
);
304 if (cur_be
->be_policy_type
!= NULL
)
305 policy_len
= strlen(cur_be
->be_policy_type
);
307 (void) strlcpy(name
, root_ds
, sizeof (name
));
308 pos
= strstr(name
, be_name
);
310 if (be_fmt
== BE_FMT_DEFAULT
) {
311 if (node_name_len
> len
[0])
312 len
[0] = node_name_len
;
314 if (root_ds_len
+ 3 > len
[0])
315 len
[0] = root_ds_len
+ 3;
318 if (mntpt_len
> len
[2])
320 if (policy_len
> len
[4])
323 for (snap
= cur_be
->be_node_snapshots
; snap
!= NULL
;
324 snap
= snap
->be_next_snapshot
) {
325 uint64_t snap_used
= snap
->be_snapshot_space_used
;
326 const char *snap_name
= snap
->be_snapshot_name
;
327 (void) strcpy(pos
, snap_name
);
329 if (be_fmt
== BE_FMT_DEFAULT
)
331 else if (be_fmt
& BE_FMT_SNAPSHOT
) {
332 int snap_len
= strlen(name
) + 3;
333 if (be_fmt
== BE_FMT_SNAPSHOT
)
334 snap_len
-= pos
- name
;
335 if (snap_len
> len
[0])
337 nicenum(snap_used
, buf
, sizeof (buf
));
338 used_len
= strlen(buf
);
339 if (used_len
> len
[3])
344 if (be_fmt
== BE_FMT_DEFAULT
) {
346 nicenum(used
, buf
, sizeof (buf
));
347 used_len
= strlen(buf
);
348 if (used_len
> len
[3])
352 nicenum(used
, buf
, sizeof (buf
));
355 for (i
= 0; i
< NUM_COLS
; i
++)
356 hdr
->cols
[i
].width
= len
[i
];
360 print_be_nodes(const char *be_name
, boolean_t parsable
, struct hdr_info
*hdr
,
361 be_node_list_t
*nodes
)
364 char datetime
[DT_BUF_LEN
];
365 be_node_list_t
*cur_be
;
367 for (cur_be
= nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
368 char active
[3] = "-\0";
370 const char *datetime_fmt
= "%F %R";
371 const char *name
= cur_be
->be_node_name
;
372 const char *mntpt
= cur_be
->be_mntpt
;
373 be_snapshot_list_t
*snap
= NULL
;
374 uint64_t used
= cur_be
->be_space_used
;
375 time_t creation
= cur_be
->be_node_creation
;
378 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 0)
384 tm
= localtime(&creation
);
385 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
387 for (snap
= cur_be
->be_node_snapshots
; snap
!= NULL
;
388 snap
= snap
->be_next_snapshot
)
389 used
+= snap
->be_snapshot_space_used
;
391 if (!cur_be
->be_global_active
)
394 if (cur_be
->be_active
)
396 if (cur_be
->be_active_on_boot
) {
397 if (!cur_be
->be_global_active
)
403 nicenum(used
, buf
, sizeof (buf
));
405 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
409 (cur_be
->be_mounted
? mntpt
: ""),
411 cur_be
->be_policy_type
,
414 (void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
415 hdr
->cols
[0].width
, name
,
416 hdr
->cols
[1].width
, active
,
417 hdr
->cols
[2].width
, (cur_be
->be_mounted
? mntpt
:
419 hdr
->cols
[3].width
, buf
,
420 hdr
->cols
[4].width
, cur_be
->be_policy_type
,
421 hdr
->cols
[5].width
, datetime
);
426 print_be_snapshots(be_node_list_t
*be
, struct hdr_info
*hdr
, boolean_t parsable
)
429 char datetime
[DT_BUF_LEN
];
430 be_snapshot_list_t
*snap
= NULL
;
432 for (snap
= be
->be_node_snapshots
; snap
!= NULL
;
433 snap
= snap
->be_next_snapshot
) {
434 char name
[ZFS_MAXNAMELEN
+1];
435 const char *datetime_fmt
= "%F %R";
436 const char *be_name
= be
->be_node_name
;
437 const char *root_ds
= be
->be_root_ds
;
438 const char *snap_name
= snap
->be_snapshot_name
;
440 uint64_t used
= snap
->be_snapshot_space_used
;
441 time_t creation
= snap
->be_snapshot_creation
;
442 struct tm
*tm
= localtime(&creation
);
444 (void) strncpy(name
, root_ds
, sizeof (name
));
445 pos
= strstr(name
, be_name
);
446 (void) strcpy(pos
, snap_name
);
448 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
449 nicenum(used
, buf
, sizeof (buf
));
452 if (hdr
->cols
[1].width
!= 0)
453 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
462 (void) printf("%s;%s;%llu;%s;%ld\n",
469 if (hdr
->cols
[1].width
!= 0)
470 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
472 hdr
->cols
[0].width
-3, name
,
473 hdr
->cols
[1].width
, "-",
474 hdr
->cols
[2].width
, "-",
475 hdr
->cols
[3].width
, buf
,
476 hdr
->cols
[4].width
, be
->be_policy_type
,
477 hdr
->cols
[5].width
, datetime
);
479 (void) printf(" %-*s %-*s %-*s %-*s\n",
480 hdr
->cols
[0].width
-3, snap_name
,
481 hdr
->cols
[3].width
, buf
,
482 hdr
->cols
[4].width
, be
->be_policy_type
,
483 hdr
->cols
[5].width
, datetime
);
488 print_fmt_nodes(const char *be_name
, enum be_fmt be_fmt
, boolean_t parsable
,
489 struct hdr_info
*hdr
, be_node_list_t
*nodes
)
492 char datetime
[DT_BUF_LEN
];
493 be_node_list_t
*cur_be
;
495 for (cur_be
= nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
496 char active
[3] = "-\0";
498 const char *datetime_fmt
= "%F %R";
499 const char *name
= cur_be
->be_node_name
;
500 const char *mntpt
= cur_be
->be_mntpt
;
501 uint64_t used
= cur_be
->be_space_used
;
502 time_t creation
= cur_be
->be_node_creation
;
505 if (be_name
!= NULL
&& strcmp(be_name
, name
) != 0)
509 (void) printf("%-s\n", name
);
513 tm
= localtime(&creation
);
514 (void) strftime(datetime
, DT_BUF_LEN
, datetime_fmt
, tm
);
516 if (cur_be
->be_active
)
518 if (cur_be
->be_active_on_boot
)
521 nicenum(used
, buf
, sizeof (buf
));
522 if (be_fmt
& BE_FMT_DATASET
)
524 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
525 cur_be
->be_node_name
,
528 (cur_be
->be_mounted
? mntpt
: ""),
530 cur_be
->be_policy_type
,
533 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
535 hdr
->cols
[0].width
-3, cur_be
->be_root_ds
,
536 hdr
->cols
[1].width
, active
,
537 hdr
->cols
[2].width
, (cur_be
->be_mounted
?
539 hdr
->cols
[3].width
, buf
,
540 hdr
->cols
[4].width
, cur_be
->be_policy_type
,
541 hdr
->cols
[5].width
, datetime
);
543 if (be_fmt
& BE_FMT_SNAPSHOT
)
544 print_be_snapshots(cur_be
, hdr
, parsable
);
549 print_nodes(const char *be_name
, boolean_t dsets
, boolean_t snaps
,
550 boolean_t parsable
, be_node_list_t
*be_nodes
)
553 enum be_fmt be_fmt
= BE_FMT_DEFAULT
;
556 be_fmt
|= BE_FMT_DATASET
;
558 be_fmt
|= BE_FMT_SNAPSHOT
;
561 init_hdr_cols(be_fmt
, &hdr
);
562 count_widths(be_fmt
, &hdr
, be_nodes
);
566 if (be_fmt
== BE_FMT_DEFAULT
)
567 print_be_nodes(be_name
, parsable
, &hdr
, be_nodes
);
569 print_fmt_nodes(be_name
, be_fmt
, parsable
, &hdr
, be_nodes
);
573 confirm_destroy(const char *name
)
575 boolean_t res
= B_FALSE
;
576 const char *yesre
= nl_langinfo(YESEXPR
);
577 const char *nore
= nl_langinfo(NOEXPR
);
582 int cflags
= REG_EXTENDED
;
584 if (regcomp(&yes_re
, yesre
, cflags
) != 0) {
585 /* should not happen */
586 (void) fprintf(stderr
, _("Failed to compile 'yes' regexp\n"));
589 if (regcomp(&no_re
, nore
, cflags
) != 0) {
590 /* should not happen */
591 (void) fprintf(stderr
, _("Failed to compile 'no' regexp\n"));
596 (void) printf(_("Are you sure you want to destroy %s?\n"
597 "This action cannot be undone (y/[n]): "), name
);
599 answer
= fgets(buf
, sizeof (buf
), stdin
);
600 if (answer
== NULL
|| *answer
== '\0' || *answer
== 10)
603 if (regexec(&yes_re
, answer
, 0, NULL
, 0) == 0) {
605 } else if (regexec(&no_re
, answer
, 0, NULL
, 0) != 0) {
606 (void) fprintf(stderr
, _("Invalid response. "
607 "Please enter 'y' or 'n'.\n"));
617 be_nvl_alloc(nvlist_t
**nvlp
)
619 assert(nvlp
!= NULL
);
621 if (nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, 0) != 0) {
622 (void) perror(_("nvlist_alloc failed.\n"));
630 be_nvl_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
634 if (nvlist_add_string(nvl
, name
, val
) != 0) {
635 (void) fprintf(stderr
, _("nvlist_add_string failed for "
636 "%s (%s).\n"), name
, val
);
644 be_nvl_add_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
*val
)
648 if (nvlist_add_nvlist(nvl
, name
, val
) != 0) {
649 (void) fprintf(stderr
, _("nvlist_add_nvlist failed for %s.\n"),
658 be_nvl_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
662 if (nvlist_add_uint16(nvl
, name
, val
) != 0) {
663 (void) fprintf(stderr
, _("nvlist_add_uint16 failed for "
664 "%s (%hu).\n"), name
, val
);
672 be_do_activate(int argc
, char **argv
)
679 while ((c
= getopt(argc
, argv
, "v")) != -1) {
682 libbe_print_errors(B_TRUE
);
700 if (be_nvl_alloc(&be_attrs
) != 0)
703 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
706 err
= be_activate(be_attrs
);
710 (void) printf(_("Activated successfully\n"));
712 case BE_ERR_BE_NOENT
:
713 (void) fprintf(stderr
, _("%s does not exist or appear "
714 "to be a valid BE.\nPlease check that the name of "
715 "the BE provided is correct.\n"), obe_name
);
719 (void) fprintf(stderr
, _("Unable to activate %s.\n"), obe_name
);
720 (void) fprintf(stderr
, _("You have insufficient privileges to "
721 "execute this command.\n"));
723 case BE_ERR_ACTIVATE_CURR
:
725 (void) fprintf(stderr
, _("Unable to activate %s.\n"), obe_name
);
726 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
730 nvlist_free(be_attrs
);
735 be_do_create(int argc
, char **argv
)
738 nvlist_t
*zfs_props
= NULL
;
739 boolean_t activate
= B_FALSE
;
740 boolean_t is_snap
= B_FALSE
;
743 char *obe_name
= NULL
;
744 char *snap_name
= NULL
;
745 char *nbe_zpool
= NULL
;
746 char *nbe_name
= NULL
;
747 char *nbe_desc
= NULL
;
748 char *propname
= NULL
;
749 char *propval
= NULL
;
752 while ((c
= getopt(argc
, argv
, "ad:e:io:p:v")) != -1) {
764 if (zfs_props
== NULL
&& be_nvl_alloc(&zfs_props
) != 0)
768 if ((propval
= strchr(propname
, '=')) == NULL
) {
769 (void) fprintf(stderr
, _("missing "
770 "'=' for -o option\n"));
775 if (nvlist_lookup_string(zfs_props
, propname
,
777 (void) fprintf(stderr
, _("property '%s' "
778 "specified multiple times\n"), propname
);
782 if (be_nvl_add_string(zfs_props
, propname
, propval
)
791 libbe_print_errors(B_TRUE
);
809 if ((snap_name
= strrchr(nbe_name
, '@')) != NULL
) {
810 if (snap_name
[1] == '\0') {
827 * Check if obe_name is really a snapshot name.
828 * If so, split it out.
830 if ((snap_name
= strrchr(obe_name
, '@')) != NULL
) {
831 if (snap_name
[1] == '\0') {
839 } else if (is_snap
) {
844 if (be_nvl_alloc(&be_attrs
) != 0)
848 if (zfs_props
!= NULL
&& be_nvl_add_nvlist(be_attrs
,
849 BE_ATTR_ORIG_BE_NAME
, zfs_props
) != 0)
852 if (obe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
853 BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
856 if (snap_name
!= NULL
&& be_nvl_add_string(be_attrs
,
857 BE_ATTR_SNAP_NAME
, snap_name
) != 0)
860 if (nbe_zpool
!= NULL
&& be_nvl_add_string(be_attrs
,
861 BE_ATTR_NEW_BE_POOL
, nbe_zpool
) != 0)
864 if (nbe_name
!= NULL
&& be_nvl_add_string(be_attrs
,
865 BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
868 if (nbe_desc
!= NULL
&& be_nvl_add_string(be_attrs
,
869 BE_ATTR_NEW_BE_DESC
, nbe_desc
) != 0)
873 err
= be_create_snapshot(be_attrs
);
875 err
= be_copy(be_attrs
);
879 if (!is_snap
&& !nbe_name
) {
881 * We requested an auto named BE; find out the
882 * name of the BE that was created for us and
883 * the auto snapshot created from the original BE.
885 if (nvlist_lookup_string(be_attrs
, BE_ATTR_NEW_BE_NAME
,
887 (void) fprintf(stderr
, _("failed to get %s "
888 "attribute\n"), BE_ATTR_NEW_BE_NAME
);
891 (void) printf(_("Auto named BE: %s\n"),
894 if (nvlist_lookup_string(be_attrs
, BE_ATTR_SNAP_NAME
,
896 (void) fprintf(stderr
, _("failed to get %s "
897 "attribute\n"), BE_ATTR_SNAP_NAME
);
900 (void) printf(_("Auto named snapshot: %s\n"),
904 if (!is_snap
&& activate
) {
905 char *args
[] = { "activate", "", NULL
};
909 err
= be_do_activate(2, args
);
913 (void) printf(_("Created successfully\n"));
915 case BE_ERR_BE_EXISTS
:
916 (void) fprintf(stderr
, _("BE %s already exists\n."
917 "Please choose a different BE name.\n"), nbe_name
);
919 case BE_ERR_SS_EXISTS
:
920 (void) fprintf(stderr
, _("BE %s snapshot %s already exists.\n"
921 "Please choose a different snapshot name.\n"), obe_name
,
927 (void) fprintf(stderr
, _("Unable to create snapshot "
928 "%s.\n"), snap_name
);
930 (void) fprintf(stderr
, _("Unable to create %s.\n"),
932 (void) fprintf(stderr
, _("You have insufficient privileges to "
933 "execute this command.\n"));
937 (void) fprintf(stderr
, _("Unable to create snapshot "
938 "%s.\n"), snap_name
);
940 (void) fprintf(stderr
, _("Unable to create %s.\n"),
942 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
946 nvlist_free(be_attrs
);
948 if (zfs_props
!= NULL
)
949 nvlist_free(zfs_props
);
955 be_do_destroy(int argc
, char **argv
)
958 boolean_t is_snap
= B_FALSE
;
959 boolean_t suppress_prompt
= B_FALSE
;
962 int destroy_flags
= 0;
966 while ((c
= getopt(argc
, argv
, "fFsv")) != -1) {
969 destroy_flags
|= BE_DESTROY_FLAG_FORCE_UNMOUNT
;
972 destroy_flags
|= BE_DESTROY_FLAG_SNAPSHOTS
;
975 libbe_print_errors(B_TRUE
);
978 suppress_prompt
= B_TRUE
;
995 if (!suppress_prompt
&& !confirm_destroy(be_name
)) {
996 (void) printf(_("%s has not been destroyed.\n"), be_name
);
1000 if ((snap_name
= strrchr(be_name
, '@')) != NULL
) {
1001 if (snap_name
[1] == '\0') {
1011 if (be_nvl_alloc(&be_attrs
) != 0)
1015 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, be_name
) != 0)
1019 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
,
1023 err
= be_destroy_snapshot(be_attrs
);
1025 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_DESTROY_FLAGS
,
1026 destroy_flags
) != 0)
1029 err
= be_destroy(be_attrs
);
1034 (void) printf(_("Destroyed successfully\n"));
1036 case BE_ERR_MOUNTED
:
1037 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1038 (void) fprintf(stderr
, _("It is currently mounted and must be "
1039 "unmounted before it can be destroyed.\n" "Use 'beadm "
1040 "unmount %s' to unmount the BE before destroying\nit or "
1041 "'beadm destroy -f %s'.\n"), be_name
, be_name
);
1043 case BE_ERR_DESTROY_CURR_BE
:
1044 (void) fprintf(stderr
, _("%s is the currently active BE and "
1045 "cannot be destroyed.\nYou must boot from another BE in "
1046 "order to destroy %s.\n"), be_name
, be_name
);
1048 case BE_ERR_ZONES_UNMOUNT
:
1049 (void) fprintf(stderr
, _("Unable to destroy one of " "%s's "
1050 "zone BE's.\nUse 'beadm destroy -f %s' or "
1051 "'zfs -f destroy <dataset>'.\n"), be_name
, be_name
);
1053 case BE_ERR_SS_NOENT
:
1054 (void) fprintf(stderr
, _("%s does not exist or appear "
1055 "to be a valid snapshot.\nPlease check that the name of "
1056 "the snapshot provided is correct.\n"), snap_name
);
1060 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1061 (void) fprintf(stderr
, _("You have insufficient privileges to "
1062 "execute this command.\n"));
1064 case BE_ERR_SS_EXISTS
:
1065 (void) fprintf(stderr
, _("Unable to destroy %s: "
1066 "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1067 "'zfs -r destroy <dataset>'.\n"), be_name
, be_name
);
1070 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1071 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1075 nvlist_free(be_attrs
);
1080 be_do_list(int argc
, char **argv
)
1082 be_node_list_t
*be_nodes
= NULL
;
1083 boolean_t all
= B_FALSE
;
1084 boolean_t dsets
= B_FALSE
;
1085 boolean_t snaps
= B_FALSE
;
1086 boolean_t parsable
= B_FALSE
;
1089 char *be_name
= NULL
;
1090 be_sort_t order
= BE_SORT_UNSPECIFIED
;
1092 while ((c
= getopt(argc
, argv
, "adk:svHK:")) != -1) {
1102 if (order
!= BE_SORT_UNSPECIFIED
) {
1103 (void) fprintf(stderr
, _("Sort key can be "
1104 "specified only once.\n"));
1108 if (strcmp(optarg
, "date") == 0) {
1110 order
= BE_SORT_DATE
;
1112 order
= BE_SORT_DATE_REV
;
1115 if (strcmp(optarg
, "name") == 0) {
1117 order
= BE_SORT_NAME
;
1119 order
= BE_SORT_NAME_REV
;
1122 if (strcmp(optarg
, "space") == 0) {
1124 order
= BE_SORT_SPACE
;
1126 order
= BE_SORT_SPACE_REV
;
1129 (void) fprintf(stderr
, _("Unknown sort key: %s\n"),
1137 libbe_print_errors(B_TRUE
);
1150 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1151 "are mutually exclusive.\n"), "-d");
1156 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1157 "are mutually exclusive.\n"), "-s");
1173 err
= be_list(be_name
, &be_nodes
);
1177 /* the default sort is ascending date, no need to sort twice */
1178 if (order
== BE_SORT_UNSPECIFIED
)
1179 order
= BE_SORT_DATE
;
1181 if (order
!= BE_SORT_DATE
)
1182 be_sort(&be_nodes
, order
);
1184 print_nodes(be_name
, dsets
, snaps
, parsable
, be_nodes
);
1186 case BE_ERR_BE_NOENT
:
1187 if (be_name
== NULL
)
1188 (void) fprintf(stderr
, _("No boot environments found "
1189 "on this system.\n"));
1191 (void) fprintf(stderr
, _("%s does not exist or appear "
1192 "to be a valid BE.\nPlease check that the name of "
1193 "the BE provided is correct.\n"), be_name
);
1197 (void) fprintf(stderr
, _("Unable to display Boot "
1199 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1202 if (be_nodes
!= NULL
)
1203 be_free_list(be_nodes
);
1208 be_do_mount(int argc
, char **argv
)
1211 boolean_t shared_fs
= B_FALSE
;
1214 int mount_flags
= 0;
1217 char *tmp_mp
= NULL
;
1219 while ((c
= getopt(argc
, argv
, "s:v")) != -1) {
1224 mount_flags
|= BE_MOUNT_FLAG_SHARED_FS
;
1226 if (strcmp(optarg
, "rw") == 0) {
1227 mount_flags
|= BE_MOUNT_FLAG_SHARED_RW
;
1228 } else if (strcmp(optarg
, "ro") != 0) {
1229 (void) fprintf(stderr
, _("The -s flag "
1230 "requires an argument [ rw | ro ]\n"));
1237 libbe_print_errors(B_TRUE
);
1248 if (argc
< 1 || argc
> 2) {
1256 mountpoint
= argv
[1];
1257 if (mountpoint
[0] != '/') {
1258 (void) fprintf(stderr
, _("Invalid mount point %s. "
1259 "Mount point must start with a /.\n"), mountpoint
);
1263 const char *tmpdir
= getenv("TMPDIR");
1264 const char *tmpname
= "tmp.XXXXXX";
1270 sz
= asprintf(&tmp_mp
, "%s/%s", tmpdir
, tmpname
);
1272 (void) fprintf(stderr
, _("internal error: "
1273 "out of memory\n"));
1277 mountpoint
= mkdtemp(tmp_mp
);
1280 if (be_nvl_alloc(&be_attrs
) != 0)
1283 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1286 if (be_nvl_add_string(be_attrs
, BE_ATTR_MOUNTPOINT
, mountpoint
) != 0)
1289 if (shared_fs
&& be_nvl_add_uint16(be_attrs
, BE_ATTR_MOUNT_FLAGS
,
1293 err
= be_mount(be_attrs
);
1297 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint
);
1299 case BE_ERR_BE_NOENT
:
1300 (void) fprintf(stderr
, _("%s does not exist or appear "
1301 "to be a valid BE.\nPlease check that the name of "
1302 "the BE provided is correct.\n"), obe_name
);
1304 case BE_ERR_MOUNTED
:
1305 (void) fprintf(stderr
, _("%s is already mounted.\n"
1306 "Please unmount the BE before mounting it again.\n"),
1311 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1312 (void) fprintf(stderr
, _("You have insufficient privileges to "
1313 "execute this command.\n"));
1315 case BE_ERR_NO_MOUNTED_ZONE
:
1316 (void) fprintf(stderr
, _("Mounted on '%s'.\nUnable to mount "
1317 "one of %s's zone BE's.\n"), mountpoint
, obe_name
);
1320 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1321 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1327 nvlist_free(be_attrs
);
1332 be_do_unmount(int argc
, char **argv
)
1338 int unmount_flags
= 0;
1340 while ((c
= getopt(argc
, argv
, "fv")) != -1) {
1343 unmount_flags
|= BE_UNMOUNT_FLAG_FORCE
;
1346 libbe_print_errors(B_TRUE
);
1364 if (be_nvl_alloc(&be_attrs
) != 0)
1368 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1371 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_UNMOUNT_FLAGS
,
1372 unmount_flags
) != 0)
1375 err
= be_unmount(be_attrs
);
1379 (void) printf(_("Unmounted successfully\n"));
1381 case BE_ERR_BE_NOENT
:
1382 (void) fprintf(stderr
, _("%s does not exist or appear "
1383 "to be a valid BE.\nPlease check that the name of "
1384 "the BE provided is correct.\n"), obe_name
);
1386 case BE_ERR_UMOUNT_CURR_BE
:
1387 (void) fprintf(stderr
, _("%s is the currently active BE.\n"
1388 "It cannot be unmounted unless another BE is the "
1389 "currently active BE.\n"), obe_name
);
1391 case BE_ERR_UMOUNT_SHARED
:
1392 (void) fprintf(stderr
, _("%s is a shared file system and it "
1393 "cannot be unmounted.\n"), obe_name
);
1397 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1398 (void) fprintf(stderr
, _("You have insufficient privileges to "
1399 "execute this command.\n"));
1402 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1403 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1407 nvlist_free(be_attrs
);
1412 be_do_rename(int argc
, char **argv
)
1420 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1423 libbe_print_errors(B_TRUE
);
1442 if (be_nvl_alloc(&be_attrs
) != 0)
1445 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1448 if (be_nvl_add_string(be_attrs
, BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
1451 err
= be_rename(be_attrs
);
1455 (void) printf(_("Renamed successfully\n"));
1457 case BE_ERR_BE_NOENT
:
1458 (void) fprintf(stderr
, _("%s does not exist or appear "
1459 "to be a valid BE.\nPlease check that the name of "
1460 "the BE provided is correct.\n"), obe_name
);
1464 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1466 (void) fprintf(stderr
, _("You have insufficient privileges to "
1467 "execute this command.\n"));
1470 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1472 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1476 nvlist_free(be_attrs
);
1481 be_do_rollback(int argc
, char **argv
)
1489 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1492 libbe_print_errors(B_TRUE
);
1503 if (argc
< 1 || argc
> 2) {
1510 snap_name
= argv
[1];
1511 else { /* argc == 1 */
1512 if ((snap_name
= strrchr(obe_name
, '@')) != NULL
) {
1513 if (snap_name
[1] == '\0') {
1518 snap_name
[0] = '\0';
1526 if (be_nvl_alloc(&be_attrs
) != 0)
1529 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1532 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
, snap_name
) != 0)
1535 err
= be_rollback(be_attrs
);
1539 (void) printf(_("Rolled back successfully\n"));
1541 case BE_ERR_BE_NOENT
:
1542 (void) fprintf(stderr
, _("%s does not exist or appear "
1543 "to be a valid BE.\nPlease check that the name of "
1544 "the BE provided is correct.\n"), obe_name
);
1546 case BE_ERR_SS_NOENT
:
1547 (void) fprintf(stderr
, _("%s does not exist or appear "
1548 "to be a valid snapshot.\nPlease check that the name of "
1549 "the snapshot provided is correct.\n"), snap_name
);
1553 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1554 "failed.\n"), obe_name
, snap_name
);
1555 (void) fprintf(stderr
, _("You have insufficient privileges to "
1556 "execute this command.\n"));
1559 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1560 "failed.\n"), obe_name
, snap_name
);
1561 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1565 nvlist_free(be_attrs
);