Print this page
10122 beadm buffer over-run
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/beadm/beadm.c
+++ new/usr/src/cmd/beadm/beadm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 25 * Copyright 2015 Toomas Soome <tsoome@me.com>
26 26 * Copyright 2015 Gary Mills
27 27 * Copyright (c) 2015 by Delphix. All rights reserved.
28 28 * Copyright 2017 Jason King
29 29 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
30 + * Copyright (c) 2018, Joyent, Inc.
30 31 */
31 32
32 33 /*
33 34 * System includes
34 35 */
35 36
36 37 #include <assert.h>
37 38 #include <stdio.h>
38 39 #include <strings.h>
39 40 #include <libzfs.h>
40 41 #include <locale.h>
41 42 #include <langinfo.h>
42 43 #include <stdlib.h>
43 44 #include <wchar.h>
44 45 #include <sys/types.h>
45 46 #include <sys/debug.h>
46 47 #include <libcmdutils.h>
47 48
48 49 #include "libbe.h"
49 50
50 51 #ifndef lint
51 52 #define _(x) gettext(x)
52 53 #else
53 54 #define _(x) (x)
54 55 #endif
55 56
56 57 #ifndef TEXT_DOMAIN
57 58 #define TEXT_DOMAIN "SYS_TEST"
58 59 #endif
59 60
60 61 #define DT_BUF_LEN (128)
61 62 #define NUM_COLS (6)
62 63 CTASSERT(DT_BUF_LEN >= NN_NUMBUF_SZ);
63 64
64 65 static int be_do_activate(int argc, char **argv);
65 66 static int be_do_create(int argc, char **argv);
66 67 static int be_do_destroy(int argc, char **argv);
67 68 static int be_do_list(int argc, char **argv);
68 69 static int be_do_mount(int argc, char **argv);
69 70 static int be_do_unmount(int argc, char **argv);
70 71 static int be_do_rename(int argc, char **argv);
71 72 static int be_do_rollback(int argc, char **argv);
72 73 static void usage(void);
73 74
74 75 /*
75 76 * single column name/width output format description
76 77 */
77 78 struct col_info {
78 79 const char *col_name;
79 80 size_t width;
80 81 };
81 82
82 83 /*
83 84 * all columns output format
84 85 */
85 86 struct hdr_info {
86 87 struct col_info cols[NUM_COLS];
87 88 };
88 89
89 90 /*
90 91 * type of possible output formats
91 92 */
92 93 enum be_fmt {
93 94 BE_FMT_DEFAULT,
94 95 BE_FMT_DATASET,
95 96 BE_FMT_SNAPSHOT,
96 97 BE_FMT_ALL
97 98 };
98 99
99 100 /*
100 101 * command handler description
101 102 */
102 103 typedef struct be_command {
103 104 const char *name;
104 105 int (*func)(int argc, char **argv);
105 106 } be_command_t;
106 107
107 108 /*
108 109 * sorted list of be commands
109 110 */
110 111 static const be_command_t be_command_tbl[] = {
111 112 { "activate", be_do_activate },
112 113 { "create", be_do_create },
113 114 { "destroy", be_do_destroy },
114 115 { "list", be_do_list },
115 116 { "mount", be_do_mount },
116 117 { "unmount", be_do_unmount },
117 118 { "umount", be_do_unmount }, /* unmount alias */
118 119 { "rename", be_do_rename },
119 120 { "rollback", be_do_rollback },
120 121 { NULL, NULL },
121 122 };
122 123
123 124 static void
124 125 usage(void)
125 126 {
126 127 (void) fprintf(stderr, _("usage:\n"
127 128 "\tbeadm subcommand cmd_options\n"
128 129 "\n"
129 130 "\tsubcommands:\n"
130 131 "\n"
131 132 "\tbeadm activate [-v] beName\n"
132 133 "\tbeadm create [-a] [-d BE_desc]\n"
133 134 "\t\t[-o property=value] ... [-p zpool] \n"
134 135 "\t\t[-e nonActiveBe | beName@snapshot] [-v] beName\n"
135 136 "\tbeadm create [-d BE_desc]\n"
136 137 "\t\t[-o property=value] ... [-p zpool] [-v] beName@snapshot\n"
137 138 "\tbeadm destroy [-Ffsv] beName \n"
138 139 "\tbeadm destroy [-Fv] beName@snapshot \n"
139 140 "\tbeadm list [[-a] | [-d] [-s]] [-H]\n"
140 141 "\t\t[-k|-K date | name | space] [-v] [beName]\n"
141 142 "\tbeadm mount [-s ro|rw] [-v] beName [mountpoint]\n"
142 143 "\tbeadm unmount [-fv] beName | mountpoint\n"
143 144 "\tbeadm umount [-fv] beName | mountpoint\n"
144 145 "\tbeadm rename [-v] origBeName newBeName\n"
145 146 "\tbeadm rollback [-v] beName snapshot\n"
146 147 "\tbeadm rollback [-v] beName@snapshot\n"));
147 148 }
148 149
149 150 static int
150 151 run_be_cmd(const char *cmdname, int argc, char **argv)
151 152 {
152 153 const be_command_t *command;
153 154
154 155 for (command = &be_command_tbl[0]; command->name != NULL; command++)
155 156 if (strcmp(command->name, cmdname) == 0)
156 157 return (command->func(argc, argv));
157 158
158 159 (void) fprintf(stderr, _("Invalid command: %s\n"), cmdname);
159 160 usage();
160 161 return (1);
161 162 }
162 163
163 164 int
164 165 main(int argc, char **argv)
165 166 {
166 167 const char *cmdname;
167 168
168 169 (void) setlocale(LC_ALL, "");
169 170 (void) textdomain(TEXT_DOMAIN);
170 171
171 172 if (argc < 2) {
172 173 usage();
173 174 return (1);
174 175 }
175 176
176 177 cmdname = argv[1];
177 178
178 179 /* Turn error printing off */
179 180 libbe_print_errors(B_FALSE);
180 181
181 182 return (run_be_cmd(cmdname, --argc, ++argv));
182 183 }
183 184
184 185 static void
185 186 print_hdr(struct hdr_info *hdr_info)
186 187 {
187 188 boolean_t first = B_TRUE;
188 189 size_t i;
189 190 for (i = 0; i < NUM_COLS; i++) {
190 191 struct col_info *col_info = &hdr_info->cols[i];
191 192 const char *name = col_info->col_name;
192 193 size_t width = col_info->width;
193 194 if (name == NULL)
194 195 continue;
195 196
196 197 if (first) {
197 198 (void) printf("%-*s", width, name);
198 199 first = B_FALSE;
199 200 } else
200 201 (void) printf(" %-*s", width, name);
201 202 }
202 203 (void) putchar('\n');
203 204 }
204 205
205 206 static void
↓ open down ↓ |
166 lines elided |
↑ open up ↑ |
206 207 init_hdr_cols(enum be_fmt be_fmt, struct hdr_info *hdr)
207 208 {
208 209 struct col_info *col = hdr->cols;
209 210 size_t i;
210 211
211 212 col[1].col_name = _("Active");
212 213 col[2].col_name = _("Mountpoint");
213 214 col[3].col_name = _("Space");
214 215 col[4].col_name = _("Policy");
215 216 col[5].col_name = _("Created");
216 - col[6].col_name = NULL;
217 217
218 218 switch (be_fmt) {
219 219 case BE_FMT_ALL:
220 220 col[0].col_name = _("BE/Dataset/Snapshot");
221 221 break;
222 222 case BE_FMT_DATASET:
223 223 col[0].col_name = _("BE/Dataset");
224 224 break;
225 225 case BE_FMT_SNAPSHOT:
226 226 col[0].col_name = _("BE/Snapshot");
227 227 col[1].col_name = NULL;
228 228 col[2].col_name = NULL;
229 229 break;
230 230 case BE_FMT_DEFAULT:
231 231 default:
232 232 col[0].col_name = _("BE");
233 233 }
234 234
235 235 for (i = 0; i < NUM_COLS; i++) {
236 236 const char *name = col[i].col_name;
237 237 col[i].width = 0;
238 238
239 239 if (name != NULL) {
240 240 wchar_t wname[128];
241 241 size_t sz = mbstowcs(wname, name, sizeof (wname) /
242 242 sizeof (wchar_t));
243 243 if (sz > 0) {
244 244 int wcsw = wcswidth(wname, sz);
245 245 if (wcsw > 0)
246 246 col[i].width = wcsw;
247 247 else
248 248 col[i].width = sz;
249 249 } else {
250 250 col[i].width = strlen(name);
251 251 }
252 252 }
253 253 }
254 254 }
255 255
256 256 static void
257 257 count_widths(enum be_fmt be_fmt, struct hdr_info *hdr, be_node_list_t *be_nodes)
258 258 {
259 259 size_t len[NUM_COLS];
260 260 char buf[DT_BUF_LEN];
261 261 int i;
262 262 be_node_list_t *cur_be;
263 263
264 264 for (i = 0; i < NUM_COLS; i++)
265 265 len[i] = hdr->cols[i].width;
266 266
267 267 for (cur_be = be_nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
268 268 char name[ZFS_MAX_DATASET_NAME_LEN + 1];
269 269 const char *be_name = cur_be->be_node_name;
270 270 const char *root_ds = cur_be->be_root_ds;
271 271 char *pos;
272 272 size_t node_name_len = strlen(cur_be->be_node_name);
273 273 size_t root_ds_len = strlen(cur_be->be_root_ds);
274 274 size_t mntpt_len = 0;
275 275 size_t policy_len = 0;
276 276 size_t used_len;
277 277 uint64_t used = cur_be->be_space_used;
278 278 be_snapshot_list_t *snap = NULL;
279 279
280 280 if (cur_be->be_mntpt != NULL)
281 281 mntpt_len = strlen(cur_be->be_mntpt);
282 282 if (cur_be->be_policy_type != NULL)
283 283 policy_len = strlen(cur_be->be_policy_type);
284 284
285 285 (void) strlcpy(name, root_ds, sizeof (name));
286 286 pos = strstr(name, be_name);
287 287
288 288 if (be_fmt == BE_FMT_DEFAULT) {
289 289 if (node_name_len > len[0])
290 290 len[0] = node_name_len;
291 291 } else {
292 292 if (root_ds_len + 3 > len[0])
293 293 len[0] = root_ds_len + 3;
294 294 }
295 295
296 296 if (mntpt_len > len[2])
297 297 len[2] = mntpt_len;
298 298 if (policy_len > len[4])
299 299 len[4] = policy_len;
300 300
301 301 for (snap = cur_be->be_node_snapshots; snap != NULL;
302 302 snap = snap->be_next_snapshot) {
303 303 uint64_t snap_used = snap->be_snapshot_space_used;
304 304 const char *snap_name = snap->be_snapshot_name;
305 305 (void) strcpy(pos, snap_name);
306 306
307 307 if (be_fmt == BE_FMT_DEFAULT)
308 308 used += snap_used;
309 309 else if (be_fmt & BE_FMT_SNAPSHOT) {
310 310 int snap_len = strlen(name) + 3;
311 311 if (be_fmt == BE_FMT_SNAPSHOT)
312 312 snap_len -= pos - name;
313 313 if (snap_len > len[0])
314 314 len[0] = snap_len;
315 315 nicenum(snap_used, buf, sizeof (buf));
316 316 used_len = strlen(buf);
317 317 if (used_len > len[3])
318 318 len[3] = used_len;
319 319 }
320 320 }
321 321
322 322 if (be_fmt == BE_FMT_DEFAULT) {
323 323 int used_len;
324 324 nicenum(used, buf, sizeof (buf));
325 325 used_len = strlen(buf);
326 326 if (used_len > len[3])
327 327 len[3] = used_len;
328 328 }
329 329
330 330 nicenum(used, buf, sizeof (buf));
331 331 }
332 332
333 333 for (i = 0; i < NUM_COLS; i++)
334 334 hdr->cols[i].width = len[i];
335 335 }
336 336
337 337 static void
338 338 print_be_nodes(const char *be_name, boolean_t parsable, struct hdr_info *hdr,
339 339 be_node_list_t *nodes)
340 340 {
341 341 char buf[64];
342 342 char datetime[DT_BUF_LEN];
343 343 be_node_list_t *cur_be;
344 344
345 345 for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
346 346 char active[3] = "-\0";
347 347 int ai = 0;
348 348 const char *datetime_fmt = "%F %R";
349 349 const char *name = cur_be->be_node_name;
350 350 const char *mntpt = cur_be->be_mntpt;
351 351 const char *uuid_str = cur_be->be_uuid_str;
352 352 be_snapshot_list_t *snap = NULL;
353 353 uint64_t used = cur_be->be_space_used;
354 354 time_t creation = cur_be->be_node_creation;
355 355 struct tm *tm;
356 356
357 357 if (be_name != NULL && strcmp(be_name, name) != 0)
358 358 continue;
359 359
360 360 if (parsable)
361 361 active[0] = '\0';
362 362
363 363 tm = localtime(&creation);
364 364 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
365 365
366 366 for (snap = cur_be->be_node_snapshots; snap != NULL;
367 367 snap = snap->be_next_snapshot)
368 368 used += snap->be_snapshot_space_used;
369 369
370 370 if (!cur_be->be_global_active)
371 371 active[ai++] = 'x';
372 372
373 373 if (cur_be->be_active)
374 374 active[ai++] = 'N';
375 375 if (cur_be->be_active_on_boot) {
376 376 if (!cur_be->be_global_active)
377 377 active[ai] = 'b';
378 378 else
379 379 active[ai] = 'R';
380 380 }
381 381
382 382 nicenum(used, buf, sizeof (buf));
383 383 if (parsable)
384 384 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
385 385 name,
386 386 (uuid_str != NULL ? uuid_str: ""),
387 387 active,
388 388 (cur_be->be_mounted ? mntpt: ""),
389 389 used,
390 390 cur_be->be_policy_type,
391 391 creation);
392 392 else
393 393 (void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
394 394 hdr->cols[0].width, name,
395 395 hdr->cols[1].width, active,
396 396 hdr->cols[2].width, (cur_be->be_mounted ? mntpt:
397 397 "-"),
398 398 hdr->cols[3].width, buf,
399 399 hdr->cols[4].width, cur_be->be_policy_type,
400 400 hdr->cols[5].width, datetime);
401 401 }
402 402 }
403 403
404 404 static void
405 405 print_be_snapshots(be_node_list_t *be, struct hdr_info *hdr, boolean_t parsable)
406 406 {
407 407 char buf[64];
408 408 char datetime[DT_BUF_LEN];
409 409 be_snapshot_list_t *snap = NULL;
410 410
411 411 for (snap = be->be_node_snapshots; snap != NULL;
412 412 snap = snap->be_next_snapshot) {
413 413 char name[ZFS_MAX_DATASET_NAME_LEN + 1];
414 414 const char *datetime_fmt = "%F %R";
415 415 const char *be_name = be->be_node_name;
416 416 const char *root_ds = be->be_root_ds;
417 417 const char *snap_name = snap->be_snapshot_name;
418 418 char *pos;
419 419 uint64_t used = snap->be_snapshot_space_used;
420 420 time_t creation = snap->be_snapshot_creation;
421 421 struct tm *tm = localtime(&creation);
422 422
423 423 (void) strncpy(name, root_ds, sizeof (name));
424 424 pos = strstr(name, be_name);
425 425 (void) strcpy(pos, snap_name);
426 426
427 427 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
428 428 nicenum(used, buf, sizeof (buf));
429 429
430 430 if (parsable)
431 431 if (hdr->cols[1].width != 0)
432 432 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
433 433 be_name,
434 434 snap_name,
435 435 "",
436 436 "",
437 437 used,
438 438 be->be_policy_type,
439 439 creation);
440 440 else
441 441 (void) printf("%s;%s;%llu;%s;%ld\n",
442 442 be_name,
443 443 snap_name,
444 444 used,
445 445 be->be_policy_type,
446 446 creation);
447 447 else
448 448 if (hdr->cols[1].width != 0)
449 449 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
450 450 "%-*s\n",
451 451 hdr->cols[0].width-3, name,
452 452 hdr->cols[1].width, "-",
453 453 hdr->cols[2].width, "-",
454 454 hdr->cols[3].width, buf,
455 455 hdr->cols[4].width, be->be_policy_type,
456 456 hdr->cols[5].width, datetime);
457 457 else
458 458 (void) printf(" %-*s %-*s %-*s %-*s\n",
459 459 hdr->cols[0].width-3, snap_name,
460 460 hdr->cols[3].width, buf,
461 461 hdr->cols[4].width, be->be_policy_type,
462 462 hdr->cols[5].width, datetime);
463 463 }
464 464 }
465 465
466 466 static void
467 467 print_fmt_nodes(const char *be_name, enum be_fmt be_fmt, boolean_t parsable,
468 468 struct hdr_info *hdr, be_node_list_t *nodes)
469 469 {
470 470 char buf[64];
471 471 char datetime[DT_BUF_LEN];
472 472 be_node_list_t *cur_be;
473 473
474 474 for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
475 475 char active[3] = "-\0";
476 476 int ai = 0;
477 477 const char *datetime_fmt = "%F %R";
478 478 const char *name = cur_be->be_node_name;
479 479 const char *mntpt = cur_be->be_mntpt;
480 480 uint64_t used = cur_be->be_space_used;
481 481 time_t creation = cur_be->be_node_creation;
482 482 struct tm *tm;
483 483
484 484 if (be_name != NULL && strcmp(be_name, name) != 0)
485 485 continue;
486 486
487 487 if (!parsable)
488 488 (void) printf("%-s\n", name);
489 489 else
490 490 active[0] = '\0';
491 491
492 492 tm = localtime(&creation);
493 493 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
494 494
495 495 if (cur_be->be_active)
496 496 active[ai++] = 'N';
497 497 if (cur_be->be_active_on_boot)
498 498 active[ai] = 'R';
499 499
500 500 nicenum(used, buf, sizeof (buf));
501 501 if (be_fmt & BE_FMT_DATASET)
502 502 if (parsable)
503 503 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
504 504 cur_be->be_node_name,
505 505 cur_be->be_root_ds,
506 506 active,
507 507 (cur_be->be_mounted ? mntpt: ""),
508 508 used,
509 509 cur_be->be_policy_type,
510 510 creation);
511 511 else
512 512 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
513 513 "%-*s\n",
514 514 hdr->cols[0].width-3, cur_be->be_root_ds,
515 515 hdr->cols[1].width, active,
516 516 hdr->cols[2].width, (cur_be->be_mounted ?
517 517 mntpt: "-"),
518 518 hdr->cols[3].width, buf,
519 519 hdr->cols[4].width, cur_be->be_policy_type,
520 520 hdr->cols[5].width, datetime);
521 521
522 522 if (be_fmt & BE_FMT_SNAPSHOT)
523 523 print_be_snapshots(cur_be, hdr, parsable);
524 524 }
525 525 }
526 526
527 527 static void
528 528 print_nodes(const char *be_name, boolean_t dsets, boolean_t snaps,
529 529 boolean_t parsable, be_node_list_t *be_nodes)
530 530 {
531 531 struct hdr_info hdr;
532 532 enum be_fmt be_fmt = BE_FMT_DEFAULT;
533 533
534 534 if (dsets)
535 535 be_fmt |= BE_FMT_DATASET;
536 536 if (snaps)
537 537 be_fmt |= BE_FMT_SNAPSHOT;
538 538
539 539 if (!parsable) {
540 540 init_hdr_cols(be_fmt, &hdr);
541 541 count_widths(be_fmt, &hdr, be_nodes);
542 542 print_hdr(&hdr);
543 543 }
544 544
545 545 if (be_fmt == BE_FMT_DEFAULT)
546 546 print_be_nodes(be_name, parsable, &hdr, be_nodes);
547 547 else
548 548 print_fmt_nodes(be_name, be_fmt, parsable, &hdr, be_nodes);
549 549 }
550 550
551 551 static boolean_t
552 552 confirm_destroy(const char *name)
553 553 {
554 554 boolean_t res = B_FALSE;
555 555 const char *yesre = nl_langinfo(YESEXPR);
556 556 const char *nore = nl_langinfo(NOEXPR);
557 557 regex_t yes_re;
558 558 regex_t no_re;
559 559 char buf[128];
560 560 char *answer;
561 561 int cflags = REG_EXTENDED;
562 562
563 563 if (regcomp(&yes_re, yesre, cflags) != 0) {
564 564 /* should not happen */
565 565 (void) fprintf(stderr, _("Failed to compile 'yes' regexp\n"));
566 566 return (res);
567 567 }
568 568 if (regcomp(&no_re, nore, cflags) != 0) {
569 569 /* should not happen */
570 570 (void) fprintf(stderr, _("Failed to compile 'no' regexp\n"));
571 571 regfree(&yes_re);
572 572 return (res);
573 573 }
574 574
575 575 (void) printf(_("Are you sure you want to destroy %s?\n"
576 576 "This action cannot be undone (y/[n]): "), name);
577 577
578 578 answer = fgets(buf, sizeof (buf), stdin);
579 579 if (answer == NULL || *answer == '\0' || *answer == 10)
580 580 goto out;
581 581
582 582 if (regexec(&yes_re, answer, 0, NULL, 0) == 0) {
583 583 res = B_TRUE;
584 584 } else if (regexec(&no_re, answer, 0, NULL, 0) != 0) {
585 585 (void) fprintf(stderr, _("Invalid response. "
586 586 "Please enter 'y' or 'n'.\n"));
587 587 }
588 588
589 589 out:
590 590 regfree(&yes_re);
591 591 regfree(&no_re);
592 592 return (res);
593 593 }
594 594
595 595 static int
596 596 be_nvl_alloc(nvlist_t **nvlp)
597 597 {
598 598 assert(nvlp != NULL);
599 599
600 600 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) {
601 601 (void) perror(_("nvlist_alloc failed.\n"));
602 602 return (1);
603 603 }
604 604
605 605 return (0);
606 606 }
607 607
608 608 static int
609 609 be_nvl_add_string(nvlist_t *nvl, const char *name, const char *val)
610 610 {
611 611 assert(nvl != NULL);
612 612
613 613 if (nvlist_add_string(nvl, name, val) != 0) {
614 614 (void) fprintf(stderr, _("nvlist_add_string failed for "
615 615 "%s (%s).\n"), name, val);
616 616 return (1);
617 617 }
618 618
619 619 return (0);
620 620 }
621 621
622 622 static int
623 623 be_nvl_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
624 624 {
625 625 assert(nvl != NULL);
626 626
627 627 if (nvlist_add_nvlist(nvl, name, val) != 0) {
628 628 (void) fprintf(stderr, _("nvlist_add_nvlist failed for %s.\n"),
629 629 name);
630 630 return (1);
631 631 }
632 632
633 633 return (0);
634 634 }
635 635
636 636 static int
637 637 be_nvl_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
638 638 {
639 639 assert(nvl != NULL);
640 640
641 641 if (nvlist_add_uint16(nvl, name, val) != 0) {
642 642 (void) fprintf(stderr, _("nvlist_add_uint16 failed for "
643 643 "%s (%hu).\n"), name, val);
644 644 return (1);
645 645 }
646 646
647 647 return (0);
648 648 }
649 649
650 650 static int
651 651 be_do_activate(int argc, char **argv)
652 652 {
653 653 nvlist_t *be_attrs;
654 654 int err = 1;
655 655 int c;
656 656 char *obe_name;
657 657
658 658 while ((c = getopt(argc, argv, "v")) != -1) {
659 659 switch (c) {
660 660 case 'v':
661 661 libbe_print_errors(B_TRUE);
662 662 break;
663 663 default:
664 664 usage();
665 665 return (1);
666 666 }
667 667 }
668 668
669 669 argc -= optind;
670 670 argv += optind;
671 671
672 672 if (argc != 1) {
673 673 usage();
674 674 return (1);
675 675 }
676 676
677 677 obe_name = argv[0];
678 678
679 679 if (be_nvl_alloc(&be_attrs) != 0)
680 680 return (1);
681 681
682 682 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
683 683 goto out;
684 684
685 685 err = be_activate(be_attrs);
686 686
687 687 switch (err) {
688 688 case BE_SUCCESS:
689 689 (void) printf(_("Activated successfully\n"));
690 690 break;
691 691 case BE_ERR_BE_NOENT:
692 692 (void) fprintf(stderr, _("%s does not exist or appear "
693 693 "to be a valid BE.\nPlease check that the name of "
694 694 "the BE provided is correct.\n"), obe_name);
695 695 break;
696 696 case BE_ERR_PERM:
697 697 case BE_ERR_ACCESS:
698 698 (void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
699 699 (void) fprintf(stderr, _("You have insufficient privileges to "
700 700 "execute this command.\n"));
701 701 break;
702 702 case BE_ERR_ACTIVATE_CURR:
703 703 default:
704 704 (void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
705 705 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
706 706 }
707 707
708 708 out:
709 709 nvlist_free(be_attrs);
710 710 return (err);
711 711 }
712 712
713 713 static int
714 714 be_do_create(int argc, char **argv)
715 715 {
716 716 nvlist_t *be_attrs;
717 717 nvlist_t *zfs_props = NULL;
718 718 boolean_t activate = B_FALSE;
719 719 boolean_t is_snap = B_FALSE;
720 720 int c;
721 721 int err = 1;
722 722 char *obe_name = NULL;
723 723 char *snap_name = NULL;
724 724 char *nbe_zpool = NULL;
725 725 char *nbe_name = NULL;
726 726 char *nbe_desc = NULL;
727 727 char *propname = NULL;
728 728 char *propval = NULL;
729 729 char *strval = NULL;
730 730
731 731 while ((c = getopt(argc, argv, "ad:e:io:p:v")) != -1) {
732 732 switch (c) {
733 733 case 'a':
734 734 activate = B_TRUE;
735 735 break;
736 736 case 'd':
737 737 nbe_desc = optarg;
738 738 break;
739 739 case 'e':
740 740 obe_name = optarg;
741 741 break;
742 742 case 'o':
743 743 if (zfs_props == NULL && be_nvl_alloc(&zfs_props) != 0)
744 744 return (1);
745 745
746 746 propname = optarg;
747 747 if ((propval = strchr(propname, '=')) == NULL) {
748 748 (void) fprintf(stderr, _("missing "
749 749 "'=' for -o option\n"));
750 750 goto out2;
751 751 }
752 752 *propval = '\0';
753 753 propval++;
754 754 if (nvlist_lookup_string(zfs_props, propname,
755 755 &strval) == 0) {
756 756 (void) fprintf(stderr, _("property '%s' "
757 757 "specified multiple times\n"), propname);
758 758 goto out2;
759 759
760 760 }
761 761 if (be_nvl_add_string(zfs_props, propname, propval)
762 762 != 0)
763 763 goto out2;
764 764
765 765 break;
766 766 case 'p':
767 767 nbe_zpool = optarg;
768 768 break;
769 769 case 'v':
770 770 libbe_print_errors(B_TRUE);
771 771 break;
772 772 default:
773 773 usage();
774 774 goto out2;
775 775 }
776 776 }
777 777
778 778 argc -= optind;
779 779 argv += optind;
780 780
781 781 if (argc != 1) {
782 782 usage();
783 783 goto out2;
784 784 }
785 785
786 786 nbe_name = argv[0];
787 787
788 788 if ((snap_name = strrchr(nbe_name, '@')) != NULL) {
789 789 if (snap_name[1] == '\0') {
790 790 usage();
791 791 goto out2;
792 792 }
793 793
794 794 snap_name[0] = '\0';
795 795 snap_name++;
796 796 is_snap = B_TRUE;
797 797 }
798 798
799 799 if (obe_name) {
800 800 if (is_snap) {
801 801 usage();
802 802 goto out2;
803 803 }
804 804
805 805 /*
806 806 * Check if obe_name is really a snapshot name.
807 807 * If so, split it out.
808 808 */
809 809 if ((snap_name = strrchr(obe_name, '@')) != NULL) {
810 810 if (snap_name[1] == '\0') {
811 811 usage();
812 812 goto out2;
813 813 }
814 814
815 815 snap_name[0] = '\0';
816 816 snap_name++;
817 817 }
818 818 } else if (is_snap) {
819 819 obe_name = nbe_name;
820 820 nbe_name = NULL;
821 821 }
822 822
823 823 if (be_nvl_alloc(&be_attrs) != 0)
824 824 goto out2;
825 825
826 826
827 827 if (zfs_props != NULL && be_nvl_add_nvlist(be_attrs,
828 828 BE_ATTR_ORIG_BE_NAME, zfs_props) != 0)
829 829 goto out;
830 830
831 831 if (obe_name != NULL && be_nvl_add_string(be_attrs,
832 832 BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
833 833 goto out;
834 834
835 835 if (snap_name != NULL && be_nvl_add_string(be_attrs,
836 836 BE_ATTR_SNAP_NAME, snap_name) != 0)
837 837 goto out;
838 838
839 839 if (nbe_zpool != NULL && be_nvl_add_string(be_attrs,
840 840 BE_ATTR_NEW_BE_POOL, nbe_zpool) != 0)
841 841 goto out;
842 842
843 843 if (nbe_name != NULL && be_nvl_add_string(be_attrs,
844 844 BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
845 845 goto out;
846 846
847 847 if (nbe_desc != NULL && be_nvl_add_string(be_attrs,
848 848 BE_ATTR_NEW_BE_DESC, nbe_desc) != 0)
849 849 goto out;
850 850
851 851 if (is_snap)
852 852 err = be_create_snapshot(be_attrs);
853 853 else
854 854 err = be_copy(be_attrs);
855 855
856 856 switch (err) {
857 857 case BE_SUCCESS:
858 858 if (!is_snap && !nbe_name) {
859 859 /*
860 860 * We requested an auto named BE; find out the
861 861 * name of the BE that was created for us and
862 862 * the auto snapshot created from the original BE.
863 863 */
864 864 if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME,
865 865 &nbe_name) != 0) {
866 866 (void) fprintf(stderr, _("failed to get %s "
867 867 "attribute\n"), BE_ATTR_NEW_BE_NAME);
868 868 break;
869 869 } else
870 870 (void) printf(_("Auto named BE: %s\n"),
871 871 nbe_name);
872 872
873 873 if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME,
874 874 &snap_name) != 0) {
875 875 (void) fprintf(stderr, _("failed to get %s "
876 876 "attribute\n"), BE_ATTR_SNAP_NAME);
877 877 break;
878 878 } else
879 879 (void) printf(_("Auto named snapshot: %s\n"),
880 880 snap_name);
881 881 }
882 882
883 883 if (!is_snap && activate) {
884 884 char *args[] = { "activate", "", NULL };
885 885 args[1] = nbe_name;
886 886 optind = 1;
887 887
888 888 err = be_do_activate(2, args);
889 889 goto out;
890 890 }
891 891
892 892 (void) printf(_("Created successfully\n"));
893 893 break;
894 894 case BE_ERR_BE_EXISTS:
895 895 (void) fprintf(stderr, _("BE %s already exists\n."
896 896 "Please choose a different BE name.\n"), nbe_name);
897 897 break;
898 898 case BE_ERR_SS_EXISTS:
899 899 (void) fprintf(stderr, _("BE %s snapshot %s already exists.\n"
900 900 "Please choose a different snapshot name.\n"), obe_name,
901 901 snap_name);
902 902 break;
903 903 case BE_ERR_PERM:
904 904 case BE_ERR_ACCESS:
905 905 if (is_snap)
906 906 (void) fprintf(stderr, _("Unable to create snapshot "
907 907 "%s.\n"), snap_name);
908 908 else
909 909 (void) fprintf(stderr, _("Unable to create %s.\n"),
910 910 nbe_name);
911 911 (void) fprintf(stderr, _("You have insufficient privileges to "
912 912 "execute this command.\n"));
913 913 break;
914 914 default:
915 915 if (is_snap)
916 916 (void) fprintf(stderr, _("Unable to create snapshot "
917 917 "%s.\n"), snap_name);
918 918 else
919 919 (void) fprintf(stderr, _("Unable to create %s.\n"),
920 920 nbe_name);
921 921 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
922 922 }
923 923
924 924 out:
925 925 nvlist_free(be_attrs);
926 926 out2:
927 927 nvlist_free(zfs_props);
928 928
929 929 return (err);
930 930 }
931 931
932 932 static int
933 933 be_do_destroy(int argc, char **argv)
934 934 {
935 935 nvlist_t *be_attrs;
936 936 boolean_t is_snap = B_FALSE;
937 937 boolean_t suppress_prompt = B_FALSE;
938 938 int err = 1;
939 939 int c;
940 940 int destroy_flags = 0;
941 941 char *snap_name;
942 942 char *be_name;
943 943
944 944 while ((c = getopt(argc, argv, "fFsv")) != -1) {
945 945 switch (c) {
946 946 case 'f':
947 947 destroy_flags |= BE_DESTROY_FLAG_FORCE_UNMOUNT;
948 948 break;
949 949 case 's':
950 950 destroy_flags |= BE_DESTROY_FLAG_SNAPSHOTS;
951 951 break;
952 952 case 'v':
953 953 libbe_print_errors(B_TRUE);
954 954 break;
955 955 case 'F':
956 956 suppress_prompt = B_TRUE;
957 957 break;
958 958 default:
959 959 usage();
960 960 return (1);
961 961 }
962 962 }
963 963
964 964 argc -= optind;
965 965 argv += optind;
966 966
967 967 if (argc != 1) {
968 968 usage();
969 969 return (1);
970 970 }
971 971
972 972 be_name = argv[0];
973 973 if (!suppress_prompt && !confirm_destroy(be_name)) {
974 974 (void) printf(_("%s has not been destroyed.\n"), be_name);
975 975 return (0);
976 976 }
977 977
978 978 if ((snap_name = strrchr(be_name, '@')) != NULL) {
979 979 if (snap_name[1] == '\0') {
980 980 usage();
981 981 return (1);
982 982 }
983 983
984 984 is_snap = B_TRUE;
985 985 *snap_name = '\0';
986 986 snap_name++;
987 987 }
988 988
989 989 if (be_nvl_alloc(&be_attrs) != 0)
990 990 return (1);
991 991
992 992
993 993 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, be_name) != 0)
994 994 goto out;
995 995
996 996 if (is_snap) {
997 997 if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME,
998 998 snap_name) != 0)
999 999 goto out;
1000 1000
1001 1001 err = be_destroy_snapshot(be_attrs);
1002 1002 } else {
1003 1003 if (be_nvl_add_uint16(be_attrs, BE_ATTR_DESTROY_FLAGS,
1004 1004 destroy_flags) != 0)
1005 1005 goto out;
1006 1006
1007 1007 err = be_destroy(be_attrs);
1008 1008 }
1009 1009
1010 1010 switch (err) {
1011 1011 case BE_SUCCESS:
1012 1012 (void) printf(_("Destroyed successfully\n"));
1013 1013 break;
1014 1014 case BE_ERR_MOUNTED:
1015 1015 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1016 1016 (void) fprintf(stderr, _("It is currently mounted and must be "
1017 1017 "unmounted before it can be destroyed.\n" "Use 'beadm "
1018 1018 "unmount %s' to unmount the BE before destroying\nit or "
1019 1019 "'beadm destroy -f %s'.\n"), be_name, be_name);
1020 1020 break;
1021 1021 case BE_ERR_DESTROY_CURR_BE:
1022 1022 (void) fprintf(stderr, _("%s is the currently active BE and "
1023 1023 "cannot be destroyed.\nYou must boot from another BE in "
1024 1024 "order to destroy %s.\n"), be_name, be_name);
1025 1025 break;
1026 1026 case BE_ERR_ZONES_UNMOUNT:
1027 1027 (void) fprintf(stderr, _("Unable to destroy one of " "%s's "
1028 1028 "zone BE's.\nUse 'beadm destroy -f %s' or "
1029 1029 "'zfs -f destroy <dataset>'.\n"), be_name, be_name);
1030 1030 break;
1031 1031 case BE_ERR_SS_NOENT:
1032 1032 (void) fprintf(stderr, _("%s does not exist or appear "
1033 1033 "to be a valid snapshot.\nPlease check that the name of "
1034 1034 "the snapshot provided is correct.\n"), snap_name);
1035 1035 break;
1036 1036 case BE_ERR_PERM:
1037 1037 case BE_ERR_ACCESS:
1038 1038 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1039 1039 (void) fprintf(stderr, _("You have insufficient privileges to "
1040 1040 "execute this command.\n"));
1041 1041 break;
1042 1042 case BE_ERR_SS_EXISTS:
1043 1043 (void) fprintf(stderr, _("Unable to destroy %s: "
1044 1044 "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1045 1045 "'zfs -r destroy <dataset>'.\n"), be_name, be_name);
1046 1046 break;
1047 1047 default:
1048 1048 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1049 1049 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1050 1050 }
1051 1051
1052 1052 out:
1053 1053 nvlist_free(be_attrs);
1054 1054 return (err);
1055 1055 }
1056 1056
1057 1057 static int
1058 1058 be_do_list(int argc, char **argv)
1059 1059 {
1060 1060 be_node_list_t *be_nodes = NULL;
1061 1061 boolean_t all = B_FALSE;
1062 1062 boolean_t dsets = B_FALSE;
1063 1063 boolean_t snaps = B_FALSE;
1064 1064 boolean_t parsable = B_FALSE;
1065 1065 int err = 1;
1066 1066 int c = 0;
1067 1067 char *be_name = NULL;
1068 1068 be_sort_t order = BE_SORT_UNSPECIFIED;
1069 1069
1070 1070 while ((c = getopt(argc, argv, "adk:svHK:")) != -1) {
1071 1071 switch (c) {
1072 1072 case 'a':
1073 1073 all = B_TRUE;
1074 1074 break;
1075 1075 case 'd':
1076 1076 dsets = B_TRUE;
1077 1077 break;
1078 1078 case 'k':
1079 1079 case 'K':
1080 1080 if (order != BE_SORT_UNSPECIFIED) {
1081 1081 (void) fprintf(stderr, _("Sort key can be "
1082 1082 "specified only once.\n"));
1083 1083 usage();
1084 1084 return (1);
1085 1085 }
1086 1086 if (strcmp(optarg, "date") == 0) {
1087 1087 if (c == 'k')
1088 1088 order = BE_SORT_DATE;
1089 1089 else
1090 1090 order = BE_SORT_DATE_REV;
1091 1091 break;
1092 1092 }
1093 1093 if (strcmp(optarg, "name") == 0) {
1094 1094 if (c == 'k')
1095 1095 order = BE_SORT_NAME;
1096 1096 else
1097 1097 order = BE_SORT_NAME_REV;
1098 1098 break;
1099 1099 }
1100 1100 if (strcmp(optarg, "space") == 0) {
1101 1101 if (c == 'k')
1102 1102 order = BE_SORT_SPACE;
1103 1103 else
1104 1104 order = BE_SORT_SPACE_REV;
1105 1105 break;
1106 1106 }
1107 1107 (void) fprintf(stderr, _("Unknown sort key: %s\n"),
1108 1108 optarg);
1109 1109 usage();
1110 1110 return (1);
1111 1111 case 's':
1112 1112 snaps = B_TRUE;
1113 1113 break;
1114 1114 case 'v':
1115 1115 libbe_print_errors(B_TRUE);
1116 1116 break;
1117 1117 case 'H':
1118 1118 parsable = B_TRUE;
1119 1119 break;
1120 1120 default:
1121 1121 usage();
1122 1122 return (1);
1123 1123 }
1124 1124 }
1125 1125
1126 1126 if (all) {
1127 1127 if (dsets) {
1128 1128 (void) fprintf(stderr, _("Invalid options: -a and %s "
1129 1129 "are mutually exclusive.\n"), "-d");
1130 1130 usage();
1131 1131 return (1);
1132 1132 }
1133 1133 if (snaps) {
1134 1134 (void) fprintf(stderr, _("Invalid options: -a and %s "
1135 1135 "are mutually exclusive.\n"), "-s");
1136 1136 usage();
1137 1137 return (1);
1138 1138 }
1139 1139
1140 1140 dsets = B_TRUE;
1141 1141 snaps = B_TRUE;
1142 1142 }
1143 1143
1144 1144 argc -= optind;
1145 1145 argv += optind;
1146 1146
1147 1147
1148 1148 if (argc == 1)
1149 1149 be_name = argv[0];
1150 1150
1151 1151 err = be_list(be_name, &be_nodes,
1152 1152 snaps ? BE_LIST_SNAPSHOTS : BE_LIST_DEFAULT);
1153 1153
1154 1154 switch (err) {
1155 1155 case BE_SUCCESS:
1156 1156 /* the default sort is ascending date, no need to sort twice */
1157 1157 if (order == BE_SORT_UNSPECIFIED)
1158 1158 order = BE_SORT_DATE;
1159 1159
1160 1160 if (order != BE_SORT_DATE) {
1161 1161 err = be_sort(&be_nodes, order);
1162 1162 if (err != BE_SUCCESS) {
1163 1163 (void) fprintf(stderr, _("Unable to sort Boot "
1164 1164 "Environment\n"));
1165 1165 (void) fprintf(stderr, "%s\n",
1166 1166 be_err_to_str(err));
1167 1167 break;
1168 1168 }
1169 1169 }
1170 1170
1171 1171 print_nodes(be_name, dsets, snaps, parsable, be_nodes);
1172 1172 break;
1173 1173 case BE_ERR_BE_NOENT:
1174 1174 if (be_name == NULL)
1175 1175 (void) fprintf(stderr, _("No boot environments found "
1176 1176 "on this system.\n"));
1177 1177 else {
1178 1178 (void) fprintf(stderr, _("%s does not exist or appear "
1179 1179 "to be a valid BE.\nPlease check that the name of "
1180 1180 "the BE provided is correct.\n"), be_name);
1181 1181 }
1182 1182 break;
1183 1183 default:
1184 1184 (void) fprintf(stderr, _("Unable to display Boot "
1185 1185 "Environment\n"));
1186 1186 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1187 1187 }
1188 1188
1189 1189 if (be_nodes != NULL)
1190 1190 be_free_list(be_nodes);
1191 1191 return (err);
1192 1192 }
1193 1193
1194 1194 static int
1195 1195 be_do_mount(int argc, char **argv)
1196 1196 {
1197 1197 nvlist_t *be_attrs;
1198 1198 boolean_t shared_fs = B_FALSE;
1199 1199 int err = 1;
1200 1200 int c;
1201 1201 int mount_flags = 0;
1202 1202 char *obe_name;
1203 1203 char *mountpoint;
1204 1204 char *tmp_mp = NULL;
1205 1205
1206 1206 while ((c = getopt(argc, argv, "s:v")) != -1) {
1207 1207 switch (c) {
1208 1208 case 's':
1209 1209 shared_fs = B_TRUE;
1210 1210
1211 1211 mount_flags |= BE_MOUNT_FLAG_SHARED_FS;
1212 1212
1213 1213 if (strcmp(optarg, "rw") == 0) {
1214 1214 mount_flags |= BE_MOUNT_FLAG_SHARED_RW;
1215 1215 } else if (strcmp(optarg, "ro") != 0) {
1216 1216 (void) fprintf(stderr, _("The -s flag "
1217 1217 "requires an argument [ rw | ro ]\n"));
1218 1218 usage();
1219 1219 return (1);
1220 1220 }
1221 1221
1222 1222 break;
1223 1223 case 'v':
1224 1224 libbe_print_errors(B_TRUE);
1225 1225 break;
1226 1226 default:
1227 1227 usage();
1228 1228 return (1);
1229 1229 }
1230 1230 }
1231 1231
1232 1232 argc -= optind;
1233 1233 argv += optind;
1234 1234
1235 1235 if (argc < 1 || argc > 2) {
1236 1236 usage();
1237 1237 return (1);
1238 1238 }
1239 1239
1240 1240 obe_name = argv[0];
1241 1241
1242 1242 if (argc == 2) {
1243 1243 mountpoint = argv[1];
1244 1244 if (mountpoint[0] != '/') {
1245 1245 (void) fprintf(stderr, _("Invalid mount point %s. "
1246 1246 "Mount point must start with a /.\n"), mountpoint);
1247 1247 return (1);
1248 1248 }
1249 1249 } else {
1250 1250 const char *tmpdir = getenv("TMPDIR");
1251 1251 const char *tmpname = "tmp.XXXXXX";
1252 1252 int sz;
1253 1253
1254 1254 if (tmpdir == NULL)
1255 1255 tmpdir = "/tmp";
1256 1256
1257 1257 sz = asprintf(&tmp_mp, "%s/%s", tmpdir, tmpname);
1258 1258 if (sz < 0) {
1259 1259 (void) fprintf(stderr, _("internal error: "
1260 1260 "out of memory\n"));
1261 1261 return (1);
1262 1262 }
1263 1263
1264 1264 mountpoint = mkdtemp(tmp_mp);
1265 1265 }
1266 1266
1267 1267 if (be_nvl_alloc(&be_attrs) != 0)
1268 1268 return (1);
1269 1269
1270 1270 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1271 1271 goto out;
1272 1272
1273 1273 if (be_nvl_add_string(be_attrs, BE_ATTR_MOUNTPOINT, mountpoint) != 0)
1274 1274 goto out;
1275 1275
1276 1276 if (shared_fs && be_nvl_add_uint16(be_attrs, BE_ATTR_MOUNT_FLAGS,
1277 1277 mount_flags) != 0)
1278 1278 goto out;
1279 1279
1280 1280 err = be_mount(be_attrs);
1281 1281
1282 1282 switch (err) {
1283 1283 case BE_SUCCESS:
1284 1284 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint);
1285 1285 break;
1286 1286 case BE_ERR_BE_NOENT:
1287 1287 (void) fprintf(stderr, _("%s does not exist or appear "
1288 1288 "to be a valid BE.\nPlease check that the name of "
1289 1289 "the BE provided is correct.\n"), obe_name);
1290 1290 break;
1291 1291 case BE_ERR_MOUNTED:
1292 1292 (void) fprintf(stderr, _("%s is already mounted.\n"
1293 1293 "Please unmount the BE before mounting it again.\n"),
1294 1294 obe_name);
1295 1295 break;
1296 1296 case BE_ERR_PERM:
1297 1297 case BE_ERR_ACCESS:
1298 1298 (void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1299 1299 (void) fprintf(stderr, _("You have insufficient privileges to "
1300 1300 "execute this command.\n"));
1301 1301 break;
1302 1302 case BE_ERR_NO_MOUNTED_ZONE:
1303 1303 (void) fprintf(stderr, _("Mounted on '%s'.\nUnable to mount "
1304 1304 "one of %s's zone BE's.\n"), mountpoint, obe_name);
1305 1305 break;
1306 1306 default:
1307 1307 (void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1308 1308 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1309 1309 }
1310 1310
1311 1311 out:
1312 1312 if (tmp_mp != NULL)
1313 1313 free(tmp_mp);
1314 1314 nvlist_free(be_attrs);
1315 1315 return (err);
1316 1316 }
1317 1317
1318 1318 static int
1319 1319 be_do_unmount(int argc, char **argv)
1320 1320 {
1321 1321 nvlist_t *be_attrs;
1322 1322 char *obe_name;
1323 1323 int err = 1;
1324 1324 int c;
1325 1325 int unmount_flags = 0;
1326 1326
1327 1327 while ((c = getopt(argc, argv, "fv")) != -1) {
1328 1328 switch (c) {
1329 1329 case 'f':
1330 1330 unmount_flags |= BE_UNMOUNT_FLAG_FORCE;
1331 1331 break;
1332 1332 case 'v':
1333 1333 libbe_print_errors(B_TRUE);
1334 1334 break;
1335 1335 default:
1336 1336 usage();
1337 1337 return (1);
1338 1338 }
1339 1339 }
1340 1340
1341 1341 argc -= optind;
1342 1342 argv += optind;
1343 1343
1344 1344 if (argc != 1) {
1345 1345 usage();
1346 1346 return (1);
1347 1347 }
1348 1348
1349 1349 obe_name = argv[0];
1350 1350
1351 1351 if (be_nvl_alloc(&be_attrs) != 0)
1352 1352 return (1);
1353 1353
1354 1354
1355 1355 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1356 1356 goto out;
1357 1357
1358 1358 if (be_nvl_add_uint16(be_attrs, BE_ATTR_UNMOUNT_FLAGS,
1359 1359 unmount_flags) != 0)
1360 1360 goto out;
1361 1361
1362 1362 err = be_unmount(be_attrs);
1363 1363
1364 1364 switch (err) {
1365 1365 case BE_SUCCESS:
1366 1366 (void) printf(_("Unmounted successfully\n"));
1367 1367 break;
1368 1368 case BE_ERR_BE_NOENT:
1369 1369 (void) fprintf(stderr, _("%s does not exist or appear "
1370 1370 "to be a valid BE.\nPlease check that the name of "
1371 1371 "the BE provided is correct.\n"), obe_name);
1372 1372 break;
1373 1373 case BE_ERR_UMOUNT_CURR_BE:
1374 1374 (void) fprintf(stderr, _("%s is the currently active BE.\n"
1375 1375 "It cannot be unmounted unless another BE is the "
1376 1376 "currently active BE.\n"), obe_name);
1377 1377 break;
1378 1378 case BE_ERR_UMOUNT_SHARED:
1379 1379 (void) fprintf(stderr, _("%s is a shared file system and it "
1380 1380 "cannot be unmounted.\n"), obe_name);
1381 1381 break;
1382 1382 case BE_ERR_PERM:
1383 1383 case BE_ERR_ACCESS:
1384 1384 (void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1385 1385 (void) fprintf(stderr, _("You have insufficient privileges to "
1386 1386 "execute this command.\n"));
1387 1387 break;
1388 1388 default:
1389 1389 (void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1390 1390 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1391 1391 }
1392 1392
1393 1393 out:
1394 1394 nvlist_free(be_attrs);
1395 1395 return (err);
1396 1396 }
1397 1397
1398 1398 static int
1399 1399 be_do_rename(int argc, char **argv)
1400 1400 {
1401 1401 nvlist_t *be_attrs;
1402 1402 char *obe_name;
1403 1403 char *nbe_name;
1404 1404 int err = 1;
1405 1405 int c;
1406 1406
1407 1407 while ((c = getopt(argc, argv, "v")) != -1) {
1408 1408 switch (c) {
1409 1409 case 'v':
1410 1410 libbe_print_errors(B_TRUE);
1411 1411 break;
1412 1412 default:
1413 1413 usage();
1414 1414 return (1);
1415 1415 }
1416 1416 }
1417 1417
1418 1418 argc -= optind;
1419 1419 argv += optind;
1420 1420
1421 1421 if (argc != 2) {
1422 1422 usage();
1423 1423 return (1);
1424 1424 }
1425 1425
1426 1426 obe_name = argv[0];
1427 1427 nbe_name = argv[1];
1428 1428
1429 1429 if (be_nvl_alloc(&be_attrs) != 0)
1430 1430 return (1);
1431 1431
1432 1432 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1433 1433 goto out;
1434 1434
1435 1435 if (be_nvl_add_string(be_attrs, BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
1436 1436 goto out;
1437 1437
1438 1438 err = be_rename(be_attrs);
1439 1439
1440 1440 switch (err) {
1441 1441 case BE_SUCCESS:
1442 1442 (void) printf(_("Renamed successfully\n"));
1443 1443 break;
1444 1444 case BE_ERR_BE_NOENT:
1445 1445 (void) fprintf(stderr, _("%s does not exist or appear "
1446 1446 "to be a valid BE.\nPlease check that the name of "
1447 1447 "the BE provided is correct.\n"), obe_name);
1448 1448 break;
1449 1449 case BE_ERR_PERM:
1450 1450 case BE_ERR_ACCESS:
1451 1451 (void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1452 1452 obe_name);
1453 1453 (void) fprintf(stderr, _("You have insufficient privileges to "
1454 1454 "execute this command.\n"));
1455 1455 break;
1456 1456 default:
1457 1457 (void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1458 1458 obe_name);
1459 1459 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1460 1460 }
1461 1461
1462 1462 out:
1463 1463 nvlist_free(be_attrs);
1464 1464 return (err);
1465 1465 }
1466 1466
1467 1467 static int
1468 1468 be_do_rollback(int argc, char **argv)
1469 1469 {
1470 1470 nvlist_t *be_attrs;
1471 1471 char *obe_name;
1472 1472 char *snap_name;
1473 1473 int err = 1;
1474 1474 int c;
1475 1475
1476 1476 while ((c = getopt(argc, argv, "v")) != -1) {
1477 1477 switch (c) {
1478 1478 case 'v':
1479 1479 libbe_print_errors(B_TRUE);
1480 1480 break;
1481 1481 default:
1482 1482 usage();
1483 1483 return (1);
1484 1484 }
1485 1485 }
1486 1486
1487 1487 argc -= optind;
1488 1488 argv += optind;
1489 1489
1490 1490 if (argc < 1 || argc > 2) {
1491 1491 usage();
1492 1492 return (1);
1493 1493 }
1494 1494
1495 1495 obe_name = argv[0];
1496 1496 if (argc == 2)
1497 1497 snap_name = argv[1];
1498 1498 else { /* argc == 1 */
1499 1499 if ((snap_name = strrchr(obe_name, '@')) != NULL) {
1500 1500 if (snap_name[1] == '\0') {
1501 1501 usage();
1502 1502 return (1);
1503 1503 }
1504 1504
1505 1505 snap_name[0] = '\0';
1506 1506 snap_name++;
1507 1507 } else {
1508 1508 usage();
1509 1509 return (1);
1510 1510 }
1511 1511 }
1512 1512
1513 1513 if (be_nvl_alloc(&be_attrs) != 0)
1514 1514 return (1);
1515 1515
1516 1516 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1517 1517 goto out;
1518 1518
1519 1519 if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME, snap_name) != 0)
1520 1520 goto out;
1521 1521
1522 1522 err = be_rollback(be_attrs);
1523 1523
1524 1524 switch (err) {
1525 1525 case BE_SUCCESS:
1526 1526 (void) printf(_("Rolled back successfully\n"));
1527 1527 break;
1528 1528 case BE_ERR_BE_NOENT:
1529 1529 (void) fprintf(stderr, _("%s does not exist or appear "
1530 1530 "to be a valid BE.\nPlease check that the name of "
1531 1531 "the BE provided is correct.\n"), obe_name);
1532 1532 break;
1533 1533 case BE_ERR_SS_NOENT:
1534 1534 (void) fprintf(stderr, _("%s does not exist or appear "
1535 1535 "to be a valid snapshot.\nPlease check that the name of "
1536 1536 "the snapshot provided is correct.\n"), snap_name);
1537 1537 break;
1538 1538 case BE_ERR_PERM:
1539 1539 case BE_ERR_ACCESS:
1540 1540 (void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1541 1541 "failed.\n"), obe_name, snap_name);
1542 1542 (void) fprintf(stderr, _("You have insufficient privileges to "
1543 1543 "execute this command.\n"));
1544 1544 break;
1545 1545 default:
1546 1546 (void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1547 1547 "failed.\n"), obe_name, snap_name);
1548 1548 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1549 1549 }
1550 1550
1551 1551 out:
1552 1552 nvlist_free(be_attrs);
1553 1553 return (err);
1554 1554 }
↓ open down ↓ |
1328 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX