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>
44 #define GPART_FLAGS "x" /* Do not commit changes by default */
47 gpart_show_error(const char *title, const char *explanation, const char *errstr)
53 if (explanation == NULL)
56 error = strtol(errstr, &errmsg, 0);
57 if (errmsg != errstr) {
58 while (errmsg[0] == ' ')
60 if (errmsg[0] != '\0')
61 sprintf(message, "%s%s. %s", explanation,
62 strerror(error), errmsg);
64 sprintf(message, "%s%s", explanation, strerror(error));
66 sprintf(message, "%s%s", explanation, errmsg);
69 dialog_msgbox(title, message, 0, 0, TRUE);
73 scheme_supports_labels(const char *scheme)
75 if (strcmp(scheme, "APM") == 0)
77 if (strcmp(scheme, "GPT") == 0)
84 newfs_command(const char *fstype, char *command, int use_default)
86 if (strcmp(fstype, "freebsd-ufs") == 0) {
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 },
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",
104 choice = dlg_checklist("UFS Options", "", 0, 0, 0,
105 nitems(items), items, NULL,
107 if (choice == 1) /* Cancel */
111 strcpy(command, "newfs ");
112 for (i = 0; i < (int)nitems(items); i++) {
113 if (items[i].state == 0)
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 ");
124 } else if (strcmp(fstype, "freebsd-zfs") == 0) {
126 DIALOG_LISTITEM items[] = {
127 {"fletcher4", "checksum algorithm: fletcher4",
128 "Use fletcher4 for data integrity checking. "
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 },
142 choice = dlg_checklist("ZFS Options", "", 0, 0, 0,
143 nitems(items), items, NULL,
145 if (choice == 1) /* Cancel */
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 |
156 sprintf(command, "%s -o cachefile=%s/zpool.cache ",
157 command, zfsboot_path);
159 for (i = 0; i < (int)nitems(items); i++) {
160 if (items[i].state == 0)
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 ");
171 } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0 ||
172 strcmp(fstype, "ms-basic-data") == 0) {
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 },
185 choice = dlg_checklist("FAT Options", "", 0, 0, 0,
186 nitems(items), items, NULL,
188 if (choice == 1) /* Cancel */
192 strcpy(command, "newfs_msdos ");
193 for (i = 0; i < (int)nitems(items); i++) {
194 if (items[i].state == 0)
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 ");
205 dialog_msgbox("Error", "No configurable options exist "
206 "for this filesystem.", 0, 0, TRUE);
212 choose_part_type(const char *def_scheme)
215 const char *scheme = NULL;
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 },
229 dialog_vars.default_item = __DECONST(char *, def_scheme);
230 cancel = dlg_menu("Partition Scheme",
231 "Select a partition scheme for this volume:", 0, 0, 0,
232 nitems(items), items, &choice, NULL);
233 dialog_vars.default_item = NULL;
238 if (!is_scheme_bootable(items[choice].name)) {
240 sprintf(message, "This partition scheme (%s) is not "
241 "bootable on this platform. Are you sure you want "
242 "to proceed?", items[choice].name);
243 dialog_vars.defaultno = TRUE;
244 cancel = dialog_yesno("Warning", message, 0, 0);
245 dialog_vars.defaultno = FALSE;
246 if (cancel) /* cancel */
250 scheme = items[choice].name;
256 gpart_partition(const char *lg_name, const char *scheme)
263 if (scheme == NULL) {
264 scheme = choose_part_type(default_scheme());
269 if (!is_scheme_bootable(scheme)) {
271 sprintf(message, "This partition scheme (%s) is not "
272 "bootable on this platform. Are you sure you want "
273 "to proceed?", scheme);
274 dialog_vars.defaultno = TRUE;
275 cancel = dialog_yesno("Warning", message, 0, 0);
276 dialog_vars.defaultno = FALSE;
277 if (cancel) { /* cancel */
278 /* Reset scheme so user can choose another */
285 r = gctl_get_handle();
286 gctl_ro_param(r, "class", -1, "PART");
287 gctl_ro_param(r, "arg0", -1, lg_name);
288 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
289 gctl_ro_param(r, "scheme", -1, scheme);
290 gctl_ro_param(r, "verb", -1, "create");
292 errstr = gctl_issue(r);
293 if (errstr != NULL && errstr[0] != '\0') {
294 gpart_show_error("Error", NULL, errstr);
301 if (bootcode_path(scheme) != NULL)
302 get_part_metadata(lg_name, 1)->bootcode = 1;
307 gpart_activate(struct gprovider *pp)
311 const char *errstr, *scheme;
312 const char *attribute = NULL;
316 * Some partition schemes need this partition to be marked 'active'
317 * for it to be bootable.
319 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
320 if (strcmp(gc->lg_name, "scheme") == 0) {
326 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)
327 attribute = "active";
331 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
332 if (strcmp(gc->lg_name, "index") == 0) {
333 idx = atoi(gc->lg_val);
338 r = gctl_get_handle();
339 gctl_ro_param(r, "class", -1, "PART");
340 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
341 gctl_ro_param(r, "verb", -1, "set");
342 gctl_ro_param(r, "attrib", -1, attribute);
343 gctl_ro_param(r, "index", sizeof(idx), &idx);
345 errstr = gctl_issue(r);
346 if (errstr != NULL && errstr[0] != '\0')
347 gpart_show_error("Error", "Error marking partition active:",
353 gpart_set_root(const char *lg_name, const char *attribute)
358 r = gctl_get_handle();
359 gctl_ro_param(r, "class", -1, "PART");
360 gctl_ro_param(r, "arg0", -1, lg_name);
361 gctl_ro_param(r, "flags", -1, "C");
362 gctl_ro_param(r, "verb", -1, "set");
363 gctl_ro_param(r, "attrib", -1, attribute);
365 errstr = gctl_issue(r);
366 if (errstr != NULL && errstr[0] != '\0')
367 gpart_show_error("Error", "Error setting parameter on disk:",
373 gpart_bootcode(struct ggeom *gp)
375 const char *bootcode;
378 const char *errstr, *scheme;
380 size_t bootsize, bytes;
384 * Write default bootcode to the newly partitioned disk, if that
385 * applies on this platform.
387 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
388 if (strcmp(gc->lg_name, "scheme") == 0) {
394 bootcode = bootcode_path(scheme);
395 if (bootcode == NULL)
398 bootfd = open(bootcode, O_RDONLY);
400 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
405 bootsize = lseek(bootfd, 0, SEEK_END);
406 boot = malloc(bootsize);
407 lseek(bootfd, 0, SEEK_SET);
409 while (bytes < bootsize)
410 bytes += read(bootfd, boot + bytes, bootsize - bytes);
413 r = gctl_get_handle();
414 gctl_ro_param(r, "class", -1, "PART");
415 gctl_ro_param(r, "arg0", -1, gp->lg_name);
416 gctl_ro_param(r, "verb", -1, "bootcode");
417 gctl_ro_param(r, "bootcode", bootsize, boot);
419 errstr = gctl_issue(r);
420 if (errstr != NULL && errstr[0] != '\0')
421 gpart_show_error("Bootcode Error", NULL, errstr);
427 gpart_partcode(struct gprovider *pp, const char *fstype)
431 const char *indexstr;
432 char message[255], command[255];
434 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
435 if (strcmp(gc->lg_name, "scheme") == 0) {
441 /* Make sure this partition scheme needs partcode on this platform */
442 if (partcode_path(scheme, fstype) == NULL)
445 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
446 if (strcmp(gc->lg_name, "index") == 0) {
447 indexstr = gc->lg_val;
452 /* Shell out to gpart for partcode for now */
453 sprintf(command, "gpart bootcode -p %s -i %s %s",
454 partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);
455 if (system(command) != 0) {
456 sprintf(message, "Error installing partcode on partition %s",
458 dialog_msgbox("Error", message, 0, 0, TRUE);
463 gpart_destroy(struct ggeom *lg_geom)
466 struct gprovider *pp;
470 /* Delete all child metadata */
471 LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
474 /* Revert any local changes to get this geom into a pristine state */
475 r = gctl_get_handle();
476 gctl_ro_param(r, "class", -1, "PART");
477 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
478 gctl_ro_param(r, "verb", -1, "undo");
479 gctl_issue(r); /* Ignore errors -- these are non-fatal */
482 /* Now destroy the geom itself */
483 r = gctl_get_handle();
484 gctl_ro_param(r, "class", -1, "PART");
485 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
486 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
487 gctl_ro_param(r, "force", sizeof(force), &force);
488 gctl_ro_param(r, "verb", -1, "destroy");
489 errstr = gctl_issue(r);
490 if (errstr != NULL && errstr[0] != '\0') {
492 * Check if we reverted away the existence of the geom
493 * altogether. Show all other errors to the user.
495 if (strtol(errstr, NULL, 0) != EINVAL)
496 gpart_show_error("Error", NULL, errstr);
500 /* And any metadata associated with the partition scheme itself */
501 delete_part_metadata(lg_geom->lg_name);
505 gpart_edit(struct gprovider *pp)
509 struct gconsumer *cp;
511 const char *errstr, *oldtype, *scheme;
512 struct partition_metadata *md;
516 int hadlabel, choice, junk, nitems;
519 DIALOG_FORMITEM items[] = {
520 {0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0,
521 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
522 "freebsd-swap)", FALSE},
523 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0,
524 FALSE, "Partition size. Append K, M, G for kilobytes, "
525 "megabytes or gigabytes.", FALSE},
526 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
527 FALSE, "Path at which to mount this partition (leave blank "
528 "for swap, set to / for root filesystem)", FALSE},
529 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
530 "Partition name. Not all partition schemes support this.",
535 * Find the PART geom we are manipulating. This may be a consumer of
536 * this provider, or its parent. Check the consumer case first.
539 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
540 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
541 /* Check for zombie geoms, treating them as blank */
543 LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
544 if (strcmp(gc->lg_name, "scheme") == 0) {
549 if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
550 gpart_partition(cp->lg_geom->lg_name, NULL);
554 /* If this is a nested partition, edit as usual */
555 if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
558 /* Destroy the geom and all sub-partitions */
559 gpart_destroy(cp->lg_geom);
561 /* Now re-partition and return */
562 gpart_partition(cp->lg_geom->lg_name, NULL);
566 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
570 /* Disk not partitioned, so partition it */
571 gpart_partition(pp->lg_name, NULL);
575 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
576 if (strcmp(gc->lg_name, "scheme") == 0) {
582 nitems = scheme_supports_labels(scheme) ? 4 : 3;
584 /* Edit editable parameters of a partition */
586 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
587 if (strcmp(gc->lg_name, "type") == 0) {
588 oldtype = gc->lg_val;
589 items[0].text = gc->lg_val;
591 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
593 items[3].text = gc->lg_val;
595 if (strcmp(gc->lg_name, "index") == 0)
596 idx = atoi(gc->lg_val);
599 TAILQ_FOREACH(md, &part_metadata, metadata) {
600 if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
601 if (md->fstab != NULL)
602 items[2].text = md->fstab->fs_file;
607 humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
608 HN_NOSPACE | HN_DECIMAL);
609 items[1].text = sizestr;
612 choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
614 if (choice) /* Cancel pressed */
617 /* If this is the root partition, check that this fs is bootable */
618 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
621 sprintf(message, "This file system (%s) is not bootable "
622 "on this system. Are you sure you want to proceed?",
624 dialog_vars.defaultno = TRUE;
625 choice = dialog_yesno("Warning", message, 0, 0);
626 dialog_vars.defaultno = FALSE;
627 if (choice == 1) /* cancel */
631 /* Check if the label has a / in it */
632 if (strchr(items[3].text, '/') != NULL) {
633 dialog_msgbox("Error", "Label contains a /, which is not an "
634 "allowed character.", 0, 0, TRUE);
638 r = gctl_get_handle();
639 gctl_ro_param(r, "class", -1, "PART");
640 gctl_ro_param(r, "arg0", -1, geom->lg_name);
641 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
642 gctl_ro_param(r, "verb", -1, "modify");
643 gctl_ro_param(r, "index", sizeof(idx), &idx);
644 if (hadlabel || items[3].text[0] != '\0')
645 gctl_ro_param(r, "label", -1, items[3].text);
646 gctl_ro_param(r, "type", -1, items[0].text);
647 errstr = gctl_issue(r);
648 if (errstr != NULL && errstr[0] != '\0') {
649 gpart_show_error("Error", NULL, errstr);
655 newfs_command(items[0].text, newfs, 1);
656 set_default_part_metadata(pp->lg_name, scheme, items[0].text,
657 items[2].text, (strcmp(oldtype, items[0].text) != 0) ?
661 if (strcmp(oldtype, items[0].text) != 0 && cp != NULL)
662 gpart_destroy(cp->lg_geom);
663 if (strcmp(oldtype, items[0].text) != 0 && strcmp(items[0].text,
665 gpart_partition(pp->lg_name, "BSD");
667 for (i = 0; i < nitems(items); i++)
668 if (items[i].text_free)
673 set_default_part_metadata(const char *name, const char *scheme,
674 const char *type, const char *mountpoint, const char *newfs)
676 struct partition_metadata *md;
677 char *zpool_name = NULL;
678 const char *default_bootmount = NULL;
681 /* Set part metadata */
682 md = get_part_metadata(name, 1);
685 if (md->newfs != NULL) {
690 if (newfs != NULL && newfs[0] != '\0') {
691 md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
692 strlen(mountpoint) + 5 + strlen(name) + 1);
693 if (strcmp("freebsd-zfs", type) == 0) {
694 zpool_name = strdup((strlen(mountpoint) == 1) ?
695 "root" : &mountpoint[1]);
696 for (i = 0; zpool_name[i] != 0; i++)
697 if (!isalnum(zpool_name[i]))
699 sprintf(md->newfs, "%s %s /dev/%s", newfs,
702 sprintf(md->newfs, "%s /dev/%s", newfs, name);
707 if (strcmp(type, "freebsd-swap") == 0)
709 if (strcmp(type, bootpart_type(scheme, &default_bootmount)) == 0) {
710 if (default_bootmount == NULL) {
712 int fd = openat(tmpdfd, "bsdinstall-esps",
713 O_CREAT | O_WRONLY | O_APPEND, 0600);
715 write(fd, md->name, strlen(md->name));
722 else if (mountpoint == NULL || strlen(mountpoint) == 0)
723 mountpoint = default_bootmount;
726 if (mountpoint == NULL || mountpoint[0] == '\0') {
727 if (md->fstab != NULL) {
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);
737 if (md->fstab == NULL) {
738 md->fstab = malloc(sizeof(struct fstab));
740 free(md->fstab->fs_spec);
741 free(md->fstab->fs_file);
742 free(md->fstab->fs_vfstype);
743 free(md->fstab->fs_mntops);
744 free(md->fstab->fs_type);
746 if (strcmp("freebsd-zfs", type) == 0) {
747 md->fstab->fs_spec = strdup(zpool_name);
749 md->fstab->fs_spec = malloc(strlen(name) +
750 strlen("/dev/") + 1);
751 sprintf(md->fstab->fs_spec, "/dev/%s", name);
753 md->fstab->fs_file = strdup(mountpoint);
754 /* Get VFS from text after freebsd-, if possible */
755 if (strncmp("freebsd-", type, 8) == 0)
756 md->fstab->fs_vfstype = strdup(&type[8]);
757 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0
758 || strcmp("ms-basic-data", type) == 0)
759 md->fstab->fs_vfstype = strdup("msdosfs");
761 md->fstab->fs_vfstype = strdup(type); /* Guess */
762 if (strcmp(type, "freebsd-swap") == 0) {
763 md->fstab->fs_type = strdup(FSTAB_SW);
764 md->fstab->fs_freq = 0;
765 md->fstab->fs_passno = 0;
766 } else if (strcmp(type, "freebsd-zfs") == 0) {
767 md->fstab->fs_type = strdup(FSTAB_RW);
768 md->fstab->fs_freq = 0;
769 md->fstab->fs_passno = 0;
771 md->fstab->fs_type = strdup(FSTAB_RW);
772 if (strcmp(mountpoint, "/") == 0) {
773 md->fstab->fs_freq = 1;
774 md->fstab->fs_passno = 1;
776 md->fstab->fs_freq = 2;
777 md->fstab->fs_passno = 2;
780 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
783 if (zpool_name != NULL)
788 int part_compare(const void *xa, const void *xb)
790 struct gprovider **a = (struct gprovider **)xa;
791 struct gprovider **b = (struct gprovider **)xb;
792 intmax_t astart, bstart;
796 LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
797 if (strcmp(gc->lg_name, "start") == 0) {
798 astart = strtoimax(gc->lg_val, NULL, 0);
801 LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
802 if (strcmp(gc->lg_name, "start") == 0) {
803 bstart = strtoimax(gc->lg_val, NULL, 0);
809 else if (astart > bstart)
816 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
819 struct gprovider *pp, **providers;
820 intmax_t sectorsize, stripesize, offset;
823 intmax_t maxsize, maxstart;
824 intmax_t partstart, partend;
827 /* Now get the maximum free size and free start */
829 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
830 if (strcmp(gc->lg_name, "first") == 0)
831 start = strtoimax(gc->lg_val, NULL, 0);
832 if (strcmp(gc->lg_name, "last") == 0)
833 end = strtoimax(gc->lg_val, NULL, 0);
837 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
839 providers = calloc(nparts, sizeof(providers[0]));
840 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
842 qsort(providers, nparts, sizeof(providers[0]), part_compare);
846 for (i = 0; i < nparts; i++) {
849 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
850 if (strcmp(gc->lg_name, "start") == 0)
851 partstart = strtoimax(gc->lg_val, NULL, 0);
852 if (strcmp(gc->lg_name, "end") == 0)
853 partend = strtoimax(gc->lg_val, NULL, 0);
856 if (partstart - lastend > maxsize) {
857 maxsize = partstart - lastend - 1;
858 maxstart = lastend + 1;
864 if (end - lastend > maxsize) {
865 maxsize = end - lastend;
866 maxstart = lastend + 1;
869 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
872 * Round the start and size of the largest available space up to
873 * the nearest multiple of the adjusted stripe size.
875 * The adjusted stripe size is the least common multiple of the
876 * actual stripe size, or the sector size if no stripe size was
877 * reported, and 4096. The reason for this is that contemporary
878 * disks often have 4096-byte physical sectors but report 512
879 * bytes instead for compatibility with older / broken operating
880 * systems and BIOSes. For the same reasons, virtualized storage
881 * may also report a 512-byte stripe size, or none at all.
883 sectorsize = pp->lg_sectorsize;
884 if ((stripesize = pp->lg_stripesize) == 0)
885 stripesize = sectorsize;
886 while (stripesize % 4096 != 0)
888 if ((offset = maxstart * sectorsize % stripesize) != 0) {
889 offset = (stripesize - offset) / sectorsize;
894 if (npartstart != NULL)
895 *npartstart = maxstart;
901 add_boot_partition(struct ggeom *geom, struct gprovider *pp,
902 const char *scheme, int interactive)
905 struct gprovider *ppi;
908 /* Check for existing freebsd-boot partition */
909 LIST_FOREACH(ppi, &geom->lg_provider, lg_provider) {
910 struct partition_metadata *md;
911 const char *bootmount = NULL;
913 LIST_FOREACH(gc, &ppi->lg_config, lg_config)
914 if (strcmp(gc->lg_name, "type") == 0)
918 if (strcmp(gc->lg_val, bootpart_type(scheme, &bootmount)) != 0)
922 * If the boot partition is not mountable and needs partcode,
923 * but doesn't have it, it doesn't satisfy our requirements.
925 md = get_part_metadata(ppi->lg_name, 0);
926 if (bootmount == NULL && (md == NULL || !md->bootcode))
929 /* If it is mountable, but mounted somewhere else, remount */
930 if (bootmount != NULL && md != NULL && md->fstab != NULL
931 && strlen(md->fstab->fs_file) > 0
932 && strcmp(md->fstab->fs_file, bootmount) != 0)
935 /* If it is mountable, but mountpoint is not set, mount it */
936 if (bootmount != NULL && md == NULL)
937 set_default_part_metadata(ppi->lg_name, scheme,
938 gc->lg_val, bootmount, NULL);
940 /* Looks good at this point, no added data needed */
945 choice = dialog_yesno("Boot Partition",
946 "This partition scheme requires a boot partition "
947 "for the disk to be bootable. Would you like to "
948 "make one now?", 0, 0);
952 if (choice == 0) { /* yes */
953 struct partition_metadata *md;
954 const char *bootmount = NULL;
955 char *bootpartname = NULL;
958 humanize_number(sizestr, 7,
959 bootpart_size(scheme), "B", HN_AUTOSCALE,
960 HN_NOSPACE | HN_DECIMAL);
962 gpart_create(pp, bootpart_type(scheme, &bootmount),
963 sizestr, bootmount, &bootpartname, 0);
965 if (bootpartname == NULL) /* Error reported to user already */
968 /* If the part is not mountable, make sure newfs isn't set */
969 if (bootmount == NULL) {
970 md = get_part_metadata(bootpartname, 0);
971 if (md != NULL && md->newfs != NULL) {
979 return (bootpart_size(scheme));
986 gpart_create(struct gprovider *pp, const char *default_type,
987 const char *default_size, const char *default_mountpoint,
988 char **partname, int interactive)
992 struct gconsumer *cp;
994 const char *errstr, *scheme;
995 char sizestr[32], startstr[32], output[64], *newpartname;
996 char newfs[255], options_fstype[64];
997 intmax_t maxsize, size, sector, firstfree, stripe;
999 int nitems, choice, junk;
1002 DIALOG_FORMITEM items[] = {
1003 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
1004 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
1005 "freebsd-swap)", FALSE},
1006 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
1007 FALSE, "Partition size. Append K, M, G for kilobytes, "
1008 "megabytes or gigabytes.", FALSE},
1009 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
1010 FALSE, "Path at which to mount partition (blank for "
1011 "swap, / for root filesystem)", FALSE},
1012 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
1013 "Partition name. Not all partition schemes support this.",
1017 if (partname != NULL)
1020 /* Record sector and stripe sizes */
1021 sector = pp->lg_sectorsize;
1022 stripe = pp->lg_stripesize;
1025 * Find the PART geom we are manipulating. This may be a consumer of
1026 * this provider, or its parent. Check the consumer case first.
1029 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1030 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1035 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
1038 /* Now get the partition scheme */
1041 LIST_FOREACH(gc, &geom->lg_config, lg_config)
1042 if (strcmp(gc->lg_name, "scheme") == 0)
1043 scheme = gc->lg_val;
1046 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
1047 if (gpart_partition(pp->lg_name, NULL) == 0)
1049 "The partition table has been successfully created."
1050 " Please press Create again to create partitions.",
1057 * If we still don't have a geom, either the user has
1058 * canceled partitioning or there has been an error which has already
1059 * been displayed, so bail.
1064 maxsize = size = gpart_max_free(geom, &firstfree);
1066 dialog_msgbox("Error", "No free space left on device.", 0, 0,
1071 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
1072 HN_NOSPACE | HN_DECIMAL);
1073 items[1].text = sizestr;
1075 /* Special-case the MBR default type for nested partitions */
1076 if (strcmp(scheme, "MBR") == 0) {
1077 items[0].text = "freebsd";
1078 items[0].help = "Filesystem type (e.g. freebsd, fat32)";
1081 nitems = scheme_supports_labels(scheme) ? 4 : 3;
1083 if (default_type != NULL)
1084 items[0].text = (char *)default_type;
1085 if (default_size != NULL)
1086 items[1].text = (char *)default_size;
1087 if (default_mountpoint != NULL)
1088 items[2].text = (char *)default_mountpoint;
1090 /* Default options */
1091 strncpy(options_fstype, items[0].text,
1092 sizeof(options_fstype));
1093 newfs_command(options_fstype, newfs, 1);
1096 dialog_vars.extra_label = "Options";
1097 dialog_vars.extra_button = TRUE;
1098 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
1100 dialog_vars.extra_button = FALSE;
1104 case 1: /* Cancel */
1106 case 3: /* Options */
1107 strncpy(options_fstype, items[0].text,
1108 sizeof(options_fstype));
1109 newfs_command(options_fstype, newfs, 0);
1115 * If the user changed the fs type after specifying options, undo
1116 * their choices in favor of the new filesystem's defaults.
1118 if (strcmp(options_fstype, items[0].text) != 0) {
1119 strncpy(options_fstype, items[0].text, sizeof(options_fstype));
1120 newfs_command(options_fstype, newfs, 1);
1124 if (strlen(items[1].text) > 0) {
1125 if (expand_number(items[1].text, &bytes) != 0) {
1128 sprintf(error, "Invalid size: %s\n", strerror(errno));
1129 dialog_msgbox("Error", error, 0, 0, TRUE);
1132 size = MIN((intmax_t)(bytes/sector), maxsize);
1135 /* Check if the label has a / in it */
1136 if (strchr(items[3].text, '/') != NULL) {
1137 dialog_msgbox("Error", "Label contains a /, which is not an "
1138 "allowed character.", 0, 0, TRUE);
1142 /* Warn if no mountpoint set */
1143 if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1144 items[2].text[0] != '/') {
1147 dialog_vars.defaultno = TRUE;
1148 choice = dialog_yesno("Warning",
1149 "This partition does not have a valid mountpoint "
1150 "(for the partition from which you intend to boot the "
1151 "operating system, the mountpoint should be /). Are you "
1152 "sure you want to continue?"
1154 dialog_vars.defaultno = FALSE;
1156 if (choice == 1) /* cancel */
1161 * Error if this scheme needs nested partitions, this is one, and
1162 * a mountpoint was set.
1164 if (strcmp(items[0].text, "freebsd") == 0 &&
1165 strlen(items[2].text) > 0) {
1166 dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1167 "nested BSD-type partition schemes and cannot have "
1168 "mountpoints. After creating one, select it and press "
1169 "Create again to add the actual file systems.", 0, 0, TRUE);
1173 /* If this is the root partition, check that this scheme is bootable */
1174 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1176 sprintf(message, "This partition scheme (%s) is not bootable "
1177 "on this platform. Are you sure you want to proceed?",
1179 dialog_vars.defaultno = TRUE;
1180 choice = dialog_yesno("Warning", message, 0, 0);
1181 dialog_vars.defaultno = FALSE;
1182 if (choice == 1) /* cancel */
1186 /* If this is the root partition, check that this fs is bootable */
1187 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1190 sprintf(message, "This file system (%s) is not bootable "
1191 "on this system. Are you sure you want to proceed?",
1193 dialog_vars.defaultno = TRUE;
1194 choice = dialog_yesno("Warning", message, 0, 0);
1195 dialog_vars.defaultno = FALSE;
1196 if (choice == 1) /* cancel */
1201 * If this is the root partition, and we need a boot partition, ask
1202 * the user to add one.
1205 if ((strcmp(items[0].text, "freebsd") == 0 ||
1206 strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0) {
1207 size_t bytes = add_boot_partition(geom, pp, scheme,
1210 /* Now adjust the part we are really adding forward */
1212 firstfree += bytes / sector;
1213 size -= (bytes + stripe)/sector;
1214 if (stripe > 0 && (firstfree*sector % stripe) != 0)
1215 firstfree += (stripe - ((firstfree*sector) %
1220 r = gctl_get_handle();
1221 gctl_ro_param(r, "class", -1, "PART");
1222 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1223 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1224 gctl_ro_param(r, "verb", -1, "add");
1226 gctl_ro_param(r, "type", -1, items[0].text);
1227 snprintf(sizestr, sizeof(sizestr), "%jd", size);
1228 gctl_ro_param(r, "size", -1, sizestr);
1229 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1230 gctl_ro_param(r, "start", -1, startstr);
1231 if (items[3].text[0] != '\0')
1232 gctl_ro_param(r, "label", -1, items[3].text);
1233 gctl_rw_param(r, "output", sizeof(output), output);
1234 errstr = gctl_issue(r);
1235 if (errstr != NULL && errstr[0] != '\0') {
1236 gpart_show_error("Error", NULL, errstr);
1240 newpartname = strtok(output, " ");
1244 * Try to destroy any geom that gpart picked up already here from
1247 r = gctl_get_handle();
1248 gctl_ro_param(r, "class", -1, "PART");
1249 gctl_ro_param(r, "arg0", -1, newpartname);
1250 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1252 gctl_ro_param(r, "force", sizeof(junk), &junk);
1253 gctl_ro_param(r, "verb", -1, "destroy");
1254 gctl_issue(r); /* Error usually expected and non-fatal */
1258 if (strcmp(items[0].text, "freebsd") == 0)
1259 gpart_partition(newpartname, "BSD");
1261 set_default_part_metadata(newpartname, scheme,
1262 items[0].text, items[2].text, newfs);
1264 for (i = 0; i < nitems(items); i++)
1265 if (items[i].text_free)
1266 free(items[i].text);
1268 if (partname != NULL)
1269 *partname = strdup(newpartname);
1273 gpart_delete(struct gprovider *pp)
1277 struct gconsumer *cp;
1283 /* Is it a partition? */
1284 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1286 /* Find out if this is the root of a gpart geom */
1288 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1289 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1294 /* If so, destroy all children */
1296 gpart_destroy(geom);
1298 /* If this is a partition, revert it, so it can be deleted */
1300 r = gctl_get_handle();
1301 gctl_ro_param(r, "class", -1, "PART");
1302 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1303 gctl_ro_param(r, "verb", -1, "undo");
1304 gctl_issue(r); /* Ignore non-fatal errors */
1310 * If this is not a partition, see if that is a problem, complain if
1311 * necessary, and return always, since we need not do anything further,
1314 if (!is_partition) {
1316 dialog_msgbox("Error",
1317 "Only partitions can be deleted.", 0, 0, TRUE);
1321 r = gctl_get_handle();
1322 gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1323 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1324 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1325 gctl_ro_param(r, "verb", -1, "delete");
1327 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1328 if (strcmp(gc->lg_name, "index") == 0) {
1329 idx = atoi(gc->lg_val);
1330 gctl_ro_param(r, "index", sizeof(idx), &idx);
1335 errstr = gctl_issue(r);
1336 if (errstr != NULL && errstr[0] != '\0') {
1337 gpart_show_error("Error", NULL, errstr);
1344 delete_part_metadata(pp->lg_name);
1348 gpart_revert_all(struct gmesh *mesh)
1350 struct gclass *classp;
1354 const char *modified;
1356 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1357 if (strcmp(classp->lg_name, "PART") == 0)
1361 if (strcmp(classp->lg_name, "PART") != 0) {
1362 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1366 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1367 modified = "true"; /* XXX: If we don't know (kernel too old),
1368 * assume there are modifications. */
1369 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1370 if (strcmp(gc->lg_name, "modified") == 0) {
1371 modified = gc->lg_val;
1376 if (strcmp(modified, "false") == 0)
1379 r = gctl_get_handle();
1380 gctl_ro_param(r, "class", -1, "PART");
1381 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1382 gctl_ro_param(r, "verb", -1, "undo");
1390 gpart_commit(struct gmesh *mesh)
1392 struct partition_metadata *md;
1393 struct gclass *classp;
1396 struct gconsumer *cp;
1397 struct gprovider *pp;
1400 const char *modified;
1403 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1404 if (strcmp(classp->lg_name, "PART") == 0)
1408 /* Figure out what filesystem / uses */
1409 rootfs = "ufs"; /* Assume ufs if nothing else present */
1410 TAILQ_FOREACH(md, &part_metadata, metadata) {
1411 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
1412 rootfs = md->fstab->fs_vfstype;
1417 if (strcmp(classp->lg_name, "PART") != 0) {
1418 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1422 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1423 modified = "true"; /* XXX: If we don't know (kernel too old),
1424 * assume there are modifications. */
1425 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1426 if (strcmp(gc->lg_name, "modified") == 0) {
1427 modified = gc->lg_val;
1432 if (strcmp(modified, "false") == 0)
1435 /* Add bootcode if necessary, before the commit */
1436 md = get_part_metadata(gp->lg_name, 0);
1437 if (md != NULL && md->bootcode)
1440 /* Now install partcode on its partitions, if necessary */
1441 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1442 md = get_part_metadata(pp->lg_name, 0);
1443 if (md == NULL || !md->bootcode)
1446 /* Mark this partition active if that's required */
1449 /* Check if the partition has sub-partitions */
1450 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1451 if (strcmp(cp->lg_geom->lg_class->lg_name,
1455 if (cp == NULL) /* No sub-partitions */
1456 gpart_partcode(pp, rootfs);
1459 r = gctl_get_handle();
1460 gctl_ro_param(r, "class", -1, "PART");
1461 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1462 gctl_ro_param(r, "verb", -1, "commit");
1464 errstr = gctl_issue(r);
1465 if (errstr != NULL && errstr[0] != '\0')
1466 gpart_show_error("Error", NULL, errstr);