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 },
226 {"VTOC8", "Sun VTOC8 Partition Table",
227 "Bootable on Sun SPARC systems", 0 },
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;
240 if (!is_scheme_bootable(items[choice].name)) {
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 */
252 scheme = items[choice].name;
258 gpart_partition(const char *lg_name, const char *scheme)
265 if (scheme == NULL) {
266 scheme = choose_part_type(default_scheme());
271 if (!is_scheme_bootable(scheme)) {
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 */
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");
294 errstr = gctl_issue(r);
295 if (errstr != NULL && errstr[0] != '\0') {
296 gpart_show_error("Error", NULL, errstr);
303 if (bootcode_path(scheme) != NULL)
304 get_part_metadata(lg_name, 1)->bootcode = 1;
309 gpart_activate(struct gprovider *pp)
313 const char *errstr, *scheme;
314 const char *attribute = NULL;
318 * Some partition schemes need this partition to be marked 'active'
319 * for it to be bootable.
321 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
322 if (strcmp(gc->lg_name, "scheme") == 0) {
328 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)
329 attribute = "active";
333 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
334 if (strcmp(gc->lg_name, "index") == 0) {
335 idx = atoi(gc->lg_val);
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);
347 errstr = gctl_issue(r);
348 if (errstr != NULL && errstr[0] != '\0')
349 gpart_show_error("Error", "Error marking partition active:",
355 gpart_set_root(const char *lg_name, const char *attribute)
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);
367 errstr = gctl_issue(r);
368 if (errstr != NULL && errstr[0] != '\0')
369 gpart_show_error("Error", "Error setting parameter on disk:",
375 gpart_bootcode(struct ggeom *gp)
377 const char *bootcode;
380 const char *errstr, *scheme;
382 size_t bootsize, bytes;
386 * Write default bootcode to the newly partitioned disk, if that
387 * applies on this platform.
389 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
390 if (strcmp(gc->lg_name, "scheme") == 0) {
396 bootcode = bootcode_path(scheme);
397 if (bootcode == NULL)
400 bootfd = open(bootcode, O_RDONLY);
402 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
407 bootsize = lseek(bootfd, 0, SEEK_END);
408 boot = malloc(bootsize);
409 lseek(bootfd, 0, SEEK_SET);
411 while (bytes < bootsize)
412 bytes += read(bootfd, boot + bytes, bootsize - bytes);
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);
421 errstr = gctl_issue(r);
422 if (errstr != NULL && errstr[0] != '\0')
423 gpart_show_error("Bootcode Error", NULL, errstr);
429 gpart_partcode(struct gprovider *pp, const char *fstype)
433 const char *indexstr;
434 char message[255], command[255];
436 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
437 if (strcmp(gc->lg_name, "scheme") == 0) {
443 /* Make sure this partition scheme needs partcode on this platform */
444 if (partcode_path(scheme, fstype) == NULL)
447 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
448 if (strcmp(gc->lg_name, "index") == 0) {
449 indexstr = gc->lg_val;
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",
460 dialog_msgbox("Error", message, 0, 0, TRUE);
465 gpart_destroy(struct ggeom *lg_geom)
468 struct gprovider *pp;
472 /* Delete all child metadata */
473 LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
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 */
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') {
494 * Check if we reverted away the existence of the geom
495 * altogether. Show all other errors to the user.
497 if (strtol(errstr, NULL, 0) != EINVAL)
498 gpart_show_error("Error", NULL, errstr);
502 /* And any metadata associated with the partition scheme itself */
503 delete_part_metadata(lg_geom->lg_name);
507 gpart_edit(struct gprovider *pp)
511 struct gconsumer *cp;
513 const char *errstr, *oldtype, *scheme;
514 struct partition_metadata *md;
518 int hadlabel, choice, junk, nitems;
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.",
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.
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 */
545 LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
546 if (strcmp(gc->lg_name, "scheme") == 0) {
551 if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
552 gpart_partition(cp->lg_geom->lg_name, NULL);
556 /* If this is a nested partition, edit as usual */
557 if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
560 /* Destroy the geom and all sub-partitions */
561 gpart_destroy(cp->lg_geom);
563 /* Now re-partition and return */
564 gpart_partition(cp->lg_geom->lg_name, NULL);
568 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
572 /* Disk not partitioned, so partition it */
573 gpart_partition(pp->lg_name, NULL);
577 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
578 if (strcmp(gc->lg_name, "scheme") == 0) {
584 nitems = scheme_supports_labels(scheme) ? 4 : 3;
586 /* Edit editable parameters of a partition */
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;
593 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
595 items[3].text = gc->lg_val;
597 if (strcmp(gc->lg_name, "index") == 0)
598 idx = atoi(gc->lg_val);
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;
609 humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
610 HN_NOSPACE | HN_DECIMAL);
611 items[1].text = sizestr;
614 choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
616 if (choice) /* Cancel pressed */
619 /* If this is the root partition, check that this fs is bootable */
620 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
623 sprintf(message, "This file system (%s) is not bootable "
624 "on this system. Are you sure you want to proceed?",
626 dialog_vars.defaultno = TRUE;
627 choice = dialog_yesno("Warning", message, 0, 0);
628 dialog_vars.defaultno = FALSE;
629 if (choice == 1) /* cancel */
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);
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);
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) ?
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,
667 gpart_partition(pp->lg_name, "BSD");
669 for (i = 0; i < nitems(items); i++)
670 if (items[i].text_free)
675 set_default_part_metadata(const char *name, const char *scheme,
676 const char *type, const char *mountpoint, const char *newfs)
678 struct partition_metadata *md;
679 char *zpool_name = NULL;
680 const char *default_bootmount = NULL;
683 /* Set part metadata */
684 md = get_part_metadata(name, 1);
687 if (md->newfs != NULL) {
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]))
701 sprintf(md->newfs, "%s %s /dev/%s", newfs,
704 sprintf(md->newfs, "%s /dev/%s", newfs, name);
709 if (strcmp(type, "freebsd-swap") == 0)
711 if (strcmp(type, bootpart_type(scheme, &default_bootmount)) == 0) {
712 if (default_bootmount == NULL) {
714 int fd = openat(tmpdfd, "bsdinstall-esps",
715 O_CREAT | O_WRONLY | O_APPEND, 0600);
717 write(fd, md->name, strlen(md->name));
724 else if (mountpoint == NULL || strlen(mountpoint) == 0)
725 mountpoint = default_bootmount;
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))
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);
744 if (md->fstab == NULL) {
745 md->fstab = malloc(sizeof(struct fstab));
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);
753 if (strcmp("freebsd-zfs", type) == 0) {
754 md->fstab->fs_spec = strdup(zpool_name);
756 md->fstab->fs_spec = malloc(strlen(name) +
757 strlen("/dev/") + 1);
758 sprintf(md->fstab->fs_spec, "/dev/%s", name);
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");
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;
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;
783 md->fstab->fs_freq = 2;
784 md->fstab->fs_passno = 2;
787 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
790 if (zpool_name != NULL)
795 int part_compare(const void *xa, const void *xb)
797 struct gprovider **a = (struct gprovider **)xa;
798 struct gprovider **b = (struct gprovider **)xb;
799 intmax_t astart, bstart;
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);
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);
816 else if (astart > bstart)
823 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
826 struct gprovider *pp, **providers;
827 intmax_t sectorsize, stripesize, offset;
830 intmax_t maxsize, maxstart;
831 intmax_t partstart, partend;
834 /* Now get the maximum free size and free start */
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);
844 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
846 providers = calloc(nparts, sizeof(providers[0]));
847 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
849 qsort(providers, nparts, sizeof(providers[0]), part_compare);
853 for (i = 0; i < nparts; i++) {
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);
863 if (partstart - lastend > maxsize) {
864 maxsize = partstart - lastend - 1;
865 maxstart = lastend + 1;
871 if (end - lastend > maxsize) {
872 maxsize = end - lastend;
873 maxstart = lastend + 1;
876 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
879 * Round the start and size of the largest available space up to
880 * the nearest multiple of the adjusted stripe size.
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.
890 sectorsize = pp->lg_sectorsize;
891 if ((stripesize = pp->lg_stripesize) == 0)
892 stripesize = sectorsize;
893 while (stripesize % 4096 != 0)
895 if ((offset = maxstart * sectorsize % stripesize) != 0) {
896 offset = (stripesize - offset) / sectorsize;
901 if (npartstart != NULL)
902 *npartstart = maxstart;
908 add_boot_partition(struct ggeom *geom, struct gprovider *pp,
909 const char *scheme, int interactive)
912 struct gprovider *ppi;
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;
920 LIST_FOREACH(gc, &ppi->lg_config, lg_config)
921 if (strcmp(gc->lg_name, "type") == 0)
925 if (strcmp(gc->lg_val, bootpart_type(scheme, &bootmount)) != 0)
929 * If the boot partition is not mountable and needs partcode,
930 * but doesn't have it, it doesn't satisfy our requirements.
932 md = get_part_metadata(ppi->lg_name, 0);
933 if (bootmount == NULL && (md == NULL || !md->bootcode))
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)
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);
947 /* Looks good at this point, no added data needed */
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);
959 if (choice == 0) { /* yes */
960 struct partition_metadata *md;
961 const char *bootmount = NULL;
962 char *bootpartname = NULL;
965 humanize_number(sizestr, 7,
966 bootpart_size(scheme), "B", HN_AUTOSCALE,
967 HN_NOSPACE | HN_DECIMAL);
969 gpart_create(pp, bootpart_type(scheme, &bootmount),
970 sizestr, bootmount, &bootpartname, 0);
972 if (bootpartname == NULL) /* Error reported to user already */
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) {
986 return (bootpart_size(scheme));
993 gpart_create(struct gprovider *pp, const char *default_type,
994 const char *default_size, const char *default_mountpoint,
995 char **partname, int interactive)
999 struct gconsumer *cp;
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;
1006 int nitems, choice, junk;
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.",
1024 if (partname != NULL)
1027 /* Record sector and stripe sizes */
1028 sector = pp->lg_sectorsize;
1029 stripe = pp->lg_stripesize;
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.
1036 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1037 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1042 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
1045 /* Now get the partition scheme */
1048 LIST_FOREACH(gc, &geom->lg_config, lg_config)
1049 if (strcmp(gc->lg_name, "scheme") == 0)
1050 scheme = gc->lg_val;
1053 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
1054 if (gpart_partition(pp->lg_name, NULL) == 0)
1056 "The partition table has been successfully created."
1057 " Please press Create again to create partitions.",
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.
1071 maxsize = size = gpart_max_free(geom, &firstfree);
1073 dialog_msgbox("Error", "No free space left on device.", 0, 0,
1078 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
1079 HN_NOSPACE | HN_DECIMAL);
1080 items[1].text = sizestr;
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)";
1088 nitems = scheme_supports_labels(scheme) ? 4 : 3;
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;
1097 /* Default options */
1098 strncpy(options_fstype, items[0].text,
1099 sizeof(options_fstype));
1100 newfs_command(options_fstype, newfs, 1);
1103 dialog_vars.extra_label = "Options";
1104 dialog_vars.extra_button = TRUE;
1105 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
1107 dialog_vars.extra_button = FALSE;
1111 case 1: /* Cancel */
1113 case 3: /* Options */
1114 strncpy(options_fstype, items[0].text,
1115 sizeof(options_fstype));
1116 newfs_command(options_fstype, newfs, 0);
1122 * If the user changed the fs type after specifying options, undo
1123 * their choices in favor of the new filesystem's defaults.
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);
1131 if (strlen(items[1].text) > 0) {
1132 if (expand_number(items[1].text, &bytes) != 0) {
1135 sprintf(error, "Invalid size: %s\n", strerror(errno));
1136 dialog_msgbox("Error", error, 0, 0, TRUE);
1139 size = MIN((intmax_t)(bytes/sector), maxsize);
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);
1149 /* Warn if no mountpoint set */
1150 if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1151 items[2].text[0] != '/') {
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?"
1161 dialog_vars.defaultno = FALSE;
1163 if (choice == 1) /* cancel */
1168 * Error if this scheme needs nested partitions, this is one, and
1169 * a mountpoint was set.
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);
1180 /* If this is the root partition, check that this scheme is bootable */
1181 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1183 sprintf(message, "This partition scheme (%s) is not bootable "
1184 "on this platform. Are you sure you want to proceed?",
1186 dialog_vars.defaultno = TRUE;
1187 choice = dialog_yesno("Warning", message, 0, 0);
1188 dialog_vars.defaultno = FALSE;
1189 if (choice == 1) /* cancel */
1193 /* If this is the root partition, check that this fs is bootable */
1194 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1197 sprintf(message, "This file system (%s) is not bootable "
1198 "on this system. Are you sure you want to proceed?",
1200 dialog_vars.defaultno = TRUE;
1201 choice = dialog_yesno("Warning", message, 0, 0);
1202 dialog_vars.defaultno = FALSE;
1203 if (choice == 1) /* cancel */
1208 * If this is the root partition, and we need a boot partition, ask
1209 * the user to add one.
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,
1217 /* Now adjust the part we are really adding forward */
1219 firstfree += bytes / sector;
1220 size -= (bytes + stripe)/sector;
1221 if (stripe > 0 && (firstfree*sector % stripe) != 0)
1222 firstfree += (stripe - ((firstfree*sector) %
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");
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);
1247 newpartname = strtok(output, " ");
1251 * Try to destroy any geom that gpart picked up already here from
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);
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 */
1265 if (strcmp(items[0].text, "freebsd") == 0)
1266 gpart_partition(newpartname, "BSD");
1268 set_default_part_metadata(newpartname, scheme,
1269 items[0].text, items[2].text, newfs);
1271 for (i = 0; i < nitems(items); i++)
1272 if (items[i].text_free)
1273 free(items[i].text);
1275 if (partname != NULL)
1276 *partname = strdup(newpartname);
1280 gpart_delete(struct gprovider *pp)
1284 struct gconsumer *cp;
1290 /* Is it a partition? */
1291 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1293 /* Find out if this is the root of a gpart geom */
1295 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1296 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1301 /* If so, destroy all children */
1303 gpart_destroy(geom);
1305 /* If this is a partition, revert it, so it can be deleted */
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 */
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,
1321 if (!is_partition) {
1323 dialog_msgbox("Error",
1324 "Only partitions can be deleted.", 0, 0, TRUE);
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");
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);
1342 errstr = gctl_issue(r);
1343 if (errstr != NULL && errstr[0] != '\0') {
1344 gpart_show_error("Error", NULL, errstr);
1351 delete_part_metadata(pp->lg_name);
1355 gpart_revert_all(struct gmesh *mesh)
1357 struct gclass *classp;
1361 const char *modified;
1363 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1364 if (strcmp(classp->lg_name, "PART") == 0)
1368 if (strcmp(classp->lg_name, "PART") != 0) {
1369 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
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;
1383 if (strcmp(modified, "false") == 0)
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");
1397 gpart_commit(struct gmesh *mesh)
1399 struct partition_metadata *md;
1400 struct gclass *classp;
1403 struct gconsumer *cp;
1404 struct gprovider *pp;
1407 const char *modified;
1410 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1411 if (strcmp(classp->lg_name, "PART") == 0)
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;
1424 if (strcmp(classp->lg_name, "PART") != 0) {
1425 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
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;
1439 if (strcmp(modified, "false") == 0)
1442 /* Add bootcode if necessary, before the commit */
1443 md = get_part_metadata(gp->lg_name, 0);
1444 if (md != NULL && md->bootcode)
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)
1453 /* Mark this partition active if that's required */
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,
1462 if (cp == NULL) /* No sub-partitions */
1463 gpart_partcode(pp, rootfs);
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");
1471 errstr = gctl_issue(r);
1472 if (errstr != NULL && errstr[0] != '\0')
1473 gpart_show_error("Error", NULL, errstr);