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