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