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