2 * Copyright (c) 2011 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
41 #define GPART_FLAGS "x" /* Do not commit changes by default */
44 gpart_show_error(const char *title, const char *explanation, const char *errstr)
50 if (explanation == NULL)
53 error = strtol(errstr, &errmsg, 0);
54 if (errmsg != errstr) {
55 while (errmsg[0] == ' ')
57 if (errmsg[0] != '\0')
58 sprintf(message, "%s%s. %s", explanation,
59 strerror(error), errmsg);
61 sprintf(message, "%s%s", explanation, strerror(error));
63 sprintf(message, "%s%s", explanation, errmsg);
66 dialog_msgbox(title, message, 0, 0, TRUE);
70 scheme_supports_labels(const char *scheme)
72 if (strcmp(scheme, "APM") == 0)
74 if (strcmp(scheme, "GPT") == 0)
81 newfs_command(const char *fstype, char *command, int use_default)
83 if (strcmp(fstype, "freebsd-ufs") == 0) {
85 DIALOG_LISTITEM items[] = {
86 {"UFS1", "UFS Version 1",
87 "Use version 1 of the UFS file system instead "
88 "of version 2 (not recommended)", 0 },
90 "Enable softupdates (default)", 1 },
91 {"SUJ", "Softupdates journaling",
92 "Enable file system journaling (default - "
93 "turn off for SSDs)", 1 },
94 {"TRIM", "Enable SSD TRIM support",
95 "Enable TRIM support, useful on solid-state drives",
101 choice = dlg_checklist("UFS Options", "", 0, 0, 0,
102 nitems(items), items, NULL,
104 if (choice == 1) /* Cancel */
108 strcpy(command, "newfs ");
109 for (i = 0; i < (int)nitems(items); i++) {
110 if (items[i].state == 0)
112 if (strcmp(items[i].name, "UFS1") == 0)
113 strcat(command, "-O1 ");
114 else if (strcmp(items[i].name, "SU") == 0)
115 strcat(command, "-U ");
116 else if (strcmp(items[i].name, "SUJ") == 0)
117 strcat(command, "-j ");
118 else if (strcmp(items[i].name, "TRIM") == 0)
119 strcat(command, "-t ");
121 } else if (strcmp(fstype, "freebsd-zfs") == 0) {
123 DIALOG_LISTITEM items[] = {
124 {"fletcher4", "checksum algorithm: fletcher4",
125 "Use fletcher4 for data integrity checking. "
127 {"fletcher2", "checksum algorithm: fletcher2",
128 "Use fletcher2 for data integrity checking. "
129 "(not recommended)", 0 },
130 {"sha256", "checksum algorithm: sha256",
131 "Use sha256 for data integrity checking. "
132 "(not recommended)", 0 },
133 {"atime", "Update atimes for files",
134 "Disable atime update", 0 },
139 choice = dlg_checklist("ZFS Options", "", 0, 0, 0,
140 nitems(items), items, NULL,
142 if (choice == 1) /* Cancel */
146 strcpy(command, "zpool create -f -m none ");
147 if (getenv("BSDINSTALL_TMPBOOT") != NULL) {
148 char zfsboot_path[MAXPATHLEN];
149 snprintf(zfsboot_path, sizeof(zfsboot_path), "%s/zfs",
150 getenv("BSDINSTALL_TMPBOOT"));
151 mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |
153 sprintf(command, "%s -o cachefile=%s/zpool.cache ",
154 command, zfsboot_path);
156 for (i = 0; i < (int)nitems(items); i++) {
157 if (items[i].state == 0)
159 if (strcmp(items[i].name, "fletcher4") == 0)
160 strcat(command, "-O checksum=fletcher4 ");
161 else if (strcmp(items[i].name, "fletcher2") == 0)
162 strcat(command, "-O checksum=fletcher2 ");
163 else if (strcmp(items[i].name, "sha256") == 0)
164 strcat(command, "-O checksum=sha256 ");
165 else if (strcmp(items[i].name, "atime") == 0)
166 strcat(command, "-O atime=off ");
168 } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0) {
170 DIALOG_LISTITEM items[] = {
171 {"FAT32", "FAT Type 32",
172 "Create a FAT32 filesystem (default)", 1 },
173 {"FAT16", "FAT Type 16",
174 "Create a FAT16 filesystem", 0 },
175 {"FAT12", "FAT Type 12",
176 "Create a FAT12 filesystem", 0 },
181 choice = dlg_checklist("FAT Options", "", 0, 0, 0,
182 nitems(items), items, NULL,
184 if (choice == 1) /* Cancel */
188 strcpy(command, "newfs_msdos ");
189 for (i = 0; i < (int)nitems(items); i++) {
190 if (items[i].state == 0)
192 if (strcmp(items[i].name, "FAT32") == 0)
193 strcat(command, "-F 32 ");
194 else if (strcmp(items[i].name, "FAT16") == 0)
195 strcat(command, "-F 16 ");
196 else if (strcmp(items[i].name, "FAT12") == 0)
197 strcat(command, "-F 12 ");
201 dialog_msgbox("Error", "No configurable options exist "
202 "for this filesystem.", 0, 0, TRUE);
208 choose_part_type(const char *def_scheme)
211 const char *scheme = NULL;
213 DIALOG_LISTITEM items[] = {
214 {"APM", "Apple Partition Map",
215 "Bootable on PowerPC Apple Hardware", 0 },
216 {"BSD", "BSD Labels",
217 "Bootable on most x86 systems", 0 },
218 {"GPT", "GUID Partition Table",
219 "Bootable on most x86 systems and EFI aware ARM64", 0 },
220 {"MBR", "DOS Partitions",
221 "Bootable on most x86 systems", 0 },
222 {"VTOC8", "Sun VTOC8 Partition Table",
223 "Bootable on Sun SPARC systems", 0 },
227 dialog_vars.default_item = __DECONST(char *, def_scheme);
228 cancel = dlg_menu("Partition Scheme",
229 "Select a partition scheme for this volume:", 0, 0, 0,
230 nitems(items), items, &choice, NULL);
231 dialog_vars.default_item = NULL;
236 if (!is_scheme_bootable(items[choice].name)) {
238 sprintf(message, "This partition scheme (%s) is not "
239 "bootable on this platform. Are you sure you want "
240 "to proceed?", items[choice].name);
241 dialog_vars.defaultno = TRUE;
242 cancel = dialog_yesno("Warning", message, 0, 0);
243 dialog_vars.defaultno = FALSE;
244 if (cancel) /* cancel */
248 scheme = items[choice].name;
254 gpart_partition(const char *lg_name, const char *scheme)
261 if (scheme == NULL) {
262 scheme = choose_part_type(default_scheme());
267 if (!is_scheme_bootable(scheme)) {
269 sprintf(message, "This partition scheme (%s) is not "
270 "bootable on this platform. Are you sure you want "
271 "to proceed?", scheme);
272 dialog_vars.defaultno = TRUE;
273 cancel = dialog_yesno("Warning", message, 0, 0);
274 dialog_vars.defaultno = FALSE;
275 if (cancel) { /* cancel */
276 /* Reset scheme so user can choose another */
283 r = gctl_get_handle();
284 gctl_ro_param(r, "class", -1, "PART");
285 gctl_ro_param(r, "arg0", -1, lg_name);
286 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
287 gctl_ro_param(r, "scheme", -1, scheme);
288 gctl_ro_param(r, "verb", -1, "create");
290 errstr = gctl_issue(r);
291 if (errstr != NULL && errstr[0] != '\0') {
292 gpart_show_error("Error", NULL, errstr);
299 if (bootcode_path(scheme) != NULL)
300 get_part_metadata(lg_name, 1)->bootcode = 1;
305 gpart_activate(struct gprovider *pp)
309 const char *errstr, *scheme;
310 const char *attribute = NULL;
314 * Some partition schemes need this partition to be marked 'active'
315 * for it to be bootable.
317 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
318 if (strcmp(gc->lg_name, "scheme") == 0) {
324 if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)
325 attribute = "active";
329 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
330 if (strcmp(gc->lg_name, "index") == 0) {
331 idx = atoi(gc->lg_val);
336 r = gctl_get_handle();
337 gctl_ro_param(r, "class", -1, "PART");
338 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
339 gctl_ro_param(r, "verb", -1, "set");
340 gctl_ro_param(r, "attrib", -1, attribute);
341 gctl_ro_param(r, "index", sizeof(idx), &idx);
343 errstr = gctl_issue(r);
344 if (errstr != NULL && errstr[0] != '\0')
345 gpart_show_error("Error", "Error marking partition active:",
351 gpart_set_root(const char *lg_name, const char *attribute)
356 r = gctl_get_handle();
357 gctl_ro_param(r, "class", -1, "PART");
358 gctl_ro_param(r, "arg0", -1, lg_name);
359 gctl_ro_param(r, "flags", -1, "C");
360 gctl_ro_param(r, "verb", -1, "set");
361 gctl_ro_param(r, "attrib", -1, attribute);
363 errstr = gctl_issue(r);
364 if (errstr != NULL && errstr[0] != '\0')
365 gpart_show_error("Error", "Error setting parameter on disk:",
371 gpart_bootcode(struct ggeom *gp)
373 const char *bootcode;
376 const char *errstr, *scheme;
378 size_t bootsize, bytes;
382 * Write default bootcode to the newly partitioned disk, if that
383 * applies on this platform.
385 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
386 if (strcmp(gc->lg_name, "scheme") == 0) {
392 bootcode = bootcode_path(scheme);
393 if (bootcode == NULL)
396 bootfd = open(bootcode, O_RDONLY);
398 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
403 bootsize = lseek(bootfd, 0, SEEK_END);
404 boot = malloc(bootsize);
405 lseek(bootfd, 0, SEEK_SET);
407 while (bytes < bootsize)
408 bytes += read(bootfd, boot + bytes, bootsize - bytes);
411 r = gctl_get_handle();
412 gctl_ro_param(r, "class", -1, "PART");
413 gctl_ro_param(r, "arg0", -1, gp->lg_name);
414 gctl_ro_param(r, "verb", -1, "bootcode");
415 gctl_ro_param(r, "bootcode", bootsize, boot);
417 errstr = gctl_issue(r);
418 if (errstr != NULL && errstr[0] != '\0')
419 gpart_show_error("Bootcode Error", NULL, errstr);
425 gpart_partcode(struct gprovider *pp, const char *fstype)
429 const char *indexstr;
430 char message[255], command[255];
432 LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
433 if (strcmp(gc->lg_name, "scheme") == 0) {
439 /* Make sure this partition scheme needs partcode on this platform */
440 if (partcode_path(scheme, fstype) == NULL)
443 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
444 if (strcmp(gc->lg_name, "index") == 0) {
445 indexstr = gc->lg_val;
450 /* Shell out to gpart for partcode for now */
451 sprintf(command, "gpart bootcode -p %s -i %s %s",
452 partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);
453 if (system(command) != 0) {
454 sprintf(message, "Error installing partcode on partition %s",
456 dialog_msgbox("Error", message, 0, 0, TRUE);
461 gpart_destroy(struct ggeom *lg_geom)
464 struct gprovider *pp;
468 /* Delete all child metadata */
469 LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
472 /* Revert any local changes to get this geom into a pristine state */
473 r = gctl_get_handle();
474 gctl_ro_param(r, "class", -1, "PART");
475 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
476 gctl_ro_param(r, "verb", -1, "undo");
477 gctl_issue(r); /* Ignore errors -- these are non-fatal */
480 /* Now destroy the geom itself */
481 r = gctl_get_handle();
482 gctl_ro_param(r, "class", -1, "PART");
483 gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
484 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
485 gctl_ro_param(r, "force", sizeof(force), &force);
486 gctl_ro_param(r, "verb", -1, "destroy");
487 errstr = gctl_issue(r);
488 if (errstr != NULL && errstr[0] != '\0') {
490 * Check if we reverted away the existence of the geom
491 * altogether. Show all other errors to the user.
493 if (strtol(errstr, NULL, 0) != EINVAL)
494 gpart_show_error("Error", NULL, errstr);
498 /* And any metadata associated with the partition scheme itself */
499 delete_part_metadata(lg_geom->lg_name);
503 gpart_edit(struct gprovider *pp)
507 struct gconsumer *cp;
509 const char *errstr, *oldtype, *scheme;
510 struct partition_metadata *md;
514 int hadlabel, choice, junk, nitems;
517 DIALOG_FORMITEM items[] = {
518 {0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0,
519 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
520 "freebsd-swap)", FALSE},
521 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0,
522 FALSE, "Partition size. Append K, M, G for kilobytes, "
523 "megabytes or gigabytes.", FALSE},
524 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
525 FALSE, "Path at which to mount this partition (leave blank "
526 "for swap, set to / for root filesystem)", FALSE},
527 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
528 "Partition name. Not all partition schemes support this.",
533 * Find the PART geom we are manipulating. This may be a consumer of
534 * this provider, or its parent. Check the consumer case first.
537 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
538 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
539 /* Check for zombie geoms, treating them as blank */
541 LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
542 if (strcmp(gc->lg_name, "scheme") == 0) {
547 if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
548 gpart_partition(cp->lg_geom->lg_name, NULL);
552 /* If this is a nested partition, edit as usual */
553 if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
556 /* Destroy the geom and all sub-partitions */
557 gpart_destroy(cp->lg_geom);
559 /* Now re-partition and return */
560 gpart_partition(cp->lg_geom->lg_name, NULL);
564 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
568 /* Disk not partitioned, so partition it */
569 gpart_partition(pp->lg_name, NULL);
573 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
574 if (strcmp(gc->lg_name, "scheme") == 0) {
580 nitems = scheme_supports_labels(scheme) ? 4 : 3;
582 /* Edit editable parameters of a partition */
584 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
585 if (strcmp(gc->lg_name, "type") == 0) {
586 oldtype = gc->lg_val;
587 items[0].text = gc->lg_val;
589 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
591 items[3].text = gc->lg_val;
593 if (strcmp(gc->lg_name, "index") == 0)
594 idx = atoi(gc->lg_val);
597 TAILQ_FOREACH(md, &part_metadata, metadata) {
598 if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
599 if (md->fstab != NULL)
600 items[2].text = md->fstab->fs_file;
605 humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
606 HN_NOSPACE | HN_DECIMAL);
607 items[1].text = sizestr;
610 choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
612 if (choice) /* Cancel pressed */
615 /* If this is the root partition, check that this fs is bootable */
616 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
619 sprintf(message, "This file system (%s) is not bootable "
620 "on this system. Are you sure you want to proceed?",
622 dialog_vars.defaultno = TRUE;
623 choice = dialog_yesno("Warning", message, 0, 0);
624 dialog_vars.defaultno = FALSE;
625 if (choice == 1) /* cancel */
629 /* Check if the label has a / in it */
630 if (strchr(items[3].text, '/') != NULL) {
631 dialog_msgbox("Error", "Label contains a /, which is not an "
632 "allowed character.", 0, 0, TRUE);
636 r = gctl_get_handle();
637 gctl_ro_param(r, "class", -1, "PART");
638 gctl_ro_param(r, "arg0", -1, geom->lg_name);
639 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
640 gctl_ro_param(r, "verb", -1, "modify");
641 gctl_ro_param(r, "index", sizeof(idx), &idx);
642 if (hadlabel || items[3].text[0] != '\0')
643 gctl_ro_param(r, "label", -1, items[3].text);
644 gctl_ro_param(r, "type", -1, items[0].text);
645 errstr = gctl_issue(r);
646 if (errstr != NULL && errstr[0] != '\0') {
647 gpart_show_error("Error", NULL, errstr);
653 newfs_command(items[0].text, newfs, 1);
654 set_default_part_metadata(pp->lg_name, scheme, items[0].text,
655 items[2].text, (strcmp(oldtype, items[0].text) != 0) ?
659 if (strcmp(oldtype, items[0].text) != 0 && cp != NULL)
660 gpart_destroy(cp->lg_geom);
661 if (strcmp(oldtype, items[0].text) != 0 && strcmp(items[0].text,
663 gpart_partition(pp->lg_name, "BSD");
665 for (i = 0; i < nitems(items); i++)
666 if (items[i].text_free)
671 set_default_part_metadata(const char *name, const char *scheme,
672 const char *type, const char *mountpoint, const char *newfs)
674 struct partition_metadata *md;
675 char *zpool_name = NULL;
678 /* Set part metadata */
679 md = get_part_metadata(name, 1);
682 if (md->newfs != NULL) {
687 if (newfs != NULL && newfs[0] != '\0') {
688 md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
689 strlen(mountpoint) + 5 + strlen(name) + 1);
690 if (strcmp("freebsd-zfs", type) == 0) {
691 zpool_name = strdup((strlen(mountpoint) == 1) ?
692 "root" : &mountpoint[1]);
693 for (i = 0; zpool_name[i] != 0; i++)
694 if (!isalnum(zpool_name[i]))
696 sprintf(md->newfs, "%s %s /dev/%s", newfs,
699 sprintf(md->newfs, "%s /dev/%s", newfs, name);
704 if (strcmp(type, "freebsd-swap") == 0)
706 if (strcmp(type, bootpart_type(scheme)) == 0)
709 /* VTOC8 needs partcode at the start of partitions */
710 if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0
711 || strcmp(type, "freebsd-zfs") == 0))
714 if (mountpoint == NULL || mountpoint[0] == '\0') {
715 if (md->fstab != NULL) {
716 free(md->fstab->fs_spec);
717 free(md->fstab->fs_file);
718 free(md->fstab->fs_vfstype);
719 free(md->fstab->fs_mntops);
720 free(md->fstab->fs_type);
725 if (md->fstab == NULL) {
726 md->fstab = malloc(sizeof(struct fstab));
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);
734 if (strcmp("freebsd-zfs", type) == 0) {
735 md->fstab->fs_spec = strdup(zpool_name);
737 md->fstab->fs_spec = malloc(strlen(name) +
738 strlen("/dev/") + 1);
739 sprintf(md->fstab->fs_spec, "/dev/%s", name);
741 md->fstab->fs_file = strdup(mountpoint);
742 /* Get VFS from text after freebsd-, if possible */
743 if (strncmp("freebsd-", type, 8) == 0)
744 md->fstab->fs_vfstype = strdup(&type[8]);
745 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0)
746 md->fstab->fs_vfstype = strdup("msdosfs");
748 md->fstab->fs_vfstype = strdup(type); /* Guess */
749 if (strcmp(type, "freebsd-swap") == 0) {
750 md->fstab->fs_type = strdup(FSTAB_SW);
751 md->fstab->fs_freq = 0;
752 md->fstab->fs_passno = 0;
753 } else if (strcmp(type, "freebsd-zfs") == 0) {
754 md->fstab->fs_type = strdup(FSTAB_RW);
755 md->fstab->fs_freq = 0;
756 md->fstab->fs_passno = 0;
758 md->fstab->fs_type = strdup(FSTAB_RW);
759 if (strcmp(mountpoint, "/") == 0) {
760 md->fstab->fs_freq = 1;
761 md->fstab->fs_passno = 1;
763 md->fstab->fs_freq = 2;
764 md->fstab->fs_passno = 2;
767 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
770 if (zpool_name != NULL)
775 int part_compare(const void *xa, const void *xb)
777 struct gprovider **a = (struct gprovider **)xa;
778 struct gprovider **b = (struct gprovider **)xb;
779 intmax_t astart, bstart;
783 LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
784 if (strcmp(gc->lg_name, "start") == 0) {
785 astart = strtoimax(gc->lg_val, NULL, 0);
788 LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
789 if (strcmp(gc->lg_name, "start") == 0) {
790 bstart = strtoimax(gc->lg_val, NULL, 0);
796 else if (astart > bstart)
803 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
806 struct gprovider *pp, **providers;
807 intmax_t sectorsize, stripesize, offset;
810 intmax_t maxsize, maxstart;
811 intmax_t partstart, partend;
814 /* Now get the maximum free size and free start */
816 LIST_FOREACH(gc, &geom->lg_config, lg_config) {
817 if (strcmp(gc->lg_name, "first") == 0)
818 start = strtoimax(gc->lg_val, NULL, 0);
819 if (strcmp(gc->lg_name, "last") == 0)
820 end = strtoimax(gc->lg_val, NULL, 0);
824 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
826 providers = calloc(nparts, sizeof(providers[0]));
827 LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
829 qsort(providers, nparts, sizeof(providers[0]), part_compare);
833 for (i = 0; i < nparts; i++) {
836 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
837 if (strcmp(gc->lg_name, "start") == 0)
838 partstart = strtoimax(gc->lg_val, NULL, 0);
839 if (strcmp(gc->lg_name, "end") == 0)
840 partend = strtoimax(gc->lg_val, NULL, 0);
843 if (partstart - lastend > maxsize) {
844 maxsize = partstart - lastend - 1;
845 maxstart = lastend + 1;
851 if (end - lastend > maxsize) {
852 maxsize = end - lastend - 1;
853 maxstart = lastend + 1;
856 pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
859 * Round the start and size of the largest available space up to
860 * the nearest multiple of the adjusted stripe size.
862 * The adjusted stripe size is the least common multiple of the
863 * actual stripe size, or the sector size if no stripe size was
864 * reported, and 4096. The reason for this is that contemporary
865 * disks often have 4096-byte physical sectors but report 512
866 * bytes instead for compatibility with older / broken operating
867 * systems and BIOSes. For the same reasons, virtualized storage
868 * may also report a 512-byte stripe size, or none at all.
870 sectorsize = pp->lg_sectorsize;
871 if ((stripesize = pp->lg_stripesize) == 0)
872 stripesize = sectorsize;
873 while (stripesize % 4096 != 0)
875 if ((offset = maxstart * sectorsize % stripesize) != 0) {
876 offset = (stripesize - offset) / sectorsize;
881 if (npartstart != NULL)
882 *npartstart = maxstart;
888 gpart_create(struct gprovider *pp, char *default_type, char *default_size,
889 char *default_mountpoint, char **partname, int interactive)
893 struct gconsumer *cp;
895 const char *errstr, *scheme;
896 char sizestr[32], startstr[32], output[64], *newpartname;
897 char newfs[255], options_fstype[64];
898 intmax_t maxsize, size, sector, firstfree, stripe;
900 int nitems, choice, junk;
903 DIALOG_FORMITEM items[] = {
904 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
905 FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
906 "freebsd-swap)", FALSE},
907 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
908 FALSE, "Partition size. Append K, M, G for kilobytes, "
909 "megabytes or gigabytes.", FALSE},
910 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
911 FALSE, "Path at which to mount partition (blank for "
912 "swap, / for root filesystem)", FALSE},
913 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
914 "Partition name. Not all partition schemes support this.",
918 if (partname != NULL)
921 /* Record sector and stripe sizes */
922 sector = pp->lg_sectorsize;
923 stripe = pp->lg_stripesize;
926 * Find the PART geom we are manipulating. This may be a consumer of
927 * this provider, or its parent. Check the consumer case first.
930 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
931 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
936 if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
939 /* Now get the partition scheme */
942 LIST_FOREACH(gc, &geom->lg_config, lg_config)
943 if (strcmp(gc->lg_name, "scheme") == 0)
947 if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
948 if (gpart_partition(pp->lg_name, NULL) == 0)
950 "The partition table has been successfully created."
951 " Please press Create again to create partitions.",
958 * If we still don't have a geom, either the user has
959 * canceled partitioning or there has been an error which has already
960 * been displayed, so bail.
965 maxsize = size = gpart_max_free(geom, &firstfree);
967 dialog_msgbox("Error", "No free space left on device.", 0, 0,
972 humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
973 HN_NOSPACE | HN_DECIMAL);
974 items[1].text = sizestr;
976 /* Special-case the MBR default type for nested partitions */
977 if (strcmp(scheme, "MBR") == 0) {
978 items[0].text = "freebsd";
979 items[0].help = "Filesystem type (e.g. freebsd, fat32)";
982 nitems = scheme_supports_labels(scheme) ? 4 : 3;
984 if (default_type != NULL)
985 items[0].text = default_type;
986 if (default_size != NULL)
987 items[1].text = default_size;
988 if (default_mountpoint != NULL)
989 items[2].text = default_mountpoint;
991 /* Default options */
992 strncpy(options_fstype, items[0].text,
993 sizeof(options_fstype));
994 newfs_command(options_fstype, newfs, 1);
997 dialog_vars.extra_label = "Options";
998 dialog_vars.extra_button = TRUE;
999 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
1001 dialog_vars.extra_button = FALSE;
1005 case 1: /* Cancel */
1007 case 3: /* Options */
1008 strncpy(options_fstype, items[0].text,
1009 sizeof(options_fstype));
1010 newfs_command(options_fstype, newfs, 0);
1016 * If the user changed the fs type after specifying options, undo
1017 * their choices in favor of the new filesystem's defaults.
1019 if (strcmp(options_fstype, items[0].text) != 0) {
1020 strncpy(options_fstype, items[0].text, sizeof(options_fstype));
1021 newfs_command(options_fstype, newfs, 1);
1025 if (strlen(items[1].text) > 0) {
1026 if (expand_number(items[1].text, &bytes) != 0) {
1029 sprintf(error, "Invalid size: %s\n", strerror(errno));
1030 dialog_msgbox("Error", error, 0, 0, TRUE);
1033 size = MIN((intmax_t)(bytes/sector), maxsize);
1036 /* Check if the label has a / in it */
1037 if (strchr(items[3].text, '/') != NULL) {
1038 dialog_msgbox("Error", "Label contains a /, which is not an "
1039 "allowed character.", 0, 0, TRUE);
1043 /* Warn if no mountpoint set */
1044 if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1045 items[2].text[0] != '/') {
1048 dialog_vars.defaultno = TRUE;
1049 choice = dialog_yesno("Warning",
1050 "This partition does not have a valid mountpoint "
1051 "(for the partition from which you intend to boot the "
1052 "operating system, the mountpoint should be /). Are you "
1053 "sure you want to continue?"
1055 dialog_vars.defaultno = FALSE;
1057 if (choice == 1) /* cancel */
1062 * Error if this scheme needs nested partitions, this is one, and
1063 * a mountpoint was set.
1065 if (strcmp(items[0].text, "freebsd") == 0 &&
1066 strlen(items[2].text) > 0) {
1067 dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1068 "nested BSD-type partition schemes and cannot have "
1069 "mountpoints. After creating one, select it and press "
1070 "Create again to add the actual file systems.", 0, 0, TRUE);
1074 /* If this is the root partition, check that this scheme is bootable */
1075 if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1077 sprintf(message, "This partition scheme (%s) is not bootable "
1078 "on this platform. Are you sure you want to proceed?",
1080 dialog_vars.defaultno = TRUE;
1081 choice = dialog_yesno("Warning", message, 0, 0);
1082 dialog_vars.defaultno = FALSE;
1083 if (choice == 1) /* cancel */
1087 /* If this is the root partition, check that this fs is bootable */
1088 if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1091 sprintf(message, "This file system (%s) is not bootable "
1092 "on this system. Are you sure you want to proceed?",
1094 dialog_vars.defaultno = TRUE;
1095 choice = dialog_yesno("Warning", message, 0, 0);
1096 dialog_vars.defaultno = FALSE;
1097 if (choice == 1) /* cancel */
1102 * If this is the root partition, and we need a boot partition, ask
1103 * the user to add one.
1106 /* Check for existing freebsd-boot partition */
1107 LIST_FOREACH(pp, &geom->lg_provider, lg_provider) {
1108 struct partition_metadata *md;
1109 md = get_part_metadata(pp->lg_name, 0);
1110 if (md == NULL || !md->bootcode)
1112 LIST_FOREACH(gc, &pp->lg_config, lg_config)
1113 if (strcmp(gc->lg_name, "type") == 0)
1115 if (gc != NULL && strcmp(gc->lg_val,
1116 bootpart_type(scheme)) == 0)
1120 /* If there isn't one, and we need one, ask */
1121 if ((strcmp(items[0].text, "freebsd") == 0 ||
1122 strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0 &&
1125 choice = dialog_yesno("Boot Partition",
1126 "This partition scheme requires a boot partition "
1127 "for the disk to be bootable. Would you like to "
1128 "make one now?", 0, 0);
1132 if (choice == 0) { /* yes */
1133 r = gctl_get_handle();
1134 gctl_ro_param(r, "class", -1, "PART");
1135 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1136 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1137 gctl_ro_param(r, "verb", -1, "add");
1138 gctl_ro_param(r, "type", -1, bootpart_type(scheme));
1139 snprintf(sizestr, sizeof(sizestr), "%jd",
1140 bootpart_size(scheme) / sector);
1141 gctl_ro_param(r, "size", -1, sizestr);
1142 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1143 gctl_ro_param(r, "start", -1, startstr);
1144 gctl_rw_param(r, "output", sizeof(output), output);
1145 errstr = gctl_issue(r);
1146 if (errstr != NULL && errstr[0] != '\0')
1147 gpart_show_error("Error", NULL, errstr);
1150 get_part_metadata(strtok(output, " "), 1)->bootcode = 1;
1152 /* Now adjust the part we are really adding forward */
1153 firstfree += bootpart_size(scheme) / sector;
1154 size -= (bootpart_size(scheme) + stripe)/sector;
1155 if (stripe > 0 && (firstfree*sector % stripe) != 0)
1156 firstfree += (stripe - ((firstfree*sector) %
1161 r = gctl_get_handle();
1162 gctl_ro_param(r, "class", -1, "PART");
1163 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1164 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1165 gctl_ro_param(r, "verb", -1, "add");
1167 gctl_ro_param(r, "type", -1, items[0].text);
1168 snprintf(sizestr, sizeof(sizestr), "%jd", size);
1169 gctl_ro_param(r, "size", -1, sizestr);
1170 snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1171 gctl_ro_param(r, "start", -1, startstr);
1172 if (items[3].text[0] != '\0')
1173 gctl_ro_param(r, "label", -1, items[3].text);
1174 gctl_rw_param(r, "output", sizeof(output), output);
1175 errstr = gctl_issue(r);
1176 if (errstr != NULL && errstr[0] != '\0') {
1177 gpart_show_error("Error", NULL, errstr);
1181 newpartname = strtok(output, " ");
1185 * Try to destroy any geom that gpart picked up already here from
1188 r = gctl_get_handle();
1189 gctl_ro_param(r, "class", -1, "PART");
1190 gctl_ro_param(r, "arg0", -1, newpartname);
1191 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1193 gctl_ro_param(r, "force", sizeof(junk), &junk);
1194 gctl_ro_param(r, "verb", -1, "destroy");
1195 gctl_issue(r); /* Error usually expected and non-fatal */
1198 if (strcmp(items[0].text, bootpart_type(scheme)) == 0)
1199 get_part_metadata(newpartname, 1)->bootcode = 1;
1200 else if (strcmp(items[0].text, "freebsd") == 0)
1201 gpart_partition(newpartname, "BSD");
1203 set_default_part_metadata(newpartname, scheme,
1204 items[0].text, items[2].text, newfs);
1206 for (i = 0; i < nitems(items); i++)
1207 if (items[i].text_free)
1208 free(items[i].text);
1210 if (partname != NULL)
1211 *partname = strdup(newpartname);
1215 gpart_delete(struct gprovider *pp)
1219 struct gconsumer *cp;
1225 /* Is it a partition? */
1226 is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1228 /* Find out if this is the root of a gpart geom */
1230 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1231 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1236 /* If so, destroy all children */
1238 gpart_destroy(geom);
1240 /* If this is a partition, revert it, so it can be deleted */
1242 r = gctl_get_handle();
1243 gctl_ro_param(r, "class", -1, "PART");
1244 gctl_ro_param(r, "arg0", -1, geom->lg_name);
1245 gctl_ro_param(r, "verb", -1, "undo");
1246 gctl_issue(r); /* Ignore non-fatal errors */
1252 * If this is not a partition, see if that is a problem, complain if
1253 * necessary, and return always, since we need not do anything further,
1256 if (!is_partition) {
1258 dialog_msgbox("Error",
1259 "Only partitions can be deleted.", 0, 0, TRUE);
1263 r = gctl_get_handle();
1264 gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1265 gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1266 gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1267 gctl_ro_param(r, "verb", -1, "delete");
1269 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1270 if (strcmp(gc->lg_name, "index") == 0) {
1271 idx = atoi(gc->lg_val);
1272 gctl_ro_param(r, "index", sizeof(idx), &idx);
1277 errstr = gctl_issue(r);
1278 if (errstr != NULL && errstr[0] != '\0') {
1279 gpart_show_error("Error", NULL, errstr);
1286 delete_part_metadata(pp->lg_name);
1290 gpart_revert_all(struct gmesh *mesh)
1292 struct gclass *classp;
1296 const char *modified;
1298 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1299 if (strcmp(classp->lg_name, "PART") == 0)
1303 if (strcmp(classp->lg_name, "PART") != 0) {
1304 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1308 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1309 modified = "true"; /* XXX: If we don't know (kernel too old),
1310 * assume there are modifications. */
1311 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1312 if (strcmp(gc->lg_name, "modified") == 0) {
1313 modified = gc->lg_val;
1318 if (strcmp(modified, "false") == 0)
1321 r = gctl_get_handle();
1322 gctl_ro_param(r, "class", -1, "PART");
1323 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1324 gctl_ro_param(r, "verb", -1, "undo");
1332 gpart_commit(struct gmesh *mesh)
1334 struct partition_metadata *md;
1335 struct gclass *classp;
1338 struct gconsumer *cp;
1339 struct gprovider *pp;
1342 const char *modified;
1345 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1346 if (strcmp(classp->lg_name, "PART") == 0)
1350 /* Figure out what filesystem / uses */
1351 rootfs = "ufs"; /* Assume ufs if nothing else present */
1352 TAILQ_FOREACH(md, &part_metadata, metadata) {
1353 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
1354 rootfs = md->fstab->fs_vfstype;
1359 if (strcmp(classp->lg_name, "PART") != 0) {
1360 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1364 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1365 modified = "true"; /* XXX: If we don't know (kernel too old),
1366 * assume there are modifications. */
1367 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1368 if (strcmp(gc->lg_name, "modified") == 0) {
1369 modified = gc->lg_val;
1374 if (strcmp(modified, "false") == 0)
1377 /* Add bootcode if necessary, before the commit */
1378 md = get_part_metadata(gp->lg_name, 0);
1379 if (md != NULL && md->bootcode)
1382 /* Now install partcode on its partitions, if necessary */
1383 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1384 md = get_part_metadata(pp->lg_name, 0);
1385 if (md == NULL || !md->bootcode)
1388 /* Mark this partition active if that's required */
1391 /* Check if the partition has sub-partitions */
1392 LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1393 if (strcmp(cp->lg_geom->lg_class->lg_name,
1397 if (cp == NULL) /* No sub-partitions */
1398 gpart_partcode(pp, rootfs);
1401 r = gctl_get_handle();
1402 gctl_ro_param(r, "class", -1, "PART");
1403 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1404 gctl_ro_param(r, "verb", -1, "commit");
1406 errstr = gctl_issue(r);
1407 if (errstr != NULL && errstr[0] != '\0')
1408 gpart_show_error("Error", NULL, errstr);