Print this page
4220 beadm mount of old BE with zones fails "Read only filesystem"
4235 beadm(1M) mount options undocumented in man page
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
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 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
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 389 if (!cur_be->be_global_active)
390 390 active[ai++] = 'x';
391 391
392 392 if (cur_be->be_active)
393 393 active[ai++] = 'N';
394 394 if (cur_be->be_active_on_boot) {
395 395 if (!cur_be->be_global_active)
396 396 active[ai] = 'b';
397 397 else
398 398 active[ai] = 'R';
399 399 }
400 400
401 401 nicenum(used, buf, sizeof (buf));
402 402 if (parsable)
403 403 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
404 404 name,
405 405 cur_be->be_uuid_str,
406 406 active,
407 407 (cur_be->be_mounted ? mntpt: ""),
408 408 used,
409 409 cur_be->be_policy_type,
410 410 creation);
411 411 else
412 412 (void) printf("%-*s %-*s %-*s %-*s %-*s %-*s\n",
413 413 hdr->cols[0].width, name,
414 414 hdr->cols[1].width, active,
415 415 hdr->cols[2].width, (cur_be->be_mounted ? mntpt:
416 416 "-"),
417 417 hdr->cols[3].width, buf,
418 418 hdr->cols[4].width, cur_be->be_policy_type,
419 419 hdr->cols[5].width, datetime);
420 420 }
421 421 }
422 422
423 423 static void
424 424 print_be_snapshots(be_node_list_t *be, struct hdr_info *hdr, boolean_t parsable)
425 425 {
426 426 char buf[64];
427 427 char datetime[DT_BUF_LEN];
428 428 be_snapshot_list_t *snap = NULL;
429 429
430 430 for (snap = be->be_node_snapshots; snap != NULL;
431 431 snap = snap->be_next_snapshot) {
432 432 char name[ZFS_MAXNAMELEN+1];
433 433 const char *datetime_fmt = "%F %R";
434 434 const char *be_name = be->be_node_name;
435 435 const char *root_ds = be->be_root_ds;
436 436 const char *snap_name = snap->be_snapshot_name;
437 437 char *pos;
438 438 uint64_t used = snap->be_snapshot_space_used;
439 439 time_t creation = snap->be_snapshot_creation;
440 440 struct tm *tm = localtime(&creation);
441 441
442 442 (void) strncpy(name, root_ds, sizeof (name));
443 443 pos = strstr(name, be_name);
444 444 (void) strcpy(pos, snap_name);
445 445
446 446 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
447 447 nicenum(used, buf, sizeof (buf));
448 448
449 449 if (parsable)
450 450 if (hdr->cols[1].width != 0)
451 451 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
452 452 be_name,
453 453 snap_name,
454 454 "",
455 455 "",
456 456 used,
457 457 be->be_policy_type,
458 458 creation);
459 459 else
460 460 (void) printf("%s;%s;%llu;%s;%ld\n",
461 461 be_name,
462 462 snap_name,
463 463 used,
464 464 be->be_policy_type,
465 465 creation);
466 466 else
467 467 if (hdr->cols[1].width != 0)
468 468 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
469 469 "%-*s\n",
470 470 hdr->cols[0].width-3, name,
471 471 hdr->cols[1].width, "-",
472 472 hdr->cols[2].width, "-",
473 473 hdr->cols[3].width, buf,
474 474 hdr->cols[4].width, be->be_policy_type,
475 475 hdr->cols[5].width, datetime);
476 476 else
477 477 (void) printf(" %-*s %-*s %-*s %-*s\n",
478 478 hdr->cols[0].width-3, snap_name,
479 479 hdr->cols[3].width, buf,
480 480 hdr->cols[4].width, be->be_policy_type,
481 481 hdr->cols[5].width, datetime);
482 482 }
483 483 }
484 484
485 485 static void
486 486 print_fmt_nodes(const char *be_name, enum be_fmt be_fmt, boolean_t parsable,
487 487 struct hdr_info *hdr, be_node_list_t *nodes)
488 488 {
489 489 char buf[64];
490 490 char datetime[DT_BUF_LEN];
491 491 be_node_list_t *cur_be;
492 492
493 493 for (cur_be = nodes; cur_be != NULL; cur_be = cur_be->be_next_node) {
494 494 char active[3] = "-\0";
495 495 int ai = 0;
496 496 const char *datetime_fmt = "%F %R";
497 497 const char *name = cur_be->be_node_name;
498 498 const char *mntpt = cur_be->be_mntpt;
499 499 uint64_t used = cur_be->be_space_used;
500 500 time_t creation = cur_be->be_node_creation;
501 501 struct tm *tm;
502 502
503 503 if (be_name != NULL && strcmp(be_name, name) != 0)
504 504 continue;
505 505
506 506 if (!parsable)
507 507 (void) printf("%-s\n", name);
508 508 else
509 509 active[0] = '\0';
510 510
511 511 tm = localtime(&creation);
512 512 (void) strftime(datetime, DT_BUF_LEN, datetime_fmt, tm);
513 513
514 514 if (cur_be->be_active)
515 515 active[ai++] = 'N';
516 516 if (cur_be->be_active_on_boot)
517 517 active[ai] = 'R';
518 518
519 519 nicenum(used, buf, sizeof (buf));
520 520 if (be_fmt & BE_FMT_DATASET)
521 521 if (parsable)
522 522 (void) printf("%s;%s;%s;%s;%llu;%s;%ld\n",
523 523 cur_be->be_node_name,
524 524 cur_be->be_root_ds,
525 525 active,
526 526 (cur_be->be_mounted ? mntpt: ""),
527 527 used,
528 528 cur_be->be_policy_type,
529 529 creation);
530 530 else
531 531 (void) printf(" %-*s %-*s %-*s %-*s %-*s "
532 532 "%-*s\n",
533 533 hdr->cols[0].width-3, cur_be->be_root_ds,
534 534 hdr->cols[1].width, active,
535 535 hdr->cols[2].width, (cur_be->be_mounted ?
536 536 mntpt: "-"),
537 537 hdr->cols[3].width, buf,
538 538 hdr->cols[4].width, cur_be->be_policy_type,
539 539 hdr->cols[5].width, datetime);
540 540
541 541 if (be_fmt & BE_FMT_SNAPSHOT)
542 542 print_be_snapshots(cur_be, hdr, parsable);
543 543 }
544 544 }
545 545
546 546 static void
547 547 print_nodes(const char *be_name, boolean_t dsets, boolean_t snaps,
548 548 boolean_t parsable, be_node_list_t *be_nodes)
549 549 {
550 550 struct hdr_info hdr;
551 551 enum be_fmt be_fmt = BE_FMT_DEFAULT;
552 552
553 553 if (dsets)
554 554 be_fmt |= BE_FMT_DATASET;
555 555 if (snaps)
556 556 be_fmt |= BE_FMT_SNAPSHOT;
557 557
558 558 if (!parsable) {
559 559 init_hdr_cols(be_fmt, &hdr);
560 560 count_widths(be_fmt, &hdr, be_nodes);
561 561 print_hdr(&hdr);
562 562 }
563 563
564 564 if (be_fmt == BE_FMT_DEFAULT)
565 565 print_be_nodes(be_name, parsable, &hdr, be_nodes);
566 566 else
567 567 print_fmt_nodes(be_name, be_fmt, parsable, &hdr, be_nodes);
568 568 }
569 569
570 570 static boolean_t
571 571 confirm_destroy(const char *name)
572 572 {
573 573 boolean_t res = B_FALSE;
574 574 const char *yesre = nl_langinfo(YESEXPR);
575 575 const char *nore = nl_langinfo(NOEXPR);
576 576 regex_t yes_re;
577 577 regex_t no_re;
578 578 char buf[128];
579 579 char *answer;
580 580 int cflags = REG_EXTENDED;
581 581
582 582 if (regcomp(&yes_re, yesre, cflags) != 0) {
583 583 /* should not happen */
584 584 (void) fprintf(stderr, _("Failed to compile 'yes' regexp\n"));
585 585 return (res);
586 586 }
587 587 if (regcomp(&no_re, nore, cflags) != 0) {
588 588 /* should not happen */
589 589 (void) fprintf(stderr, _("Failed to compile 'no' regexp\n"));
590 590 regfree(&yes_re);
591 591 return (res);
592 592 }
593 593
594 594 (void) printf(_("Are you sure you want to destroy %s?\n"
595 595 "This action cannot be undone (y/[n]): "), name);
596 596
597 597 answer = fgets(buf, sizeof (buf), stdin);
598 598 if (answer == NULL || *answer == '\0' || *answer == 10)
599 599 goto out;
600 600
601 601 if (regexec(&yes_re, answer, 0, NULL, 0) == 0) {
602 602 res = B_TRUE;
603 603 } else if (regexec(&no_re, answer, 0, NULL, 0) != 0) {
604 604 (void) fprintf(stderr, _("Invalid response. "
605 605 "Please enter 'y' or 'n'.\n"));
606 606 }
607 607
608 608 out:
609 609 regfree(&yes_re);
610 610 regfree(&no_re);
611 611 return (res);
612 612 }
613 613
614 614 static int
615 615 be_nvl_alloc(nvlist_t **nvlp)
616 616 {
617 617 assert(nvlp != NULL);
618 618
619 619 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) {
620 620 (void) perror(_("nvlist_alloc failed.\n"));
621 621 return (1);
622 622 }
623 623
624 624 return (0);
625 625 }
626 626
627 627 static int
628 628 be_nvl_add_string(nvlist_t *nvl, const char *name, const char *val)
629 629 {
630 630 assert(nvl != NULL);
631 631
632 632 if (nvlist_add_string(nvl, name, val) != 0) {
633 633 (void) fprintf(stderr, _("nvlist_add_string failed for "
634 634 "%s (%s).\n"), name, val);
635 635 return (1);
636 636 }
637 637
638 638 return (0);
639 639 }
640 640
641 641 static int
642 642 be_nvl_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
643 643 {
644 644 assert(nvl != NULL);
645 645
646 646 if (nvlist_add_nvlist(nvl, name, val) != 0) {
647 647 (void) fprintf(stderr, _("nvlist_add_nvlist failed for %s.\n"),
648 648 name);
649 649 return (1);
650 650 }
651 651
652 652 return (0);
653 653 }
654 654
655 655 static int
656 656 be_nvl_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
657 657 {
658 658 assert(nvl != NULL);
659 659
660 660 if (nvlist_add_uint16(nvl, name, val) != 0) {
661 661 (void) fprintf(stderr, _("nvlist_add_uint16 failed for "
662 662 "%s (%hu).\n"), name, val);
663 663 return (1);
664 664 }
665 665
666 666 return (0);
667 667 }
668 668
669 669 static int
670 670 be_do_activate(int argc, char **argv)
671 671 {
672 672 nvlist_t *be_attrs;
673 673 int err = 1;
674 674 int c;
675 675 char *obe_name;
676 676
677 677 while ((c = getopt(argc, argv, "v")) != -1) {
678 678 switch (c) {
679 679 case 'v':
680 680 libbe_print_errors(B_TRUE);
681 681 break;
682 682 default:
683 683 usage();
684 684 return (1);
685 685 }
686 686 }
687 687
688 688 argc -= optind;
689 689 argv += optind;
690 690
691 691 if (argc != 1) {
692 692 usage();
693 693 return (1);
694 694 }
695 695
696 696 obe_name = argv[0];
697 697
698 698 if (be_nvl_alloc(&be_attrs) != 0)
699 699 return (1);
700 700
701 701 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
702 702 goto out;
703 703
704 704 err = be_activate(be_attrs);
705 705
706 706 switch (err) {
707 707 case BE_SUCCESS:
708 708 (void) printf(_("Activated successfully\n"));
709 709 break;
710 710 case BE_ERR_BE_NOENT:
711 711 (void) fprintf(stderr, _("%s does not exist or appear "
712 712 "to be a valid BE.\nPlease check that the name of "
713 713 "the BE provided is correct.\n"), obe_name);
714 714 break;
715 715 case BE_ERR_PERM:
716 716 case BE_ERR_ACCESS:
717 717 (void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
718 718 (void) fprintf(stderr, _("You have insufficient privileges to "
719 719 "execute this command.\n"));
720 720 break;
721 721 case BE_ERR_ACTIVATE_CURR:
722 722 default:
723 723 (void) fprintf(stderr, _("Unable to activate %s.\n"), obe_name);
724 724 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
725 725 }
726 726
727 727 out:
728 728 nvlist_free(be_attrs);
729 729 return (err);
730 730 }
731 731
732 732 static int
733 733 be_do_create(int argc, char **argv)
734 734 {
735 735 nvlist_t *be_attrs;
736 736 nvlist_t *zfs_props = NULL;
737 737 boolean_t activate = B_FALSE;
738 738 boolean_t is_snap = B_FALSE;
739 739 int c;
740 740 int err = 1;
741 741 char *obe_name = NULL;
742 742 char *snap_name = NULL;
743 743 char *nbe_zpool = NULL;
744 744 char *nbe_name = NULL;
745 745 char *nbe_desc = NULL;
746 746 char *propname = NULL;
747 747 char *propval = NULL;
748 748 char *strval = NULL;
749 749
750 750 while ((c = getopt(argc, argv, "ad:e:io:p:v")) != -1) {
751 751 switch (c) {
752 752 case 'a':
753 753 activate = B_TRUE;
754 754 break;
755 755 case 'd':
756 756 nbe_desc = optarg;
757 757 break;
758 758 case 'e':
759 759 obe_name = optarg;
760 760 break;
761 761 case 'o':
762 762 if (zfs_props == NULL && be_nvl_alloc(&zfs_props) != 0)
763 763 return (1);
764 764
765 765 propname = optarg;
766 766 if ((propval = strchr(propname, '=')) == NULL) {
767 767 (void) fprintf(stderr, _("missing "
768 768 "'=' for -o option\n"));
769 769 goto out2;
770 770 }
771 771 *propval = '\0';
772 772 propval++;
773 773 if (nvlist_lookup_string(zfs_props, propname,
774 774 &strval) == 0) {
775 775 (void) fprintf(stderr, _("property '%s' "
776 776 "specified multiple times\n"), propname);
777 777 goto out2;
778 778
779 779 }
780 780 if (be_nvl_add_string(zfs_props, propname, propval)
781 781 != 0)
782 782 goto out2;
783 783
784 784 break;
785 785 case 'p':
786 786 nbe_zpool = optarg;
787 787 break;
788 788 case 'v':
789 789 libbe_print_errors(B_TRUE);
790 790 break;
791 791 default:
792 792 usage();
793 793 goto out2;
794 794 }
795 795 }
796 796
797 797 argc -= optind;
798 798 argv += optind;
799 799
800 800 if (argc != 1) {
801 801 usage();
802 802 goto out2;
803 803 }
804 804
805 805 nbe_name = argv[0];
806 806
807 807 if ((snap_name = strrchr(nbe_name, '@')) != NULL) {
808 808 if (snap_name[1] == '\0') {
809 809 usage();
810 810 goto out2;
811 811 }
812 812
813 813 snap_name[0] = '\0';
814 814 snap_name++;
815 815 is_snap = B_TRUE;
816 816 }
817 817
818 818 if (obe_name) {
819 819 if (is_snap) {
820 820 usage();
821 821 goto out2;
822 822 }
823 823
824 824 /*
825 825 * Check if obe_name is really a snapshot name.
826 826 * If so, split it out.
827 827 */
828 828 if ((snap_name = strrchr(obe_name, '@')) != NULL) {
829 829 if (snap_name[1] == '\0') {
830 830 usage();
831 831 goto out2;
832 832 }
833 833
834 834 snap_name[0] = '\0';
835 835 snap_name++;
836 836 }
837 837 } else if (is_snap) {
838 838 obe_name = nbe_name;
839 839 nbe_name = NULL;
840 840 }
841 841
842 842 if (be_nvl_alloc(&be_attrs) != 0)
843 843 goto out2;
844 844
845 845
846 846 if (zfs_props != NULL && be_nvl_add_nvlist(be_attrs,
847 847 BE_ATTR_ORIG_BE_NAME, zfs_props) != 0)
848 848 goto out;
849 849
850 850 if (obe_name != NULL && be_nvl_add_string(be_attrs,
851 851 BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
852 852 goto out;
853 853
854 854 if (snap_name != NULL && be_nvl_add_string(be_attrs,
855 855 BE_ATTR_SNAP_NAME, snap_name) != 0)
856 856 goto out;
857 857
858 858 if (nbe_zpool != NULL && be_nvl_add_string(be_attrs,
859 859 BE_ATTR_NEW_BE_POOL, nbe_zpool) != 0)
860 860 goto out;
861 861
862 862 if (nbe_name != NULL && be_nvl_add_string(be_attrs,
863 863 BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
864 864 goto out;
865 865
866 866 if (nbe_desc != NULL && be_nvl_add_string(be_attrs,
867 867 BE_ATTR_NEW_BE_DESC, nbe_desc) != 0)
868 868 goto out;
869 869
870 870 if (is_snap)
871 871 err = be_create_snapshot(be_attrs);
872 872 else
873 873 err = be_copy(be_attrs);
874 874
875 875 switch (err) {
876 876 case BE_SUCCESS:
877 877 if (!is_snap && !nbe_name) {
878 878 /*
879 879 * We requested an auto named BE; find out the
880 880 * name of the BE that was created for us and
881 881 * the auto snapshot created from the original BE.
882 882 */
883 883 if (nvlist_lookup_string(be_attrs, BE_ATTR_NEW_BE_NAME,
884 884 &nbe_name) != 0) {
885 885 (void) fprintf(stderr, _("failed to get %s "
886 886 "attribute\n"), BE_ATTR_NEW_BE_NAME);
887 887 break;
888 888 } else
889 889 (void) printf(_("Auto named BE: %s\n"),
890 890 nbe_name);
891 891
892 892 if (nvlist_lookup_string(be_attrs, BE_ATTR_SNAP_NAME,
893 893 &snap_name) != 0) {
894 894 (void) fprintf(stderr, _("failed to get %s "
895 895 "attribute\n"), BE_ATTR_SNAP_NAME);
896 896 break;
897 897 } else
898 898 (void) printf(_("Auto named snapshot: %s\n"),
899 899 snap_name);
900 900 }
901 901
902 902 if (!is_snap && activate) {
903 903 char *args[] = { "activate", "", NULL };
904 904 args[1] = nbe_name;
905 905 optind = 1;
906 906
907 907 err = be_do_activate(2, args);
908 908 goto out;
909 909 }
910 910
911 911 (void) printf(_("Created successfully\n"));
912 912 break;
913 913 case BE_ERR_BE_EXISTS:
914 914 (void) fprintf(stderr, _("BE %s already exists\n."
915 915 "Please choose a different BE name.\n"), nbe_name);
916 916 break;
917 917 case BE_ERR_SS_EXISTS:
918 918 (void) fprintf(stderr, _("BE %s snapshot %s already exists.\n"
919 919 "Please choose a different snapshot name.\n"), obe_name,
920 920 snap_name);
921 921 break;
922 922 case BE_ERR_PERM:
923 923 case BE_ERR_ACCESS:
924 924 if (is_snap)
925 925 (void) fprintf(stderr, _("Unable to create snapshot "
926 926 "%s.\n"), snap_name);
927 927 else
928 928 (void) fprintf(stderr, _("Unable to create %s.\n"),
929 929 nbe_name);
930 930 (void) fprintf(stderr, _("You have insufficient privileges to "
931 931 "execute this command.\n"));
932 932 break;
933 933 default:
934 934 if (is_snap)
935 935 (void) fprintf(stderr, _("Unable to create snapshot "
936 936 "%s.\n"), snap_name);
937 937 else
938 938 (void) fprintf(stderr, _("Unable to create %s.\n"),
939 939 nbe_name);
940 940 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
941 941 }
942 942
943 943 out:
944 944 nvlist_free(be_attrs);
945 945 out2:
946 946 if (zfs_props != NULL)
947 947 nvlist_free(zfs_props);
948 948
949 949 return (err);
950 950 }
951 951
952 952 static int
953 953 be_do_destroy(int argc, char **argv)
954 954 {
955 955 nvlist_t *be_attrs;
956 956 boolean_t is_snap = B_FALSE;
957 957 boolean_t suppress_prompt = B_FALSE;
958 958 int err = 1;
959 959 int c;
960 960 int destroy_flags = 0;
961 961 char *snap_name;
962 962 char *be_name;
963 963
964 964 while ((c = getopt(argc, argv, "fFsv")) != -1) {
965 965 switch (c) {
966 966 case 'f':
967 967 destroy_flags |= BE_DESTROY_FLAG_FORCE_UNMOUNT;
968 968 break;
969 969 case 's':
970 970 destroy_flags |= BE_DESTROY_FLAG_SNAPSHOTS;
971 971 break;
972 972 case 'v':
973 973 libbe_print_errors(B_TRUE);
974 974 break;
975 975 case 'F':
976 976 suppress_prompt = B_TRUE;
977 977 break;
978 978 default:
979 979 usage();
980 980 return (1);
981 981 }
982 982 }
983 983
984 984 argc -= optind;
985 985 argv += optind;
986 986
987 987 if (argc != 1) {
988 988 usage();
989 989 return (1);
990 990 }
991 991
992 992 be_name = argv[0];
993 993 if (!suppress_prompt && !confirm_destroy(be_name)) {
994 994 (void) printf(_("%s has not been destroyed.\n"), be_name);
995 995 return (0);
996 996 }
997 997
998 998 if ((snap_name = strrchr(be_name, '@')) != NULL) {
999 999 if (snap_name[1] == '\0') {
1000 1000 usage();
1001 1001 return (1);
1002 1002 }
1003 1003
1004 1004 is_snap = B_TRUE;
1005 1005 *snap_name = '\0';
1006 1006 snap_name++;
1007 1007 }
1008 1008
1009 1009 if (be_nvl_alloc(&be_attrs) != 0)
1010 1010 return (1);
1011 1011
1012 1012
1013 1013 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, be_name) != 0)
1014 1014 goto out;
1015 1015
1016 1016 if (is_snap) {
1017 1017 if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME,
1018 1018 snap_name) != 0)
1019 1019 goto out;
1020 1020
1021 1021 err = be_destroy_snapshot(be_attrs);
1022 1022 } else {
1023 1023 if (be_nvl_add_uint16(be_attrs, BE_ATTR_DESTROY_FLAGS,
1024 1024 destroy_flags) != 0)
1025 1025 goto out;
1026 1026
1027 1027 err = be_destroy(be_attrs);
1028 1028 }
1029 1029
1030 1030 switch (err) {
1031 1031 case BE_SUCCESS:
1032 1032 (void) printf(_("Destroyed successfully\n"));
1033 1033 break;
1034 1034 case BE_ERR_MOUNTED:
1035 1035 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1036 1036 (void) fprintf(stderr, _("It is currently mounted and must be "
1037 1037 "unmounted before it can be destroyed.\n" "Use 'beadm "
1038 1038 "unmount %s' to unmount the BE before destroying\nit or "
1039 1039 "'beadm destroy -f %s'.\n"), be_name, be_name);
1040 1040 break;
1041 1041 case BE_ERR_DESTROY_CURR_BE:
1042 1042 (void) fprintf(stderr, _("%s is the currently active BE and "
1043 1043 "cannot be destroyed.\nYou must boot from another BE in "
1044 1044 "order to destroy %s.\n"), be_name, be_name);
1045 1045 break;
1046 1046 case BE_ERR_ZONES_UNMOUNT:
1047 1047 (void) fprintf(stderr, _("Unable to destroy one of " "%s's "
1048 1048 "zone BE's.\nUse 'beadm destroy -f %s' or "
1049 1049 "'zfs -f destroy <dataset>'.\n"), be_name, be_name);
1050 1050 break;
1051 1051 case BE_ERR_SS_NOENT:
1052 1052 (void) fprintf(stderr, _("%s does not exist or appear "
1053 1053 "to be a valid snapshot.\nPlease check that the name of "
1054 1054 "the snapshot provided is correct.\n"), snap_name);
1055 1055 break;
1056 1056 case BE_ERR_PERM:
1057 1057 case BE_ERR_ACCESS:
1058 1058 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1059 1059 (void) fprintf(stderr, _("You have insufficient privileges to "
1060 1060 "execute this command.\n"));
1061 1061 break;
1062 1062 case BE_ERR_SS_EXISTS:
1063 1063 (void) fprintf(stderr, _("Unable to destroy %s: "
1064 1064 "BE has snapshots.\nUse 'beadm destroy -s %s' or "
1065 1065 "'zfs -r destroy <dataset>'.\n"), be_name, be_name);
1066 1066 break;
1067 1067 default:
1068 1068 (void) fprintf(stderr, _("Unable to destroy %s.\n"), be_name);
1069 1069 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1070 1070 }
1071 1071
1072 1072 out:
1073 1073 nvlist_free(be_attrs);
1074 1074 return (err);
1075 1075 }
1076 1076
1077 1077 static int
1078 1078 be_do_list(int argc, char **argv)
1079 1079 {
1080 1080 be_node_list_t *be_nodes = NULL;
1081 1081 boolean_t all = B_FALSE;
1082 1082 boolean_t dsets = B_FALSE;
1083 1083 boolean_t snaps = B_FALSE;
1084 1084 boolean_t parsable = B_FALSE;
1085 1085 int err = 1;
1086 1086 int c = 0;
1087 1087 char *be_name = NULL;
1088 1088
1089 1089 while ((c = getopt(argc, argv, "adsvH")) != -1) {
1090 1090 switch (c) {
1091 1091 case 'a':
1092 1092 all = B_TRUE;
1093 1093 break;
1094 1094 case 'd':
1095 1095 dsets = B_TRUE;
1096 1096 break;
1097 1097 case 's':
1098 1098 snaps = B_TRUE;
1099 1099 break;
1100 1100 case 'v':
1101 1101 libbe_print_errors(B_TRUE);
1102 1102 break;
1103 1103 case 'H':
1104 1104 parsable = B_TRUE;
1105 1105 break;
1106 1106 default:
1107 1107 usage();
1108 1108 return (1);
1109 1109 }
1110 1110 }
1111 1111
1112 1112 if (all) {
1113 1113 if (dsets) {
1114 1114 (void) fprintf(stderr, _("Invalid options: -a and %s "
1115 1115 "are mutually exclusive.\n"), "-d");
1116 1116 usage();
1117 1117 return (1);
1118 1118 }
1119 1119 if (snaps) {
1120 1120 (void) fprintf(stderr, _("Invalid options: -a and %s "
1121 1121 "are mutually exclusive.\n"), "-s");
1122 1122 usage();
1123 1123 return (1);
1124 1124 }
1125 1125
1126 1126 dsets = B_TRUE;
1127 1127 snaps = B_TRUE;
1128 1128 }
1129 1129
1130 1130 argc -= optind;
1131 1131 argv += optind;
1132 1132
1133 1133
1134 1134 if (argc == 1)
1135 1135 be_name = argv[0];
1136 1136
1137 1137 err = be_list(be_name, &be_nodes);
1138 1138
1139 1139 switch (err) {
1140 1140 case BE_SUCCESS:
1141 1141 print_nodes(be_name, dsets, snaps, parsable, be_nodes);
1142 1142 break;
1143 1143 case BE_ERR_BE_NOENT:
1144 1144 if (be_name == NULL)
1145 1145 (void) fprintf(stderr, _("No boot environments found "
1146 1146 "on this system.\n"));
1147 1147 else {
1148 1148 (void) fprintf(stderr, _("%s does not exist or appear "
1149 1149 "to be a valid BE.\nPlease check that the name of "
1150 1150 "the BE provided is correct.\n"), be_name);
1151 1151 }
1152 1152 break;
1153 1153 default:
1154 1154 (void) fprintf(stderr, _("Unable to display Boot "
1155 1155 "Environment\n"));
1156 1156 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1157 1157 }
1158 1158
1159 1159 if (be_nodes != NULL)
1160 1160 be_free_list(be_nodes);
1161 1161 return (err);
1162 1162 }
1163 1163
1164 1164 static int
1165 1165 be_do_mount(int argc, char **argv)
1166 1166 {
1167 1167 nvlist_t *be_attrs;
1168 1168 boolean_t shared_fs = B_FALSE;
1169 1169 int err = 1;
1170 1170 int c;
1171 1171 int mount_flags = 0;
1172 1172 char *obe_name;
1173 1173 char *mountpoint;
1174 1174 char *tmp_mp = NULL;
1175 1175
1176 1176 while ((c = getopt(argc, argv, "s:v")) != -1) {
1177 1177 switch (c) {
1178 1178 case 's':
1179 1179 shared_fs = B_TRUE;
1180 1180
1181 1181 mount_flags |= BE_MOUNT_FLAG_SHARED_FS;
1182 1182
1183 1183 if (strcmp(optarg, "rw") == 0) {
1184 1184 mount_flags |= BE_MOUNT_FLAG_SHARED_RW;
1185 1185 } else if (strcmp(optarg, "ro") != 0) {
1186 1186 (void) fprintf(stderr, _("The -s flag "
1187 1187 "requires an argument [ rw | ro ]\n"));
1188 1188 usage();
1189 1189 return (1);
1190 1190 }
1191 1191
1192 1192 break;
1193 1193 case 'v':
1194 1194 libbe_print_errors(B_TRUE);
1195 1195 break;
1196 1196 default:
1197 1197 usage();
1198 1198 return (1);
1199 1199 }
1200 1200 }
1201 1201
1202 1202 argc -= optind;
1203 1203 argv += optind;
1204 1204
1205 1205 if (argc < 1 || argc > 2) {
1206 1206 usage();
1207 1207 return (1);
1208 1208 }
1209 1209
1210 1210 obe_name = argv[0];
1211 1211
1212 1212 if (argc == 2) {
1213 1213 mountpoint = argv[1];
1214 1214 if (mountpoint[0] != '/') {
1215 1215 (void) fprintf(stderr, _("Invalid mount point %s. "
1216 1216 "Mount point must start with a /.\n"), mountpoint);
1217 1217 return (1);
1218 1218 }
1219 1219 } else {
1220 1220 const char *tmpdir = getenv("TMPDIR");
1221 1221 const char *tmpname = "tmp.XXXXXX";
1222 1222 int sz;
1223 1223
1224 1224 if (tmpdir == NULL)
1225 1225 tmpdir = "/tmp";
1226 1226
1227 1227 sz = asprintf(&tmp_mp, "%s/%s", tmpdir, tmpname);
1228 1228 if (sz < 0) {
1229 1229 (void) fprintf(stderr, _("internal error: "
1230 1230 "out of memory\n"));
1231 1231 return (1);
1232 1232 }
1233 1233
1234 1234 mountpoint = mkdtemp(tmp_mp);
1235 1235 }
1236 1236
1237 1237 if (be_nvl_alloc(&be_attrs) != 0)
1238 1238 return (1);
1239 1239
1240 1240 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1241 1241 goto out;
1242 1242
1243 1243 if (be_nvl_add_string(be_attrs, BE_ATTR_MOUNTPOINT, mountpoint) != 0)
1244 1244 goto out;
1245 1245
1246 1246 if (shared_fs && be_nvl_add_uint16(be_attrs, BE_ATTR_MOUNT_FLAGS,
1247 1247 mount_flags) != 0)
1248 1248 goto out;
1249 1249
1250 1250 err = be_mount(be_attrs);
1251 1251
1252 1252 switch (err) {
1253 1253 case BE_SUCCESS:
1254 1254 (void) printf(_("Mounted successfully on: '%s'\n"), mountpoint);
1255 1255 break;
1256 1256 case BE_ERR_BE_NOENT:
1257 1257 (void) fprintf(stderr, _("%s does not exist or appear "
1258 1258 "to be a valid BE.\nPlease check that the name of "
1259 1259 "the BE provided is correct.\n"), obe_name);
1260 1260 break;
1261 1261 case BE_ERR_MOUNTED:
↓ open down ↓ |
1261 lines elided |
↑ open up ↑ |
1262 1262 (void) fprintf(stderr, _("%s is already mounted.\n"
1263 1263 "Please unmount the BE before mounting it again.\n"),
1264 1264 obe_name);
1265 1265 break;
1266 1266 case BE_ERR_PERM:
1267 1267 case BE_ERR_ACCESS:
1268 1268 (void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1269 1269 (void) fprintf(stderr, _("You have insufficient privileges to "
1270 1270 "execute this command.\n"));
1271 1271 break;
1272 + case BE_ERR_NO_MOUNTED_ZONE:
1273 + (void) fprintf(stderr, _("Mounted on '%s'.\nUnable to mount "
1274 + "one of %s's zone BE's.\n"), mountpoint, obe_name);
1275 + break;
1272 1276 default:
1273 1277 (void) fprintf(stderr, _("Unable to mount %s.\n"), obe_name);
1274 1278 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1275 1279 }
1276 1280
1277 1281 out:
1278 1282 if (tmp_mp != NULL)
1279 1283 free(tmp_mp);
1280 1284 nvlist_free(be_attrs);
1281 1285 return (err);
1282 1286 }
1283 1287
1284 1288 static int
1285 1289 be_do_unmount(int argc, char **argv)
1286 1290 {
1287 1291 nvlist_t *be_attrs;
1288 1292 char *obe_name;
1289 1293 int err = 1;
1290 1294 int c;
1291 1295 int unmount_flags = 0;
1292 1296
1293 1297 while ((c = getopt(argc, argv, "fv")) != -1) {
1294 1298 switch (c) {
1295 1299 case 'f':
1296 1300 unmount_flags |= BE_UNMOUNT_FLAG_FORCE;
1297 1301 break;
1298 1302 case 'v':
1299 1303 libbe_print_errors(B_TRUE);
1300 1304 break;
1301 1305 default:
1302 1306 usage();
1303 1307 return (1);
1304 1308 }
1305 1309 }
1306 1310
1307 1311 argc -= optind;
1308 1312 argv += optind;
1309 1313
1310 1314 if (argc != 1) {
1311 1315 usage();
1312 1316 return (1);
1313 1317 }
1314 1318
1315 1319 obe_name = argv[0];
1316 1320
1317 1321 if (be_nvl_alloc(&be_attrs) != 0)
1318 1322 return (1);
1319 1323
1320 1324
1321 1325 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1322 1326 goto out;
1323 1327
1324 1328 if (be_nvl_add_uint16(be_attrs, BE_ATTR_UNMOUNT_FLAGS,
1325 1329 unmount_flags) != 0)
1326 1330 goto out;
1327 1331
1328 1332 err = be_unmount(be_attrs);
1329 1333
1330 1334 switch (err) {
1331 1335 case BE_SUCCESS:
1332 1336 (void) printf(_("Unmounted successfully\n"));
1333 1337 break;
1334 1338 case BE_ERR_BE_NOENT:
1335 1339 (void) fprintf(stderr, _("%s does not exist or appear "
1336 1340 "to be a valid BE.\nPlease check that the name of "
1337 1341 "the BE provided is correct.\n"), obe_name);
1338 1342 break;
1339 1343 case BE_ERR_UMOUNT_CURR_BE:
1340 1344 (void) fprintf(stderr, _("%s is the currently active BE.\n"
1341 1345 "It cannot be unmounted unless another BE is the "
1342 1346 "currently active BE.\n"), obe_name);
1343 1347 break;
1344 1348 case BE_ERR_UMOUNT_SHARED:
1345 1349 (void) fprintf(stderr, _("%s is a shared file system and it "
1346 1350 "cannot be unmounted.\n"), obe_name);
1347 1351 break;
1348 1352 case BE_ERR_PERM:
1349 1353 case BE_ERR_ACCESS:
1350 1354 (void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1351 1355 (void) fprintf(stderr, _("You have insufficient privileges to "
1352 1356 "execute this command.\n"));
1353 1357 break;
1354 1358 default:
1355 1359 (void) fprintf(stderr, _("Unable to unmount %s.\n"), obe_name);
1356 1360 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1357 1361 }
1358 1362
1359 1363 out:
1360 1364 nvlist_free(be_attrs);
1361 1365 return (err);
1362 1366 }
1363 1367
1364 1368 static int
1365 1369 be_do_rename(int argc, char **argv)
1366 1370 {
1367 1371 nvlist_t *be_attrs;
1368 1372 char *obe_name;
1369 1373 char *nbe_name;
1370 1374 int err = 1;
1371 1375 int c;
1372 1376
1373 1377 while ((c = getopt(argc, argv, "v")) != -1) {
1374 1378 switch (c) {
1375 1379 case 'v':
1376 1380 libbe_print_errors(B_TRUE);
1377 1381 break;
1378 1382 default:
1379 1383 usage();
1380 1384 return (1);
1381 1385 }
1382 1386 }
1383 1387
1384 1388 argc -= optind;
1385 1389 argv += optind;
1386 1390
1387 1391 if (argc != 2) {
1388 1392 usage();
1389 1393 return (1);
1390 1394 }
1391 1395
1392 1396 obe_name = argv[0];
1393 1397 nbe_name = argv[1];
1394 1398
1395 1399 if (be_nvl_alloc(&be_attrs) != 0)
1396 1400 return (1);
1397 1401
1398 1402 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1399 1403 goto out;
1400 1404
1401 1405 if (be_nvl_add_string(be_attrs, BE_ATTR_NEW_BE_NAME, nbe_name) != 0)
1402 1406 goto out;
1403 1407
1404 1408 err = be_rename(be_attrs);
1405 1409
1406 1410 switch (err) {
1407 1411 case BE_SUCCESS:
1408 1412 (void) printf(_("Renamed successfully\n"));
1409 1413 break;
1410 1414 case BE_ERR_BE_NOENT:
1411 1415 (void) fprintf(stderr, _("%s does not exist or appear "
1412 1416 "to be a valid BE.\nPlease check that the name of "
1413 1417 "the BE provided is correct.\n"), obe_name);
1414 1418 break;
1415 1419 case BE_ERR_PERM:
1416 1420 case BE_ERR_ACCESS:
1417 1421 (void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1418 1422 obe_name);
1419 1423 (void) fprintf(stderr, _("You have insufficient privileges to "
1420 1424 "execute this command.\n"));
1421 1425 break;
1422 1426 default:
1423 1427 (void) fprintf(stderr, _("Rename of BE %s failed.\n"),
1424 1428 obe_name);
1425 1429 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1426 1430 }
1427 1431
1428 1432 out:
1429 1433 nvlist_free(be_attrs);
1430 1434 return (err);
1431 1435 }
1432 1436
1433 1437 static int
1434 1438 be_do_rollback(int argc, char **argv)
1435 1439 {
1436 1440 nvlist_t *be_attrs;
1437 1441 char *obe_name;
1438 1442 char *snap_name;
1439 1443 int err = 1;
1440 1444 int c;
1441 1445
1442 1446 while ((c = getopt(argc, argv, "v")) != -1) {
1443 1447 switch (c) {
1444 1448 case 'v':
1445 1449 libbe_print_errors(B_TRUE);
1446 1450 break;
1447 1451 default:
1448 1452 usage();
1449 1453 return (1);
1450 1454 }
1451 1455 }
1452 1456
1453 1457 argc -= optind;
1454 1458 argv += optind;
1455 1459
1456 1460 if (argc < 1 || argc > 2) {
1457 1461 usage();
1458 1462 return (1);
1459 1463 }
1460 1464
1461 1465 obe_name = argv[0];
1462 1466 if (argc == 2)
1463 1467 snap_name = argv[1];
1464 1468 else { /* argc == 1 */
1465 1469 if ((snap_name = strrchr(obe_name, '@')) != NULL) {
1466 1470 if (snap_name[1] == '\0') {
1467 1471 usage();
1468 1472 return (1);
1469 1473 }
1470 1474
1471 1475 snap_name[0] = '\0';
1472 1476 snap_name++;
1473 1477 } else {
1474 1478 usage();
1475 1479 return (1);
1476 1480 }
1477 1481 }
1478 1482
1479 1483 if (be_nvl_alloc(&be_attrs) != 0)
1480 1484 return (1);
1481 1485
1482 1486 if (be_nvl_add_string(be_attrs, BE_ATTR_ORIG_BE_NAME, obe_name) != 0)
1483 1487 goto out;
1484 1488
1485 1489 if (be_nvl_add_string(be_attrs, BE_ATTR_SNAP_NAME, snap_name) != 0)
1486 1490 goto out;
1487 1491
1488 1492 err = be_rollback(be_attrs);
1489 1493
1490 1494 switch (err) {
1491 1495 case BE_SUCCESS:
1492 1496 (void) printf(_("Rolled back successfully\n"));
1493 1497 break;
1494 1498 case BE_ERR_BE_NOENT:
1495 1499 (void) fprintf(stderr, _("%s does not exist or appear "
1496 1500 "to be a valid BE.\nPlease check that the name of "
1497 1501 "the BE provided is correct.\n"), obe_name);
1498 1502 break;
1499 1503 case BE_ERR_SS_NOENT:
1500 1504 (void) fprintf(stderr, _("%s does not exist or appear "
1501 1505 "to be a valid snapshot.\nPlease check that the name of "
1502 1506 "the snapshot provided is correct.\n"), snap_name);
1503 1507 break;
1504 1508 case BE_ERR_PERM:
1505 1509 case BE_ERR_ACCESS:
1506 1510 (void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1507 1511 "failed.\n"), obe_name, snap_name);
1508 1512 (void) fprintf(stderr, _("You have insufficient privileges to "
1509 1513 "execute this command.\n"));
1510 1514 break;
1511 1515 default:
1512 1516 (void) fprintf(stderr, _("Rollback of BE %s snapshot %s "
1513 1517 "failed.\n"), obe_name, snap_name);
1514 1518 (void) fprintf(stderr, "%s\n", be_err_to_str(err));
1515 1519 }
1516 1520
1517 1521 out:
1518 1522 nvlist_free(be_attrs);
1519 1523 return (err);
1520 1524 }
↓ open down ↓ |
239 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX