1 /*- 2 * Copyright (c) 2011 Nathan Whitehorn 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/stat.h> 31 #include <errno.h> 32 #include <libutil.h> 33 #include <inttypes.h> 34 35 #include <libgeom.h> 36 #include <dialog.h> 37 #include <dlg_keys.h> 38 39 #include "partedit.h" 40 41 #define GPART_FLAGS "x" /* Do not commit changes by default */ 42 43 static void 44 gpart_show_error(const char *title, const char *explanation, const char *errstr) 45 { 46 char *errmsg; 47 char message[512]; 48 int error; 49 50 if (explanation == NULL) 51 explanation = ""; 52 53 error = strtol(errstr, &errmsg, 0); 54 if (errmsg != errstr) { 55 while (errmsg[0] == ' ') 56 errmsg++; 57 if (errmsg[0] != '\0') 58 sprintf(message, "%s%s. %s", explanation, 59 strerror(error), errmsg); 60 else 61 sprintf(message, "%s%s", explanation, strerror(error)); 62 } else { 63 sprintf(message, "%s%s", explanation, errmsg); 64 } 65 66 dialog_msgbox(title, message, 0, 0, TRUE); 67 } 68 69 static int 70 scheme_supports_labels(const char *scheme) 71 { 72 if (strcmp(scheme, "APM") == 0) 73 return (1); 74 if (strcmp(scheme, "GPT") == 0) 75 return (1); 76 77 return (0); 78 } 79 80 static void 81 newfs_command(const char *fstype, char *command, int use_default) 82 { 83 if (strcmp(fstype, "freebsd-ufs") == 0) { 84 int i; 85 DIALOG_LISTITEM items[] = { 86 {"UFS1", "UFS Version 1", 87 "Use version 1 of the UFS file system instead " 88 "of version 2 (not recommended)", 0 }, 89 {"SU", "Softupdates", 90 "Enable softupdates (default)", 1 }, 91 {"SUJ", "Softupdates journaling", 92 "Enable file system journaling (default - " 93 "turn off for SSDs)", 1 }, 94 {"TRIM", "Enable SSD TRIM support", 95 "Enable TRIM support, useful on solid-state drives", 96 0 }, 97 }; 98 99 if (!use_default) { 100 int choice; 101 choice = dlg_checklist("UFS Options", "", 0, 0, 0, 102 sizeof(items)/sizeof(items[0]), items, NULL, 103 FLAG_CHECK, &i); 104 if (choice == 1) /* Cancel */ 105 return; 106 } 107 108 strcpy(command, "newfs "); 109 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) { 110 if (items[i].state == 0) 111 continue; 112 if (strcmp(items[i].name, "UFS1") == 0) 113 strcat(command, "-O1 "); 114 else if (strcmp(items[i].name, "SU") == 0) 115 strcat(command, "-U "); 116 else if (strcmp(items[i].name, "SUJ") == 0) 117 strcat(command, "-j "); 118 else if (strcmp(items[i].name, "TRIM") == 0) 119 strcat(command, "-t "); 120 } 121 } else if (strcmp(fstype, "freebsd-zfs") == 0) { 122 int i; 123 DIALOG_LISTITEM items[] = { 124 {"fletcher4", "checksum algorithm: fletcher4", 125 "Use fletcher4 for data integrity checking. " 126 "(default)", 1 }, 127 {"fletcher2", "checksum algorithm: fletcher2", 128 "Use fletcher2 for data integrity checking. " 129 "(not recommended)", 0 }, 130 {"sha256", "checksum algorithm: sha256", 131 "Use sha256 for data integrity checking. " 132 "(not recommended)", 0 }, 133 {"atime", "Update atimes for files", 134 "Disable atime update", 0 }, 135 }; 136 137 if (!use_default) { 138 int choice; 139 choice = dlg_checklist("ZFS Options", "", 0, 0, 0, 140 sizeof(items)/sizeof(items[0]), items, NULL, 141 FLAG_CHECK, &i); 142 if (choice == 1) /* Cancel */ 143 return; 144 } 145 146 strcpy(command, "zpool create -f -m none "); 147 if (getenv("BSDINSTALL_TMPBOOT") != NULL) { 148 char zfsboot_path[MAXPATHLEN]; 149 sprintf(zfsboot_path, "%s/zfs", 150 getenv("BSDINSTALL_TMPBOOT")); 151 mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP | 152 S_IROTH | S_IXOTH); 153 sprintf(command, "%s -o cachefile=%s/zpool.cache ", 154 command, zfsboot_path); 155 } 156 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) { 157 if (items[i].state == 0) 158 continue; 159 if (strcmp(items[i].name, "fletcher4") == 0) 160 strcat(command, "-O checksum=fletcher4 "); 161 else if (strcmp(items[i].name, "fletcher2") == 0) 162 strcat(command, "-O checksum=fletcher2 "); 163 else if (strcmp(items[i].name, "sha256") == 0) 164 strcat(command, "-O checksum=sha256 "); 165 else if (strcmp(items[i].name, "atime") == 0) 166 strcat(command, "-O atime=off "); 167 } 168 } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0) { 169 int i; 170 DIALOG_LISTITEM items[] = { 171 {"FAT32", "FAT Type 32", 172 "Create a FAT32 filesystem (default)", 1 }, 173 {"FAT16", "FAT Type 16", 174 "Create a FAT16 filesystem", 0 }, 175 {"FAT12", "FAT Type 12", 176 "Create a FAT12 filesystem", 0 }, 177 }; 178 179 if (!use_default) { 180 int choice; 181 choice = dlg_checklist("FAT Options", "", 0, 0, 0, 182 sizeof(items)/sizeof(items[0]), items, NULL, 183 FLAG_RADIO, &i); 184 if (choice == 1) /* Cancel */ 185 return; 186 } 187 188 strcpy(command, "newfs_msdos "); 189 for (i = 0; i < (int)(sizeof(items)/sizeof(items[0])); i++) { 190 if (items[i].state == 0) 191 continue; 192 if (strcmp(items[i].name, "FAT32") == 0) 193 strcat(command, "-F 32 "); 194 else if (strcmp(items[i].name, "FAT16") == 0) 195 strcat(command, "-F 16 "); 196 else if (strcmp(items[i].name, "FAT12") == 0) 197 strcat(command, "-F 12 "); 198 } 199 } else { 200 if (!use_default) 201 dialog_msgbox("Error", "No configurable options exist " 202 "for this filesystem.", 0, 0, TRUE); 203 command[0] = '\0'; 204 } 205 } 206 207 const char * 208 choose_part_type(const char *def_scheme) 209 { 210 int cancel, choice; 211 const char *scheme = NULL; 212 213 DIALOG_LISTITEM items[] = { 214 {"APM", "Apple Partition Map", 215 "Bootable on PowerPC Apple Hardware", 0 }, 216 {"BSD", "BSD Labels", 217 "Bootable on most x86 systems", 0 }, 218 {"GPT", "GUID Partition Table", 219 "Bootable on most x86 systems and EFI aware ARM64", 0 }, 220 {"MBR", "DOS Partitions", 221 "Bootable on most x86 systems", 0 }, 222 {"VTOC8", "Sun VTOC8 Partition Table", 223 "Bootable on Sun SPARC systems", 0 }, 224 }; 225 226 parttypemenu: 227 dialog_vars.default_item = __DECONST(char *, def_scheme); 228 cancel = dlg_menu("Partition Scheme", 229 "Select a partition scheme for this volume:", 0, 0, 0, 230 sizeof(items) / sizeof(items[0]), items, &choice, NULL); 231 dialog_vars.default_item = NULL; 232 233 if (cancel) 234 return NULL; 235 236 if (!is_scheme_bootable(items[choice].name)) { 237 char message[512]; 238 sprintf(message, "This partition scheme (%s) is not " 239 "bootable on this platform. Are you sure you want " 240 "to proceed?", items[choice].name); 241 dialog_vars.defaultno = TRUE; 242 cancel = dialog_yesno("Warning", message, 0, 0); 243 dialog_vars.defaultno = FALSE; 244 if (cancel) /* cancel */ 245 goto parttypemenu; 246 } 247 248 scheme = items[choice].name; 249 250 return scheme; 251 } 252 253 int 254 gpart_partition(const char *lg_name, const char *scheme) 255 { 256 int cancel; 257 struct gctl_req *r; 258 const char *errstr; 259 260 schememenu: 261 if (scheme == NULL) { 262 scheme = choose_part_type(default_scheme()); 263 264 if (scheme == NULL) 265 return (-1); 266 267 if (!is_scheme_bootable(scheme)) { 268 char message[512]; 269 sprintf(message, "This partition scheme (%s) is not " 270 "bootable on this platform. Are you sure you want " 271 "to proceed?", scheme); 272 dialog_vars.defaultno = TRUE; 273 cancel = dialog_yesno("Warning", message, 0, 0); 274 dialog_vars.defaultno = FALSE; 275 if (cancel) { /* cancel */ 276 /* Reset scheme so user can choose another */ 277 scheme = NULL; 278 goto schememenu; 279 } 280 } 281 } 282 283 r = gctl_get_handle(); 284 gctl_ro_param(r, "class", -1, "PART"); 285 gctl_ro_param(r, "arg0", -1, lg_name); 286 gctl_ro_param(r, "flags", -1, GPART_FLAGS); 287 gctl_ro_param(r, "scheme", -1, scheme); 288 gctl_ro_param(r, "verb", -1, "create"); 289 290 errstr = gctl_issue(r); 291 if (errstr != NULL && errstr[0] != '\0') { 292 gpart_show_error("Error", NULL, errstr); 293 gctl_free(r); 294 scheme = NULL; 295 goto schememenu; 296 } 297 gctl_free(r); 298 299 if (bootcode_path(scheme) != NULL) 300 get_part_metadata(lg_name, 1)->bootcode = 1; 301 return (0); 302 } 303 304 static void 305 gpart_activate(struct gprovider *pp) 306 { 307 struct gconfig *gc; 308 struct gctl_req *r; 309 const char *errstr, *scheme; 310 const char *attribute = NULL; 311 intmax_t idx; 312 313 /* 314 * Some partition schemes need this partition to be marked 'active' 315 * for it to be bootable. 316 */ 317 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) { 318 if (strcmp(gc->lg_name, "scheme") == 0) { 319 scheme = gc->lg_val; 320 break; 321 } 322 } 323 324 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0) 325 attribute = "active"; 326 else 327 return; 328 329 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 330 if (strcmp(gc->lg_name, "index") == 0) { 331 idx = atoi(gc->lg_val); 332 break; 333 } 334 } 335 336 r = gctl_get_handle(); 337 gctl_ro_param(r, "class", -1, "PART"); 338 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name); 339 gctl_ro_param(r, "verb", -1, "set"); 340 gctl_ro_param(r, "attrib", -1, attribute); 341 gctl_ro_param(r, "index", sizeof(idx), &idx); 342 343 errstr = gctl_issue(r); 344 if (errstr != NULL && errstr[0] != '\0') 345 gpart_show_error("Error", "Error marking partition active:", 346 errstr); 347 gctl_free(r); 348 } 349 350 void 351 gpart_set_root(const char *lg_name, const char *attribute) 352 { 353 struct gctl_req *r; 354 const char *errstr; 355 356 r = gctl_get_handle(); 357 gctl_ro_param(r, "class", -1, "PART"); 358 gctl_ro_param(r, "arg0", -1, lg_name); 359 gctl_ro_param(r, "flags", -1, "C"); 360 gctl_ro_param(r, "verb", -1, "set"); 361 gctl_ro_param(r, "attrib", -1, attribute); 362 363 errstr = gctl_issue(r); 364 if (errstr != NULL && errstr[0] != '\0') 365 gpart_show_error("Error", "Error setting parameter on disk:", 366 errstr); 367 gctl_free(r); 368 } 369 370 static void 371 gpart_bootcode(struct ggeom *gp) 372 { 373 const char *bootcode; 374 struct gconfig *gc; 375 struct gctl_req *r; 376 const char *errstr, *scheme; 377 uint8_t *boot; 378 size_t bootsize, bytes; 379 int bootfd; 380 381 /* 382 * Write default bootcode to the newly partitioned disk, if that 383 * applies on this platform. 384 */ 385 LIST_FOREACH(gc, &gp->lg_config, lg_config) { 386 if (strcmp(gc->lg_name, "scheme") == 0) { 387 scheme = gc->lg_val; 388 break; 389 } 390 } 391 392 bootcode = bootcode_path(scheme); 393 if (bootcode == NULL) 394 return; 395 396 bootfd = open(bootcode, O_RDONLY); 397 if (bootfd < 0) { 398 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0, 399 TRUE); 400 return; 401 } 402 403 bootsize = lseek(bootfd, 0, SEEK_END); 404 boot = malloc(bootsize); 405 lseek(bootfd, 0, SEEK_SET); 406 bytes = 0; 407 while (bytes < bootsize) 408 bytes += read(bootfd, boot + bytes, bootsize - bytes); 409 close(bootfd); 410 411 r = gctl_get_handle(); 412 gctl_ro_param(r, "class", -1, "PART"); 413 gctl_ro_param(r, "arg0", -1, gp->lg_name); 414 gctl_ro_param(r, "verb", -1, "bootcode"); 415 gctl_ro_param(r, "bootcode", bootsize, boot); 416 417 errstr = gctl_issue(r); 418 if (errstr != NULL && errstr[0] != '\0') 419 gpart_show_error("Bootcode Error", NULL, errstr); 420 gctl_free(r); 421 free(boot); 422 } 423 424 static void 425 gpart_partcode(struct gprovider *pp, const char *fstype) 426 { 427 struct gconfig *gc; 428 const char *scheme; 429 const char *indexstr; 430 char message[255], command[255]; 431 432 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) { 433 if (strcmp(gc->lg_name, "scheme") == 0) { 434 scheme = gc->lg_val; 435 break; 436 } 437 } 438 439 /* Make sure this partition scheme needs partcode on this platform */ 440 if (partcode_path(scheme, fstype) == NULL) 441 return; 442 443 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 444 if (strcmp(gc->lg_name, "index") == 0) { 445 indexstr = gc->lg_val; 446 break; 447 } 448 } 449 450 /* Shell out to gpart for partcode for now */ 451 sprintf(command, "gpart bootcode -p %s -i %s %s", 452 partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name); 453 if (system(command) != 0) { 454 sprintf(message, "Error installing partcode on partition %s", 455 pp->lg_name); 456 dialog_msgbox("Error", message, 0, 0, TRUE); 457 } 458 } 459 460 void 461 gpart_destroy(struct ggeom *lg_geom) 462 { 463 struct gctl_req *r; 464 struct gprovider *pp; 465 const char *errstr; 466 int force = 1; 467 468 /* Delete all child metadata */ 469 LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider) 470 gpart_delete(pp); 471 472 /* Revert any local changes to get this geom into a pristine state */ 473 r = gctl_get_handle(); 474 gctl_ro_param(r, "class", -1, "PART"); 475 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name); 476 gctl_ro_param(r, "verb", -1, "undo"); 477 gctl_issue(r); /* Ignore errors -- these are non-fatal */ 478 gctl_free(r); 479 480 /* Now destroy the geom itself */ 481 r = gctl_get_handle(); 482 gctl_ro_param(r, "class", -1, "PART"); 483 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name); 484 gctl_ro_param(r, "flags", -1, GPART_FLAGS); 485 gctl_ro_param(r, "force", sizeof(force), &force); 486 gctl_ro_param(r, "verb", -1, "destroy"); 487 errstr = gctl_issue(r); 488 if (errstr != NULL && errstr[0] != '\0') { 489 /* 490 * Check if we reverted away the existence of the geom 491 * altogether. Show all other errors to the user. 492 */ 493 if (strtol(errstr, NULL, 0) != EINVAL) 494 gpart_show_error("Error", NULL, errstr); 495 } 496 gctl_free(r); 497 498 /* And any metadata associated with the partition scheme itself */ 499 delete_part_metadata(lg_geom->lg_name); 500 } 501 502 void 503 gpart_edit(struct gprovider *pp) 504 { 505 struct gctl_req *r; 506 struct gconfig *gc; 507 struct gconsumer *cp; 508 struct ggeom *geom; 509 const char *errstr, *oldtype, *scheme; 510 struct partition_metadata *md; 511 char sizestr[32]; 512 char newfs[255]; 513 intmax_t idx; 514 int hadlabel, choice, junk, nitems; 515 unsigned i; 516 517 DIALOG_FORMITEM items[] = { 518 {0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0, 519 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, " 520 "freebsd-swap)", FALSE}, 521 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0, 522 FALSE, "Partition size. Append K, M, G for kilobytes, " 523 "megabytes or gigabytes.", FALSE}, 524 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0, 525 FALSE, "Path at which to mount this partition (leave blank " 526 "for swap, set to / for root filesystem)", FALSE}, 527 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE, 528 "Partition name. Not all partition schemes support this.", 529 FALSE}, 530 }; 531 532 /* 533 * Find the PART geom we are manipulating. This may be a consumer of 534 * this provider, or its parent. Check the consumer case first. 535 */ 536 geom = NULL; 537 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers) 538 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) { 539 /* Check for zombie geoms, treating them as blank */ 540 scheme = NULL; 541 LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) { 542 if (strcmp(gc->lg_name, "scheme") == 0) { 543 scheme = gc->lg_val; 544 break; 545 } 546 } 547 if (scheme == NULL || strcmp(scheme, "(none)") == 0) { 548 gpart_partition(cp->lg_geom->lg_name, NULL); 549 return; 550 } 551 552 /* If this is a nested partition, edit as usual */ 553 if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0) 554 break; 555 556 /* Destroy the geom and all sub-partitions */ 557 gpart_destroy(cp->lg_geom); 558 559 /* Now re-partition and return */ 560 gpart_partition(cp->lg_geom->lg_name, NULL); 561 return; 562 } 563 564 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0) 565 geom = pp->lg_geom; 566 567 if (geom == NULL) { 568 /* Disk not partitioned, so partition it */ 569 gpart_partition(pp->lg_name, NULL); 570 return; 571 } 572 573 LIST_FOREACH(gc, &geom->lg_config, lg_config) { 574 if (strcmp(gc->lg_name, "scheme") == 0) { 575 scheme = gc->lg_val; 576 break; 577 } 578 } 579 580 nitems = scheme_supports_labels(scheme) ? 4 : 3; 581 582 /* Edit editable parameters of a partition */ 583 hadlabel = 0; 584 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 585 if (strcmp(gc->lg_name, "type") == 0) { 586 oldtype = gc->lg_val; 587 items[0].text = gc->lg_val; 588 } 589 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) { 590 hadlabel = 1; 591 items[3].text = gc->lg_val; 592 } 593 if (strcmp(gc->lg_name, "index") == 0) 594 idx = atoi(gc->lg_val); 595 } 596 597 TAILQ_FOREACH(md, &part_metadata, metadata) { 598 if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) { 599 if (md->fstab != NULL) 600 items[2].text = md->fstab->fs_file; 601 break; 602 } 603 } 604 605 humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE, 606 HN_NOSPACE | HN_DECIMAL); 607 items[1].text = sizestr; 608 609 editpart: 610 choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk); 611 612 if (choice) /* Cancel pressed */ 613 goto endedit; 614 615 /* If this is the root partition, check that this fs is bootable */ 616 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme, 617 items[0].text)) { 618 char message[512]; 619 sprintf(message, "This file system (%s) is not bootable " 620 "on this system. Are you sure you want to proceed?", 621 items[0].text); 622 dialog_vars.defaultno = TRUE; 623 choice = dialog_yesno("Warning", message, 0, 0); 624 dialog_vars.defaultno = FALSE; 625 if (choice == 1) /* cancel */ 626 goto editpart; 627 } 628 629 /* Check if the label has a / in it */ 630 if (strchr(items[3].text, '/') != NULL) { 631 dialog_msgbox("Error", "Label contains a /, which is not an " 632 "allowed character.", 0, 0, TRUE); 633 goto editpart; 634 } 635 636 r = gctl_get_handle(); 637 gctl_ro_param(r, "class", -1, "PART"); 638 gctl_ro_param(r, "arg0", -1, geom->lg_name); 639 gctl_ro_param(r, "flags", -1, GPART_FLAGS); 640 gctl_ro_param(r, "verb", -1, "modify"); 641 gctl_ro_param(r, "index", sizeof(idx), &idx); 642 if (hadlabel || items[3].text[0] != '\0') 643 gctl_ro_param(r, "label", -1, items[3].text); 644 gctl_ro_param(r, "type", -1, items[0].text); 645 errstr = gctl_issue(r); 646 if (errstr != NULL && errstr[0] != '\0') { 647 gpart_show_error("Error", NULL, errstr); 648 gctl_free(r); 649 goto editpart; 650 } 651 gctl_free(r); 652 653 newfs_command(items[0].text, newfs, 1); 654 set_default_part_metadata(pp->lg_name, scheme, items[0].text, 655 items[2].text, (strcmp(oldtype, items[0].text) != 0) ? 656 newfs : NULL); 657 658 endedit: 659 if (strcmp(oldtype, items[0].text) != 0 && cp != NULL) 660 gpart_destroy(cp->lg_geom); 661 if (strcmp(oldtype, items[0].text) != 0 && strcmp(items[0].text, 662 "freebsd") == 0) 663 gpart_partition(pp->lg_name, "BSD"); 664 665 for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++) 666 if (items[i].text_free) 667 free(items[i].text); 668 } 669 670 void 671 set_default_part_metadata(const char *name, const char *scheme, 672 const char *type, const char *mountpoint, const char *newfs) 673 { 674 struct partition_metadata *md; 675 char *zpool_name = NULL; 676 int i; 677 678 /* Set part metadata */ 679 md = get_part_metadata(name, 1); 680 681 if (newfs) { 682 if (md->newfs != NULL) { 683 free(md->newfs); 684 md->newfs = NULL; 685 } 686 687 if (newfs != NULL && newfs[0] != '\0') { 688 md->newfs = malloc(strlen(newfs) + strlen(" /dev/") + 689 strlen(mountpoint) + 5 + strlen(name) + 1); 690 if (strcmp("freebsd-zfs", type) == 0) { 691 zpool_name = strdup((strlen(mountpoint) == 1) ? 692 "root" : &mountpoint[1]); 693 for (i = 0; zpool_name[i] != 0; i++) 694 if (!isalnum(zpool_name[i])) 695 zpool_name[i] = '_'; 696 sprintf(md->newfs, "%s %s /dev/%s", newfs, 697 zpool_name, name); 698 } else { 699 sprintf(md->newfs, "%s /dev/%s", newfs, name); 700 } 701 } 702 } 703 704 if (strcmp(type, "freebsd-swap") == 0) 705 mountpoint = "none"; 706 if (strcmp(type, bootpart_type(scheme)) == 0) 707 md->bootcode = 1; 708 709 /* VTOC8 needs partcode at the start of partitions */ 710 if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0 711 || strcmp(type, "freebsd-zfs") == 0)) 712 md->bootcode = 1; 713 714 if (mountpoint == NULL || mountpoint[0] == '\0') { 715 if (md->fstab != NULL) { 716 free(md->fstab->fs_spec); 717 free(md->fstab->fs_file); 718 free(md->fstab->fs_vfstype); 719 free(md->fstab->fs_mntops); 720 free(md->fstab->fs_type); 721 free(md->fstab); 722 md->fstab = NULL; 723 } 724 } else { 725 if (md->fstab == NULL) { 726 md->fstab = malloc(sizeof(struct fstab)); 727 } else { 728 free(md->fstab->fs_spec); 729 free(md->fstab->fs_file); 730 free(md->fstab->fs_vfstype); 731 free(md->fstab->fs_mntops); 732 free(md->fstab->fs_type); 733 } 734 if (strcmp("freebsd-zfs", type) == 0) { 735 md->fstab->fs_spec = strdup(zpool_name); 736 } else { 737 md->fstab->fs_spec = malloc(strlen(name) + 738 strlen("/dev/") + 1); 739 sprintf(md->fstab->fs_spec, "/dev/%s", name); 740 } 741 md->fstab->fs_file = strdup(mountpoint); 742 /* Get VFS from text after freebsd-, if possible */ 743 if (strncmp("freebsd-", type, 8) == 0) 744 md->fstab->fs_vfstype = strdup(&type[8]); 745 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0) 746 md->fstab->fs_vfstype = strdup("msdosfs"); 747 else 748 md->fstab->fs_vfstype = strdup(type); /* Guess */ 749 if (strcmp(type, "freebsd-swap") == 0) { 750 md->fstab->fs_type = strdup(FSTAB_SW); 751 md->fstab->fs_freq = 0; 752 md->fstab->fs_passno = 0; 753 } else if (strcmp(type, "freebsd-zfs") == 0) { 754 md->fstab->fs_type = strdup(FSTAB_RW); 755 md->fstab->fs_freq = 0; 756 md->fstab->fs_passno = 0; 757 } else { 758 md->fstab->fs_type = strdup(FSTAB_RW); 759 if (strcmp(mountpoint, "/") == 0) { 760 md->fstab->fs_freq = 1; 761 md->fstab->fs_passno = 1; 762 } else { 763 md->fstab->fs_freq = 2; 764 md->fstab->fs_passno = 2; 765 } 766 } 767 md->fstab->fs_mntops = strdup(md->fstab->fs_type); 768 } 769 770 if (zpool_name != NULL) 771 free(zpool_name); 772 } 773 774 static 775 int part_compare(const void *xa, const void *xb) 776 { 777 struct gprovider **a = (struct gprovider **)xa; 778 struct gprovider **b = (struct gprovider **)xb; 779 intmax_t astart, bstart; 780 struct gconfig *gc; 781 782 astart = bstart = 0; 783 LIST_FOREACH(gc, &(*a)->lg_config, lg_config) 784 if (strcmp(gc->lg_name, "start") == 0) { 785 astart = strtoimax(gc->lg_val, NULL, 0); 786 break; 787 } 788 LIST_FOREACH(gc, &(*b)->lg_config, lg_config) 789 if (strcmp(gc->lg_name, "start") == 0) { 790 bstart = strtoimax(gc->lg_val, NULL, 0); 791 break; 792 } 793 794 if (astart < bstart) 795 return -1; 796 else if (astart > bstart) 797 return 1; 798 else 799 return 0; 800 } 801 802 intmax_t 803 gpart_max_free(struct ggeom *geom, intmax_t *npartstart) 804 { 805 struct gconfig *gc; 806 struct gprovider *pp, **providers; 807 intmax_t sectorsize, stripesize, offset; 808 intmax_t lastend; 809 intmax_t start, end; 810 intmax_t maxsize, maxstart; 811 intmax_t partstart, partend; 812 int i, nparts; 813 814 /* Now get the maximum free size and free start */ 815 start = end = 0; 816 LIST_FOREACH(gc, &geom->lg_config, lg_config) { 817 if (strcmp(gc->lg_name, "first") == 0) 818 start = strtoimax(gc->lg_val, NULL, 0); 819 if (strcmp(gc->lg_name, "last") == 0) 820 end = strtoimax(gc->lg_val, NULL, 0); 821 } 822 823 i = nparts = 0; 824 LIST_FOREACH(pp, &geom->lg_provider, lg_provider) 825 nparts++; 826 providers = calloc(nparts, sizeof(providers[0])); 827 LIST_FOREACH(pp, &geom->lg_provider, lg_provider) 828 providers[i++] = pp; 829 qsort(providers, nparts, sizeof(providers[0]), part_compare); 830 831 lastend = start - 1; 832 maxsize = 0; 833 for (i = 0; i < nparts; i++) { 834 pp = providers[i]; 835 836 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 837 if (strcmp(gc->lg_name, "start") == 0) 838 partstart = strtoimax(gc->lg_val, NULL, 0); 839 if (strcmp(gc->lg_name, "end") == 0) 840 partend = strtoimax(gc->lg_val, NULL, 0); 841 } 842 843 if (partstart - lastend > maxsize) { 844 maxsize = partstart - lastend - 1; 845 maxstart = lastend + 1; 846 } 847 848 lastend = partend; 849 } 850 851 if (end - lastend > maxsize) { 852 maxsize = end - lastend - 1; 853 maxstart = lastend + 1; 854 } 855 856 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider; 857 858 /* 859 * Round the start and size of the largest available space up to 860 * the nearest multiple of the adjusted stripe size. 861 * 862 * The adjusted stripe size is the least common multiple of the 863 * actual stripe size, or the sector size if no stripe size was 864 * reported, and 4096. The reason for this is that contemporary 865 * disks often have 4096-byte physical sectors but report 512 866 * bytes instead for compatibility with older / broken operating 867 * systems and BIOSes. For the same reasons, virtualized storage 868 * may also report a 512-byte stripe size, or none at all. 869 */ 870 sectorsize = pp->lg_sectorsize; 871 if ((stripesize = pp->lg_stripesize) == 0) 872 stripesize = sectorsize; 873 while (stripesize % 4096 != 0) 874 stripesize *= 2; 875 if ((offset = maxstart * sectorsize % stripesize) != 0) { 876 offset = (stripesize - offset) / sectorsize; 877 maxstart += offset; 878 maxsize -= offset; 879 } 880 881 if (npartstart != NULL) 882 *npartstart = maxstart; 883 884 return (maxsize); 885 } 886 887 void 888 gpart_create(struct gprovider *pp, char *default_type, char *default_size, 889 char *default_mountpoint, char **partname, int interactive) 890 { 891 struct gctl_req *r; 892 struct gconfig *gc; 893 struct gconsumer *cp; 894 struct ggeom *geom; 895 const char *errstr, *scheme; 896 char sizestr[32], startstr[32], output[64], *newpartname; 897 char newfs[255], options_fstype[64]; 898 intmax_t maxsize, size, sector, firstfree, stripe; 899 uint64_t bytes; 900 int nitems, choice, junk; 901 unsigned i; 902 903 DIALOG_FORMITEM items[] = { 904 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0, 905 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, " 906 "freebsd-swap)", FALSE}, 907 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0, 908 FALSE, "Partition size. Append K, M, G for kilobytes, " 909 "megabytes or gigabytes.", FALSE}, 910 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0, 911 FALSE, "Path at which to mount partition (blank for " 912 "swap, / for root filesystem)", FALSE}, 913 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE, 914 "Partition name. Not all partition schemes support this.", 915 FALSE}, 916 }; 917 918 if (partname != NULL) 919 *partname = NULL; 920 921 /* Record sector and stripe sizes */ 922 sector = pp->lg_sectorsize; 923 stripe = pp->lg_stripesize; 924 925 /* 926 * Find the PART geom we are manipulating. This may be a consumer of 927 * this provider, or its parent. Check the consumer case first. 928 */ 929 geom = NULL; 930 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers) 931 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) { 932 geom = cp->lg_geom; 933 break; 934 } 935 936 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0) 937 geom = pp->lg_geom; 938 939 /* Now get the partition scheme */ 940 scheme = NULL; 941 if (geom != NULL) { 942 LIST_FOREACH(gc, &geom->lg_config, lg_config) 943 if (strcmp(gc->lg_name, "scheme") == 0) 944 scheme = gc->lg_val; 945 } 946 947 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) { 948 if (gpart_partition(pp->lg_name, NULL) == 0) 949 dialog_msgbox("", 950 "The partition table has been successfully created." 951 " Please press Create again to create partitions.", 952 0, 0, TRUE); 953 954 return; 955 } 956 957 /* 958 * If we still don't have a geom, either the user has 959 * canceled partitioning or there has been an error which has already 960 * been displayed, so bail. 961 */ 962 if (geom == NULL) 963 return; 964 965 maxsize = size = gpart_max_free(geom, &firstfree); 966 if (size <= 0) { 967 dialog_msgbox("Error", "No free space left on device.", 0, 0, 968 TRUE); 969 return; 970 } 971 972 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE, 973 HN_NOSPACE | HN_DECIMAL); 974 items[1].text = sizestr; 975 976 /* Special-case the MBR default type for nested partitions */ 977 if (strcmp(scheme, "MBR") == 0) { 978 items[0].text = "freebsd"; 979 items[0].help = "Filesystem type (e.g. freebsd, fat32)"; 980 } 981 982 nitems = scheme_supports_labels(scheme) ? 4 : 3; 983 984 if (default_type != NULL) 985 items[0].text = default_type; 986 if (default_size != NULL) 987 items[1].text = default_size; 988 if (default_mountpoint != NULL) 989 items[2].text = default_mountpoint; 990 991 /* Default options */ 992 strncpy(options_fstype, items[0].text, 993 sizeof(options_fstype)); 994 newfs_command(options_fstype, newfs, 1); 995 addpartform: 996 if (interactive) { 997 dialog_vars.extra_label = "Options"; 998 dialog_vars.extra_button = TRUE; 999 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems, 1000 items, &junk); 1001 dialog_vars.extra_button = FALSE; 1002 switch (choice) { 1003 case 0: /* OK */ 1004 break; 1005 case 1: /* Cancel */ 1006 return; 1007 case 3: /* Options */ 1008 strncpy(options_fstype, items[0].text, 1009 sizeof(options_fstype)); 1010 newfs_command(options_fstype, newfs, 0); 1011 goto addpartform; 1012 } 1013 } 1014 1015 /* 1016 * If the user changed the fs type after specifying options, undo 1017 * their choices in favor of the new filesystem's defaults. 1018 */ 1019 if (strcmp(options_fstype, items[0].text) != 0) { 1020 strncpy(options_fstype, items[0].text, sizeof(options_fstype)); 1021 newfs_command(options_fstype, newfs, 1); 1022 } 1023 1024 size = maxsize; 1025 if (strlen(items[1].text) > 0) { 1026 if (expand_number(items[1].text, &bytes) != 0) { 1027 char error[512]; 1028 1029 sprintf(error, "Invalid size: %s\n", strerror(errno)); 1030 dialog_msgbox("Error", error, 0, 0, TRUE); 1031 goto addpartform; 1032 } 1033 size = MIN((intmax_t)(bytes/sector), maxsize); 1034 } 1035 1036 /* Check if the label has a / in it */ 1037 if (strchr(items[3].text, '/') != NULL) { 1038 dialog_msgbox("Error", "Label contains a /, which is not an " 1039 "allowed character.", 0, 0, TRUE); 1040 goto addpartform; 1041 } 1042 1043 /* Warn if no mountpoint set */ 1044 if (strcmp(items[0].text, "freebsd-ufs") == 0 && 1045 items[2].text[0] != '/') { 1046 dialog_vars.defaultno = TRUE; 1047 choice = dialog_yesno("Warning", 1048 "This partition does not have a valid mountpoint " 1049 "(for the partition from which you intend to boot the " 1050 "operating system, the mountpoint should be /). Are you " 1051 "sure you want to continue?" 1052 , 0, 0); 1053 dialog_vars.defaultno = FALSE; 1054 if (choice == 1) /* cancel */ 1055 goto addpartform; 1056 } 1057 1058 /* 1059 * Error if this scheme needs nested partitions, this is one, and 1060 * a mountpoint was set. 1061 */ 1062 if (strcmp(items[0].text, "freebsd") == 0 && 1063 strlen(items[2].text) > 0) { 1064 dialog_msgbox("Error", "Partitions of type \"freebsd\" are " 1065 "nested BSD-type partition schemes and cannot have " 1066 "mountpoints. After creating one, select it and press " 1067 "Create again to add the actual file systems.", 0, 0, TRUE); 1068 goto addpartform; 1069 } 1070 1071 /* If this is the root partition, check that this scheme is bootable */ 1072 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) { 1073 char message[512]; 1074 sprintf(message, "This partition scheme (%s) is not bootable " 1075 "on this platform. Are you sure you want to proceed?", 1076 scheme); 1077 dialog_vars.defaultno = TRUE; 1078 choice = dialog_yesno("Warning", message, 0, 0); 1079 dialog_vars.defaultno = FALSE; 1080 if (choice == 1) /* cancel */ 1081 goto addpartform; 1082 } 1083 1084 /* If this is the root partition, check that this fs is bootable */ 1085 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme, 1086 items[0].text)) { 1087 char message[512]; 1088 sprintf(message, "This file system (%s) is not bootable " 1089 "on this system. Are you sure you want to proceed?", 1090 items[0].text); 1091 dialog_vars.defaultno = TRUE; 1092 choice = dialog_yesno("Warning", message, 0, 0); 1093 dialog_vars.defaultno = FALSE; 1094 if (choice == 1) /* cancel */ 1095 goto addpartform; 1096 } 1097 1098 /* 1099 * If this is the root partition, and we need a boot partition, ask 1100 * the user to add one. 1101 */ 1102 1103 /* Check for existing freebsd-boot partition */ 1104 LIST_FOREACH(pp, &geom->lg_provider, lg_provider) { 1105 struct partition_metadata *md; 1106 md = get_part_metadata(pp->lg_name, 0); 1107 if (md == NULL || !md->bootcode) 1108 continue; 1109 LIST_FOREACH(gc, &pp->lg_config, lg_config) 1110 if (strcmp(gc->lg_name, "type") == 0) 1111 break; 1112 if (gc != NULL && strcmp(gc->lg_val, 1113 bootpart_type(scheme)) == 0) 1114 break; 1115 } 1116 1117 /* If there isn't one, and we need one, ask */ 1118 if ((strcmp(items[0].text, "freebsd") == 0 || 1119 strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0 && 1120 pp == NULL) { 1121 if (interactive) 1122 choice = dialog_yesno("Boot Partition", 1123 "This partition scheme requires a boot partition " 1124 "for the disk to be bootable. Would you like to " 1125 "make one now?", 0, 0); 1126 else 1127 choice = 0; 1128 1129 if (choice == 0) { /* yes */ 1130 r = gctl_get_handle(); 1131 gctl_ro_param(r, "class", -1, "PART"); 1132 gctl_ro_param(r, "arg0", -1, geom->lg_name); 1133 gctl_ro_param(r, "flags", -1, GPART_FLAGS); 1134 gctl_ro_param(r, "verb", -1, "add"); 1135 gctl_ro_param(r, "type", -1, bootpart_type(scheme)); 1136 snprintf(sizestr, sizeof(sizestr), "%jd", 1137 bootpart_size(scheme) / sector); 1138 gctl_ro_param(r, "size", -1, sizestr); 1139 snprintf(startstr, sizeof(startstr), "%jd", firstfree); 1140 gctl_ro_param(r, "start", -1, startstr); 1141 gctl_rw_param(r, "output", sizeof(output), output); 1142 errstr = gctl_issue(r); 1143 if (errstr != NULL && errstr[0] != '\0') 1144 gpart_show_error("Error", NULL, errstr); 1145 gctl_free(r); 1146 1147 get_part_metadata(strtok(output, " "), 1)->bootcode = 1; 1148 1149 /* Now adjust the part we are really adding forward */ 1150 firstfree += bootpart_size(scheme) / sector; 1151 size -= (bootpart_size(scheme) + stripe)/sector; 1152 if (stripe > 0 && (firstfree*sector % stripe) != 0) 1153 firstfree += (stripe - ((firstfree*sector) % 1154 stripe)) / sector; 1155 } 1156 } 1157 1158 r = gctl_get_handle(); 1159 gctl_ro_param(r, "class", -1, "PART"); 1160 gctl_ro_param(r, "arg0", -1, geom->lg_name); 1161 gctl_ro_param(r, "flags", -1, GPART_FLAGS); 1162 gctl_ro_param(r, "verb", -1, "add"); 1163 1164 gctl_ro_param(r, "type", -1, items[0].text); 1165 snprintf(sizestr, sizeof(sizestr), "%jd", size); 1166 gctl_ro_param(r, "size", -1, sizestr); 1167 snprintf(startstr, sizeof(startstr), "%jd", firstfree); 1168 gctl_ro_param(r, "start", -1, startstr); 1169 if (items[3].text[0] != '\0') 1170 gctl_ro_param(r, "label", -1, items[3].text); 1171 gctl_rw_param(r, "output", sizeof(output), output); 1172 errstr = gctl_issue(r); 1173 if (errstr != NULL && errstr[0] != '\0') { 1174 gpart_show_error("Error", NULL, errstr); 1175 gctl_free(r); 1176 goto addpartform; 1177 } 1178 newpartname = strtok(output, " "); 1179 gctl_free(r); 1180 1181 /* 1182 * Try to destroy any geom that gpart picked up already here from 1183 * dirty blocks. 1184 */ 1185 r = gctl_get_handle(); 1186 gctl_ro_param(r, "class", -1, "PART"); 1187 gctl_ro_param(r, "arg0", -1, newpartname); 1188 gctl_ro_param(r, "flags", -1, GPART_FLAGS); 1189 junk = 1; 1190 gctl_ro_param(r, "force", sizeof(junk), &junk); 1191 gctl_ro_param(r, "verb", -1, "destroy"); 1192 gctl_issue(r); /* Error usually expected and non-fatal */ 1193 gctl_free(r); 1194 1195 if (strcmp(items[0].text, bootpart_type(scheme)) == 0) 1196 get_part_metadata(newpartname, 1)->bootcode = 1; 1197 else if (strcmp(items[0].text, "freebsd") == 0) 1198 gpart_partition(newpartname, "BSD"); 1199 else 1200 set_default_part_metadata(newpartname, scheme, 1201 items[0].text, items[2].text, newfs); 1202 1203 for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++) 1204 if (items[i].text_free) 1205 free(items[i].text); 1206 1207 if (partname != NULL) 1208 *partname = strdup(newpartname); 1209 } 1210 1211 void 1212 gpart_delete(struct gprovider *pp) 1213 { 1214 struct gconfig *gc; 1215 struct ggeom *geom; 1216 struct gconsumer *cp; 1217 struct gctl_req *r; 1218 const char *errstr; 1219 intmax_t idx; 1220 int is_partition; 1221 1222 /* Is it a partition? */ 1223 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0); 1224 1225 /* Find out if this is the root of a gpart geom */ 1226 geom = NULL; 1227 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers) 1228 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) { 1229 geom = cp->lg_geom; 1230 break; 1231 } 1232 1233 /* If so, destroy all children */ 1234 if (geom != NULL) { 1235 gpart_destroy(geom); 1236 1237 /* If this is a partition, revert it, so it can be deleted */ 1238 if (is_partition) { 1239 r = gctl_get_handle(); 1240 gctl_ro_param(r, "class", -1, "PART"); 1241 gctl_ro_param(r, "arg0", -1, geom->lg_name); 1242 gctl_ro_param(r, "verb", -1, "undo"); 1243 gctl_issue(r); /* Ignore non-fatal errors */ 1244 gctl_free(r); 1245 } 1246 } 1247 1248 /* 1249 * If this is not a partition, see if that is a problem, complain if 1250 * necessary, and return always, since we need not do anything further, 1251 * error or no. 1252 */ 1253 if (!is_partition) { 1254 if (geom == NULL) 1255 dialog_msgbox("Error", 1256 "Only partitions can be deleted.", 0, 0, TRUE); 1257 return; 1258 } 1259 1260 r = gctl_get_handle(); 1261 gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name); 1262 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name); 1263 gctl_ro_param(r, "flags", -1, GPART_FLAGS); 1264 gctl_ro_param(r, "verb", -1, "delete"); 1265 1266 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 1267 if (strcmp(gc->lg_name, "index") == 0) { 1268 idx = atoi(gc->lg_val); 1269 gctl_ro_param(r, "index", sizeof(idx), &idx); 1270 break; 1271 } 1272 } 1273 1274 errstr = gctl_issue(r); 1275 if (errstr != NULL && errstr[0] != '\0') { 1276 gpart_show_error("Error", NULL, errstr); 1277 gctl_free(r); 1278 return; 1279 } 1280 1281 gctl_free(r); 1282 1283 delete_part_metadata(pp->lg_name); 1284 } 1285 1286 void 1287 gpart_revert_all(struct gmesh *mesh) 1288 { 1289 struct gclass *classp; 1290 struct gconfig *gc; 1291 struct ggeom *gp; 1292 struct gctl_req *r; 1293 const char *modified; 1294 1295 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 1296 if (strcmp(classp->lg_name, "PART") == 0) 1297 break; 1298 } 1299 1300 if (strcmp(classp->lg_name, "PART") != 0) { 1301 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE); 1302 return; 1303 } 1304 1305 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 1306 modified = "true"; /* XXX: If we don't know (kernel too old), 1307 * assume there are modifications. */ 1308 LIST_FOREACH(gc, &gp->lg_config, lg_config) { 1309 if (strcmp(gc->lg_name, "modified") == 0) { 1310 modified = gc->lg_val; 1311 break; 1312 } 1313 } 1314 1315 if (strcmp(modified, "false") == 0) 1316 continue; 1317 1318 r = gctl_get_handle(); 1319 gctl_ro_param(r, "class", -1, "PART"); 1320 gctl_ro_param(r, "arg0", -1, gp->lg_name); 1321 gctl_ro_param(r, "verb", -1, "undo"); 1322 1323 gctl_issue(r); 1324 gctl_free(r); 1325 } 1326 } 1327 1328 void 1329 gpart_commit(struct gmesh *mesh) 1330 { 1331 struct partition_metadata *md; 1332 struct gclass *classp; 1333 struct ggeom *gp; 1334 struct gconfig *gc; 1335 struct gconsumer *cp; 1336 struct gprovider *pp; 1337 struct gctl_req *r; 1338 const char *errstr; 1339 const char *modified; 1340 const char *rootfs; 1341 1342 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 1343 if (strcmp(classp->lg_name, "PART") == 0) 1344 break; 1345 } 1346 1347 /* Figure out what filesystem / uses */ 1348 rootfs = "ufs"; /* Assume ufs if nothing else present */ 1349 TAILQ_FOREACH(md, &part_metadata, metadata) { 1350 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) { 1351 rootfs = md->fstab->fs_vfstype; 1352 break; 1353 } 1354 } 1355 1356 if (strcmp(classp->lg_name, "PART") != 0) { 1357 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE); 1358 return; 1359 } 1360 1361 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 1362 modified = "true"; /* XXX: If we don't know (kernel too old), 1363 * assume there are modifications. */ 1364 LIST_FOREACH(gc, &gp->lg_config, lg_config) { 1365 if (strcmp(gc->lg_name, "modified") == 0) { 1366 modified = gc->lg_val; 1367 break; 1368 } 1369 } 1370 1371 if (strcmp(modified, "false") == 0) 1372 continue; 1373 1374 /* Add bootcode if necessary, before the commit */ 1375 md = get_part_metadata(gp->lg_name, 0); 1376 if (md != NULL && md->bootcode) 1377 gpart_bootcode(gp); 1378 1379 /* Now install partcode on its partitions, if necessary */ 1380 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 1381 md = get_part_metadata(pp->lg_name, 0); 1382 if (md == NULL || !md->bootcode) 1383 continue; 1384 1385 /* Mark this partition active if that's required */ 1386 gpart_activate(pp); 1387 1388 /* Check if the partition has sub-partitions */ 1389 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers) 1390 if (strcmp(cp->lg_geom->lg_class->lg_name, 1391 "PART") == 0) 1392 break; 1393 1394 if (cp == NULL) /* No sub-partitions */ 1395 gpart_partcode(pp, rootfs); 1396 } 1397 1398 r = gctl_get_handle(); 1399 gctl_ro_param(r, "class", -1, "PART"); 1400 gctl_ro_param(r, "arg0", -1, gp->lg_name); 1401 gctl_ro_param(r, "verb", -1, "commit"); 1402 1403 errstr = gctl_issue(r); 1404 if (errstr != NULL && errstr[0] != '\0') 1405 gpart_show_error("Error", NULL, errstr); 1406 gctl_free(r); 1407 } 1408 } 1409 1410