2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
43 #define GPART_FLAGS "x" /* Do not commit changes by default */
46 gpart_show_error(const char *title, const char *explanation, const char *errstr)
52 if (explanation == NULL)
55 error = strtol(errstr, &errmsg, 0);
56 if (errmsg != errstr) {
57 while (errmsg[0] == ' ')
59 if (errmsg[0] != '\0')
60 sprintf(message, "%s%s. %s", explanation,
61 strerror(error), errmsg);
63 sprintf(message, "%s%s", explanation, strerror(error));
65 sprintf(message, "%s%s", explanation, errmsg);
68 dialog_msgbox(title, message, 0, 0, TRUE);
72 scheme_supports_labels(const char *scheme)
74 if (strcmp(scheme, "APM") == 0)
76 if (strcmp(scheme, "GPT") == 0)
83 newfs_command(const char *fstype, char *command, int use_default)
85 if (strcmp(fstype, "freebsd-ufs") == 0) {
87 DIALOG_LISTITEM items[] = {
88 {"UFS1", "UFS Version 1",
89 "Use version 1 of the UFS file system instead "
90 "of version 2 (not recommended)", 0 },
92 "Enable softupdates (default)", 1 },
93 {"SUJ", "Softupdates journaling",
94 "Enable file system journaling (default - "
95 "turn off for SSDs)", 1 },
96 {"TRIM", "Enable SSD TRIM support",
97 "Enable TRIM support, useful on solid-state drives",
103 choice = dlg_checklist("UFS Options", "", 0, 0, 0,
104 nitems(items), items, NULL,
106 if (choice == 1) /* Cancel */
110 strcpy(command, "newfs ");
111 for (i = 0; i < (int)nitems(items); i++) {
112 if (items[i].state == 0)
114 if (strcmp(items[i].name, "UFS1") == 0)
115 strcat(command, "-O1 ");
116 else if (strcmp(items[i].name, "SU") == 0)
117 strcat(command, "-U ");
118 else if (strcmp(items[i].name, "SUJ") == 0)
119 strcat(command, "-j ");
120 else if (strcmp(items[i].name, "TRIM") == 0)
121 strcat(command, "-t ");
123 } else if (strcmp(fstype, "freebsd-zfs") == 0) {
125 DIALOG_LISTITEM items[] = {
126 {"fletcher4", "checksum algorithm: fletcher4",
127 "Use fletcher4 for data integrity checking. "
129 {"fletcher2", "checksum algorithm: fletcher2",
130 "Use fletcher2 for data integrity checking. "
131 "(not recommended)", 0 },
132 {"sha256", "checksum algorithm: sha256",
133 "Use sha256 for data integrity checking. "
134 "(not recommended)", 0 },
135 {"atime", "Update atimes for files",
136 "Disable atime update", 0 },
141 choice = dlg_checklist("ZFS Options", "", 0, 0, 0,
142 nitems(items), items, NULL,
144 if (choice == 1) /* Cancel */
148 strcpy(command, "zpool create -f -m none ");
149 if (getenv("BSDINSTALL_TMPBOOT") != NULL) {
150 char zfsboot_path[MAXPATHLEN];
151 snprintf(zfsboot_path, sizeof(zfsboot_path), "%s/zfs",
152 getenv("BSDINSTALL_TMPBOOT"));
153 mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |
155 sprintf(command, "%s -o cachefile=%s/zpool.cache ",
156 command, zfsboot_path);
158 for (i = 0; i < (int)nitems(items); i++) {
159 if (items[i].state == 0)
161 if (strcmp(items[i].name, "fletcher4") == 0)
162 strcat(command, "-O checksum=fletcher4 ");
163 else if (strcmp(items[i].name, "fletcher2") == 0)
164 strcat(command, "-O checksum=fletcher2 ");
165 else if (strcmp(items[i].name, "sha256") == 0)
166 strcat(command, "-O checksum=sha256 ");
167 else if (strcmp(items[i].name, "atime") == 0)
168 strcat(command, "-O atime=off ");
170 } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0) {
172 DIALOG_LISTITEM items[] = {
173 {"FAT32", "FAT Type 32",
174 "Create a FAT32 filesystem (default)", 1 },
175 {"FAT16", "FAT Type 16",
176 "Create a FAT16 filesystem", 0 },
177 {"FAT12", "FAT Type 12",
178 "Create a FAT12 filesystem", 0 },
183 choice = dlg_checklist("FAT Options", "", 0, 0, 0,
184 nitems(items), items, NULL,
186 if (choice == 1) /* Cancel */
190 strcpy(command, "newfs_msdos ");
191 for (i = 0; i < (int)nitems(items); i++) {
192 if (items[i].state == 0)
194 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 ");
201 dialog_msgbox("Error", "No configurable options exist "
202 "for this filesystem.", 0, 0, TRUE);
208 choose_part_type(const char *def_scheme)
211 const char *scheme = NULL;
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 },
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 nitems(items), items, &choice, NULL);
231 dialog_vars.default_item = NULL;
236 if (!is_scheme_bootable(items[choice].name)) {
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 */
248 scheme = items[choice].name;
254 gpart_partition(const char *lg_name, const char *scheme)
261 if (scheme == NULL) {
262 scheme = choose_part_type(default_scheme());
267 if (!is_scheme_bootable(scheme)) {
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 */
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");
290 errstr = gctl_issue(r);
291 if (errstr != NULL && errstr[0] != '\0') {
292 gpart_show_error("Error", NULL, errstr);
299 if (bootcode_path(scheme) != NULL)
300 get_part_metadata(lg_name, 1)->bootcode = 1;
305 gpart_activate(struct gprovider *pp)
309 const char *errstr, *scheme;
310 const char *attribute = NULL;
314 * Some partition schemes need this partition to be marked 'active'
315 * for it to be bootable.
317 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
318 if (strcmp(gc->lg_name, "scheme") == 0) {
324 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)
325 attribute = "active";
329 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
330 if (strcmp(gc->lg_name, "index") == 0) {
331 idx = atoi(gc->lg_val);
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);
343 errstr = gctl_issue(r);
344 if (errstr != NULL && errstr[0] != '\0')
345 gpart_show_error("Error", "Error marking partition active:",
351 gpart_set_root(const char *lg_name, const char *attribute)
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);
363 errstr = gctl_issue(r);
364 if (errstr != NULL && errstr[0] != '\0')
365 gpart_show_error("Error", "Error setting parameter on disk:",
371 gpart_bootcode(struct ggeom *gp)
373 const char *bootcode;
376 const char *errstr, *scheme;
378 size_t bootsize, bytes;
382 * Write default bootcode to the newly partitioned disk, if that
383 * applies on this platform.
385 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
386 if (strcmp(gc->lg_name, "scheme") == 0) {
392 bootcode = bootcode_path(scheme);
393 if (bootcode == NULL)
396 bootfd = open(bootcode, O_RDONLY);
398 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
403 bootsize = lseek(bootfd, 0, SEEK_END);
404 boot = malloc(bootsize);
405 lseek(bootfd, 0, SEEK_SET);
407 while (bytes < bootsize)
408 bytes += read(bootfd, boot + bytes, bootsize - bytes);
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);
417 errstr = gctl_issue(r);
418 if (errstr != NULL && errstr[0] != '\0')
419 gpart_show_error("Bootcode Error", NULL, errstr);
425 gpart_partcode(struct gprovider *pp, const char *fstype)
429 const char *indexstr;
430 char message[255], command[255];
432 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
433 if (strcmp(gc->lg_name, "scheme") == 0) {
439 /* Make sure this partition scheme needs partcode on this platform */
440 if (partcode_path(scheme, fstype) == NULL)
443 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
444 if (strcmp(gc->lg_name, "index") == 0) {
445 indexstr = gc->lg_val;
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",
456 dialog_msgbox("Error", message, 0, 0, TRUE);
461 gpart_destroy(struct ggeom *lg_geom)
464 struct gprovider *pp;
468 /* Delete all child metadata */
469 LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
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 */
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') {
490 * Check if we reverted away the existence of the geom
491 * altogether. Show all other errors to the user.
493 if (strtol(errstr, NULL, 0) != EINVAL)
494 gpart_show_error("Error", NULL, errstr);
498 /* And any metadata associated with the partition scheme itself */
499 delete_part_metadata(lg_geom->lg_name);
503 gpart_edit(struct gprovider *pp)
507 struct gconsumer *cp;
509 const char *errstr, *oldtype, *scheme;
510 struct partition_metadata *md;
514 int hadlabel, choice, junk, nitems;
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.",
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.
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 */
541 LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
542 if (strcmp(gc->lg_name, "scheme") == 0) {
547 if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
548 gpart_partition(cp->lg_geom->lg_name, NULL);
552 /* If this is a nested partition, edit as usual */
553 if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
556 /* Destroy the geom and all sub-partitions */
557 gpart_destroy(cp->lg_geom);
559 /* Now re-partition and return */
560 gpart_partition(cp->lg_geom->lg_name, NULL);
564 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
568 /* Disk not partitioned, so partition it */
569 gpart_partition(pp->lg_name, NULL);
573 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
574 if (strcmp(gc->lg_name, "scheme") == 0) {
580 nitems = scheme_supports_labels(scheme) ? 4 : 3;
582 /* Edit editable parameters of a partition */
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;
589 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
591 items[3].text = gc->lg_val;
593 if (strcmp(gc->lg_name, "index") == 0)
594 idx = atoi(gc->lg_val);
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;
605 humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
606 HN_NOSPACE | HN_DECIMAL);
607 items[1].text = sizestr;
610 choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
612 if (choice) /* Cancel pressed */
615 /* If this is the root partition, check that this fs is bootable */
616 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
619 sprintf(message, "This file system (%s) is not bootable "
620 "on this system. Are you sure you want to proceed?",
622 dialog_vars.defaultno = TRUE;
623 choice = dialog_yesno("Warning", message, 0, 0);
624 dialog_vars.defaultno = FALSE;
625 if (choice == 1) /* cancel */
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);
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);
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) ?
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,
663 gpart_partition(pp->lg_name, "BSD");
665 for (i = 0; i < nitems(items); i++)
666 if (items[i].text_free)
671 set_default_part_metadata(const char *name, const char *scheme,
672 const char *type, const char *mountpoint, const char *newfs)
674 struct partition_metadata *md;
675 char *zpool_name = NULL;
676 const char *default_bootmount = NULL;
679 /* Set part metadata */
680 md = get_part_metadata(name, 1);
683 if (md->newfs != NULL) {
688 if (newfs != NULL && newfs[0] != '\0') {
689 md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
690 strlen(mountpoint) + 5 + strlen(name) + 1);
691 if (strcmp("freebsd-zfs", type) == 0) {
692 zpool_name = strdup((strlen(mountpoint) == 1) ?
693 "root" : &mountpoint[1]);
694 for (i = 0; zpool_name[i] != 0; i++)
695 if (!isalnum(zpool_name[i]))
697 sprintf(md->newfs, "%s %s /dev/%s", newfs,
700 sprintf(md->newfs, "%s /dev/%s", newfs, name);
705 if (strcmp(type, "freebsd-swap") == 0)
707 if (strcmp(type, bootpart_type(scheme, &default_bootmount)) == 0) {
708 if (default_bootmount == NULL)
710 else if (mountpoint == NULL || strlen(mountpoint) == 0)
711 mountpoint = default_bootmount;
714 /* VTOC8 needs partcode at the start of partitions */
715 if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0
716 || strcmp(type, "freebsd-zfs") == 0))
719 if (mountpoint == NULL || mountpoint[0] == '\0') {
720 if (md->fstab != NULL) {
721 free(md->fstab->fs_spec);
722 free(md->fstab->fs_file);
723 free(md->fstab->fs_vfstype);
724 free(md->fstab->fs_mntops);
725 free(md->fstab->fs_type);
730 if (md->fstab == NULL) {
731 md->fstab = malloc(sizeof(struct fstab));
733 free(md->fstab->fs_spec);
734 free(md->fstab->fs_file);
735 free(md->fstab->fs_vfstype);
736 free(md->fstab->fs_mntops);
737 free(md->fstab->fs_type);
739 if (strcmp("freebsd-zfs", type) == 0) {
740 md->fstab->fs_spec = strdup(zpool_name);
742 md->fstab->fs_spec = malloc(strlen(name) +
743 strlen("/dev/") + 1);
744 sprintf(md->fstab->fs_spec, "/dev/%s", name);
746 md->fstab->fs_file = strdup(mountpoint);
747 /* Get VFS from text after freebsd-, if possible */
748 if (strncmp("freebsd-", type, 8) == 0)
749 md->fstab->fs_vfstype = strdup(&type[8]);
750 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0)
751 md->fstab->fs_vfstype = strdup("msdosfs");
753 md->fstab->fs_vfstype = strdup(type); /* Guess */
754 if (strcmp(type, "freebsd-swap") == 0) {
755 md->fstab->fs_type = strdup(FSTAB_SW);
756 md->fstab->fs_freq = 0;
757 md->fstab->fs_passno = 0;
758 } else if (strcmp(type, "freebsd-zfs") == 0) {
759 md->fstab->fs_type = strdup(FSTAB_RW);
760 md->fstab->fs_freq = 0;
761 md->fstab->fs_passno = 0;
763 md->fstab->fs_type = strdup(FSTAB_RW);
764 if (strcmp(mountpoint, "/") == 0) {
765 md->fstab->fs_freq = 1;
766 md->fstab->fs_passno = 1;
768 md->fstab->fs_freq = 2;
769 md->fstab->fs_passno = 2;
772 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
775 if (zpool_name != NULL)
780 int part_compare(const void *xa, const void *xb)
782 struct gprovider **a = (struct gprovider **)xa;
783 struct gprovider **b = (struct gprovider **)xb;
784 intmax_t astart, bstart;
788 LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
789 if (strcmp(gc->lg_name, "start") == 0) {
790 astart = strtoimax(gc->lg_val, NULL, 0);
793 LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
794 if (strcmp(gc->lg_name, "start") == 0) {
795 bstart = strtoimax(gc->lg_val, NULL, 0);
801 else if (astart > bstart)
808 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
811 struct gprovider *pp, **providers;
812 intmax_t sectorsize, stripesize, offset;
815 intmax_t maxsize, maxstart;
816 intmax_t partstart, partend;
819 /* Now get the maximum free size and free start */
821 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
822 if (strcmp(gc->lg_name, "first") == 0)
823 start = strtoimax(gc->lg_val, NULL, 0);
824 if (strcmp(gc->lg_name, "last") == 0)
825 end = strtoimax(gc->lg_val, NULL, 0);
829 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
831 providers = calloc(nparts, sizeof(providers[0]));
832 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
834 qsort(providers, nparts, sizeof(providers[0]), part_compare);
838 for (i = 0; i < nparts; i++) {
841 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
842 if (strcmp(gc->lg_name, "start") == 0)
843 partstart = strtoimax(gc->lg_val, NULL, 0);
844 if (strcmp(gc->lg_name, "end") == 0)
845 partend = strtoimax(gc->lg_val, NULL, 0);
848 if (partstart - lastend > maxsize) {
849 maxsize = partstart - lastend - 1;
850 maxstart = lastend + 1;
856 if (end - lastend > maxsize) {
857 maxsize = end - lastend - 1;
858 maxstart = lastend + 1;
861 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
864 * Round the start and size of the largest available space up to
865 * the nearest multiple of the adjusted stripe size.
867 * The adjusted stripe size is the least common multiple of the
868 * actual stripe size, or the sector size if no stripe size was
869 * reported, and 4096. The reason for this is that contemporary
870 * disks often have 4096-byte physical sectors but report 512
871 * bytes instead for compatibility with older / broken operating
872 * systems and BIOSes. For the same reasons, virtualized storage
873 * may also report a 512-byte stripe size, or none at all.
875 sectorsize = pp->lg_sectorsize;
876 if ((stripesize = pp->lg_stripesize) == 0)
877 stripesize = sectorsize;
878 while (stripesize % 4096 != 0)
880 if ((offset = maxstart * sectorsize % stripesize) != 0) {
881 offset = (stripesize - offset) / sectorsize;
886 if (npartstart != NULL)
887 *npartstart = maxstart;
893 add_boot_partition(struct ggeom *geom, struct gprovider *pp,
894 const char *scheme, int interactive)
897 struct gprovider *ppi;
900 /* Check for existing freebsd-boot partition */
901 LIST_FOREACH(ppi, &geom->lg_provider, lg_provider) {
902 struct partition_metadata *md;
903 const char *bootmount = NULL;
905 LIST_FOREACH(gc, &ppi->lg_config, lg_config)
906 if (strcmp(gc->lg_name, "type") == 0)
910 if (strcmp(gc->lg_val, bootpart_type(scheme, &bootmount)) != 0)
914 * If the boot partition is not mountable and needs partcode,
915 * but doesn't have it, it doesn't satisfy our requirements.
917 md = get_part_metadata(ppi->lg_name, 0);
918 if (bootmount == NULL && (md == NULL || !md->bootcode))
921 /* If it is mountable, but mounted somewhere else, remount */
922 if (bootmount != NULL && md != NULL && md->fstab != NULL
923 && strlen(md->fstab->fs_file) > 0
924 && strcmp(md->fstab->fs_file, bootmount) != 0)
927 /* If it is mountable, but mountpoint is not set, mount it */
928 if (bootmount != NULL && md == NULL)
929 set_default_part_metadata(ppi->lg_name, scheme,
930 gc->lg_val, bootmount, NULL);
932 /* Looks good at this point, no added data needed */
937 choice = dialog_yesno("Boot Partition",
938 "This partition scheme requires a boot partition "
939 "for the disk to be bootable. Would you like to "
940 "make one now?", 0, 0);
944 if (choice == 0) { /* yes */
945 const char *bootmount = NULL;
948 humanize_number(sizestr, 7,
949 bootpart_size(scheme), "B", HN_AUTOSCALE,
950 HN_NOSPACE | HN_DECIMAL);
952 gpart_create(pp, bootpart_type(scheme, &bootmount),
953 sizestr, bootmount, NULL, 0);
955 return (bootpart_size(scheme));
962 gpart_create(struct gprovider *pp, const char *default_type,
963 const char *default_size, const char *default_mountpoint,
964 char **partname, int interactive)
968 struct gconsumer *cp;
970 const char *errstr, *scheme;
971 char sizestr[32], startstr[32], output[64], *newpartname;
972 char newfs[255], options_fstype[64];
973 intmax_t maxsize, size, sector, firstfree, stripe;
975 int nitems, choice, junk;
978 DIALOG_FORMITEM items[] = {
979 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
980 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
981 "freebsd-swap)", FALSE},
982 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
983 FALSE, "Partition size. Append K, M, G for kilobytes, "
984 "megabytes or gigabytes.", FALSE},
985 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
986 FALSE, "Path at which to mount partition (blank for "
987 "swap, / for root filesystem)", FALSE},
988 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
989 "Partition name. Not all partition schemes support this.",
993 if (partname != NULL)
996 /* Record sector and stripe sizes */
997 sector = pp->lg_sectorsize;
998 stripe = pp->lg_stripesize;
1001 * Find the PART geom we are manipulating. This may be a consumer of
1002 * this provider, or its parent. Check the consumer case first.
1005 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1006 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1011 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
1014 /* Now get the partition scheme */
1017 LIST_FOREACH(gc, &geom->lg_config, lg_config)
1018 if (strcmp(gc->lg_name, "scheme") == 0)
1019 scheme = gc->lg_val;
1022 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
1023 if (gpart_partition(pp->lg_name, NULL) == 0)
1025 "The partition table has been successfully created."
1026 " Please press Create again to create partitions.",
1033 * If we still don't have a geom, either the user has
1034 * canceled partitioning or there has been an error which has already
1035 * been displayed, so bail.
1040 maxsize = size = gpart_max_free(geom, &firstfree);
1042 dialog_msgbox("Error", "No free space left on device.", 0, 0,
1047 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
1048 HN_NOSPACE | HN_DECIMAL);
1049 items[1].text = sizestr;
1051 /* Special-case the MBR default type for nested partitions */
1052 if (strcmp(scheme, "MBR") == 0) {
1053 items[0].text = "freebsd";
1054 items[0].help = "Filesystem type (e.g. freebsd, fat32)";
1057 nitems = scheme_supports_labels(scheme) ? 4 : 3;
1059 if (default_type != NULL)
1060 items[0].text = (char *)default_type;
1061 if (default_size != NULL)
1062 items[1].text = (char *)default_size;
1063 if (default_mountpoint != NULL)
1064 items[2].text = (char *)default_mountpoint;
1066 /* Default options */
1067 strncpy(options_fstype, items[0].text,
1068 sizeof(options_fstype));
1069 newfs_command(options_fstype, newfs, 1);
1072 dialog_vars.extra_label = "Options";
1073 dialog_vars.extra_button = TRUE;
1074 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
1076 dialog_vars.extra_button = FALSE;
1080 case 1: /* Cancel */
1082 case 3: /* Options */
1083 strncpy(options_fstype, items[0].text,
1084 sizeof(options_fstype));
1085 newfs_command(options_fstype, newfs, 0);
1091 * If the user changed the fs type after specifying options, undo
1092 * their choices in favor of the new filesystem's defaults.
1094 if (strcmp(options_fstype, items[0].text) != 0) {
1095 strncpy(options_fstype, items[0].text, sizeof(options_fstype));
1096 newfs_command(options_fstype, newfs, 1);
1100 if (strlen(items[1].text) > 0) {
1101 if (expand_number(items[1].text, &bytes) != 0) {
1104 sprintf(error, "Invalid size: %s\n", strerror(errno));
1105 dialog_msgbox("Error", error, 0, 0, TRUE);
1108 size = MIN((intmax_t)(bytes/sector), maxsize);
1111 /* Check if the label has a / in it */
1112 if (strchr(items[3].text, '/') != NULL) {
1113 dialog_msgbox("Error", "Label contains a /, which is not an "
1114 "allowed character.", 0, 0, TRUE);
1118 /* Warn if no mountpoint set */
1119 if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1120 items[2].text[0] != '/') {
1123 dialog_vars.defaultno = TRUE;
1124 choice = dialog_yesno("Warning",
1125 "This partition does not have a valid mountpoint "
1126 "(for the partition from which you intend to boot the "
1127 "operating system, the mountpoint should be /). Are you "
1128 "sure you want to continue?"
1130 dialog_vars.defaultno = FALSE;
1132 if (choice == 1) /* cancel */
1137 * Error if this scheme needs nested partitions, this is one, and
1138 * a mountpoint was set.
1140 if (strcmp(items[0].text, "freebsd") == 0 &&
1141 strlen(items[2].text) > 0) {
1142 dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1143 "nested BSD-type partition schemes and cannot have "
1144 "mountpoints. After creating one, select it and press "
1145 "Create again to add the actual file systems.", 0, 0, TRUE);
1149 /* If this is the root partition, check that this scheme is bootable */
1150 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1152 sprintf(message, "This partition scheme (%s) is not bootable "
1153 "on this platform. Are you sure you want to proceed?",
1155 dialog_vars.defaultno = TRUE;
1156 choice = dialog_yesno("Warning", message, 0, 0);
1157 dialog_vars.defaultno = FALSE;
1158 if (choice == 1) /* cancel */
1162 /* If this is the root partition, check that this fs is bootable */
1163 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1166 sprintf(message, "This file system (%s) is not bootable "
1167 "on this system. Are you sure you want to proceed?",
1169 dialog_vars.defaultno = TRUE;
1170 choice = dialog_yesno("Warning", message, 0, 0);
1171 dialog_vars.defaultno = FALSE;
1172 if (choice == 1) /* cancel */
1177 * If this is the root partition, and we need a boot partition, ask
1178 * the user to add one.
1181 if ((strcmp(items[0].text, "freebsd") == 0 ||
1182 strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0) {
1183 size_t bytes = add_boot_partition(geom, pp, scheme,
1186 /* Now adjust the part we are really adding forward */
1188 firstfree += bytes / sector;
1189 size -= (bytes + stripe)/sector;
1190 if (stripe > 0 && (firstfree*sector % stripe) != 0)
1191 firstfree += (stripe - ((firstfree*sector) %
1196 r = gctl_get_handle();
1197 gctl_ro_param(r, "class", -1, "PART");
1198 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1199 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1200 gctl_ro_param(r, "verb", -1, "add");
1202 gctl_ro_param(r, "type", -1, items[0].text);
1203 snprintf(sizestr, sizeof(sizestr), "%jd", size);
1204 gctl_ro_param(r, "size", -1, sizestr);
1205 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1206 gctl_ro_param(r, "start", -1, startstr);
1207 if (items[3].text[0] != '\0')
1208 gctl_ro_param(r, "label", -1, items[3].text);
1209 gctl_rw_param(r, "output", sizeof(output), output);
1210 errstr = gctl_issue(r);
1211 if (errstr != NULL && errstr[0] != '\0') {
1212 gpart_show_error("Error", NULL, errstr);
1216 newpartname = strtok(output, " ");
1220 * Try to destroy any geom that gpart picked up already here from
1223 r = gctl_get_handle();
1224 gctl_ro_param(r, "class", -1, "PART");
1225 gctl_ro_param(r, "arg0", -1, newpartname);
1226 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1228 gctl_ro_param(r, "force", sizeof(junk), &junk);
1229 gctl_ro_param(r, "verb", -1, "destroy");
1230 gctl_issue(r); /* Error usually expected and non-fatal */
1234 if (strcmp(items[0].text, "freebsd") == 0)
1235 gpart_partition(newpartname, "BSD");
1237 set_default_part_metadata(newpartname, scheme,
1238 items[0].text, items[2].text, newfs);
1240 for (i = 0; i < nitems(items); i++)
1241 if (items[i].text_free)
1242 free(items[i].text);
1244 if (partname != NULL)
1245 *partname = strdup(newpartname);
1249 gpart_delete(struct gprovider *pp)
1253 struct gconsumer *cp;
1259 /* Is it a partition? */
1260 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1262 /* Find out if this is the root of a gpart geom */
1264 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1265 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1270 /* If so, destroy all children */
1272 gpart_destroy(geom);
1274 /* If this is a partition, revert it, so it can be deleted */
1276 r = gctl_get_handle();
1277 gctl_ro_param(r, "class", -1, "PART");
1278 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1279 gctl_ro_param(r, "verb", -1, "undo");
1280 gctl_issue(r); /* Ignore non-fatal errors */
1286 * If this is not a partition, see if that is a problem, complain if
1287 * necessary, and return always, since we need not do anything further,
1290 if (!is_partition) {
1292 dialog_msgbox("Error",
1293 "Only partitions can be deleted.", 0, 0, TRUE);
1297 r = gctl_get_handle();
1298 gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1299 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1300 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1301 gctl_ro_param(r, "verb", -1, "delete");
1303 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1304 if (strcmp(gc->lg_name, "index") == 0) {
1305 idx = atoi(gc->lg_val);
1306 gctl_ro_param(r, "index", sizeof(idx), &idx);
1311 errstr = gctl_issue(r);
1312 if (errstr != NULL && errstr[0] != '\0') {
1313 gpart_show_error("Error", NULL, errstr);
1320 delete_part_metadata(pp->lg_name);
1324 gpart_revert_all(struct gmesh *mesh)
1326 struct gclass *classp;
1330 const char *modified;
1332 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1333 if (strcmp(classp->lg_name, "PART") == 0)
1337 if (strcmp(classp->lg_name, "PART") != 0) {
1338 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1342 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1343 modified = "true"; /* XXX: If we don't know (kernel too old),
1344 * assume there are modifications. */
1345 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1346 if (strcmp(gc->lg_name, "modified") == 0) {
1347 modified = gc->lg_val;
1352 if (strcmp(modified, "false") == 0)
1355 r = gctl_get_handle();
1356 gctl_ro_param(r, "class", -1, "PART");
1357 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1358 gctl_ro_param(r, "verb", -1, "undo");
1366 gpart_commit(struct gmesh *mesh)
1368 struct partition_metadata *md;
1369 struct gclass *classp;
1372 struct gconsumer *cp;
1373 struct gprovider *pp;
1376 const char *modified;
1379 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1380 if (strcmp(classp->lg_name, "PART") == 0)
1384 /* Figure out what filesystem / uses */
1385 rootfs = "ufs"; /* Assume ufs if nothing else present */
1386 TAILQ_FOREACH(md, &part_metadata, metadata) {
1387 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
1388 rootfs = md->fstab->fs_vfstype;
1393 if (strcmp(classp->lg_name, "PART") != 0) {
1394 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1398 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1399 modified = "true"; /* XXX: If we don't know (kernel too old),
1400 * assume there are modifications. */
1401 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1402 if (strcmp(gc->lg_name, "modified") == 0) {
1403 modified = gc->lg_val;
1408 if (strcmp(modified, "false") == 0)
1411 /* Add bootcode if necessary, before the commit */
1412 md = get_part_metadata(gp->lg_name, 0);
1413 if (md != NULL && md->bootcode)
1416 /* Now install partcode on its partitions, if necessary */
1417 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1418 md = get_part_metadata(pp->lg_name, 0);
1419 if (md == NULL || !md->bootcode)
1422 /* Mark this partition active if that's required */
1425 /* Check if the partition has sub-partitions */
1426 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1427 if (strcmp(cp->lg_geom->lg_class->lg_name,
1431 if (cp == NULL) /* No sub-partitions */
1432 gpart_partcode(pp, rootfs);
1435 r = gctl_get_handle();
1436 gctl_ro_param(r, "class", -1, "PART");
1437 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1438 gctl_ro_param(r, "verb", -1, "commit");
1440 errstr = gctl_issue(r);
1441 if (errstr != NULL && errstr[0] != '\0')
1442 gpart_show_error("Error", NULL, errstr);