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 = open("/tmp/bsdinstall-esps", O_CREAT | O_WRONLY | O_APPEND,
717 write(fd, md->name, strlen(md->name));
723 else if (mountpoint == NULL || strlen(mountpoint) == 0)
724 mountpoint = default_bootmount;
727 /* VTOC8 needs partcode at the start of partitions */
728 if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0
729 || strcmp(type, "freebsd-zfs") == 0))
732 if (mountpoint == NULL || mountpoint[0] == '\0') {
733 if (md->fstab != NULL) {
734 free(md->fstab->fs_spec);
735 free(md->fstab->fs_file);
736 free(md->fstab->fs_vfstype);
737 free(md->fstab->fs_mntops);
738 free(md->fstab->fs_type);
743 if (md->fstab == NULL) {
744 md->fstab = malloc(sizeof(struct fstab));
746 free(md->fstab->fs_spec);
747 free(md->fstab->fs_file);
748 free(md->fstab->fs_vfstype);
749 free(md->fstab->fs_mntops);
750 free(md->fstab->fs_type);
752 if (strcmp("freebsd-zfs", type) == 0) {
753 md->fstab->fs_spec = strdup(zpool_name);
755 md->fstab->fs_spec = malloc(strlen(name) +
756 strlen("/dev/") + 1);
757 sprintf(md->fstab->fs_spec, "/dev/%s", name);
759 md->fstab->fs_file = strdup(mountpoint);
760 /* Get VFS from text after freebsd-, if possible */
761 if (strncmp("freebsd-", type, 8) == 0)
762 md->fstab->fs_vfstype = strdup(&type[8]);
763 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0
764 || strcmp("ms-basic-data", type) == 0)
765 md->fstab->fs_vfstype = strdup("msdosfs");
767 md->fstab->fs_vfstype = strdup(type); /* Guess */
768 if (strcmp(type, "freebsd-swap") == 0) {
769 md->fstab->fs_type = strdup(FSTAB_SW);
770 md->fstab->fs_freq = 0;
771 md->fstab->fs_passno = 0;
772 } else if (strcmp(type, "freebsd-zfs") == 0) {
773 md->fstab->fs_type = strdup(FSTAB_RW);
774 md->fstab->fs_freq = 0;
775 md->fstab->fs_passno = 0;
777 md->fstab->fs_type = strdup(FSTAB_RW);
778 if (strcmp(mountpoint, "/") == 0) {
779 md->fstab->fs_freq = 1;
780 md->fstab->fs_passno = 1;
782 md->fstab->fs_freq = 2;
783 md->fstab->fs_passno = 2;
786 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
789 if (zpool_name != NULL)
794 int part_compare(const void *xa, const void *xb)
796 struct gprovider **a = (struct gprovider **)xa;
797 struct gprovider **b = (struct gprovider **)xb;
798 intmax_t astart, bstart;
802 LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
803 if (strcmp(gc->lg_name, "start") == 0) {
804 astart = strtoimax(gc->lg_val, NULL, 0);
807 LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
808 if (strcmp(gc->lg_name, "start") == 0) {
809 bstart = strtoimax(gc->lg_val, NULL, 0);
815 else if (astart > bstart)
822 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
825 struct gprovider *pp, **providers;
826 intmax_t sectorsize, stripesize, offset;
829 intmax_t maxsize, maxstart;
830 intmax_t partstart, partend;
833 /* Now get the maximum free size and free start */
835 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
836 if (strcmp(gc->lg_name, "first") == 0)
837 start = strtoimax(gc->lg_val, NULL, 0);
838 if (strcmp(gc->lg_name, "last") == 0)
839 end = strtoimax(gc->lg_val, NULL, 0);
843 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
845 providers = calloc(nparts, sizeof(providers[0]));
846 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
848 qsort(providers, nparts, sizeof(providers[0]), part_compare);
852 for (i = 0; i < nparts; i++) {
855 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
856 if (strcmp(gc->lg_name, "start") == 0)
857 partstart = strtoimax(gc->lg_val, NULL, 0);
858 if (strcmp(gc->lg_name, "end") == 0)
859 partend = strtoimax(gc->lg_val, NULL, 0);
862 if (partstart - lastend > maxsize) {
863 maxsize = partstart - lastend - 1;
864 maxstart = lastend + 1;
870 if (end - lastend > maxsize) {
871 maxsize = end - lastend;
872 maxstart = lastend + 1;
875 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
878 * Round the start and size of the largest available space up to
879 * the nearest multiple of the adjusted stripe size.
881 * The adjusted stripe size is the least common multiple of the
882 * actual stripe size, or the sector size if no stripe size was
883 * reported, and 4096. The reason for this is that contemporary
884 * disks often have 4096-byte physical sectors but report 512
885 * bytes instead for compatibility with older / broken operating
886 * systems and BIOSes. For the same reasons, virtualized storage
887 * may also report a 512-byte stripe size, or none at all.
889 sectorsize = pp->lg_sectorsize;
890 if ((stripesize = pp->lg_stripesize) == 0)
891 stripesize = sectorsize;
892 while (stripesize % 4096 != 0)
894 if ((offset = maxstart * sectorsize % stripesize) != 0) {
895 offset = (stripesize - offset) / sectorsize;
900 if (npartstart != NULL)
901 *npartstart = maxstart;
907 add_boot_partition(struct ggeom *geom, struct gprovider *pp,
908 const char *scheme, int interactive)
911 struct gprovider *ppi;
914 /* Check for existing freebsd-boot partition */
915 LIST_FOREACH(ppi, &geom->lg_provider, lg_provider) {
916 struct partition_metadata *md;
917 const char *bootmount = NULL;
919 LIST_FOREACH(gc, &ppi->lg_config, lg_config)
920 if (strcmp(gc->lg_name, "type") == 0)
924 if (strcmp(gc->lg_val, bootpart_type(scheme, &bootmount)) != 0)
928 * If the boot partition is not mountable and needs partcode,
929 * but doesn't have it, it doesn't satisfy our requirements.
931 md = get_part_metadata(ppi->lg_name, 0);
932 if (bootmount == NULL && (md == NULL || !md->bootcode))
935 /* If it is mountable, but mounted somewhere else, remount */
936 if (bootmount != NULL && md != NULL && md->fstab != NULL
937 && strlen(md->fstab->fs_file) > 0
938 && strcmp(md->fstab->fs_file, bootmount) != 0)
941 /* If it is mountable, but mountpoint is not set, mount it */
942 if (bootmount != NULL && md == NULL)
943 set_default_part_metadata(ppi->lg_name, scheme,
944 gc->lg_val, bootmount, NULL);
946 /* Looks good at this point, no added data needed */
951 choice = dialog_yesno("Boot Partition",
952 "This partition scheme requires a boot partition "
953 "for the disk to be bootable. Would you like to "
954 "make one now?", 0, 0);
958 if (choice == 0) { /* yes */
959 struct partition_metadata *md;
960 const char *bootmount = NULL;
961 char *bootpartname = NULL;
964 humanize_number(sizestr, 7,
965 bootpart_size(scheme), "B", HN_AUTOSCALE,
966 HN_NOSPACE | HN_DECIMAL);
968 gpart_create(pp, bootpart_type(scheme, &bootmount),
969 sizestr, bootmount, &bootpartname, 0);
971 if (bootpartname == NULL) /* Error reported to user already */
974 /* If the part is not mountable, make sure newfs isn't set */
975 if (bootmount == NULL) {
976 md = get_part_metadata(bootpartname, 0);
977 if (md != NULL && md->newfs != NULL) {
985 return (bootpart_size(scheme));
992 gpart_create(struct gprovider *pp, const char *default_type,
993 const char *default_size, const char *default_mountpoint,
994 char **partname, int interactive)
998 struct gconsumer *cp;
1000 const char *errstr, *scheme;
1001 char sizestr[32], startstr[32], output[64], *newpartname;
1002 char newfs[255], options_fstype[64];
1003 intmax_t maxsize, size, sector, firstfree, stripe;
1005 int nitems, choice, junk;
1008 DIALOG_FORMITEM items[] = {
1009 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
1010 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
1011 "freebsd-swap)", FALSE},
1012 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
1013 FALSE, "Partition size. Append K, M, G for kilobytes, "
1014 "megabytes or gigabytes.", FALSE},
1015 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
1016 FALSE, "Path at which to mount partition (blank for "
1017 "swap, / for root filesystem)", FALSE},
1018 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
1019 "Partition name. Not all partition schemes support this.",
1023 if (partname != NULL)
1026 /* Record sector and stripe sizes */
1027 sector = pp->lg_sectorsize;
1028 stripe = pp->lg_stripesize;
1031 * Find the PART geom we are manipulating. This may be a consumer of
1032 * this provider, or its parent. Check the consumer case first.
1035 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1036 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1041 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
1044 /* Now get the partition scheme */
1047 LIST_FOREACH(gc, &geom->lg_config, lg_config)
1048 if (strcmp(gc->lg_name, "scheme") == 0)
1049 scheme = gc->lg_val;
1052 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
1053 if (gpart_partition(pp->lg_name, NULL) == 0)
1055 "The partition table has been successfully created."
1056 " Please press Create again to create partitions.",
1063 * If we still don't have a geom, either the user has
1064 * canceled partitioning or there has been an error which has already
1065 * been displayed, so bail.
1070 maxsize = size = gpart_max_free(geom, &firstfree);
1072 dialog_msgbox("Error", "No free space left on device.", 0, 0,
1077 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
1078 HN_NOSPACE | HN_DECIMAL);
1079 items[1].text = sizestr;
1081 /* Special-case the MBR default type for nested partitions */
1082 if (strcmp(scheme, "MBR") == 0) {
1083 items[0].text = "freebsd";
1084 items[0].help = "Filesystem type (e.g. freebsd, fat32)";
1087 nitems = scheme_supports_labels(scheme) ? 4 : 3;
1089 if (default_type != NULL)
1090 items[0].text = (char *)default_type;
1091 if (default_size != NULL)
1092 items[1].text = (char *)default_size;
1093 if (default_mountpoint != NULL)
1094 items[2].text = (char *)default_mountpoint;
1096 /* Default options */
1097 strncpy(options_fstype, items[0].text,
1098 sizeof(options_fstype));
1099 newfs_command(options_fstype, newfs, 1);
1102 dialog_vars.extra_label = "Options";
1103 dialog_vars.extra_button = TRUE;
1104 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
1106 dialog_vars.extra_button = FALSE;
1110 case 1: /* Cancel */
1112 case 3: /* Options */
1113 strncpy(options_fstype, items[0].text,
1114 sizeof(options_fstype));
1115 newfs_command(options_fstype, newfs, 0);
1121 * If the user changed the fs type after specifying options, undo
1122 * their choices in favor of the new filesystem's defaults.
1124 if (strcmp(options_fstype, items[0].text) != 0) {
1125 strncpy(options_fstype, items[0].text, sizeof(options_fstype));
1126 newfs_command(options_fstype, newfs, 1);
1130 if (strlen(items[1].text) > 0) {
1131 if (expand_number(items[1].text, &bytes) != 0) {
1134 sprintf(error, "Invalid size: %s\n", strerror(errno));
1135 dialog_msgbox("Error", error, 0, 0, TRUE);
1138 size = MIN((intmax_t)(bytes/sector), maxsize);
1141 /* Check if the label has a / in it */
1142 if (strchr(items[3].text, '/') != NULL) {
1143 dialog_msgbox("Error", "Label contains a /, which is not an "
1144 "allowed character.", 0, 0, TRUE);
1148 /* Warn if no mountpoint set */
1149 if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1150 items[2].text[0] != '/') {
1153 dialog_vars.defaultno = TRUE;
1154 choice = dialog_yesno("Warning",
1155 "This partition does not have a valid mountpoint "
1156 "(for the partition from which you intend to boot the "
1157 "operating system, the mountpoint should be /). Are you "
1158 "sure you want to continue?"
1160 dialog_vars.defaultno = FALSE;
1162 if (choice == 1) /* cancel */
1167 * Error if this scheme needs nested partitions, this is one, and
1168 * a mountpoint was set.
1170 if (strcmp(items[0].text, "freebsd") == 0 &&
1171 strlen(items[2].text) > 0) {
1172 dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1173 "nested BSD-type partition schemes and cannot have "
1174 "mountpoints. After creating one, select it and press "
1175 "Create again to add the actual file systems.", 0, 0, TRUE);
1179 /* If this is the root partition, check that this scheme is bootable */
1180 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1182 sprintf(message, "This partition scheme (%s) is not bootable "
1183 "on this platform. Are you sure you want to proceed?",
1185 dialog_vars.defaultno = TRUE;
1186 choice = dialog_yesno("Warning", message, 0, 0);
1187 dialog_vars.defaultno = FALSE;
1188 if (choice == 1) /* cancel */
1192 /* If this is the root partition, check that this fs is bootable */
1193 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1196 sprintf(message, "This file system (%s) is not bootable "
1197 "on this system. Are you sure you want to proceed?",
1199 dialog_vars.defaultno = TRUE;
1200 choice = dialog_yesno("Warning", message, 0, 0);
1201 dialog_vars.defaultno = FALSE;
1202 if (choice == 1) /* cancel */
1207 * If this is the root partition, and we need a boot partition, ask
1208 * the user to add one.
1211 if ((strcmp(items[0].text, "freebsd") == 0 ||
1212 strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0) {
1213 size_t bytes = add_boot_partition(geom, pp, scheme,
1216 /* Now adjust the part we are really adding forward */
1218 firstfree += bytes / sector;
1219 size -= (bytes + stripe)/sector;
1220 if (stripe > 0 && (firstfree*sector % stripe) != 0)
1221 firstfree += (stripe - ((firstfree*sector) %
1226 r = gctl_get_handle();
1227 gctl_ro_param(r, "class", -1, "PART");
1228 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1229 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1230 gctl_ro_param(r, "verb", -1, "add");
1232 gctl_ro_param(r, "type", -1, items[0].text);
1233 snprintf(sizestr, sizeof(sizestr), "%jd", size);
1234 gctl_ro_param(r, "size", -1, sizestr);
1235 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1236 gctl_ro_param(r, "start", -1, startstr);
1237 if (items[3].text[0] != '\0')
1238 gctl_ro_param(r, "label", -1, items[3].text);
1239 gctl_rw_param(r, "output", sizeof(output), output);
1240 errstr = gctl_issue(r);
1241 if (errstr != NULL && errstr[0] != '\0') {
1242 gpart_show_error("Error", NULL, errstr);
1246 newpartname = strtok(output, " ");
1250 * Try to destroy any geom that gpart picked up already here from
1253 r = gctl_get_handle();
1254 gctl_ro_param(r, "class", -1, "PART");
1255 gctl_ro_param(r, "arg0", -1, newpartname);
1256 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1258 gctl_ro_param(r, "force", sizeof(junk), &junk);
1259 gctl_ro_param(r, "verb", -1, "destroy");
1260 gctl_issue(r); /* Error usually expected and non-fatal */
1264 if (strcmp(items[0].text, "freebsd") == 0)
1265 gpart_partition(newpartname, "BSD");
1267 set_default_part_metadata(newpartname, scheme,
1268 items[0].text, items[2].text, newfs);
1270 for (i = 0; i < nitems(items); i++)
1271 if (items[i].text_free)
1272 free(items[i].text);
1274 if (partname != NULL)
1275 *partname = strdup(newpartname);
1279 gpart_delete(struct gprovider *pp)
1283 struct gconsumer *cp;
1289 /* Is it a partition? */
1290 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1292 /* Find out if this is the root of a gpart geom */
1294 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1295 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1300 /* If so, destroy all children */
1302 gpart_destroy(geom);
1304 /* If this is a partition, revert it, so it can be deleted */
1306 r = gctl_get_handle();
1307 gctl_ro_param(r, "class", -1, "PART");
1308 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1309 gctl_ro_param(r, "verb", -1, "undo");
1310 gctl_issue(r); /* Ignore non-fatal errors */
1316 * If this is not a partition, see if that is a problem, complain if
1317 * necessary, and return always, since we need not do anything further,
1320 if (!is_partition) {
1322 dialog_msgbox("Error",
1323 "Only partitions can be deleted.", 0, 0, TRUE);
1327 r = gctl_get_handle();
1328 gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1329 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1330 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1331 gctl_ro_param(r, "verb", -1, "delete");
1333 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1334 if (strcmp(gc->lg_name, "index") == 0) {
1335 idx = atoi(gc->lg_val);
1336 gctl_ro_param(r, "index", sizeof(idx), &idx);
1341 errstr = gctl_issue(r);
1342 if (errstr != NULL && errstr[0] != '\0') {
1343 gpart_show_error("Error", NULL, errstr);
1350 delete_part_metadata(pp->lg_name);
1354 gpart_revert_all(struct gmesh *mesh)
1356 struct gclass *classp;
1360 const char *modified;
1362 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1363 if (strcmp(classp->lg_name, "PART") == 0)
1367 if (strcmp(classp->lg_name, "PART") != 0) {
1368 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1372 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1373 modified = "true"; /* XXX: If we don't know (kernel too old),
1374 * assume there are modifications. */
1375 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1376 if (strcmp(gc->lg_name, "modified") == 0) {
1377 modified = gc->lg_val;
1382 if (strcmp(modified, "false") == 0)
1385 r = gctl_get_handle();
1386 gctl_ro_param(r, "class", -1, "PART");
1387 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1388 gctl_ro_param(r, "verb", -1, "undo");
1396 gpart_commit(struct gmesh *mesh)
1398 struct partition_metadata *md;
1399 struct gclass *classp;
1402 struct gconsumer *cp;
1403 struct gprovider *pp;
1406 const char *modified;
1409 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1410 if (strcmp(classp->lg_name, "PART") == 0)
1414 /* Figure out what filesystem / uses */
1415 rootfs = "ufs"; /* Assume ufs if nothing else present */
1416 TAILQ_FOREACH(md, &part_metadata, metadata) {
1417 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
1418 rootfs = md->fstab->fs_vfstype;
1423 if (strcmp(classp->lg_name, "PART") != 0) {
1424 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1428 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1429 modified = "true"; /* XXX: If we don't know (kernel too old),
1430 * assume there are modifications. */
1431 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1432 if (strcmp(gc->lg_name, "modified") == 0) {
1433 modified = gc->lg_val;
1438 if (strcmp(modified, "false") == 0)
1441 /* Add bootcode if necessary, before the commit */
1442 md = get_part_metadata(gp->lg_name, 0);
1443 if (md != NULL && md->bootcode)
1446 /* Now install partcode on its partitions, if necessary */
1447 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1448 md = get_part_metadata(pp->lg_name, 0);
1449 if (md == NULL || !md->bootcode)
1452 /* Mark this partition active if that's required */
1455 /* Check if the partition has sub-partitions */
1456 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1457 if (strcmp(cp->lg_geom->lg_class->lg_name,
1461 if (cp == NULL) /* No sub-partitions */
1462 gpart_partcode(pp, rootfs);
1465 r = gctl_get_handle();
1466 gctl_ro_param(r, "class", -1, "PART");
1467 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1468 gctl_ro_param(r, "verb", -1, "commit");
1470 errstr = gctl_issue(r);
1471 if (errstr != NULL && errstr[0] != '\0')
1472 gpart_show_error("Error", NULL, errstr);