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>
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 [-v] beName\n"
127 "\tbeadm create [-a] [-d BE_desc]\n"
128 "\t\t[-o property=value] ... [-p zpool] \n"
129 "\t\t[-e nonActiveBe | beName@snapshot] [-v] beName\n"
130 "\tbeadm create [-d BE_desc]\n"
131 "\t\t[-o property=value] ... [-p zpool] [-v] beName@snapshot\n"
132 "\tbeadm destroy [-Ffsv] beName \n"
133 "\tbeadm destroy [-Fv] beName@snapshot \n"
134 "\tbeadm list [[-a] | [-d] [-s]] [-H]\n"
135 "\t\t[-k|-K date | name | space] [-v] [beName]\n"
136 "\tbeadm mount [-s ro|rw] [-v] beName [mountpoint]\n"
137 "\tbeadm unmount [-fv] beName | mountpoint\n"
138 "\tbeadm umount [-fv] beName | mountpoint\n"
139 "\tbeadm rename [-v] origBeName newBeName\n"
140 "\tbeadm rollback [-v] beName snapshot\n"
141 "\tbeadm rollback [-v] beName@snapshot\n"));
145 run_be_cmd(const char *cmdname
, int argc
, char **argv
)
147 const be_command_t
*command
;
149 for (command
= &be_command_tbl
[0]; command
->name
!= NULL
; command
++)
150 if (strcmp(command
->name
, cmdname
) == 0)
151 return (command
->func(argc
, argv
));
153 (void) fprintf(stderr
, _("Invalid command: %s\n"), cmdname
);
159 main(int argc
, char **argv
)
163 (void) setlocale(LC_ALL
, "");
164 (void) textdomain(TEXT_DOMAIN
);
173 /* Turn error printing off */
174 libbe_print_errors(B_FALSE
);
176 return (run_be_cmd(cmdname
, --argc
, ++argv
));
180 print_hdr(struct hdr_info
*hdr_info
)
182 boolean_t first
= B_TRUE
;
184 for (i
= 0; i
< NUM_COLS
; i
++) {
185 struct col_info
*col_info
= &hdr_info
->cols
[i
];
186 const char *name
= col_info
->col_name
;
187 size_t width
= col_info
->width
;
192 (void) printf("%-*s", width
, name
);
195 (void) printf(" %-*s", width
, name
);
197 (void) putchar('\n');
201 init_hdr_cols(enum be_fmt be_fmt
, struct hdr_info
*hdr
)
203 struct col_info
*col
= hdr
->cols
;
206 col
[1].col_name
= _("Active");
207 col
[2].col_name
= _("Mountpoint");
208 col
[3].col_name
= _("Space");
209 col
[4].col_name
= _("Policy");
210 col
[5].col_name
= _("Created");
211 col
[6].col_name
= NULL
;
215 col
[0].col_name
= _("BE/Dataset/Snapshot");
218 col
[0].col_name
= _("BE/Dataset");
220 case BE_FMT_SNAPSHOT
:
221 col
[0].col_name
= _("BE/Snapshot");
222 col
[1].col_name
= NULL
;
223 col
[2].col_name
= NULL
;
227 col
[0].col_name
= _("BE");
230 for (i
= 0; i
< NUM_COLS
; i
++) {
231 const char *name
= col
[i
].col_name
;
236 size_t sz
= mbstowcs(wname
, name
, sizeof (wname
) /
239 int wcsw
= wcswidth(wname
, sz
);
245 col
[i
].width
= strlen(name
);
252 nicenum(uint64_t num
, char *buf
, size_t buflen
)
263 u
= " KMGTPE"[index
];
266 (void) snprintf(buf
, buflen
, "%llu", n
);
269 for (i
= 2; i
>= 0; i
--) {
270 if (snprintf(buf
, buflen
, "%.*f%c", i
,
271 (double)num
/ (1ULL << 10 * index
), u
) <= 5)
278 count_widths(enum be_fmt be_fmt
, struct hdr_info
*hdr
, be_node_list_t
*be_nodes
)
280 size_t len
[NUM_COLS
];
281 char buf
[DT_BUF_LEN
];
283 be_node_list_t
*cur_be
;
285 for (i
= 0; i
< NUM_COLS
; i
++)
286 len
[i
] = hdr
->cols
[i
].width
;
288 for (cur_be
= be_nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
289 char name
[ZFS_MAX_DATASET_NAME_LEN
+ 1];
290 const char *be_name
= cur_be
->be_node_name
;
291 const char *root_ds
= cur_be
->be_root_ds
;
293 size_t node_name_len
= strlen(cur_be
->be_node_name
);
294 size_t root_ds_len
= strlen(cur_be
->be_root_ds
);
295 size_t mntpt_len
= 0;
296 size_t policy_len
= 0;
298 uint64_t used
= cur_be
->be_space_used
;
299 be_snapshot_list_t
*snap
= NULL
;
301 if (cur_be
->be_mntpt
!= NULL
)
302 mntpt_len
= strlen(cur_be
->be_mntpt
);
303 if (cur_be
->be_policy_type
!= NULL
)
304 policy_len
= strlen(cur_be
->be_policy_type
);
306 (void) strlcpy(name
, root_ds
, sizeof (name
));
307 pos
= strstr(name
, be_name
);
309 if (be_fmt
== BE_FMT_DEFAULT
) {
310 if (node_name_len
> len
[0])
311 len
[0] = node_name_len
;
313 if (root_ds_len
+ 3 > len
[0])
314 len
[0] = root_ds_len
+ 3;
317 if (mntpt_len
> len
[2])
319 if (policy_len
> len
[4])
322 for (snap
= cur_be
->be_node_snapshots
; snap
!= NULL
;
323 snap
= snap
->be_next_snapshot
) {
324 uint64_t snap_used
= snap
->be_snapshot_space_used
;
325 const char *snap_name
= snap
->be_snapshot_name
;
326 (void) strcpy(pos
, snap_name
);
328 if (be_fmt
== BE_FMT_DEFAULT
)
330 else if (be_fmt
& BE_FMT_SNAPSHOT
) {
331 int snap_len
= strlen(name
) + 3;
332 if (be_fmt
== BE_FMT_SNAPSHOT
)
333 snap_len
-= pos
- name
;
334 if (snap_len
> len
[0])
336 nicenum(snap_used
, buf
, sizeof (buf
));
337 used_len
= strlen(buf
);
338 if (used_len
> len
[3])
343 if (be_fmt
== BE_FMT_DEFAULT
) {
345 nicenum(used
, buf
, sizeof (buf
));
346 used_len
= strlen(buf
);
347 if (used_len
> len
[3])
351 nicenum(used
, buf
, sizeof (buf
));
354 for (i
= 0; i
< NUM_COLS
; i
++)
355 hdr
->cols
[i
].width
= len
[i
];
359 print_be_nodes(const char *be_name
, boolean_t parsable
, struct hdr_info
*hdr
,
360 be_node_list_t
*nodes
)
363 char datetime
[DT_BUF_LEN
];
364 be_node_list_t
*cur_be
;
366 for (cur_be
= nodes
; cur_be
!= NULL
; cur_be
= cur_be
->be_next_node
) {
367 char active
[3] = "-\0";
369 const char *datetime_fmt
= "%F %R";
370 const char *name
= cur_be
->be_node_name
;
371 const char *mntpt
= cur_be
->be_mntpt
;
372 const char *uuid_str
= cur_be
->be_uuid_str
;
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",
407 (uuid_str
!= NULL
? uuid_str
: ""),
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_MAX_DATASET_NAME_LEN
+ 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 nvlist_free(zfs_props
);
954 be_do_destroy(int argc
, char **argv
)
957 boolean_t is_snap
= B_FALSE
;
958 boolean_t suppress_prompt
= B_FALSE
;
961 int destroy_flags
= 0;
965 while ((c
= getopt(argc
, argv
, "fFsv")) != -1) {
968 destroy_flags
|= BE_DESTROY_FLAG_FORCE_UNMOUNT
;
971 destroy_flags
|= BE_DESTROY_FLAG_SNAPSHOTS
;
974 libbe_print_errors(B_TRUE
);
977 suppress_prompt
= B_TRUE
;
994 if (!suppress_prompt
&& !confirm_destroy(be_name
)) {
995 (void) printf(_("%s has not been destroyed.\n"), be_name
);
999 if ((snap_name
= strrchr(be_name
, '@')) != NULL
) {
1000 if (snap_name
[1] == '\0') {
1010 if (be_nvl_alloc(&be_attrs
) != 0)
1014 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, be_name
) != 0)
1018 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
,
1022 err
= be_destroy_snapshot(be_attrs
);
1024 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_DESTROY_FLAGS
,
1025 destroy_flags
) != 0)
1028 err
= be_destroy(be_attrs
);
1033 (void) printf(_("Destroyed successfully\n"));
1035 case BE_ERR_MOUNTED
:
1036 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1037 (void) fprintf(stderr
, _("It is currently mounted and must be "
1038 "unmounted before it can be destroyed.\n" "Use 'beadm "
1039 "unmount %s' to unmount the BE before destroying\nit or "
1040 "'beadm destroy -f %s'.\n"), be_name
, be_name
);
1042 case BE_ERR_DESTROY_CURR_BE
:
1043 (void) fprintf(stderr
, _("%s is the currently active BE and "
1044 "cannot be destroyed.\nYou must boot from another BE in "
1045 "order to destroy %s.\n"), be_name
, be_name
);
1047 case BE_ERR_ZONES_UNMOUNT
:
1048 (void) fprintf(stderr
, _("Unable to destroy one of " "%s's "
1049 "zone BE's.\nUse 'beadm destroy -f %s' or "
1050 "'zfs -f destroy <dataset>'.\n"), be_name
, be_name
);
1052 case BE_ERR_SS_NOENT
:
1053 (void) fprintf(stderr
, _("%s does not exist or appear "
1054 "to be a valid snapshot.\nPlease check that the name of "
1055 "the snapshot provided is correct.\n"), snap_name
);
1059 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1060 (void) fprintf(stderr
, _("You have insufficient privileges to "
1061 "execute this command.\n"));
1063 case BE_ERR_SS_EXISTS
:
1064 (void) fprintf(stderr
, _("Unable to destroy %s: "
1065 "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1066 "'zfs -r destroy <dataset>'.\n"), be_name
, be_name
);
1069 (void) fprintf(stderr
, _("Unable to destroy %s.\n"), be_name
);
1070 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1074 nvlist_free(be_attrs
);
1079 be_do_list(int argc
, char **argv
)
1081 be_node_list_t
*be_nodes
= NULL
;
1082 boolean_t all
= B_FALSE
;
1083 boolean_t dsets
= B_FALSE
;
1084 boolean_t snaps
= B_FALSE
;
1085 boolean_t parsable
= B_FALSE
;
1088 char *be_name
= NULL
;
1089 be_sort_t order
= BE_SORT_UNSPECIFIED
;
1091 while ((c
= getopt(argc
, argv
, "adk:svHK:")) != -1) {
1101 if (order
!= BE_SORT_UNSPECIFIED
) {
1102 (void) fprintf(stderr
, _("Sort key can be "
1103 "specified only once.\n"));
1107 if (strcmp(optarg
, "date") == 0) {
1109 order
= BE_SORT_DATE
;
1111 order
= BE_SORT_DATE_REV
;
1114 if (strcmp(optarg
, "name") == 0) {
1116 order
= BE_SORT_NAME
;
1118 order
= BE_SORT_NAME_REV
;
1121 if (strcmp(optarg
, "space") == 0) {
1123 order
= BE_SORT_SPACE
;
1125 order
= BE_SORT_SPACE_REV
;
1128 (void) fprintf(stderr
, _("Unknown sort key: %s\n"),
1136 libbe_print_errors(B_TRUE
);
1149 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1150 "are mutually exclusive.\n"), "-d");
1155 (void) fprintf(stderr
, _("Invalid options: -a and %s "
1156 "are mutually exclusive.\n"), "-s");
1172 err
= be_list(be_name
, &be_nodes
);
1176 /* the default sort is ascending date, no need to sort twice */
1177 if (order
== BE_SORT_UNSPECIFIED
)
1178 order
= BE_SORT_DATE
;
1180 if (order
!= BE_SORT_DATE
) {
1181 err
= be_sort(&be_nodes
, order
);
1182 if (err
!= BE_SUCCESS
) {
1183 (void) fprintf(stderr
, _("Unable to sort Boot "
1185 (void) fprintf(stderr
, "%s\n",
1186 be_err_to_str(err
));
1191 print_nodes(be_name
, dsets
, snaps
, parsable
, be_nodes
);
1193 case BE_ERR_BE_NOENT
:
1194 if (be_name
== NULL
)
1195 (void) fprintf(stderr
, _("No boot environments found "
1196 "on this system.\n"));
1198 (void) fprintf(stderr
, _("%s does not exist or appear "
1199 "to be a valid BE.\nPlease check that the name of "
1200 "the BE provided is correct.\n"), be_name
);
1204 (void) fprintf(stderr
, _("Unable to display Boot "
1206 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1209 if (be_nodes
!= NULL
)
1210 be_free_list(be_nodes
);
1215 be_do_mount(int argc
, char **argv
)
1218 boolean_t shared_fs
= B_FALSE
;
1221 int mount_flags
= 0;
1224 char *tmp_mp
= NULL
;
1226 while ((c
= getopt(argc
, argv
, "s:v")) != -1) {
1231 mount_flags
|= BE_MOUNT_FLAG_SHARED_FS
;
1233 if (strcmp(optarg
, "rw") == 0) {
1234 mount_flags
|= BE_MOUNT_FLAG_SHARED_RW
;
1235 } else if (strcmp(optarg
, "ro") != 0) {
1236 (void) fprintf(stderr
, _("The -s flag "
1237 "requires an argument [ rw | ro ]\n"));
1244 libbe_print_errors(B_TRUE
);
1255 if (argc
< 1 || argc
> 2) {
1263 mountpoint
= argv
[1];
1264 if (mountpoint
[0] != '/') {
1265 (void) fprintf(stderr
, _("Invalid mount point %s. "
1266 "Mount point must start with a /.\n"), mountpoint
);
1270 const char *tmpdir
= getenv("TMPDIR");
1271 const char *tmpname
= "tmp.XXXXXX";
1277 sz
= asprintf(&tmp_mp
, "%s/%s", tmpdir
, tmpname
);
1279 (void) fprintf(stderr
, _("internal error: "
1280 "out of memory\n"));
1284 mountpoint
= mkdtemp(tmp_mp
);
1287 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_string(be_attrs
, BE_ATTR_MOUNTPOINT
, mountpoint
) != 0)
1296 if (shared_fs
&& be_nvl_add_uint16(be_attrs
, BE_ATTR_MOUNT_FLAGS
,
1300 err
= be_mount(be_attrs
);
1304 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint
);
1306 case BE_ERR_BE_NOENT
:
1307 (void) fprintf(stderr
, _("%s does not exist or appear "
1308 "to be a valid BE.\nPlease check that the name of "
1309 "the BE provided is correct.\n"), obe_name
);
1311 case BE_ERR_MOUNTED
:
1312 (void) fprintf(stderr
, _("%s is already mounted.\n"
1313 "Please unmount the BE before mounting it again.\n"),
1318 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1319 (void) fprintf(stderr
, _("You have insufficient privileges to "
1320 "execute this command.\n"));
1322 case BE_ERR_NO_MOUNTED_ZONE
:
1323 (void) fprintf(stderr
, _("Mounted on '%s'.\nUnable to mount "
1324 "one of %s's zone BE's.\n"), mountpoint
, obe_name
);
1327 (void) fprintf(stderr
, _("Unable to mount %s.\n"), obe_name
);
1328 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1334 nvlist_free(be_attrs
);
1339 be_do_unmount(int argc
, char **argv
)
1345 int unmount_flags
= 0;
1347 while ((c
= getopt(argc
, argv
, "fv")) != -1) {
1350 unmount_flags
|= BE_UNMOUNT_FLAG_FORCE
;
1353 libbe_print_errors(B_TRUE
);
1371 if (be_nvl_alloc(&be_attrs
) != 0)
1375 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1378 if (be_nvl_add_uint16(be_attrs
, BE_ATTR_UNMOUNT_FLAGS
,
1379 unmount_flags
) != 0)
1382 err
= be_unmount(be_attrs
);
1386 (void) printf(_("Unmounted successfully\n"));
1388 case BE_ERR_BE_NOENT
:
1389 (void) fprintf(stderr
, _("%s does not exist or appear "
1390 "to be a valid BE.\nPlease check that the name of "
1391 "the BE provided is correct.\n"), obe_name
);
1393 case BE_ERR_UMOUNT_CURR_BE
:
1394 (void) fprintf(stderr
, _("%s is the currently active BE.\n"
1395 "It cannot be unmounted unless another BE is the "
1396 "currently active BE.\n"), obe_name
);
1398 case BE_ERR_UMOUNT_SHARED
:
1399 (void) fprintf(stderr
, _("%s is a shared file system and it "
1400 "cannot be unmounted.\n"), obe_name
);
1404 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1405 (void) fprintf(stderr
, _("You have insufficient privileges to "
1406 "execute this command.\n"));
1409 (void) fprintf(stderr
, _("Unable to unmount %s.\n"), obe_name
);
1410 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1414 nvlist_free(be_attrs
);
1419 be_do_rename(int argc
, char **argv
)
1427 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1430 libbe_print_errors(B_TRUE
);
1449 if (be_nvl_alloc(&be_attrs
) != 0)
1452 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1455 if (be_nvl_add_string(be_attrs
, BE_ATTR_NEW_BE_NAME
, nbe_name
) != 0)
1458 err
= be_rename(be_attrs
);
1462 (void) printf(_("Renamed successfully\n"));
1464 case BE_ERR_BE_NOENT
:
1465 (void) fprintf(stderr
, _("%s does not exist or appear "
1466 "to be a valid BE.\nPlease check that the name of "
1467 "the BE provided is correct.\n"), obe_name
);
1471 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1473 (void) fprintf(stderr
, _("You have insufficient privileges to "
1474 "execute this command.\n"));
1477 (void) fprintf(stderr
, _("Rename of BE %s failed.\n"),
1479 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1483 nvlist_free(be_attrs
);
1488 be_do_rollback(int argc
, char **argv
)
1496 while ((c
= getopt(argc
, argv
, "v")) != -1) {
1499 libbe_print_errors(B_TRUE
);
1510 if (argc
< 1 || argc
> 2) {
1517 snap_name
= argv
[1];
1518 else { /* argc == 1 */
1519 if ((snap_name
= strrchr(obe_name
, '@')) != NULL
) {
1520 if (snap_name
[1] == '\0') {
1525 snap_name
[0] = '\0';
1533 if (be_nvl_alloc(&be_attrs
) != 0)
1536 if (be_nvl_add_string(be_attrs
, BE_ATTR_ORIG_BE_NAME
, obe_name
) != 0)
1539 if (be_nvl_add_string(be_attrs
, BE_ATTR_SNAP_NAME
, snap_name
) != 0)
1542 err
= be_rollback(be_attrs
);
1546 (void) printf(_("Rolled back successfully\n"));
1548 case BE_ERR_BE_NOENT
:
1549 (void) fprintf(stderr
, _("%s does not exist or appear "
1550 "to be a valid BE.\nPlease check that the name of "
1551 "the BE provided is correct.\n"), obe_name
);
1553 case BE_ERR_SS_NOENT
:
1554 (void) fprintf(stderr
, _("%s does not exist or appear "
1555 "to be a valid snapshot.\nPlease check that the name of "
1556 "the snapshot provided is correct.\n"), snap_name
);
1560 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1561 "failed.\n"), obe_name
, snap_name
);
1562 (void) fprintf(stderr
, _("You have insufficient privileges to "
1563 "execute this command.\n"));
1566 (void) fprintf(stderr
, _("Rollback of BE %s snapshot %s "
1567 "failed.\n"), obe_name
, snap_name
);
1568 (void) fprintf(stderr
, "%s\n", be_err_to_str(err
));
1572 nvlist_free(be_attrs
);